No matter which expert advisor I try (downloaded some from here and tried them), when I do the backtest, it opens the positions but they never close, so the tester is forced to close all positions at the last tested candle (see picture)
seems to be a general problem with the MT5 settings?
and also, how is it even possible that I have opposite positions open at the same time (long AND short simultaneously, see picture below), I thought the FIFO rule only permits 1 open position per symbol
normally, the first long position should be closed when the second position (short position) is taken, so the blue line should connect to the red arrow instead of the very last candle from the first picture,
I would really appreciate some help here...thanks!
It should be errors in expert or journal tabs
Hello, no there are no errors, only after some time, there are so many open positions that there is not enough margin left to open another trade, so at some point it says error not enough margin, and at the end of the test, it closes all positions "position closed due to end of test"
Maybe it's time for some code.
Okay, The problem in my specific example was using a simple bollinger bands EA that I modified, basically buying once the price is below the lower band and selling when the price is above the middle line. The problem is, that once the EA is selling, it is opening a new (short) position instead of closing the opened buy position. That is why it opens thousands of sell orders because the first buy order is still open.
Here's the code:
#property copyright "Copyright 2016, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" input int bands_period= 20; // Bollinger Bands period input int bands_shift = 0; // Bollinger Bands shift input double BBdeviation= 2; // Standard deviation input double BBLot=0.1; // Lots to trade input double SL=0; input double TP=0; input int EA_Magic=12345; // EA Magic Number input ENUM_TIMEFRAMES bollingerTimeframe=PERIOD_M5; //--- global variables int bollingerHandle; // Bolinger Bands handle double BBUp[],BBLow[],BBMiddle[]; // dynamic arrays for numerical values of Bollinger Bands //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- Do we have sufficient bars to work if(Bars(_Symbol,_Period)<60) // total number of bars is less than 60? { Alert("We have less than 60 bars on the chart, an Expert Advisor terminated!!"); return(-1); } bollingerHandle=iBands(NULL,bollingerTimeframe,bands_period,bands_shift,BBdeviation,PRICE_CLOSE); return(0); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- release indicator handles IndicatorRelease(bollingerHandle); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { MqlTradeRequest mrequest; // Will be used for trade requests MqlTradeResult mresult; // Will be used for results of trade requests MqlTradeCheckResult check; ZeroMemory(mrequest); ZeroMemory(mresult); MqlTick latest_price; // To be used for getting recent/latest price quotes MqlRates mrate[]; // To be used to store the prices, volumes and spread of each bar double Ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK); // Ask price double Bid = SymbolInfoDouble(_Symbol,SYMBOL_BID); // Bid price // the rates arrays ArraySetAsSeries(mrate,true); // the indicator arrays ArraySetAsSeries(BBUp,true); ArraySetAsSeries(BBLow,true); ArraySetAsSeries(BBMiddle,true); //--- Copy the new values of our indicators to buffers (arrays) using the handle if(CopyBuffer(bollingerHandle,0,0,3,BBMiddle)<0 || CopyBuffer(bollingerHandle,1,0,3,BBUp)<0 || CopyBuffer(bollingerHandle,2,0,3,BBLow)<0) { Alert("Error copying BollingerBands indicator Buffers - error:",GetLastError(),"!!"); return; } if(PositionSelect(_Symbol) && Ask>BBMiddle[0]){ //check if there is a position opened and check if condition for closing the position is met //if both is true, we are ready to close position: ZeroMemory(mrequest); ZeroMemory(mresult); mrequest.action = TRADE_ACTION_DEAL; // immediate order execution mrequest.price = Bid; // latest Bid price mrequest.sl = 0; // Stop Loss mrequest.tp = 0; // Take Profit mrequest.symbol = _Symbol; // currency pair mrequest.volume = BBLot; // number of lots to trade mrequest.magic = EA_Magic; // Order Magic Number mrequest.type= ORDER_TYPE_SELL; // Sell Order mrequest.type_filling = ORDER_FILLING_FOK; // Order execution type mrequest.deviation=100; // Deviation from current price //--- send order OrderSend(mrequest,mresult); if (OrderCheck (mrequest,check) ) { if (!OrderSend(mrequest, mresult)) //--- if order send fail you know what causing it { Print ("error ",GetLastError()," return code ",mresult.retcode," broker comments ", mresult.comment); } else { if (mresult.retcode != (0 || TRADE_RETCODE_PLACED || TRADE_RETCODE_DONE) ) { Print ("error ",GetLastError()," return code ",mresult.retcode," broker comments ", mresult.comment); } } } else { Print ("error ",GetLastError()," check code ",check.retcode); } if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed { Alert("A Sell order has been successfully placed with Ticket#:",mresult.order,"!!"); } else { Alert("The Sell order request could not be completed -error:",GetLastError()); ResetLastError(); return; } Print("Schliesse Position Ask: ",Ask," Bid: ",Bid); if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL){Print("Short Position still opened");} Sleep(300000); }else{ //if no position is open yet or no condition for closing an open position is met, code continues here and we want to look for an entry signal: if(Ask<BBLow[0]){ //looking for signal for long position if(!PositionSelect(_Symbol)) //only open position when there are no opened positions yet { ZeroMemory(mrequest); ZeroMemory(mresult); mrequest.action = TRADE_ACTION_DEAL; // Immediate order execution mrequest.price = NormalizeDouble(Ask,_Digits); // Lastest Ask price mrequest.sl = 0; // Stop Loss mrequest.tp = 0; // Take Profit mrequest.symbol = _Symbol; // Symbol mrequest.volume = BBLot; // Number of lots to trade mrequest.magic = 0; // Magic Number mrequest.type = ORDER_TYPE_BUY; // Buy Order mrequest.type_filling = ORDER_FILLING_FOK; // Order execution type mrequest.deviation=100; // Deviation from current price OrderSend(mrequest,mresult); // Send order } Print("Open Position on Ask: ",Ask," Bid: ",Bid); if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY){Print("Long Position is opened");} } if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed { Alert("A Buy order has been successfully placed with Ticket#:",mresult.order,"!!"); } else { Alert("The Buy order request could not be completed -error:",GetLastError()); ResetLastError(); return; } } }
Your are using EA provided for a netting account on a hedge account.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
No matter which expert advisor I try (downloaded some from here and tried them), when I do the backtest, it opens the positions but they never close, so the tester is forced to close all positions at the last tested candle (see picture)
seems to be a general problem with the MT5 settings?
and also, how is it even possible that I have opposite positions open at the same time (long AND short simultaneously, see picture below), I thought the FIFO rule only permits 1 open position per symbol
normally, the first long position should be closed when the second position (short position) is taken, so the blue line should connect to the red arrow instead of the very last candle from the first picture,
I would really appreciate some help here...thanks!