Best way to add MaxBarsToCount (History) to the limit?

 

Dear Admin, would be great if we didn't merge this thread and leave it separate -

question (problem) very common and specific thread much easier for Search

(plus this question allready in general thread, but no answer...)

--------------------------------------------------------------------

When we limit MaxBarsToCount (History) sometimes it require to add Correction, etc

is the best (safest, easiest, universal) way exist? (rule of tumb? dirty triks?)

----------------------

like here we have light fisher 4 stoch smothing:

----------

int start()

{

int counted_bars=IndicatorCounted();

//---- check for possible errors

if(counted_bars<0) return(-1);

int limit=Bars-counted_bars;

if(limit>maxbars)limit=maxbars;

if (limit>Bars-lenth-1)limit=Bars-lenth-1;

//----

for (int shift = limit; shift>=0;shift--)

{

AuxBuffer[shift]=(iStochastic(NULL,0,lenth,2,1,MODE_SMA,0,MODE_MAI N,shift)/100-0.5)

+0.5*AuxBuffer[shift+1];

FishBuffer[shift]= 0.25* MathLog((1+AuxBuffer[shift])/(1-AuxBuffer[shift]))+

0.5*FishBuffer[shift+1];

SignalBuffer[shift]=FishBuffer[shift+1];

}

//----

return(0);

}

------------------------

for fisher limit f-la:

int limit;

int counted_bars=IndicatorCounted();

if(counted_bars>0) counted_bars--;

limit=Bars-counted_bars;

for(int i=limit; i>=0; i--)

{

....

for Stoch:

int start()

{

int i,k;

int counted_bars=IndicatorCounted();

double price;

//----

if(Bars<=draw_begin2) return(0);

//---- initial zero

if(counted_bars<1)

{

for(i=1;i<=draw_begin1;i++) MainBuffer=0;

for(i=1;i<=draw_begin2;i++) SignalBuffer=0;

}

//---- minimums counting

i=Bars-KPeriod;

if(counted_bars>KPeriod) i=Bars-counted_bars-1;

while(i>=0)

{

double min=1000000;

k=i+KPeriod-1;

while(k>=i)

{

price=Low[k];

if(min>price) min=price;

k--;

}

LowesBuffer=min;

i--;

}

....

p.s. in attached indicator, based on clean fisher transform and Stoch; MaxBars (tail) needs to be straighten-up a bit... (when MaxBars out - no problem)

pps. same thing with many others; try to add maxBars to regular (stoc) macd and look on the tail...

 

thank you everybody who helped!

here it is:

when we have no arrays - works basic formula:

limit = MathMin(limit, MaxBarsToCount);

but when we have 4ex. MAonArray - we need more bars (MA period) to count first MaonArray bar:

MaxBarsToCount + MA period

so, two ways possible- count more or draw less:

- draw MaxBarsToCount but count more

limit = MathMin(limit, MaxBarsToCount)+ MA period

- or count the same but draw less

SetIndexDrawBegin(i,Bars-MaxBarsToCount+MA period);

_____

in all cases SetIndexDrawBegin in "int start()" not at "init()" section

for (i=0;i<indicator_buffers;i++) SetIndexDrawBegin(i,Bars-MaxBarsToCount + MA period);

return(0);

}

 
fxbs:
another way - but that's - dirty trick: ...

There is no ONE best solution.

It depends of what you want to do: for example, some indics must run at every tick of bar 0, but some other needs to run only once at the open of the bar, it's stupid to let them run at each tick; some cannot run the last "x" bars of the history because they need the datas, some run from the past to the present and some others no, and so on.

The best solution is the one YOU understand !

I must admit that sometime it's very tricky...

I such cases, in order to test how it works, I suggest you to add a command inside the "for" loop to print or comment the bar number, for example :

for(int i = limitblablabla...)

{

Print(i + " " + Bars + " " + IndicatorCounted());

In a generic case, if you only want to add a MaxBars count at a working indic , the easiest way is to add at the first line of the "for" loop this line :

for(int i = limitblablabla...) //original command , no need to understand it

{

if(i > MaxBars) continue;

....

 

perky's trick - eft sto smz (post1) tale cut-off

 
Michel:
There is no ONE best solution.

It depends of what you want to do: for example, some indics must run at every tick of bar 0, but some other needs to run only once at the open of the bar, it's stupid to let them run at each tick; some cannot run the last "x" bars of the history because they need the datas, some run from the past to the present and some others no, and so on.

The best solution is the one YOU understand !

I must admit that sometime it's very tricky...

I such cases, in order to test how it works, I suggest you to add a command inside the "for" loop to print or comment the bar number, for example :

for(int i = limitblablabla...)

{

Print(i + " " + Bars + " " + IndicatorCounted());

In a generic case, if you only want to add a MaxBars count at a working indic , the easiest way is to add at the first line of the "for" loop this line :

for(int i = limitblablabla...) //original command , no need to understand it

{

if(i > MaxBars) continue;

....

Thanks a lot, Michel!

- that's how we learn, day by day

 

Hey, Michel!

how to use this line:

if(i > MaxBars) continue;

i tryed - no go...

but if put like this:

if(i < MaxBarsToCount)

- works (MACD):

int start()

{

int limit;

int counted_bars=IndicatorCounted();

//---- last counted bar will be recounted

if(counted_bars>0) counted_bars--;

limit=Bars-counted_bars;

//---- macd counted in the 1-st buffer

for(int i=0; i<limit; i++)

if(i < MaxBarsToCount)

MacdBuffer=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-

iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);

//---- signal line counted in the 2-nd buffer

for(i=0; i<limit; i++)

if(i < MaxBarsToCount-SignalSMA)

SignalBuffer=iMAOnArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);

//---- done

return(0);
 
fxbs:
Hey, Michel!

how to use this line:

if(i > MaxBars) continue;

i tryed - no go...

but if put like this:

if(i < MaxBarsToCount)

- works (MACD):

int start()

{

int limit;

int counted_bars=IndicatorCounted();

//---- last counted bar will be recounted

if(counted_bars>0) counted_bars--;

limit=Bars-counted_bars;

//---- macd counted in the 1-st buffer

for(int i=0; i<limit; i++)

if(i < MaxBarsToCount)

MacdBuffer=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-

iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);

//---- signal line counted in the 2-nd buffer

for(i=0; i<limit; i++)

if(i < MaxBarsToCount-SignalSMA)

SignalBuffer=iMAOnArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);

//---- done

return(0);

both should do the same, but the problem here is that you don't use the brackets for the for loop because there is only one command.

"if(i < MaxBarsToCount) MacdBuffer=iMA(NULL,0,FastEMA... ;" is only one command ( there is only one ";" ) so the for loop doesn't need any brackets.

Now, the use "if(i >MaxBarsToCount) continue;" means one more command in the for loop, so you have to use brackets to group them together.

so :

for(int i=0; i<limit; i++)

if(i > MaxBarsToCount) continue;

does'nt work because there is only one command, the if test line, which does nothing.

but

for(int i=0; i<limit; i++)

{

if(i > MaxBarsToCount) continue;

MacdBuffer=iMA(NULL,0,FastEMA,0,MODE blabla ;

blahblahblah...;

}

works.

I a general case, the for loop use brackets, thus "if(i > MaxBarsToCount) continue;" is ready to use (you do not need to add other brakets or modify the code)

 

________

whole thing 4 MAGD:

//+------------------------------------------------------------------+

//| Custom indicator initialization function |

//+------------------------------------------------------------------+

int init()

{

//---- drawing settings

SetIndexStyle(0,DRAW_HISTOGRAM);

SetIndexStyle(1,DRAW_LINE);

SetIndexDrawBegin(1,SignalSMA);

IndicatorDigits(Digits+1);

//---- indicator buffers mapping

SetIndexBuffer(0,MacdBuffer);

SetIndexBuffer(1,SignalBuffer);

//---- name for DataWindow and indicator subwindow label

IndicatorShortName("MACD("+FastEMA+","+SlowEMA+","+SignalSMA+")");

SetIndexLabel(0,"MACD");

SetIndexLabel(1,"Signal");

//

//

// two aproaches

// make an BarsToDraw that is going to be < MaxBarsToCount

//

// or when setting limit add max (calculated here) to MaxBarsToCount

// calculate MaxBarsToCount+max and than

// draw just MaxBarsToCount on chart

//

//

int max = MathMax(FastEMA,SlowEMA);

max = MathMax(SignalSMA,max);

BarsToDraw = MaxBarsToCount-max;

//---- initialization done

return(0);

}

//+------------------------------------------------------------------+

//| Moving Averages Convergence/Divergence |

//+------------------------------------------------------------------+

int start()

{

int limit;

int counted_bars=IndicatorCounted();

//---- last counted bar will be recounted

if(counted_bars>0) counted_bars--;

limit=Bars-counted_bars;

limit = MathMin(limit, MaxBarsToCount);

//---- macd counted in the 1-st buffer

for(int i=0; i<limit; i++)

MacdBuffer=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-

iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);

//---- signal line counted in the 2-nd buffer

for(i=0; i<limit; i++)

SignalBuffer=iMAOnArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);

//

//

// don't draw everything

//

//

for (i=0;i<indicator_buffers;i++) SetIndexDrawBegin(i,Bars-BarsToDraw);

return(0);

}

//+------------------------------------------
 

another way - but that's - dirty trick:

simple macd;

only "-SignalSMA" added - that's it

//---- signal line counted in the 2-nd buffer

for(i=0; i<limit-SignalSMA; i++)

before:

int start()

{

int limit;

int counted_bars=IndicatorCounted();

//---- last counted bar will be recounted

if(counted_bars>0) counted_bars--;

limit=Bars-counted_bars;

limit = MathMin(limit, MaxBarsToCount);

//---- macd counted in the 1-st buffer

for(int i=0; i<limit; i++)

MacdBuffer=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-

iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);

//---- signal line counted in the 2-nd buffer

for(i=0; i<limit; i++)

SignalBuffer=iMAOnArray(MacdBuffer,0,SignalSMA,0,MODE_SMA,i);

//---- done

return(0);

}

//+------------------------------------------------------------------+[/PHP]

after

//+------------------------------------------------------------------+

int start()

{

int limit;

int counted_bars=IndicatorCounted();

//---- last counted bar will be recounted

if(counted_bars>0) counted_bars--;

limit=Bars-counted_bars;

limit = MathMin(limit, MaxBarsToCount);

//---- macd counted in the 1-st buffer

for(int i=0; i<limit; i++)

MacdBuffer=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-

iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);

//---- signal line counted in the 2-nd buffer

for(i=0; i<limit-SignalSMA; i++)

SignalBuffer=iMAOnArray(MacdBuffer,0,SignalSMA,0,MODE_SMA,i);

//---- done

return(0);

}

//+------------------------------------------------------------------+[/PHP]

not cleanest solution, but ....

(as been mentioned- dirty trick; and TRO can use it for his book...) : - )))))))))

____________

P.S.

whoops - people say this slaking approach no good: limit-SignalSMA can be negative - not refreshing, etc, etc...

whoopsy daisy ... what to do?

____________

gets trickier and trickier...

MaxBarsToCount instead of limit - should take care of it?

for(i=0; i<MaxBarsToCount -SignalSMA; i++)

instead of

for(i=0; i<limit-SignalSMA; i++)

-sure, but we sacrificing our beloved limit....

well, gets even more trickier:

[PHP] limit=Bars-counted_bars;

limit = MathMin(limit, MaxBarsToCount);

//---- macd counted in the 1-st buffer

for(int i=0; i<limit+SignalSMA; i++)

MacdBuffer=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-

iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);

//---- signal line counted in the 2-nd buffer

// for(i=0; i<limit-SignalSMA; i++)

for(i=0; i<limit; i++)

SignalBuffer=iMAOnArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);

//---- done

so: for(int i=0; i<limit+SignalSMA; i++)

they wouldn't call it "dirty trick" for nothing...

to do it right - everybody can; - try to do it ... different.... : ))))

new update: now - even more less dirtier (setting limit4 each buffer):

[PHP] limit=Bars-counted_bars;

limit = MathMin(limit, MaxBarsToCount);

//---- macd counted in the 1-st buffer

for(int i=0; i<limit; i++)

MacdBuffer=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-

iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);

//---- signal line counted in the 2-nd buffer

limit = MathMin(limit, MaxBarsToCount-SignalSMA);

for(i=0; i<limit; i++)

SignalBuffer=iMAOnArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);

//---- done

return(0);

1. limit = MathMin(limit, MaxBarsToCount);

...

2. limit = MathMin(limit, MaxBarsToCount-SignalSMA);

!nobody messes with our Limit!! : ((((

Files:
 

Thank you, Michel!

well, i did as you sayed -

//---- signal line counted in the 2-nd buffer

for(i=0; i<limit; i++)

{

if(i > MaxBarsToCount) continue;

SignalBuffer=iMAOnArray(NULL,0,FastEMA,0,MODE blabla ;

blahblahblah...;

}

//---- done[/PHP]

works good but when onArray - old story - steping on tail

(maybe should have use more braskets: )

well, 4 onArray buffer :

if(i > MaxBarsToCount - SignalSMA) continue;

and that's it! : ))))))))

[PHP]int start()

{

int limit;

int counted_bars=IndicatorCounted();

//---- last counted bar will be recounted

if(counted_bars>0) counted_bars--;

limit=Bars-counted_bars;

// limit = MathMin(limit, MaxBarsToCount);

//---- macd counted in the 1-st buffer

for(int i=0; i<limit; i++)

{

if(i > MaxBarsToCount) continue;

MacdBuffer=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-

iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);

}

//---- signal line counted in the 2-nd buffer

for(i=0; i<limit; i++)

{

if(i > MaxBarsToCount - SignalSMA) continue;

SignalBuffer=iMAOnArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);

}

//---- done

return(0);

}

thaks, Michel, - we still have our limit (no counted bars recount) -

clean solution too

Files:
 

MAHA (Hu MA HA) -

Hull_maha - another tale chopped (buchered)-off (nicely)

dem maybe i should be a surgeon

int pos, limit;

// if (pos>=(Bars-ExtCountedBars-2)) pos=Bars-ExtCountedBars-2;

limit=Bars-CountedBars;

limit = MathMin(limit, ShowBars+MathPow(MaPeriod,2));

for (pos=limit;pos>=0;pos--)

{

// while(pos>=0)

[...] ...

// pos--;

}

//----

for (int i=0;i<indicator_buffers;i++) SetIndexDrawBegin(i,Bars-ShowBars);
Files:
Reason: