Need help to troubleshoot a simple code

 

Hi

I would need some help with my code. I have stripped it to the bare minimum and posted it below. Basically the code below says when 12EMA crosses above 26EMA and if the current price is above the closing price of Bar[1]+30pips, i would enter into a long order for 2 lots. I will then close 1 lot when price>=orderopenprice+DailyATR.

Now the problem is as soon as i long 2 lots, 1 lot will automatically close within 1-2 minutes. Im sure it is due to the part which is underlined but i am unable to find anything wrong with it. This happened on backtesting on MT4 on a demo account with 2 different brokers. The order so far had not closed on forward testing. I previously had an earlier version of the code and it worked as expected but i have deleted it. Does anyone know what is wrong?


int init(){

  return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {

//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start() 
{
////////////////////////////////////////////////////////////////////////////////////////
double RSI_0=iRSI(NULL,0,14,PRICE_OPEN,0); //initialisation current RSI
double Daily_ATR=iATR(NULL,PERIOD_D1,20,0);//initialisation current ATR
double Buy_Stop_Loss1;  


////////CURRENT 12MA CROSSES ABOVE 26MA////////////////////////////////////////////
if(iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,1)< iMA(NULL,0,26,0,MODE_EMA,PRICE_CLOSE,1) 
&& iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,0)>iMA(NULL,0,26,0,MODE_EMA,PRICE_CLOSE,0) 
&& RSI_0>50) //if 12MA crosses above>26MA and RSI>50
{
////////////////entry////////////////////////////////////////////////////////

if(OrderSelect(0,SELECT_BY_POS)!=true) //if there is no open order
if(Bid>Close[1]+0.003)  //if current bid price becomes more than 30pips than Bar1 close
{
Buy_Stop_Loss1=(Low[1]-0.001);   //Initial stop Loss of "long order"
OrderSend("EURUSD",OP_BUY,2.0,Ask,3,Buy_Stop_Loss1,0,NULL,0,0,Red); //enter market long order for 2lots
}     
}


///////////////////closing 1 position after 1st TP reached  ///////////////////////
if((OrderSelect(0,SELECT_BY_POS)==true) && (OrderType() == OP_BUY) && (OrderLots()==2.0) )  //if there is any 2lots open buy order
{
if(Bid>=OrderOpenPrice()+Daily_ATR) //if ask price is more than equal to TP, TP(Orderopenprice + DailyATR)
OrderClose(OrderTicket(),1.0,Bid,3,Aqua);    //close 1 lot of open order at current ask price
}

return(0);}
 

I backtested on a IBFX demo and noticed this problem from data in 2008 till 2010. However testing from Jan 2010 till Jul 2010 found no problems. I find this very weird.

Tested on MBT demo account and also the same issue, however i noted 1 out of 5 the 5 long orders from Jan to Jul 2010 closed correctly, the other 4 closed as soon as long order is entered. The inconsistencies really puzzle me.

 

Something I'd try is to insert some Alerts where you think your problem is. The idea is that you want to see exactly what's going on when MT starts executing the possible trouble spot. For simple logical errors, this works the vast majority of the time. For example, do an Alert(Daily_ATR) and compare the results with what you expect by inspecting the chart. What I do in the backtester is open up the .txt log, do a CTRL+F to find any result, then go to the date and compare. I usually do a few trials of this (which you should do since the problem is inconsistent). Another thing you could do is use Visual Mode (that eliminates the problem of a bunch of Alerts showing up in the journal).

Just my $0.02. Hope it helps.

 
ubuntuboy:

Something I'd try is to insert some Alerts where you think your problem is. The idea is that you want to see exactly what's going on when MT starts executing the possible trouble spot. For simple logical errors, this works the vast majority of the time. For example, do an Alert(Daily_ATR) and compare the results with what you expect by inspecting the chart. What I do in the backtester is open up the .txt log, do a CTRL+F to find any result, then go to the date and compare. I usually do a few trials of this (which you should do since the problem is inconsistent). Another thing you could do is use Visual Mode (that eliminates the problem of a bunch of Alerts showing up in the journal).

Just my $0.02. Hope it helps.


Hi

Thanks for the advice. Actually i had alerts in the code but i stripped them off to the bare minimum to post it here for easier and quicker reading. I am too using visualisation. The red(Buy) and aqua(Close) arrows showed up just adjacent to each other. I will follow you advice and insert the Alert for Daily_ATR just to be sure.

 

Have removed the Daily_ATR from the equation and replaced it with an arbitary number of 0.0100 and all the errors are gone.

So the problem was putting the Daily_ATR in the equation, does anyone have any idea why the Daily_ATR was causing the problem? I would really like to use the Daily_ATR rather than an arbitary no. of pips value

since the price range for different pairs differs

 
One thing might be that you're using a 0 shift, and I don't know much about ATR, but since the 0th bar is the one currently being formed, I would think indicators at bar 0 would also be shifting. For some applications it's not a big deal, but for this I think it might be causing you trouble.
Reason: