MT5 only backtest a few hours

 

Hello, 

I have a simple strategy but my backtest only returns a couple of hours worth of trades (usually from midnight to 1am, or from midnight to 4am). 

Even though I can see in the chart that the buy/sell condition happened multiple times after those hours.

I also noticed that the stop loss and take profit prices on the backtest report are all over the place, meaning, they vary wildly, instead of being in a closer range to the points I indicated on the code.

I'm at the end of my rope, what am I doing wrong?


Thank you.


Strategy:

-Buy 1 NQ contract (Nasdaq Futures) when price on the 3 minute chart is larger than the 100 EMA

-Sell 1 NQ contract when price on the 3 minute chart is smaller than the 100 EMA


Conditions:

-Stop loss: 400 points

-Take profit: 1100 points


Backtest:

I'm using the current contract ENQM20

3  minute char

I tried a custom range of 22 - 26 march 2020 and also 1 month 

This new contract started on March 20, 2020 , so those dates are available.


Code:


#include<Trade\Trade.mqh>

//Create an instance of CTrade

CTrade trade;



void OnTick()

{

//we calculate the ask price

double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK), _Digits);



//we calculate the bid price

double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID), _Digits);



//we create an array for the prices

MqlRates PriceInfo[];



//we fill the array with the price data

int PriceData =CopyRates(_Symbol,_Period,0,3,PriceInfo);



//create a string for the signal

string signal="";



//create an array for several prices

double myMovingAverageArray[];



//define the properties of the moving average

int movingAverageDefinition = iMA (_Symbol,_Period,100,0,MODE_EMA,PRICE_CLOSE);



//defined EA, one line, current candle, 3 candles, store result

CopyBuffer(movingAverageDefinition,0,0,3,myMovingAverageArray);



//if the price is above the EMA

if (PriceInfo[1].close > myMovingAverageArray[1])



//and was below the SMA before

if (PriceInfo[2].close < myMovingAverageArray[2])

{

signal="buy";

}



//if the price is below the SMA

if (PriceInfo[1].close < myMovingAverageArray[1])



//and was above the SMA before

if (PriceInfo[2].close > myMovingAverageArray[2])

{

signal="sell";

}



//sell 1 contract

if (signal =="sell" && PositionsTotal()<1)

trade.Sell(1,NULL,Bid,Bid+400*_Point,Bid-1100* _Point,NULL);



//Buy 1 contract

if (signal =="buy" && PositionsTotal()<1)

trade.Buy(1,NULL,Ask,Ask-400*_Point,Ask+1100* _Point,NULL);



//chart output

Comment ("The signal is now: ",signal);



}
Basic Principles - Trading Operations - MetaTrader 5 Help
Basic Principles - Trading Operations - MetaTrader 5 Help
  • www.metatrader5.com
is an instruction given to a broker to buy or sell a financial instrument. There are two main types of orders: Market and Pending. In addition, there are special Take Profit and Stop Loss levels. is the commercial exchange (buying or selling) of a financial security. Buying is executed at the demand price (Ask), and Sell is performed at the...
This website uses cookies. Learn more about our Cookies Policy.