//+------------------------------------------------------------------+ //| Include | //+------------------------------------------------------------------+ #include // Include trade functions #include // Include indicators functions //+------------------------------------------------------------------+ //| Inputs | //+------------------------------------------------------------------+ //--- inputs for expert input string Expert_Title ="EA_SMA7_SMA14_1"; // Document name ulong Expert_MagicNumber =16314; // bool Expert_EveryTick =false; // //--- inputs for main signal input int Signal_ThresholdOpen =10; // Signal threshold value to open [0...100] input int Signal_ThresholdClose =10; // Signal threshold value to close [0...100] input double Signal_StopLevel =10.0; // Stop Loss level (in points) input double Signal_TakeLevel =10.0; // Take Profit level (in points) input int Signal_Expiration =4; // Expiration of pending orders (in bars) input int Signal_0_MA_PeriodMA =7; // Moving Average(7,0,MODE_SMA,...) M1 Period of averaging input int Signal_0_MA_Shift =0; // Moving Average(7,0,MODE_SMA,...) M1 Time shift input ENUM_MA_METHOD Signal_0_MA_Method =MODE_SMA; // Moving Average(7,0,MODE_SMA,...) M1 Method of averaging input ENUM_APPLIED_PRICE Signal_0_MA_Applied =PRICE_CLOSE; // Moving Average(7,0,MODE_SMA,...) M1 Prices series input int Signal_1_MA_PeriodMA =14; // Moving Average(14,0,...) M1 Period of averaging input int Signal_1_MA_Shift =0; // Moving Average(14,0,...) M1 Time shift input ENUM_MA_METHOD Signal_1_MA_Method =MODE_SMA; // Moving Average(14,0,...) M1 Method of averaging input ENUM_APPLIED_PRICE Signal_1_MA_Applied =PRICE_CLOSE; // Moving Average(14,0,...) M1 Prices series //--- inputs for money input double Money_FixMargin_Percent=3.0; // Percentage of margin //+------------------------------------------------------------------+ //| Global expert object | //+------------------------------------------------------------------+ CTrade trade; double fastMA[], slowMA[]; int fastMAHandle, slowMAHandle; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- Create handles for the moving averages fastMAHandle = iMA(_Symbol, PERIOD_M1, Signal_0_MA_PeriodMA, Signal_0_MA_Shift, Signal_0_MA_Method, Signal_0_MA_Applied); slowMAHandle = iMA(_Symbol, PERIOD_M1, Signal_1_MA_PeriodMA, Signal_1_MA_Shift, Signal_1_MA_Method, Signal_1_MA_Applied); if (fastMAHandle == INVALID_HANDLE || slowMAHandle == INVALID_HANDLE) { Print("Error creating indicator handles"); return(INIT_FAILED); } ArraySetAsSeries(fastMA, true); ArraySetAsSeries(slowMA, true); //--- ok return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- Release indicator handles if (fastMAHandle != INVALID_HANDLE) IndicatorRelease(fastMAHandle); if (slowMAHandle != INVALID_HANDLE) IndicatorRelease(slowMAHandle); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- Get the current values of the moving averages CopyBuffer(fastMAHandle, 0, 0, 3, fastMA); CopyBuffer(slowMAHandle, 0, 0, 3, slowMA); double fastMA_prev = fastMA[1]; double slowMA_prev = slowMA[1]; double fastMA_curr = fastMA[0]; double slowMA_curr = slowMA[0]; //--- Check for buy signal if (fastMA_prev < slowMA_prev && fastMA_curr > slowMA_curr) { //--- Close any sell orders CloseOrders(POSITION_TYPE_SELL); //--- Open a buy order trade.Buy(1.0, _Symbol, SymbolInfoDouble(_Symbol, SYMBOL_ASK), Signal_StopLevel, Signal_TakeLevel, "Buy Order"); } //--- Check for sell signal if (fastMA_prev > slowMA_prev && fastMA_curr < slowMA_curr) { //--- Close any buy orders CloseOrders(POSITION_TYPE_BUY); //--- Open a sell order trade.Sell(1.0, _Symbol, SymbolInfoDouble(_Symbol, SYMBOL_BID), Signal_StopLevel, Signal_TakeLevel, "Sell Order"); } } //+------------------------------------------------------------------+ //| Close orders of a specific type | //+------------------------------------------------------------------+ void CloseOrders(int orderType) { for (int i = PositionsTotal() - 1; i >= 0; i--) { if (PositionSelectByTicket(i)) { if (PositionGetInteger(POSITION_TYPE) == orderType && PositionGetString(POSITION_SYMBOL) == _Symbol) { trade.PositionClose(PositionGetInteger(POSITION_IDENTIFIER)); } } } } //+------------------------------------------------------------------+