Reputation: 3
I try to migrate the script from v2 to v3 PineScript. However, it turns out that in v3 the same code returns different values. How it's possible and what I did wrong? Below you may find the code of this script.
That's how it looks like on the chart https://www.tradingview.com/x/itg3HS0T/?
Test1 - v2, green, gray lines Test2 - v3, pink, blue lines
Thank you in advance for your help!:)
//@version=2
study("Test2",overlay=true)
long_timeframe = input(title="Long timeframe", type=resolution, defval="180")
short_timeframe = input(title="Long timeframe", type=resolution, defval="60")
step_shift = input(0,"Step Shift")
ha_symbol = heikinashi(tickerid)
long_ha_close = security(ha_symbol, long_timeframe, hlc3)
short_ha_close = security(ha_symbol, short_timeframe, hlc3)
long_step = ema(long_ha_close[step_shift],1)
short_step = ema(short_ha_close[step_shift],1)
plot(long_step,title="LongStep",color=white,linewidth=2,style=line)
plot(short_step,title="ShortStep",color=silver,linewidth=2,style=line)
Upvotes: 0
Views: 575
Reputation: 8799
The difference is due to the fact that security()
function's lookahead
parameter has a default value of on
in v2 and off
in v3, so need to set it to on
explicitly in v3.
//@version=3
study("Test3",overlay=true)
long_timeframe = input(title="Long timeframe", type=resolution, defval="180")
short_timeframe = input(title="Long timeframe", type=resolution, defval="60")
step_shift = input(0,"Step Shift")
ha_symbol = heikinashi(tickerid)
// These 2 lines yield same result as v2 version of the script.
long_ha_close = security(ha_symbol, long_timeframe, hlc3, lookahead=barmerge.lookahead_on)
short_ha_close = security(ha_symbol, short_timeframe, hlc3, lookahead=barmerge.lookahead_on)
// To avoid repainting, these 2 lines are preferable.
// long_ha_close = security(ha_symbol, long_timeframe, hlc3[1], lookahead=barmerge.lookahead_on)
// short_ha_close = security(ha_symbol, short_timeframe, hlc3[1], lookahead=barmerge.lookahead_on)
long_step = ema(long_ha_close[step_shift],1)
short_step = ema(short_ha_close[step_shift],1)
plot(long_step,title="LongStep",color=aqua,linewidth=2,style=line)
plot(short_step,title="ShortStep",color=fuchsia,linewidth=2,style=line)
Upvotes: 2