Really odd: EA function gets called before OnInit()... - page 2

 
Alain Verleyen:
void CheckForTrade()
   {
   Print("checkfortrade");
   static int prev_level = GetPriceLevel(Close[0]);
   int curr_level = GetPriceLevel(Close[0]);
...
   }
A static is initialized BEFORE OnInit() so your GetPriceLevel() function is called, your message "the pips is..." is printed and you get a Zero Divide error as "pips" is 0.
Also you are using Close[0] before OnInit() so you will get sooner or later an Array Out of Range error too.

Ahhh I see! I didn't know that. I thought it would be initialized the first time the function is called. I've got to remember that, thanks :)

Marco vd Heijden:

Ah i was too late ;)

Anyway try this:

void CheckForTrade()
   {
   Print("checkfortrade");
   int prev_level = GetPriceLevel(Close[0]);
   int curr_level = GetPriceLevel(Close[0]);
   if (curr_level != prev_level)
      {
      // there must be hedged positions for each level
      if (GetLongTicket(curr_level) < 0) OpenLongPosition();
      if (GetShortTicket(curr_level) < 0) OpenShortPosition();
      
      // buy low sell high
      if (curr_level < prev_level) // buy low when price dropped
         {
         int pos = curr_level - ShortTicketsStartLevel;
         // realize profits from positions that are in profit
         for (int i = pos+InpTakeProfitLevels; i < ArraySize(ShortTickets); i++) // look above
            CloseShortPosition(i);
         // realize loss from positions that are too far away below
         for (int i = pos-InpLevels; i >= 0; i--) // look below
            CloseShortPosition(i);
         }
      else if (curr_level > prev_level) // sell high when price rises
         {
         int pos = curr_level - LongTicketsStartLevel;
         // realize profits from positions that are in profit
         for (int i = pos-InpTakeProfitLevels; i >= 0; i--) // look below
            CloseLongPosition(i);
         // realize loss from positions that are too far away above
         for (int i = pos+InpLevels; i < ArraySize(ShortTickets); i++) // look above
            CloseLongPosition(i);
         }
      }
   prev_level = curr_level;
   }

 That won't work, you just made the static variable non static, now it's always the same as prev_level. I made it static so the function remembers it the next time. The fix should be easy now that I know what went wrong.

 

Thank you all for your help :) 

 
At least it initializes so it does get rid of the issue..
 
  1. RichPiano: Ahhh I see! I didn't know that.
    Yes you did
    whroeder1:
    1. You haven't posted any code that calls that, so we can't possibly help. There are no mind readers here - we can't see your code.
    2. The order of initialization is global and static variables, then OnInit. Do you have a class with a constructor that calls things?

  2. Do not look at chart values (close[0], tick_value, etc.) until the first OnTick. There may be no chart yet.
  3. Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 forum.) Always use time. New candle - MQL4 forum
    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
  4. Why did you post your MT4/EA question in the MT5/Technical Indicators section instead of the MT4 section?
 
whroeder1:






  1. Why did you post your MT4/EA question in the MT5/Technical Indicators section instead of the MT4 section?
Good point - moved.
Reason: