Download MetaTrader 5

Doubt (noob?) about MQ4 code running comparison only once in OnTick()

To add comments, please log in or register
Unable to find the right application? Order it!
Rossella.Dy
13
Rossella.Dy 2015.02.10 19:52 

Hi all,

 I have just started approaching this language but I know other scripting languages.

As usual, you start from the simple things and there is something that is half-a-way working, and this confuses me; the articles on the net and the posts on the forum I came across do not seem to provide an answer.

 

In the OnTick() function, I do a simple check and (of course) I would like to do that until I stop it and detach it from the chart.

It works fine the first time: when the first condition is verified, it prints the "Buy" string, when the second condition is verified it prints the "Sell" string.

 

 PROBLEM: it works ONLY the firs iteration, meaning that after the sell it never Prints the "Buy" again (even if the conditions are indeed verified).

Am I missing something specific about the MQ4 rationale? Do I need to refresh values (I considered and tried the RefreshRates() but does not seem to fit the purpose here)? 

Any hints? 


 Thank you, appreciated

 

Sorry if it's pseudocode but I got the real compiler in another laptop 

gTicket = 0 

if  ( (gTicket==0) && (Value_a<Value_b) )

        Print ("Buy at: ", Tick.Bid)

        gTicket = 1 

 else if ( (gTicket==1) && (Value_c<Value_d) )

        Print ("Sell at: ", Tick.Ask)

        gTicket = 0
end
Alain Verleyen
Moderator
30719
Alain Verleyen 2015.02.10 20:44  

Please post real mql4 code. Including the declaration of your variables.

Impossible to help as is.

Rossella.Dy
13
Rossella.Dy 2015.02.10 22:59  

You are right. 
Furthermore, after more simulation, I realized that what I stated is not true: the EA does not stop, the problem is that it DOES NOT do what logic would expect.

 

The example is meant to see if I understand how getting instant values etc. works in MQ4, so I (partially copied) an example of detecting when one of the moving average lines crosses another, and when said line crosses a third one (I don't know if this makes sense in real trading, that's not my goal here).

PROBLEM: the three lines displayed on the chart are sometimes VERY clear, and anyone can see when a condition of Buy or Sell is met BUT... in many cases the Algorithm does not see that or, even stranger, it verified a condition (and printed a Sell) when the lines were PARALLEL (ascending).

 

The non-logical verification of the conditions made me think there was a problem in regularly checking the values, from where my first post.

Still not sure that can be the cause, honestly it's confusing.

 It was meant to be just a simple code to begin with (it does never print the Refresh  also, not interested but it may be a hint).

 

Thanks!

int gTicket = 0;

double gValue_a_OLD = 0;      // Moving Average shift 1 OLD
double gValue_b_OLD = 0;      // Moving Average shift 2 OLD
double gValue_c_OLD = 0;      // Moving Average OLD
double gValue_a = 0;          // Moving Average shift 1
double gValue_b = 0;          // Moving Average shift 2
double gValue_c = 0;          // Moving Average

MqlTick gLastTick;            // Structure to keep info of last Tick

string gSymbol = "GBPUSD";


int OnInit()
  {
   gValue_a_OLD = iMA (gSymbol, 0, 11, 0, MODE_SMA, PRICE_MEDIAN, 10);
   gValue_b_OLD = iMA (gSymbol, 0, 10, 0, MODE_SMA, PRICE_MEDIAN, 7);
   gValue_c_OLD = iMA (gSymbol, 0, 14, 0, MODE_SMA, PRICE_TYPICAL, 0);
   
   Print ("INIT STARTED");
   
   return(INIT_SUCCEEDED);
  }


void OnDeinit(const int reason)
  {
   Print ("EA exited with reason: ", reason);
  }


void OnTick()
  {

   if ( RefreshRates() )
   {
      Print ("RefreshRates ()");
   }
   
   gValue_a = iMA (gSymbol, 0, 11, 0, MODE_SMA, PRICE_MEDIAN, 10);
   gValue_b = iMA (gSymbol, 0, 10, 0, MODE_SMA, PRICE_MEDIAN, 7);
   gValue_c = iMA (gSymbol, 0, 14, 0, MODE_SMA, PRICE_TYPICAL, 0);
   
   if ( (gTicket == 0) && (gValue_c_OLD < gValue_b_OLD) && (gValue_c > gValue_b) )
   {
      if ( SymbolInfoTick(gSymbol, gLastTick) )
      {
         Print ("BUY trigger at: ", gLastTick.bid);
         gTicket = 1;
      }
      else
      {
         Print ("ERROR in SymbolInfoTick = ", GetLastError());
      }
   }
   else if ( (gTicket == 1) && (gValue_c_OLD > gValue_b_OLD) && (gValue_c < gValue_b) )
   {
      if ( SymbolInfoTick(gSymbol, gLastTick) )
      {
         Print ("SELL trigger at: ", gLastTick.ask);
         gTicket = 0;
      }
      else
      {
         Print ("ERROR in SymbolInfoTick = ", GetLastError());
      }
   }
   
   gValue_c_OLD = gValue_c;
   gValue_b_OLD = gValue_b;
   gValue_c_OLD = gValue_c;
  }
To add comments, please log in or register