0

I have this code working without error. Basically, this code is to show value of Moving Averages on five previous bars per 5 minutes. MA's current value is omitted.

int     TrendMinDurationBar = 5,
        SlowPeriod          = 14,
        FastPeriod          = 7;

void OnTick()
{
    if ( NewBar( PERIOD_M5 ) == true ) MA( PERIOD_M5 );
}

void MA( int TF )
{
    double Slow[], Fast[];
    ArrayResize( Slow, TrendMinDurationBar + 1 );
    ArrayResize( Fast, TrendMinDurationBar + 1 );

    for (  int i = 1; i <= TrendMinDurationBar; i++ )
    {      Slow[i] = NormalizeDouble( iMA( Symbol(), TF, SlowPeriod, 0, MODE_EMA, PRICE_OPEN, i ), Digits );
           Fast[i] = NormalizeDouble( iMA( Symbol(), TF, FastPeriod, 0, MODE_EMA, PRICE_OPEN, i ), Digits );
           Alert( "DataSlow" + ( string )i + ": " + DoubleToStr( Slow[i], Digits ) );
    }
}

bool NewBar( int TF )
{
    static datetime lastbar = 0;
           datetime curbar  = iTime( Symbol(), TF, 0 );

    if (  lastbar != curbar )
    {     lastbar  = curbar; return( true );
    }
    else                     return( false );
}

When #property strict is included, the code is only working once after compiled. After new bar on M5 chart exist, it doesn't make any iteration.

What's the solution if I insist to use #property strict?

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
R. Silver
  • 1
  • 2

2 Answers2

0

Welcome to another New-MQL4.56789 Catch-22

My candidate from Help > MQL4 Reference > Updated MQL4
is
this one ( column [New MQL4 with #property strict] )

Functions of any type should return a value

and one more to be reviewed, code simply loses the logic even for static double alternative it would be extremely inefficient under these circumstances:

Local arrays are released when exiting {} block


user3666197
  • 1
  • 6
  • 50
  • 92
  • I did move local arrays into void OnTick() to prevent arrays released after exiting the block, it means void MA() not called by iteration, but it doesn't work either. – R. Silver Oct 28 '15 at 06:16
  • Did you also change void to return some value as noted in Help: – user3666197 Oct 28 '15 at 22:39
  • I never try it because when I'm trying to use *#property strict* back again the code running well. I don't know what happened. This is so frustrating. Maybe if *#property strict* usage make stuck, then remove it for a while and after everything is running smooth, *#property strict* can be used again. Just like a reset button. I do thank you for your help anyway. – R. Silver Oct 29 '15 at 04:19
  • I'm gonna write another question regarding the code above, hope you can give a little help. Thanks @user3666197 – R. Silver Oct 29 '15 at 04:37
  • Well, MQL4, namely the "new"-MQL4 is really a challenge. On the other hand I do not consider the possibility of "on/off"-magic to be real for finite state machines ( the code/compiler ecosystems are ). Sometimes a "behind the curtain" update of MetaLang.exe / compiler syntax may appear, after which it is wise to re-read the documentation ( no relevant and serious delta-changes are published, afaik ). So, get ready and **pack a lot of patience :o)** – user3666197 Oct 29 '15 at 10:20
0

Works perfectly well with #property strict as an EA in MT4 Build 950.

Are you sure you are running it as EA and not as Script or Indicator?

Enivid
  • 210
  • 2
  • 9
  • 1
    this is more like a comment than an answr to the question – slfan Mar 17 '16 at 20:22
  • Suggestion to run the program as an EA instead of as Indicator or Script could be an answer to the problem. But you are right, it does look more like a comment due to the fact that we lack more information from the OP. – Enivid Mar 18 '16 at 07:48