Plotting indicator to future

With the below code taking a base value of 100 , a period of 30 and a percentage
value of 1 I have tried to do the following

for the first 15 period the base value will increase by 1% per period and for the 2nd half the base value will decrease by 1% per period.

 p = Param("%",1,1,99,1);
d = ParamDate("Start","23-11-2016");

value = 100;
phase = 30;

bar = BarsSince(DateNum()==d); 

t = bar % phase;

amount = (value*p/100) * (bar%(phase/2));
x = IIf(t <= (phase/2), (value + amount), 
                 IIf(t > (phase/2), (value - amount),Null));


As I am taking the barsSince function it will plot the indicator upto the last bar.
Now I want to plot this 100 period in future , so what should I do?

please help. Thanks.

@nudip, please, take a look at this KB article:

Drawing line extensions on future bars using AFL

I do not know if it will be ok for what you want to do, but you should note this:

All the calculations in AFL language are performed within the ‘real bars’ area, i.e. on the available elements of the array, between array item 0 until array item (Barcount-1).

The calculations past the very last bar are not possible, because that would require a longer array than the one we work on.

As a result, the article suggests an alternative approach.

Moderator comment: This excerpt is misleading. Calculations past very last bar are possible, but you need to use technique described in KB article. Which means: read the entire article


thanks for the reply sir. Xshift will not work for me. I want to calculate the indicator value for future bar say barcount + 250.

@mradtke @fxshrat sir please suggest if anything is possible

@nudip, why do you think Xshift will not work?

It appears as if your "calculation" is rythmic, so can you not just set your "start date" back a few multiples of your calculation cycle to provide you with enough data that you can use the Xshift?


please elaborate a bit more. thanks for the guidance sir. I have go through the linke provided by @beppe, but not able to implement it in my code


XShift and Ref() together work perfectly for calculating and displaying indicators that have future values. Look at "Ichimoku clould" code shipped with AmiBroker, or "Displaced Moving Average" also shipped with AmIBroker. It can be created via drag drop (price + DispMA overlay).

Plot( C, "Close", colorDefault, styleCandle ); 
Displacement = Param("Displacement", 15, -50, 50 );
Periods = Param("Periods", 20, 1, 100 );
m = MA( Close, Periods );
Plot( m, _DEFAULT_NAME(), colorRed, styleLine, 0, 0, Displacement );

Your code can be adopted too easily instead of assuming things without actually trying. You already had all necessary information served by @beppe and @snoopy.pa30 it was just a matter of making some effort and following their advice (actually just reading the Knowledge Base article to the very end: - as it has everything explained step-by-step with every possible detail)

If you have put a single second of thought you would just change SINGLE LINE of your code from

Plot(x,"x",colorAqua); // original

into this:

Plot(x,"x",colorAqua, styleLine, 0, 0, phase); // after change it plots into the future!

Surpise. It plots into the future.

If you want this sawtooth to start at specified date you need to change 5th line of your code to:

bar = BarsSince(Ref(DateNum()==d, phase)); 

Again, this Ref() technique is explained in great deal of detail in the KB article quotes. It was just a matter of reading and copy-pasting code from KB.

You can apply (2 * phase) or (3 * phase) shift or whatever multiple as @snoopy.pa30 already suggested earlier.

Remember: Everything in AmiBroker is possible.

PS. This thread shows that excerpts from Knowledge Base only give excuse NOT to read the entire KB article.

1 Like