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 expertinputstring Expert_Title ="EA_SMA7_SMA14_1"; // Document nameulong Expert_MagicNumber =16314; //bool Expert_EveryTick =false; ////--- inputs for main signalinputint Signal_ThresholdOpen =10; // Signal threshold value to open [0...100]inputint Signal_ThresholdClose =10; // Signal threshold value to close [0...100]inputdouble Signal_StopLevel =10.0; // Stop Loss level (in points)inputdouble Signal_TakeLevel =10.0; // Take Profit level (in points)inputint Signal_Expiration =4; // Expiration of pending orders (in bars)inputint Signal_0_MA_PeriodMA =7; // Moving Average(7,0,MODE_SMA,...) M1 Period of averaginginputint Signal_0_MA_Shift =0; // Moving Average(7,0,MODE_SMA,...) M1 Time shiftinputENUM_MA_METHOD Signal_0_MA_Method =MODE_SMA; // Moving Average(7,0,MODE_SMA,...) M1 Method of averaginginputENUM_APPLIED_PRICE Signal_0_MA_Applied =PRICE_CLOSE; // Moving Average(7,0,MODE_SMA,...) M1 Prices seriesinputint Signal_1_MA_PeriodMA =14; // Moving Average(14,0,...) M1 Period of averaginginputint Signal_1_MA_Shift =0; // Moving Average(14,0,...) M1 Time shiftinputENUM_MA_METHOD Signal_1_MA_Method =MODE_SMA; // Moving Average(14,0,...) M1 Method of averaginginputENUM_APPLIED_PRICE Signal_1_MA_Applied =PRICE_CLOSE; // Moving Average(14,0,...) M1 Prices series//--- inputs for moneyinputdouble Money_FixMargin_Percent=3.0; // Percentage of margin//+------------------------------------------------------------------+//| Global expert object |//+------------------------------------------------------------------+
CTrade trade;
double fastMA[], slowMA[];
int fastMAHandle, slowMAHandle;
//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+intOnInit()
{
//--- 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);
//--- okreturn(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+//| Expert deinitialization function |//+------------------------------------------------------------------+voidOnDeinit(constint reason)
{
//--- Release indicator handlesif (fastMAHandle != INVALID_HANDLE) IndicatorRelease(fastMAHandle);
if (slowMAHandle != INVALID_HANDLE) IndicatorRelease(slowMAHandle);
}
//+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+voidOnTick()
{
//--- Get the current values of the moving averagesCopyBuffer(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 signalif (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 signalif (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));
}
}
}
}
//+------------------------------------------------------------------+
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: