How can I close an order after 25 bars?(or solve this error)

 

hi, I'm getting this error:

'==' - illegal operation use    moving average cross strategy.mq5       170     18

when using this:

      if(holding == bar[0])
        {
         trade.PositionClose(_Symbol, 5);
        }

with this

   MqlRates bar[];
   ArraySetAsSeries(bar,true);
   int start_pos_b=0,count_b=25;
   CopyRates(m_symbol.Name(),PERIOD_CURRENT,start_pos_b,count_b,bar);

and

input int holding = 25; // holding period

What is wrong, and how can I fix this?

 

I've also tried with this

if(bar[holding-1] != EMPTY_VALUE )
        {
         trade.PositionClose(_Symbol, 5);
        }

but the error persits.

'!=' - illegal operation use    moving average cross strategy.mq5       148     25

what's wrong?

 

Create a global variable called "current_hold=0;"

  • set it to zero on init
  • when you enter a trade set it to 0
  • for every new bar that forms after a trade increase it by 1
  • when you hit the limit (the input variable) then close the trade and reset the current_hold to zero
 
  1. Lorentzos Roussos #: for every new bar that forms after a trade increase it by 1

    EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static / global count variables will have been lost.

  2. Javier Santiago Gaston De Iriarte Cabrera: How can I close an order after 25 bars

    Get the order open time, use iBarShift to find the bar index, close when it is 25 or greater.

 
William Roeder #:
  1. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static / global count variables will have been lost.

  2. Get the order open time, use iBarShift to find the bar index, close when it is 25 or greater.

I haven't even though of that . 

Use this OP

 

In your code "bar" is an array of MqlRates which is a struct data type.

You are getting "illegal operation use" error because you are trying to compare struct with integer. This can't be done.

Read again documentation on CopyRates and study the code example on that page. 

Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / History Data Structure
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / History Data Structure
  • www.mql5.com
History Data Structure - Data Structures - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
William Roeder #:
  1. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static / global count variables will have been lost.

  2. Get the order open time, use iBarShift to find the bar index, close when it is 25 or greater.

Thanks, it worked

 
I was not very sure of how to use iBarshift, so I tried with Bars() and setting start time when order is trigered, and end date time to now. and seems to work.
Reason: