Close position after X hours

 

Hi guys,

i'm new and I need your help.

I'm writing an EA and I want close position at market after xx hours (maxHour value) if i'm NOT at breakeven..So i wrote like this (long example)

for (int i = OrdersTotal() - 1; i >= 0; i--)
         {if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

                 if((OrderOpenTime()<=TimeCurrent()-MaxHour*60*60) && (OrderProfit()<0))
                 {
                   bool ret3=OrderClose(OrderTicket(),OrderLots(),Ask,4,clrRed);
                     if (ret3==false)
                    Print("OrderClose error - ", GetLastError());
                    }
                 }

 

Debug is correct, but when i try to backtest it doesn't exit position.

EA entry good on 9am time... 

Why? What's wrong?

 

Sorry for my english 

 

What do you get from

          Print("OrderClose error - ", GetLastError());

You don't check whether it is a Buy order, but use Ask to close. It is better to use OrderClosePrice() which will not matter whether it is a Buy or a Sell.

 

Thank you modified OrderClosePrice()...

 I get no error ...the trades goes to stoploss 

 

Then either

for (int i = OrdersTotal() - 1; i >= 0; i--)

is not being executed or

     if((OrderOpenTime()<=TimeCurrent()-MaxHour*60*60) && (OrderProfit()<0))

is not returning true

 
GumRai:

Then either

is not being executed or

is not returning true


So what can I do? I not understand 
 

You check.

A simple Print will show if a block of code is being executed

Print("for loop is being executed");
for (int i = OrdersTotal() - 1; i >= 0; i--)

If the print shows in the log then you know that the for loop is being executed.

Remove the print code once you have checked

Next check

     if((OrderOpenTime()<=TimeCurrent()-MaxHour*60*60) && (OrderProfit()<0))

What value have you assigned to MaxHour?

 

just 1 to try and see immediately...ok i will check and let you know 

 
GumRai:

You check.

A simple Print will show if a block of code is being executed

If the print shows in the log then you know that the for loop is being executed.

Remove the print code once you have checked


No...in the logs there's not my print comand!!

 
Then find out why it is not being executed
 
ok...but how?? i'm not expert
 

If you have written the EA as you say, you should be able to follow the code to see what it is doing and why the for loop is not being executed.

If you can't, then you are trying to work on something that is too complicated for your level.