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

帅比萌擦擦* 提交于 2020-12-13 03:11:33

问题


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)

回答1:


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?



来源:https://stackoverflow.com/questions/64968694/pinescript-create-a-source-with-n-candles-in-future-for-forcasts-in-tradingview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!