Airwave
Airwave

Reputation: 306

PineScript create a source with n Candles in future for forcasts in Tradingview

in addition to my question in EMA for One Candle in Future I'm now trying to modify a source for some forecasts in Tradingview.

In this modification I would like to modify the default series in that way, that I move every candle in the series N places into the past and overwrite the the places that were then vacated with the newest bar.

See picture for better description

I only need to do this on the current bar, as I want to plot the result with offset=n into the future.

Current idea is following, but I cannot compile. Error is Syntax error at input '='. Could you help me to create this function please?

Thanks so much in advance.

//@version=4

study(title="candle experiment", shorttitle="candle_experiement")

sourcePlusTwoCandle(src , length) => 
    newSource := na
    for i = 2 to length+2 
        newSource[i]=src[i-2] //move every value places into the past
    newSource[1]=src[0] //overwrite the "vacated place" with current bar
    newSource[0]=src[0] //overwrite the "vacated place" with current bar

candles = 0.0
if not barstate.isconfirmed 
    candles = sourcePlusTwoCandle(close, 20)

plot(candles, color=color.white,offset=2,linewidth=6)

Upvotes: 1

Views: 1394

Answers (1)

Bjorn Mistiaen
Bjorn Mistiaen

Reputation: 6865

The way you should use the assignment operator is like this:

//@version=4
study(title="candle experiment", shorttitle="candle_experiement")

var float newSource = na
var float candles   = na

sourcePlusTwoCandle(src, length) => 
    newSource := na
    for i = 2 to length+2 
        newSource[i] := src[i-2] //move every value places into the past
    newSource[1] := src[0] //overwrite the "vacated place" with current bar
    newSource[0] := src[0] //overwrite the "vacated place" with current bar

candles := 0.0
if not barstate.isconfirmed 
    candles := sourcePlusTwoCandle(close, 20)

plot(candles, color=color.white,offset=2,linewidth=6)

But that too won't compile, because you're trying to assign a value to a bar in the past.
That cannot be done in Pine. Why that is, is explained in Can I use the := operator to assign values to past values of a series?

Upvotes: 1

Related Questions