简体   繁体   中英

PineScript problem, shifting current series into the past

I want to shift "close" series into the past by X elements and then populate those X elements with data to simulate how %B, RSI, ema/sma, etc is affected in the future.

hist[5] = close

hist[4] = 5

hist[3] = 5.5

hist[2] = 5

hist[1] = 4.5

hist[0] = 6

I want to compute ema/sma/rsi etc on series "hist". How can I do this with PineScript?

What you want is not possible. I can not get the close of the last bar 6 bars early. You can not peek into the future . But if you only want to know some values of these indicators, just fill in the close by hand and let it plot the result. This will show up in the beginning of the chart.

//@version=5
indicator("My script", overlay=false)

data = array.new<float>()
array.push(data, 100)
array.push(data, 5)
array.push(data, 5.5)
array.push(data, 4.5)
array.push(data, 6)

current = array.get(data, bar_index < array.size(data) ? bar_index : 4)
plot(ta.ema(current, 5))

But if you really want this. Then you need to implement the TA functions your selfs one by one and hardcode the values as you already did.

Example of EMA

//@version=5
indicator("My script", overlay=false)

m(weight, price) => price * weight
ema(close) => (m(1, close) + m(2, 5) + m(3, 5.5) + m(4, 4.5) + m(5, 6)) / 15

plot(ema(close))

SMA is the same but then a 1 for all weights and then divide by 5 and not 15. The others are also not to hard but more work. Hope i awnsered your question. Or at least give you something to work with.

Extra tip: Some TA functions do need the data in a specified order like ta.sma but ta.ema does. Thus maybe you can take a shortcut.

For questions about implementing your own TA functions, just ask here at stackoverflow.

While you can refer to past values of series, you cannot modify them. Only the current series value can be assigned a value.

https://www.tradingview.com/pine-script-docs/en/v4/language/Operators.html#history-reference-operator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM