Any MQL4 programmers? What is wrong with this code? - mql4

When I try to divide the two doubles in a buffer my indicator blacks out, and the values go extreme in the second window -90000000 and 90000000
#property indicator_separate_window // Îòîáðàæåíèå â îòäåëüíîì îêíå
#property indicator_buffers 3 // Êîëè÷åñòâî áóôåðîâ
#property indicator_color1 Red // Öâåò ïåðâîé ëèíèè
#property indicator_color2 Blue // Öâåò âòîðîé ëèíèè
#property indicator_color3 Green
double FillBuffer[];
double DBuffer[];
double AverageBuffer[];
double H,L;
double point=Point();
int init() // Ñïåöèàëüíàÿ ôóíêöèÿ init()
{
int period = _Period;
string symbol = Symbol();
int digits = _Digits ;
point = _Point ;
if(digits == 5 || digits == 3) { digits = digits - 1 ; point = point * 10 ; }
SetIndexBuffer(0,DBuffer);
SetIndexBuffer(1,FillBuffer);
SetIndexBuffer(2,AverageBuffer);
SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,1);
SetIndexLabel(0, "ADR");
return(INIT_SUCCEEDED);
}
int start()
{
int i, limit, counted_bars;
counted_bars = IndicatorCounted();
//---- check for possible errors
if (counted_bars<0) return(-1);
//---- last counted bar will be recounted
if (counted_bars>0) counted_bars--;
limit = Bars - counted_bars;
for (i = limit; i >= 0; i--)
{
double dbuff= iHigh(NULL,0,i)- iLow(NULL,0,i);
double D0 = iHigh(NULL,0,i+1)- iLow(NULL,0,i+1);
double D1 = iHigh(NULL,0,i+2)- iLow(NULL,0,i+2);
double D2 = iHigh(NULL,0,i+3)- iLow(NULL,0,i+3);
double D3 = iHigh(NULL,0,i+4)- iLow(NULL,0,i+4);
double D4 = iHigh(NULL,0,i+5)- iLow(NULL,0,i+5);
double Average = ((D0+D1+D2+D3+D4)/5)/point;
FillBuffer[i]=dbuff/Average;
}
return(0);
When I try to divide the two values in FillBuffer[] my indicator blacks out. But if I just have either the dbuff or Average in the buffer it will show lines but I want the percentage one is of the other to be printed.

There is not much wrong with the code, but it does not paint the line:
Some polishing may help, but the core logic of the MQL4 Custom Indicator is forgotten in the code. If one does not assign a way, how to plot ( paint ) the line on screen, the GUI will remain "blacked-out" even though the values may have gotten calculated.
Performance warning:
Custom Indicators ( all Custom Indicators ) share one common solo-thread (!!), so proper performance tuning is warmly recommended in this type of MQL4-code-execution blocks.
Some further acceleration might be achieved by reducing / avoiding all the repetitive re-averaging via a sliding-window implementation replacement. While this is not so risky at 5-BARs deep re-processing, for deeper TimeSeries convolutions, the effect is significant and impressive in Strategy Tester accelerated mode of computing's run-times ( getting down to minutes instead of hours ). Worth one's time and efforts.
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 Green
double FillBuffer[];
double DBuffer[]; // NEVER FILLED IN
double AverageBuffer[]; // NEVER FILLED IN
double point = Point();
int init()
{ int period = _Period; // NEVER CONSUMED
string symbol = Symbol(); // NEVER CONSUMED
int digits = _Digits ; // LOCAL SCOPE ONLY VISIBLE INSIDE init(){...}
point = _Point ;
if ( digits == 5 || digits == 3 ) { digits -= 1;
point *= 10;
}
SetIndexBuffer( 0, DBuffer );
SetIndexBuffer( 1, FillBuffer ); // 1: ASSIGNED, BUT NEVER SET TO HAVE ANY { DRAW_LINE | DRAW_ ... } GUI OUTPUT
SetIndexBuffer( 2, AverageBuffer ); // 2: ASSIGNED, BUT NEVER SET TO HAVE ANY { DRAW_LINE | DRAW_ ... } GUI OUTPUT
SetIndexStyle ( 0, DRAW_LINE, STYLE_SOLID, 1 ); // 0: SET AS DRAW_LINE ( BUT NEVER FILLED IN WITH DATA )
SetIndexLabel( 0, "ADR" );
return( INIT_SUCCEEDED );
}
int start()
{ int i, limit, counted_bars;
counted_bars = IndicatorCounted();
//----check for possible errors
if ( counted_bars < 0 ) return( -1 );
//----last counted bar will be recounted
if ( counted_bars > 0 ) counted_bars--;
limit = ( Bars - 5 ) - counted_bars; // AVOID 1st 5 BARS,
int i0, i1, i2, i3, i4, i5; // WHERE (i+5) WILL OVERFLOW THE TIMESERIES LEFT EDGE
for ( i = limit,
i1 = i + 1,
i2 = i + 2,
i3 = i + 3,
i4 = i + 4,
i5 = i + 5; i >= 0; i--,
i1--,
i2--,
i3--,
i4--,
i5--
)
{ FillBuffer[i] = ( High[i]
- Low[i]
)
/ ( ( High[i1] + High[i2] + High[i3] + High[i4] + High[i5] )
- ( Low[i1] + Low[i2] + Low[i3] + Low[i4] + Low[i5] )
)
/ 5.
/ point;
/* double dbuff = iHigh( NULL, 0, i ) - iLow( NULL, 0, i );
double D0 = iHigh( NULL, 0, i+1 ) - iLow( NULL, 0, i+1 );
double D1 = iHigh( NULL, 0, i+2 ) - iLow( NULL, 0, i+2 );
double D2 = iHigh( NULL, 0, i+3 ) - iLow( NULL, 0, i+3 );
double D3 = iHigh( NULL, 0, i+4 ) - iLow( NULL, 0, i+4 );
double D4 = iHigh( NULL, 0, i+5 ) - iLow( NULL, 0, i+5 );
double Average = ( ( D0 + D1 + D2 + D3 + D4 ) / 5 ) / point;
FillBuffer[i] = dbuff / Average;
*/
}
return( 0 );
}

Related

How to stop Indicator from calculating on every tick, when I only need the indicator at bar close?

This is an indicator I downloaded from the internet but I have made some modifications. I noticed the indicator calculates the linear regression line and the upper and lower bands on EVERY tick.
I find that to be resource wasteful as I only need the line to be calculated at the close of each bar; i.e. when bar 0 ends and a new bar 0 is formed.
It should not calculate anything while bar 0 is still incomplete.
How I do make the necessary changes?
Thanks!!
//+------------------------------------------------------------------+
//| Linear Regression Line.mq4 |
//| MQL Service |
//| scripts#mqlservice.com |
//+------------------------------------------------------------------+
#property copyright "MQL Service"
#property link "www.mqlservice.com"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 White
#property indicator_width1 2
#property indicator_color2 Orange
#property indicator_width2 2
#property indicator_color3 Orange
#property indicator_width3 2
//---- input parameters
extern int LRLPeriod = 20;
extern int Number_SD = 2;
//---- buffers
double LRLBuffer[], LRLBuffer_Upper[], LRLBuffer_Lower[];
//int shift = 0;
int n = 0;
double sumx = 0;
double sumy = 0;
double sumxy = 0;
double sumx2 = 0;
double sumy2 = 0;
double yint = 0;
double r = 0;
double m = 0;
//+------------------------------------------------------------------+
//| INITIALIZATION FUNCTION |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0, DRAW_LINE);
SetIndexBuffer(0, LRLBuffer);
SetIndexStyle(1, DRAW_LINE);
SetIndexBuffer(1, LRLBuffer_Upper);
SetIndexStyle(2, DRAW_LINE);
SetIndexBuffer(2, LRLBuffer_Lower);
IndicatorDigits(Digits);
if (LRLPeriod < 2)
LRLPeriod = 2;
IndicatorShortName("Linear Regression Line ("+LRLPeriod+")");
SetIndexDrawBegin(0, LRLPeriod+2);
IndicatorDigits(MarketInfo(Symbol(), MODE_DIGITS)+4);
return(0);
}
//+------------------------------------------------------------------+
//| DEINITIALIZATION FUNCTION |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| ITERATION FUNCTION |
//+------------------------------------------------------------------+
int start()
{
int limit, j, Counted_bars;
int counted_bars = IndicatorCounted();
if (counted_bars < 0)
counted_bars = 0;
if (counted_bars > 0)
counted_bars--;
limit = Bars - counted_bars;
for (int shift=limit-1; shift >= 0; shift--)
{
sumx = 0;
sumy = 0;
sumxy = 0;
sumx2 = 0;
sumy2 = 0;
for (n = 0; n <= LRLPeriod-1; n++)
{
sumx = sumx + n;
sumy = sumy + Close[shift + n];
sumxy = sumxy + n * Close[shift + n];
sumx2 = sumx2 + n * n;
sumy2 = sumy2 + Close[shift + n] * Close[shift + n];
}
double temp = LRLPeriod * sumx2 - sumx * sumx;
if (temp == 0)
temp = .0000001;
// m = (LRLPeriod * sumxy - sumx * sumy) / (LRLPeriod * sumx2 - sumx * sumx);
m = (LRLPeriod * sumxy - sumx * sumy) / temp;
temp = LRLPeriod;
if (temp == 0)
temp = .0000001;
yint = (sumy + m * sumx) / temp; // was LRLPeriod (obviously)
temp = MathSqrt((LRLPeriod * sumx2 - sumx * sumx) * (LRLPeriod * sumy2 - sumy * sumy));
if (temp == 0)
temp = .0000001;
r = (LRLPeriod * sumxy - sumx * sumy) / temp;
LRLBuffer[shift] = yint - m * LRLPeriod;
//Print (" "+shift+" "+LRLBuffer[shift]);
}
//----------Added Upper and Lower Bands--------------//
int nBARs = 0;
double LRLBuffer_CPY[];
ArraySetAsSeries(LRLBuffer_CPY,True);
j = Bars - Counted_bars - 1;
while( j > 0 )
{
ArrayCopy( LRLBuffer_CPY, LRLBuffer, 0, j, WHOLE_ARRAY );
double StDev = iStdDevOnArray( LRLBuffer_CPY, nBARs, LRLPeriod, 0, MODE_SMA, 0 );
LRLBuffer_Upper[j] = LRLBuffer[j] + (Number_SD * StDev);
LRLBuffer_Lower[j] = LRLBuffer[j] - (Number_SD * StDev);
j--;
}
return(0);
}
//+------------------------------------------------------------------+
The easiest way is to use standard OnCalculate(***) function and run the main cycle only if(rates_total>prev_calculated).
Also, in the main cycle try to have for(int shift=limit-1;shift>0;shift--){ (be attentive), and by the way, are you sure that you need shift=limit-1 not just shift=limit?
New-MQL4 syntax OnCalculate() will not help achieve the goal,rather use this:
The OnCalculate() syntax is available, but the code-block actually remains outside-of-your control, when it gets launched and how many progressive steps will it be allowed to process.
The solution is to use the "old"-syntax and add "soft"-locking.
Using static variables helps keep interim values stored between calls and a just-HOT-end recalculations thus need not re-iterate from the whole depths, if shifting registers are used to just update from the just-frozen [1] Bar. The rest of the Bar [0] duration it does .NOP/JIT/RET and your code does not devastate fragility of a single-solo-thread performance, shared ( Yes! SHARED!! ) by ALL Custom Indicators ( a truly devilish anti-pattern in the recent New-MQL4 updates ... ):
int start(){
static aCurrentTIME = EMPTY;
if ( aCurrentTIME == Time[0] ) return 0; // .NOP/JIT/RET --^
aCurrentTIME = Time[0]; // .MOV/LOCK
// -------------------------------------------- // .CALC:
int limit = Bars - counted_bars;
...
/* update just the "HOT"-end has just got into OHLCVT[1]-cells */
// -------------------------------------------- // .FIN
}
depending on the case, you also could:
datetime calculatedBarTime;
void OnCalculate()
{
if ( calculatedBarTime != Time[0] )
{
onBar();
}
}
void onBar()
{
calculatedBarTime = Time[0];
// on bar logic....
}

How to automatically calculate fibonnacci levels from yesterday/prev day in MQL4?

how do I calculate the fibo levels from yesterday/previous day.
This is how far I am currently:
int shift = iBarShift( NULL, PERIOD_D1, Time[0] ) + 1; // yesterday
HiPrice = iHigh( NULL, PERIOD_D1, shift);
LoPrice = iLow ( NULL, PERIOD_D1, shift);
StartTime = iTime( NULL, PERIOD_D1, shift);
if ( TimeDayOfWeek( StartTime ) == 0 /* Sunday */ )
{ // Add fridays high and low
HiPrice = MathMax( HiPrice, iHigh( NULL, PERIOD_D1, shift + 1 ) );
LoPrice = MathMin( LoPrice, iLow( NULL, PERIOD_D1, shift + 1 ) );
}
Range = HiPrice - LoPrice;
I think now I should have all values necessary for calculating it.
I am not sure on how I now can calculate the different levels now:
23.6 38.2 50.0 61.8 76.4 and -23.6 -38.2 -50.0 -61.8 -76.4 -100
All necessary Fibo-levels can be added manually as an array - this is the easiest way as far as I know. Then simply loop over such array and
+values are ( high + array[i] / 100 * range ),
values below the fibo - ( low - array[i] / 100 * range ),
where
array[] = { 23.6, 38.2, .. } ( only positive values are enough )
Fibonacci Levels need a direction, so in your above code you will either want to swap to using open and close values of the previous bar or impose a direction onto the high and low. This will let you know which way to draw the extensions and retraces.
Here is a function I have written previously for this question. This function assumes price1 is at an earlier timepoint than price2 then calculates the direction and levels, returning a FibLevel structure.
struct FibLevel {
double retrace38;
double retrace50;
double retrace61;
double extension61;
double extension100;
double extension138;
double extension161;
};
void FibLevel(double price1, double price2,FibLevel &fiblevel)
{
double range = MathAbs(price1-price2);
fiblevel.retrace38 =(price1<price2)?price2-range*0.382:price1+range*0.382;
fiblevel.retrace50 =(price1<price2)?price2-range*0.500:price1+range*0.500;
fiblevel.retrace61 =(price1<price2)?price2-range*0.618:price1+range*0.618;
fiblevel.extension61 =(price1<price2)?price2+range*0.618:price1-range*0.618;
fiblevel.extension100=(price1<price2)?price2+range :price1-range;
fiblevel.extension138=(price1<price2)?price2+range*1.382:price1-range*1.382;
fiblevel.extension161=(price1<price2)?price2+range*1.618:price1-range*1.618;
}

While there are no MQL4 errors, why there was no GUI drawing produced?

For a learning purpose, I am trying to code a simple MA indicator that changes color when price crosses. Though there are no errors, it draws nothing. Could you review the attached code to show me my mistake?
#property indicator_chart_window
#property indicator_buffers 2
extern int maperiod = 20;
extern int maprice = PRICE_CLOSE;
extern int mamethod = MODE_SMA;
extern color colorup = Green;
extern color colordn = Red;
double mamain[];
double bufferup[];
double bufferdn[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init(){
//--- indicator buffers mapping
SetIndexBuffer( 0, bufferup );
SetIndexStyle( 0, DRAW_LINE, 0, 2, colorup );
SetIndexBuffer( 1, bufferdn );
SetIndexStyle( 1, DRAW_LINE, 0, 2, colordn );
//---
return( 0 );
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start(){
//---
int counted_bars = IndicatorCounted();
if ( counted_bars < 0 ) return( -1 );
if ( counted_bars > 0 ) counted_bars--;
int limit = Bars - counted_bars;
for ( int i = limit; i >= 0 ; i-- )
{ mamain[i] = iMA( NULL, 0, maperiod, 0, 0, 0, i );
if ( mamain[i] >= iClose( NULL, 0, i ) ) bufferup[i] = mamain[i];
if ( mamain[i] <= iClose( NULL, 0, i ) ) bufferdn[i] = mamain[i];
}
//--- return value of prev_calculated for next call
return( 0 );
}
//+------------------------------------------------------------------+
"Old"-MQL4 may work for some time, but still, get used to new features:
extern ENUM_APPLIED_PRICE MAprice = PRICE_CLOSE; // AVOIDS incompatible values
extern ENUM_MA_METHOD MAmethod = MODE_SMA; // AVOIDS incompatible values
#define MAshift 0 // ADDS code == intent match-robustness
extern int MAperiod = 20;
extern color colorUP = clrGreen;
extern color colorDN = clrRed;
double bufferUP[];
double bufferDN[];
int init(){
ArrayInitialize bufferUP, EMPTY_VALUE );
SetIndexBuffer( 0, bufferUP );
SetIndexStyle( 0, DRAW_LINE, EMPTY, 2, colorUP );
SetIndexLabel( 0, "MA_ABOVE_Close" );
ArrayInitialize bufferDN, EMPTY_VALUE );
SetIndexBuffer( 1, bufferDN );
SetIndexStyle( 1, DRAW_LINE, EMPTY, 2, colorDN );
SetIndexLabel( 1, "MA_UNDER_Close" );
return( 0 );
}
int start(){
int counted_bars = IndicatorCounted();
if ( counted_bars < 0 ) return( -1 );
if ( counted_bars > 0 ) counted_bars--;
for ( int i = Bars - counted_bars; i >= 0 ; i-- ){
double C = iClose( _Symbol, PERIOD_CURRENT, i ),
A = iMA( _Symbol, PERIOD_CURRENT,
MAperiod,
MAshift,
MAmethod,
MAprice,
i
);
if ( A >= C ) bufferUP[i] = A;
if ( A <= C ) bufferDN[i] = A;
}
return( 0 );
}
New-MQL4.56789 uses another call-signature if #property strict:
int OnCalculate( const int rates_total,
const int prev_calculated,
const datetime &time_______ARR[],
const double &open_______ARR[],
const double &high_______ARR[],
const double &low________ARR[],
const double &close______ARR[],
const long &tick_volumeARR[],
const long &volume_____ARR[],
const int &spread_____ARR[]
){
//--- the main loop of calculations
for( int i = prev_calculated - 1;
( i < rates_total
&& !IsStopped() ); // AVOID SHARED (!) solo-THREAD BLOCKING
i++
){
// -----------------------------------------------------------------
double A = iMA( _Symbol, PERIOD_CURRENT,
MAperiod,
MAshift,
MAmethod,
MAprice,
i
);
if ( A >= close______ARR[i] ) bufferUP[i] = A;
if ( A <= close______ARR[i] ) bufferDN[i] = A;
// -----------------------------------------------------------------
}
return( rates_total );
}
your buffer mamain[] is not initialized.
int init(){
IndicatorBuffers(3);
SetIndexBuffer(2,mamain);
}
rates_total and prev_calculated seems preferred but of course you can use IndicatorCounted() but keep in mind the corner situation with the first bar: when you first attach the indicator to the chart, your counted_bars = 0 and limit = Bars, but mamain[] and other indicator buffers have Bars elements only, from 0 to Bars-1. so better to use
int limit = Bars - counted_bars - 1;
About resolving issues - in addition to asking here you can always try to attach your indicator to a chart and see it there's no error (terminal window - Experts folder), that will make delevopment faster

My First MQL4 EA does not generate any Orders. Why?

I'm trying to build a first EA, code below, but it doesn't execute any trades.
I know it is very simple, but logically, I think this should Buy and Sell.
I'm trying to only use a code that I understand.
I'd appreciate it if anyone had any feedback!
//
extern int sma_short = 10;
extern int sma_long = 20;
extern double fakeout = 0.0005 ;
extern double stoploss = 150;
extern double risk = 1;
extern int slippage = 5;
extern int magicnumber = 12345;
extern bool SignalMail = false;
extern bool UseTrailingStop = true;
extern int TrailingStop = 150;
double sma_short_t3;
double sma_short_t0;
double sma_long_t3;
double sma_long_t0;
double sma_diff_t3;
double sma_diff_t0;
double lots;
double stoplosslevel;
int P = 1;
int ticket, ticket2;
int total = OrdersTotal();
bool OpenLong = false;
bool OpenShort = false;
bool CloseLong = false;
bool CloseShort = false;
bool isYenPair = false;
bool OpenOrder = false;
int OnInit()
{
if ( Digits == 5 || Digits == 3 || Digits == 1 ) P = 10; else P = 1; // To account for 5 digit brokers
if ( Digits == 3 || Digits == 2 ) isYenPair = true; // Adjust for YenPair
return( INIT_SUCCEEDED );
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit( const int reason )
{
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void Start()
{
sma_short_t3 = iMA( NULL, 0, sma_short, 0, MODE_SMA, PRICE_CLOSE, 3 );
sma_short_t0 = iMA( NULL, 0, sma_short, 0, MODE_SMA, PRICE_CLOSE, 0 );
sma_long_t3 = iMA( NULL, 0, sma_long, 0, MODE_SMA, PRICE_CLOSE, 3 );
sma_long_t0 = iMA( NULL, 0, sma_long, 0, MODE_SMA, PRICE_CLOSE, 0 );
sma_diff_t3 = sma_long_t3 - sma_short_t3;
sma_diff_t0 = sma_long_t0 - sma_short_t0;
if ( OpenOrder )
{
if ( CloseLong || CloseShort )
{
OrderClose( OrderTicket(), OrderLots(), Bid, slippage, MediumSeaGreen );
OpenOrder = False;
CloseLong = False;
CloseShort = False;
}
}
if ( sma_diff_t3 < 0 && sma_diff_t0 > fakeout )
{
OpenLong = True ;
CloseShort = True;
}
if ( sma_diff_t3 > 0 && sma_diff_t0 < -fakeout )
{
OpenShort = True;
CloseLong = True;
}
lots = risk * 0.01 * AccountBalance() / ( MarketInfo( Symbol(), MODE_LOTSIZE ) * stoploss * P * Point ); // Sizing Algo based on account size
if ( isYenPair == true ) lots = lots * 100; // Adjust for Yen Pairs
lots = NormalizeDouble( lots, 2 );
if ( OpenLong )
{
stoplosslevel = Ask - stoploss * Point * P;
OrderSend( Symbol(), OP_BUY, lots, Ask, slippage, stoplosslevel, 0, "Buy(#" + magicnumber + ")", magicnumber, 0, DodgerBlue );
OpenOrder = True;
}
if ( OpenShort )
{
stoplosslevel = Bid + stoploss * Point * P;
OrderSend( Symbol(), OP_SELL, lots, Ask, slippage, stoplosslevel, 0, "Buy(#" + magicnumber + ")", magicnumber, 0, DodgerBlue );
OpenOrder = True ;
}
}
//+------------------------------------------------------------------+
and why do you use (MarketInfo(Symbol(),MODE_LOTSIZE)? what is the idea of that? first try with double lots = 1.00; and if problem still exists - please add a line telling about the reason why ea failed to send. sth like int ticket = OrderSend(***); if(ticket<0)Print("error=",GetLastError()); or more complex telling about the actual prices, lots, stoploss etc.
Few things in MQL4 to rather get used to:
All PriceDOMAIN data has to be NormalizeDouble() before sending to MetaTrader 4 Server.
All EquityDOMAIN data has to follow a set of discrete values,having MathMin( aMinLOT_SIZE + N * aMinLOT_STEP, aMaxLOT_SIZE ). Normalisation of EquityDOMAIN data is broker-specificand instrument-specific, so need not be always 2.
For XTO, OrderSend(), OrderMOdify(), OrderClose(), one ought follow something like this:
if ( OpenLong )
{ stoplosslevel = NormalizeDouble( Ask - stoploss * Point * P, _Digits ); // ALWAYS NormalizeDouble()
int RetCODE = OrderSend( _Symbol,
OP_BUY,
lots,
Ask,
slippage,
stoplosslevel,
0,
"Buy(#" + magicnumber + ")",
magicnumber,
0,
DodgerBlue
);
if ( RetCODE < 0 )
{ Print( "EXC: Tried to go LONG, OrderSend() failed to get confirmed ( Errno: ", GetLastError(), " )" );
}
else
{ OpenOrder = True;
...
}
...
}

Can't make iteration with #property strict

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?
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?
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

Resources