As the title state I am running into issues trying to test my EA on using the strategy tester.
I have include the complete code.
When I attempt to "test" the EA, there is a significant delay and no trades are opened.
Also, when I use the Visual mode the strategy tester opens two windows and again, no trades are opened and no chart is displayed.
This error began to occur after I added the Dynamic lot size calculation based on an ATR stop loss.
The code compiles with out error and I am unable to determine what is causing the issue.
Any assistance is appreciated.
See my full EA below:
//+------------------------------------------------------------------+//| Moving Average 1.0.mq5 |//| Erik Williams |//| https://www.mql5.com |//+------------------------------------------------------------------+#property copyright"Erik Williams"#property link"https://www.mql5.com"#property version"1.20"#include <Trade\Trade.mqh> // Get code from other places//--- input parametersinputint MA_1D_Period=5; // 1D MA Periods (5) - Weighted - Adjust later if needed inputint MA_4H_Period=10; // 4H MA Periods (10) - Wighted - Adjust later if neededinputint MA_1H_Period=10; // 1H MA Periods (10) - Weighted - Adjust later if needed inputint ATR_1H_Period=14; // 1H ATR Period (14)inputint EA_Magic=12345; // EA Magic Numberinputdouble RiskAmmount=100; // Risk Per Trade in Dollars /////////////////////////////---Work in progress---/////////////////////////////--- Other Parameters
CTrade trade; // Declaring trade function int MA_1D_Handle; //Handle for MA_Dailyint MA_4H_Handle; //Handle for MA_4Hourint MA_1H_Handle; //Handle for MA_1Hourint ATR_1H_Handle; //Handle for ATRdouble Dynamic_Lot_Size_Handle; //Handle for Lot Size Calculation double MA_1D_Val[], MA_4H_Val[], MA_1H_Val[]; //Dynamic Array For MA values double ATR_1H_Val[]; //Dynamic Array for ATR Valuesdouble STP, TKP; // Stop Loss and Take Profit Valuesdouble Final_Lots; // Lot Size Varible MqlRates mrate[]; //To be used for storing prices, volume, and spread of each bar//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+intOnInit()
{
//--- Get Handle for 1 Day MA
MA_1D_Handle= iMA(_Symbol,PERIOD_D1,MA_1D_Period,0,MODE_LWMA,PRICE_CLOSE);
//--- Get Handle for 4 Hour MA
MA_4H_Handle= iMA(_Symbol,PERIOD_H4,MA_4H_Period,0,MODE_LWMA,PRICE_CLOSE);
//---Get Handle For 1 Hour MA
MA_1H_Handle= iMA(_Symbol,PERIOD_H1,MA_1H_Period,0,MODE_LWMA,PRICE_CLOSE);
//---Get Handle For 1 Hour ATR
ATR_1H_Handle= iATR(_Symbol,PERIOD_H1,ATR_1H_Period);
double TickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
double TickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
double TicksPerPoint = TickSize/point;
double PointValue = TickValue/TicksPerPoint;
//---Ensuring all data in the arrays are stored newest to oldestArraySetAsSeries(mrate,true); // Price ArrayArraySetAsSeries(MA_1D_Val,true); // 1 Day MA ArrayArraySetAsSeries(MA_4H_Val, true); // 4 Hour MA ArrayArraySetAsSeries(MA_1H_Val, true); // 1 Hour MA ArrayArraySetAsSeries(ATR_1H_Val, true); // 1 Hour ATR Array //--- Handling currency pairs with 5 or 3 Digit prices instead of 4
STP = (ATR_1H_Val[1]*1.5);
TKP = ATR_1H_Val[1];
if(_Digits==5 || _Digits==3)
{
STP = STP*10;
TKP = TKP*10;
}
// Calculating lot size based on Fixed risk (in base currency) and Fixex stop loss valuedouble RiskPoints = STP; //Stop Loss in Points//---Get Handle For RiskLots Calculation
Dynamic_Lot_Size_Handle= RiskAmmount/(PointValue*RiskPoints); //Calculating Lot Size
Final_Lots= NormalizeDouble(Dynamic_Lot_Size_Handle, 2); //Loading Varible For Lot Size //---return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+//| Expert deinitialization function |//+------------------------------------------------------------------+voidOnDeinit(constint reason)
{
//---Release our indicator Handles IndicatorRelease(MA_1D_Handle);
IndicatorRelease(MA_4H_Handle);
IndicatorRelease(MA_1H_Handle);
IndicatorRelease(ATR_1H_Handle);
}
//+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+voidOnTick()
{
//---Create an empty string for Buy and Sell Entrysstring entry ="";
//--- Create an empty string for Buy and Sell Exits string exit ="";
//---Get the Ask Pricedouble Ask=NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK),_Digits);
//---Get the bid price double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
//---- Check if this is the start of a new candlebar ----// We will use the static Old_Time variable to serve the bar time.// At each OnTick execution we will check the current bar time with the saved one.// If the bar time isn't equal to the saved time, it indicates that we have a new tick.staticdatetime Old_Time;
datetime New_Time[1];
bool IsNewBar=false;
// copying the last bar time to the element New_Time[0]int copied=CopyTime(_Symbol,PERIOD_H1,0,1,New_Time);
if(copied>0) // ok, the data has been copied successfully
{
if(Old_Time!=New_Time[0]) // if old time isn't equal to new bar time
{
IsNewBar=true; // if it isn't a first call, the new bar has appearedif(MQL5InfoInteger(MQL5_DEBUGGING)) Print("We have new bar here ",New_Time[0]," old time was ",Old_Time);
Old_Time=New_Time[0]; // saving bar time
}
}
else
{
Alert("Error in copying historical times data, error =",GetLastError());
ResetLastError();
return;
}
//--- EA should only check for new trade if we have a new barif(IsNewBar==false)
{
return;
}
//--- Defining Structures we will use for the tradeMqlTick latest_price; //To be used for getting the latest price quoteMqlTradeRequest mrequest; //To be used for sending trade requests MqlTradeResult mresult; //To be used to get our trade results //--- Get the last price quote using the MQL5 MqlTick Structureif(!SymbolInfoTick(_Symbol,latest_price))
{
Alert("Error getting the latest price quote - error:",GetLastError(),"!!");
return;
}
//--- Get the details of the latest 3 barsCopyRates(_Symbol,PERIOD_H1,0,3,mrate);
//--- Copy the new values of our indicators to buffers using the handleCopyBuffer(MA_1D_Handle,0,0,3,MA_1D_Val);
CopyBuffer(MA_4H_Handle,0,0,3,MA_4H_Val);
CopyBuffer(MA_1H_Handle,0,0,3,MA_1H_Val);
CopyBuffer(ATR_1H_Handle,0,0,3,ATR_1H_Val);
//--- Checking to make sure we only have one open position bool Buy_opened=false; // variable to hold the result of Buy opened positionbool Sell_opened=false; // variable to hold the result of Sell opened positionif (PositionSelect(_Symbol) ==true) // we have an opened position
{
if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
{
Buy_opened = true; //It is a Buy
}
elseif(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
{
Sell_opened = true; // It is a Sell
}
}
//--- Exit Rules - Long Exit - Using Exit indicator ---// /*
1. Exit long trade if most recent close price is below 1 Hour Moving average
*///--- Declaring the conditions for closing long/buy tradesbool Close_Buy_Condition_1 = (mrate[2].close > MA_1H_Val[2]);
bool Close_Buy_Condition_2 = (mrate[1].close < MA_1H_Val[1]);
//Long positions - Check if Exit condition is trueif (
(Close_Buy_Condition_1 == true)
&& (Close_Buy_Condition_2 == true)
)
{
exit = "close long";
}
// Close open buy positions if (exit == "close long")
trade.PositionClose(_Symbol, ULONG_MAX);
//--- Exit Rules - Short Exit - Using Exit indicator ---// /*
1. Exit long trade if closing price is Above 1 Hrour Moving average
*///--- Declaring the conditions for closing Short/Sell tradesbool Close_Sell_Condition_1 = (mrate[2].close < MA_1D_Val[2]);
bool Close_Sell_Condition_2 = (mrate[1].close > MA_1H_Val[1]);
//Short Positions - Check if Exit condition is trueif (
(Close_Sell_Condition_1 == true)
&& (Close_Sell_Condition_2 == true)
)
{
exit = "close short";
}
// Close open sell positionif (exit == "close short")
trade.PositionClose(_Symbol, ULONG_MAX);
//--- Entry Rules - Long Entry --- ///*
1. 1 Hour Closing Price must be greater than the current[0] 1 Day MA Value
2. 1 Hour Closing Price must be greater than 4 Hour Moving Average
3. 1 Hour Closing Price must be greater than 1 hour Moving Average
*///--- Declaring the buy conditions using bool (True/False) functionsbool Buy_Condition_1 = (mrate[1].close > MA_1D_Val[1]);
bool Buy_Condition_2 = (mrate[1].close > MA_4H_Val[1]);
bool Buy_Condition_3 = (mrate[1].close > MA_1H_Val[1]);
//Buy Entry Check all conditions are trueif(
(Buy_Condition_1 == true)
&& (Buy_Condition_2 == true)
&& (Buy_Condition_3 == true)
)
{
entry = "buy";
}
//Place Buy Entry if (entry == "buy" && PositionsTotal()<1)
trade.Buy(Final_Lots, NULL, Ask,(Ask - STP * _Point),(Ask + TKP * _Point),NULL);
//--- Entry Rules - Short Entry --- ///*
1. 1 Hour Closing Price must be less than the current[0] 1 Day MA Value
2. 1 Hour Closing Price must be less than 4 Hour Moving Average
3. 1 Hour Closing Price must be less than 1 hour Moving Average
*///--- Declaring the buy conditions using bool (True/False) functionsbool Sell_Condition_1 = (mrate[1].close < MA_1D_Val[1]);
bool Sell_Condition_2 = (mrate[1].close < MA_4H_Val[1]);
bool Sell_Condition_3 = (mrate[1].close < MA_1H_Val[1]);
//Buy Entry Check all conditions are trueif(
(Sell_Condition_1 == true)
&& (Sell_Condition_2 == true)
&& (Sell_Condition_3 == true)
)
{
entry = "sell";
}
//Place Sell Entry if (entry == "sell" && PositionsTotal()<1)
trade.Sell(Final_Lots, NULL, Bid,(Bid + STP * _Point),(Bid - TKP * _Point),NULL);
}
//+------------------------------------------------------------------+
Hello Everyone,
As the title state I am running into issues trying to test my EA on using the strategy tester.
I have include the complete code.
When I attempt to "test" the EA, there is a significant delay and no trades are opened.
Also, when I use the Visual mode the strategy tester opens two windows and again, no trades are opened and no chart is displayed.
This error began to occur after I added the Dynamic lot size calculation based on an ATR stop loss.
The code compiles with out error and I am unable to determine what is causing the issue.
Any assistance is appreciated.
See my full EA below: