Dears,
i'm new to pine script and i'm having an issue trying to backtest a strategy. The idea is to but or sell Vix futures based on the percentil of the VVIX. but when i run the script, it is not holding the position longer than a day and keeps trading in and out the strategy without the expected trigger. i'd be thankfull if anyone can help me figure out what is going on.
thank you
//@version=4
// set backtesting interval
startDate = input(title="Start Date", type=input.time, defval=timestamp("01 Jan 2016 00:00"))
endDate = input(title="End Date", type=input.time, defval=timestamp("31 Dec 2170 23:59"))
inDateRange = (time >= startDate) and (time < endDate)
// strategy is buy 1 vix future when vvix percentil is <= 10 and hold it until vvix percentil is >= 50 or to sell 1 vix future when vvix percentil is >=90 and hold it until VVIX percentil is <=50
strategy("vix selling given vvix level", overlay=false)
percentil = percentrank(close, 250)
vvixpercentil = security("VVIX", "D", percentil)
buy = vvixpercentil >=10
sell = vvixpercentil <=90
close_buy = vvixpercentil >=50
close_sell = vvixpercentil <=50
if(buy and strategy.position_size == 0 and inDateRange)
strategy.entry(id="long", long=true, when = buy)
if(sell and strategy.position_size == 0 and inDateRange)
strategy.entry(id="short", long=false, when = buy)
if (strategy.position_size > 0 and close_buy)
strategy.exit(id="exit long", stop=close)
if (strategy.position_size < 0 and close_sell)
strategy.exit(id="exit short", stop=close)
plot(vvixpercentil)