A mystery in Time

Post Reply
fxprotrader
Trader
Posts: 26
Joined: Sat Jun 16, 2012 11:26 pm

A mystery in Time

Post by fxprotrader »

I'm hoping somebody can shed some light on as to why this does not work.

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));

}
TimeDivisor=60 for M1.
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.
gvm1.PNG

TimeDivisor=280 for M5.
Here's a screenshot for M5
gvm5.PNG
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.
You do not have the required permissions to view the files attached to this post.
Big Be
Trader
Posts: 52
Joined: Thu Nov 01, 2012 6:12 pm

Re: A mystery in Time

Post by Big Be »

1. Do not try to do it in the print statement
2. Use OrderOpenTime( ).
After you select your order (may need to use MODE_TRADES), then
int EntryBarNumber = iBarShift(Symbol(),0,OrderOpenTime(),false);
Should work.

Big Be
"Big Be"
fxprotrader
Trader
Posts: 26
Joined: Sat Jun 16, 2012 11:26 pm

Re: A mystery in Time

Post by fxprotrader »

Big Be wrote:1. Do not try to do it in the print statement
2. Use OrderOpenTime( ).
After you select your order (may need to use MODE_TRADES), then
int EntryBarNumber = iBarShift(Symbol(),0,OrderOpenTime(),false);
Should work.

Big Be
Big Be-
Thanks for your suggestions.
I'm only using the print statement so that I can see the value that I have at each bar. This would not be the way I would use the information once I have what I need.

You did give me a good idea which I thought was working however it relies on MODE_HISTORY using iBarShift(). I need a solution that does not require knowing what closed orders are in history, except for the bar after the close of an order. At that point I need to set the timestamp to the GV which I can retrieve at any time then calculate how many bars since that order closed.
The reason for this is MODE_HISTORY is dependent upon what the user setting is in Account History, for example if he has it set to Today which means only today's trades would show and let's say the last order closed at a loss late in the day. A new order triggers on the next day I would be unable to check whether the last order was a loss or not because there is no trades in history.

The only question I really need to have an answer to is the TimeDivisor.
Why on M1 does it work perfectly if I set it to 60. But when I moved to M5 there doesn't seem to be a number that works.

double TimeDivisor=60;
if(Period() == 5){
TimeDivisor=294;
}
User avatar
gaheitman
Trader
Posts: 655
Joined: Tue Nov 15, 2011 10:55 pm
Location: Richmond, VA, US

Re: A mystery in Time

Post by gaheitman »

fxprotrader wrote:
Big Be wrote:1. Do not try to do it in the print statement
2. Use OrderOpenTime( ).
After you select your order (may need to use MODE_TRADES), then
int EntryBarNumber = iBarShift(Symbol(),0,OrderOpenTime(),false);
Should work.

Big Be
Big Be-
Thanks for your suggestions.
I'm only using the print statement so that I can see the value that I have at each bar. This would not be the way I would use the information once I have what I need.

You did give me a good idea which I thought was working however it relies on MODE_HISTORY using iBarShift(). I need a solution that does not require knowing what closed orders are in history, except for the bar after the close of an order. At that point I need to set the timestamp to the GV which I can retrieve at any time then calculate how many bars since that order closed.
The reason for this is MODE_HISTORY is dependent upon what the user setting is in Account History, for example if he has it set to Today which means only today's trades would show and let's say the last order closed at a loss late in the day. A new order triggers on the next day I would be unable to check whether the last order was a loss or not because there is no trades in history.

The only question I really need to have an answer to is the TimeDivisor.
Why on M1 does it work perfectly if I set it to 60. But when I moved to M5 there doesn't seem to be a number that works.

double TimeDivisor=60;
if(Period() == 5){
TimeDivisor=294;
}
I think you want to combine the methods. Take your stored time and use that as the parameter to iBarShift().

With Empty4, we aren't guaranteed to have a bar for every time period. So, while there should be 1440 bars a day on a 1-minute chart, we won't always have that many. We certainly don't have them spanning the weekends, and for the quiet times or on holidays you will lose many bars because Empty4 only gives you a bar if it gets a tick during that time (or your broker sends a tick). This means your method will always run into problems even if you figure out the TimeDivisor. The built in procedure iBarShift() will give you the correct bar offset (which is equivalent to the count back) for the actual chart/timeframe you are interested in. It is logically the same as the following loop

Code: Select all

bar = 0;
while (Time[bar] > TimeToCheck)
   bar = bar + 1;
George
fxprotrader
Trader
Posts: 26
Joined: Sat Jun 16, 2012 11:26 pm

Re: A mystery in Time

Post by fxprotrader »

gaheitman wrote:
fxprotrader wrote:
Big Be wrote:1. Do not try to do it in the print statement
2. Use OrderOpenTime( ).
After you select your order (may need to use MODE_TRADES), then
int EntryBarNumber = iBarShift(Symbol(),0,OrderOpenTime(),false);
Should work.

Big Be
Big Be-
Thanks for your suggestions.
I'm only using the print statement so that I can see the value that I have at each bar. This would not be the way I would use the information once I have what I need.

You did give me a good idea which I thought was working however it relies on MODE_HISTORY using iBarShift(). I need a solution that does not require knowing what closed orders are in history, except for the bar after the close of an order. At that point I need to set the timestamp to the GV which I can retrieve at any time then calculate how many bars since that order closed.
The reason for this is MODE_HISTORY is dependent upon what the user setting is in Account History, for example if he has it set to Today which means only today's trades would show and let's say the last order closed at a loss late in the day. A new order triggers on the next day I would be unable to check whether the last order was a loss or not because there is no trades in history.

The only question I really need to have an answer to is the TimeDivisor.
Why on M1 does it work perfectly if I set it to 60. But when I moved to M5 there doesn't seem to be a number that works.

double TimeDivisor=60;
if(Period() == 5){
TimeDivisor=294;
}
I think you want to combine the methods. Take your stored time and use that as the parameter to iBarShift().

With Empty4, we aren't guaranteed to have a bar for every time period. So, while there should be 1440 bars a day on a 1-minute chart, we won't always have that many. We certainly don't have them spanning the weekends, and for the quiet times or on holidays you will lose many bars because Empty4 only gives you a bar if it gets a tick during that time (or your criminal sends a tick). This means your method will always run into problems even if you figure out the TimeDivisor. The built in procedure iBarShift() will give you the correct bar offset (which is equivalent to the count back) for the actual chart/timeframe you are interested in. It is logically the same as the following loop

Code: Select all

bar = 0;
while (Time[bar] > TimeToCheck)
   bar = bar + 1;
George
George-
Thanks, works absolutely perfectly.
User avatar
Jemook
Trader
Posts: 1085
Joined: Thu May 10, 2012 10:19 am
Location: Bondi, Sydney, Australia

Re: A mystery in Time

Post by Jemook »

George to the rescue!! Love your work mate.
Please note I am no longer affiliated with Global Prime. I've moved on to my next adventure with Afterprime.

Catch me here: https://www.afterprime.com
Post Reply

Return to “Coders Hangout”