Moving average is not updating its value

 
Hi guys, I wrote this function(below) and when I call it in star() the values remain the same and are not updated

 bool checkMyTrend()
   {
      double Maa1 = iMA(Symbol(),0,periodM1,0,MODE_SMA, PRICE_CLOSE,shiftM1);
      double Maa2 = iMA(Symbol(),0,periodM2,0,MODE_SMA, PRICE_CLOSE,shiftM2);
      double ecartLong = Maa2-Maa1; 
      double ecartShort = Maa1-Maa2; 
      double ecarttype = ratioEcart * UsePoint;
      bool trendy;
      
      if(ecartShort>ecarttype || ecartLong>ecarttype)
         {
            trendy=true;
            Alert ("trend is trigged in this pairs : " + Symbol()); 
         }
      else
         {
            trendy=false;
         }  
         return (trendy);
   }

Could you advice what I missed?
 

Please edit your post, and use the SRC button to place your code sample.

Is this an EA or an Indicator?

Where are the variables "shiftM1" and "shiftM1" set and what are their values?

Calling this function at the "start()" event, means that you will calling this function on every tick, but the values will only change when there is a new bar formed. During the rest of the time, there will be no change to the values. The only exception is if "shiftM1" or "shiftM2" are 0.

So, instead of calling it on every tick, detect when a new bar is formed (by looking at the bar time) and only then update the values.

 
FMIC:

Please edit your post, and use the SRC button to place your code sample.

Is this an EA or an Indicator?

Where are the variables "shiftM1" and "shiftM1" set and what are their values?

Calling this function at the "start()" event, means that you will calling this function onevery tick, but the values will only change when there is a new bar formed. During the rest of the time, there will be no change to the values. The only exception is if "shiftM1" or "shiftM2" are 0.

So, instead of calling it on every tick, detect when a new bar is formed (by looking at the bar time) and only then update the values.

Sorry I wrote my post from my phone and there was no src button, anyway.. now it should be fine

OK Shift variables are just external ones and I set them up as 0.

And actually even when new bar is formed (timeframe 15)  the values are unchanged. this is why I wrote here :)

 
bmhassene1:

Sorry I wrote my post from my phone and there was no src button, anyway.. now it should be fine

OK Shift variables are just external ones and I set them up as 0.

And actually even when new bar is formed (timeframe 15)  the values are unchanged. this is why I wrote here :)

What are the values of "periodM1" and "periodM2"?

How are you testing your code?

Also, without knowing the rest of the code context, it is difficult to pinpoint the exact cause. Can you provide a skeleton, but working, example in order to compile and test? It does not have to be your complete code, just enough for it to be tested and the results reproduced on our end!

 
Your code
 Simplified
bool trendy;

if(ecartShort>ecarttype || ecartLong>ecarttype)
      {
   trendy=true;
   Alert ("trend is trigged in this pairs : " + Symbol());
      }
else
   {
   trendy=false;
   }
bool trendy=ecartShort>ecarttype || ecartLong>ecarttype;
if(trendy) Alert("trend triggered on: " + _Symbol);

 

By the way, you can also simplify your code, without affecting the logic, in the following way:

// Attention! Untested code example.
bool checkMyTrend()
{
   double
      Maa1      = iMA( _Symbol, PERIOD_CURRENT, periodM1, 0, MODE_SMA, PRICE_CLOSE, shiftM1 ),
      Maa2      = iMA( _Symbol, PERIOD_CURRENT, periodM2, 0, MODE_SMA, PRICE_CLOSE, shiftM2 ),
      dMaa      = fabs( Maa1 - Maa2 ),
      ecarttype = ratioEcart * UsePoint;

   bool trendy  = dMaa > ecarttype;
      
   if( trendy ) Alert( "trend is triggered in this pairs : " + _Symbol ); 

   return( trendy );
}
 

here is were i call the function in start() :

 

              while (x1==0)
                      { 
                        bool checkTrend = checkMyTrend();
                        bool directionTrend = checkMyDirection();
                        while       (checkTrend==false)
                          {
                             checkTrend = checkMyTrend();
                             Sleep(10000);
                             Print("no signal is available to open a trade");
                          } 
                       if (directionTrend==true)
                          {
                                      x1=openOrderBuy(LotSizeLong,UseSlippage, Bid - myStopLoss, Bid+mytakeprofit);
                          }
                       if (directionTrend==false)
                          {
                                      x1=openOrderSell(LotSizeShort,UseSlippage,Ask + myStopLoss,Ask - mytakeprofit);
                         }
                        }       
 
bmhassene1: here is were i call the function in start() :

As I stated before, and I quote:

Can you provide a skeleton, but working, example in order to compile and test? It does not have to be your complete code, just enough for it to be tested and the results reproduced on our end!

Snippets of code, are not sufficient for us to test if your code logic is correct. Also the code you have posted does not show how you have "printed" the values and come to the conclusion that they are not updating.

We cannot read your mind nor are we unable to "guess" the context of your code logic, so please supply proper code. It does not have to be your complete code, just enough for it to be compiled, tested and the results be reproducible on our end!

 

well that was the main part, nothing interesting that could help in this topic I'm a programmer who just land in mql4 world. by the way is it possible to "refresh" the iMA values on each tick instead of each period?

int start()
   {
     double spread = MarketInfo(Symbol(), MODE_SPREAD)*UsePoint;
     ATR= iATR(Symbol(), 0, periodATR,shiftATR);
     while(TrendStart==true)
       {
          bool seekForTrend = true;
          LotSizeLong=minLot; LotSizeShort=minLot;
          while (x1==0)
            {   
                 bool checkTrend = checkMyTrend();
                 bool directionTrend = checkMyDirection();
                 while  (checkTrend==false)
                     {
                        checkTrend = checkMyTrend();
                        Sleep(10000);
                        Print("no signal is available to open a trade");
                     } 
                 if (directionTrend==true)
                     {
                        x1=openOrderBuy(LotSizeLong,UseSlippage, Bid - myStopLoss, Bid+mytakeprofit);
                     }
                 if (directionTrend==false)
                     {
                        x1=openOrderSell(LotSizeShort,UseSlippage,Ask + myStopLoss,Ask - mytakeprofit);
                     }
            }      
         if(x1>0)
           {
              TrendStart=false;
           }
     }

   }
 
bmhassene1: well that was the main part, nothing interesting that could help in this topic I'm a programmer who just land in mql4 world. by the way is it possible to "refresh" the iMA values on each tick instead of each period?

We are getting to a BAD start here. This is now the 3rd time that I ask for you to post a workable, testable piece of code that replicates your problem, yet you continue to post only snippets of code that cannot be tested in order to replicate the problem.

However, I think I have discovered your dilemma. One major problem that I can report in your most recent code snippet is that you are stuck in loops (in a the "while  (checkTrend==false)" and the "while(TrendStart==true)"), waiting for a new change in the "checkMyTrend()" and never releasing the current tick event. That is not how the "tick" event should be processed. If you do this in the strategy tester, you will be in a never-ending loop, and always stuck on the same tick and never moving onto the next one, with the results of iMA always returning the same value as you have reported.

THIS, it seems is the reason for your problem. Don't use a loop! Just check it on the tick event and return from the "start()" handler so that it can be called for the next tick.

Also, your question about "refresh" makes no sense if done correctly (which you are not). You are using the "start()" event handle which is called every tick and you are checking the iATR() and iMA() values on every tick but stuck in a loop.

Please study more examples in the Tutorial and in Code Base until you understand this!

 
FMIC:

We are getting to a BAD start here. .


When I said nothing interesting I mean from my part hhh. Don't get me wrong :) correct sentence would be : "nothing interesting I have to share it more, that could help in this topic"
And yes I'm still learning mql4 I just landed in this world few weeks ago. I'm still studying some tutorial but this language have a lack of support and examples.
And thank you sir for your help. 

Reason: