I have a function that checks once per bar to see if an order was closed on that bar at a loss. If it was a loss, it will set the Global Variable with the timestamp of that bar, otherwise set to 0. When the next trade signal is triggered I then check that GV and find out how many bars ago the loss occurred.
I'm using this EA on two different time frames the M1 and M5. On the M1 it works exactly as I need it to but on the M5 it doesn't.
here's the function:
Code: Select all
//+----------------------------------------------------------------------------+
// |
//+----------------------------------------------------------------------------+
double SetLastOrderStatus(){
int TimeDivisor=60;
if(Period() == 5){
TimeDivisor=280;
}
//check last order
//sell
for(int pos=OrdersHistoryTotal();pos>=0; pos--){
if (OrderSelect(pos,SELECT_BY_POS,MODE_HISTORY)&&OrderMagicNumber()==MagicNumber&& OrderSymbol()==Symbol()){
if(OrderType()==OP_SELL){
if(OrderProfit()>=0){
GlobalVariableSet(Symbol()+"_SellLastOrderStatus",0);
}
else{
GlobalVariableSet(Symbol()+"_SellLastOrderStatus",OrderCloseTime());
break;
}
}//buy
else if(OrderType()==OP_BUY){
if(OrderProfit()>=0){
GlobalVariableSet(Symbol()+"_BuyLastOrderStatus",0);
}
else{
GlobalVariableSet(Symbol()+"_BuyLastOrderStatus",OrderCloseTime());
break;
}
}
}
}
Print("GetGV:" +TimeNow+" TimeDiv:"+TimeDivisor+" BarCnt:"+MathAbs((Time[0]-GlobalVariableGet(Symbol()+"_SellLastOrderStatus"))/TimeDivisor));
}
Here's a screenshot of the M1. Note that BarCnt counts in sequence from 0 to 26. It does not miss any numbers in between. And on the chart I have labeled 0 as the bar where the last loss occurred and 26 is the last bar and corresponds to the BarTime.
TimeDivisor=280 for M5.
Here's a screenshot for M5 Note that it counts in sequence until it gets to 15.
I have tried from 278 to 283 and none work correctly, they all skip a number somewhere in between 1 and 20.
I don't know why it would be 280 on M5. 60 on the M1 makes sense because there are 60 minutes in an hour but I have no idea what 280 corresponds to and why it seems to be the closest to what I need but still skips a number. 300 would make more sense because if 60 works on M1 and there's 5 times more minutes on the M5 than 5×60 = 300. Or are there only 4 times more minutes in an hour in which case 4×60 = 240.