Welcome to Forex-TSD!, one of the largest Forex forums worldwide, where you will be able to find the most complete and reliable Forex information imaginable.
From the list below, select the forum that you want to visit and register to post, as many times you want. It’s absolutely free. Click here for registering on Forex-TSD.
Exclusive Forum
The Exclusive Forum is the only paid section. Once you subscribe, you will get free access to real cutting-edge Trading Systems (automated and not), Indicators, Signals, Articles, etc., that will help and guide you, in ways that you could only imagine, with your Forex trading.
Elite Section
Get access to private discussions, specialized support, indicators and trading systems reported every week.
Advanced Elite Section
For professional traders, trading system developers and any other member who may need to use and/or convert, the most cutting-edge exclusive indicators and trading systems for MT4 and MT5.
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];
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...
//+------------------------------------------------------------------+
//| 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[i]=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[i]=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:
PHP Code:
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[i]=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[i]=iMAOnArray(MacdBuffer,0,SignalSMA,0,MODE_SMA,i);
//---- done
return(0);
}
//+------------------------------------------------------------------+
after
PHP Code:
//+------------------------------------------------------------------+
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[i]=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[i]=iMAOnArray(MacdBuffer,0,SignalSMA,0,MODE_SMA,i);
//---- done
return(0);
}
//+------------------------------------------------------------------+
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 Code:
limit=Bars-counted_bars;
limit = MathMin(limit, MaxBarsToCount);
//---- macd counted in the 1-st buffer
for(int i=0; i<limit+SignalSMA; i++)
MacdBuffer[i]=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[i]=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):
//---- macd counted in the 1-st buffer
for(int i=0; i<limit; i++)
MacdBuffer[i]=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++)
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 :
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;
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 :
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):
PHP Code:
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[i]=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)
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):
PHP Code:
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[i]=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)
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[i]=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[i]=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)
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[i]=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[i]=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