EA not performing operations

 
I am not a professional developer and I am trying to make an EA based on published examples, my personal additions, my limited general programming knowledge (html, php, databases, but not MQL5) and the help of the AI Assistant built into the MQL5 Meta Editor.

I have made several small EAs and none of them work, although the code has no errors apparently and the examples I based them on all work.

I have tested it in Strategy Tester and in a demo account (of course once compiled) and although there is no error message and apparently all the code is executed correctly, the opening and closing operations are not performed.

I have tested it in Strategy Tester over monthly periods and the entire current year, and the conditions required to open and close trades have been met several times. I have no problems on the platform because the base examples and other EAs are running properly, so I can't think of any other reason why it might not work (except in the code).

I would appreciate any comments on this matter that could help me find this error. Thank you very much for your help. The EA code is as follows:

//+------------------------------------------------------------------+

//| Include                                                          |

//+------------------------------------------------------------------+

#include <Trade\Trade.mqh> // Include trade functions

#include <Indicators\Indicators.mqh> // 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));

           }

        }

     }

  }

//+------------------------------------------------------------------+

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Execution of trade operations results in the opening of a position, changing of its volume and/or direction, or its disappearance. Trade operations...
 

When you post code please use the CODE button (Alt-S)!

Use the CODE button

 
This was my first post here. Thank you for your comment Sergey