OrderClosePrice() returns incorrect value

 

Dear community,


I'm a newbie here, and I seem to be receiving incorrect results from the MT4 tester (Version 4.00 Build 225) when running this piece of code:


if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_HISTORY)==true){
// Closed trade
order_closed_bar = Bars;
order_states[rec][ORDER_CLOSED_BAR] = order_closed_bar;
order_states[rec][ORDER_OPEN_PRICE] = OrderOpenPrice();
order_states[rec][ORDER_CLOSE_PRICE] = OrderClosePrice();
order_states[rec][ORDER_CLOSE_TIME] = OrderCloseTime();
log_trade(ticket);
Print("Rec: ", rec, " containing order: ", ticket, " opened at: ", OrderOpenPrice(), " closed at: ", OrderClosePrice());
} else Print("OrderSelect failed error code is ", GetLastError(), " ticket: ", ticket);


The code runs, it does not produce any errors and the price returned by OrderOpenPrice() is correct. But, the price returned by OrderClosePrice() is VERY different from the price in the tester's "Results" and "Journal" windows:


For example, trade #7 can be seen to open in the "Results" window at at 1.48580 and closes at 1.49242:


13 2009.01.07 08:30 sell 7 0.10 1.48580 0.00000 0.00000 0.00 9727.80
14 2009.01.07 11:30 close 7 0.10 1.49242 0.00000 0.00000 -66.20 9661.60


The same can be seen in the Journal. My code above also opens at 1.48580 BUT OrderClosePrice() returns 1.48620 :(

2010.01.06 12:14:06 2009.01.07 11:30 spandau_ballet_v3.1 GBPUSD,M30: close #7 sell 0.10 GBPUSD at 1.48580 at price 1.49242
2010.01.06 12:14:06 2009.01.07 08:30 spandau_ballet_v3.1 GBPUSD,M30: Rec: 6 containing order: 7 opened at: 1.4858 closed at: 1.4862



Can anybody suggest what I might have done wrong?


Many thanks,


Monu

 

order_closed_bar = Bars;

order_states[rec][ORDER_CLOSED_BAR] = order_closed_bar;


Bars could be anything 6, 16000? It also will max out at something (see options.) Then every entry will have the same value.


rec, is what? and are you resizing the array each time?


order_states[rec][ORDER_CLOSE_TIME] = OrderCloseTime();

order_states[][] is a double? but you're storing ints/doubles/ and datetimes in it?


Can anybody suggest what I might have done wrong?

Does "log_trade(ticket)" do another orderSelect, possible non history?
 

Hi WHR,


Thanks for your reply. I have solved the problem for myself.


It turns out that for some reason "MODE_HISTORY" was returning orders that were not closed and therefore the OrderClosePrice() was incorrect. I worked around this problem by testing that OrderCloseTime() != 0, which solves the problem.


Thanks again for taking the trouble to reply!


Regards,


Monu

 
monuogbe:


It turns out that for some reason "MODE_HISTORY" was returning orders that were not closed and therefore the OrderClosePrice() was incorrect.

I highly doubt it.... If OrderSelect() was that buggy this forum would have been full of posts about it and they would have fixed it a long time ago. There must be something else wrong.

Reason: