LoMeinMayne92
New member
Hi All,
I'm trying to convert code that I've been using in pinescript to thinkscript so that I can use this platform's tick charts. I know that TOS only provides VIX data and not VXN/VXD/VXO, that's fine, but if anyone can help me out here or at least help me get started that would be awesome! Here is the pinescript:
I'm trying to convert code that I've been using in pinescript to thinkscript so that I can use this platform's tick charts. I know that TOS only provides VIX data and not VXN/VXD/VXO, that's fine, but if anyone can help me out here or at least help me get started that would be awesome! Here is the pinescript:
Code:
//@version=4
study("Expected Move by VIX v4", overlay=true)
//
timeinrange(res, sess) => time(res, sess) != 0
ticker = syminfo.ticker
src=input(title="Source", type=input.source, defval=hl2)
IV_src=input(title="IV Source", type=input.symbol, defval='CBOE:VIX')
default_to_vix9d = input(true, title="Default to VIX when IV is not available")
noPlotOutside = input(true, "Don't plot outside of hours")
optimalEntryTimes = input("1900-1200", "Best Hours", input.session)
resCustom1 = input(title = "secondary res", type = input.resolution, defval = "15")
resCustom2 = input(title = "tertiary res", type = input.resolution, defval = "30")
resCustom3 = input(title = "4th res", type = input.resolution, defval = "60")
resCustom4 = input(title = "5th res", type = input.resolution, defval = "120")
resCustom5 = input(title = "6th res", type = input.resolution, defval = "240")
resCustom6 = input(title = "7th res", type = input.resolution, defval = "1D")
resCustom7 = input(title = "8th res", type = input.resolution, defval = "1W")
resCustom8 = input(title = "9th res", type = input.resolution, defval = "1M")
resCustom9 = input(title = "10th res", type = input.resolution, defval = "1")
doNYOpen = input(defval=false, type = input.bool, title="NY Open On")
doNYSession = input(defval=false, type = input.bool, title="NY Session On")
doNYClose = input(defval=false, type = input.bool, title="NY Close On")
doAussieOpen = input(defval=false, type = input.bool, title="Aussie Open On")
doAussieSession = input(defval=false, type = input.bool, title="Aussie Session On")
doAussieClose = input(defval=false, type = input.bool, title="Aussie Close On")
doAsiaOpen = input(defval=false, type = input.bool, title="Asia Open On")
doAsiaSession = input(defval=false, type = input.bool, title="Asia Session On")
doAsiaClose = input(defval=false, type = input.bool, title="Asia Close On")
doBerOpen = input(defval=false, type = input.bool, title="Berlin Open On")
doBerSession = input(defval=false, type = input.bool, title="Berlin Session On")
doBerClose = input(defval=false, type = input.bool, title="Berlin Close On")
doEurOpen = input(defval=false, type = input.bool, title="Euro Open On")
doEurSession = input(defval=false, type = input.bool, title="Euro Session On")
doEurClose = input(defval=false, type = input.bool, title="Euro Close On")
doOptimalEOpen = input(defval=false, type = input.bool, title="Optimal Entry Open On")
doOptimalESession = input(defval=false, type = input.bool, title="Optimal Entry Session On")
doOptimalEClose = input(defval=false, type = input.bool, title="Optimal Entry Close On")
vix(sym) =>
security(tickerid("CBOE", sym, syminfo.session), timeframe.period, src, true)
d2exp = 30
IV = ticker == "AAPL"? vix("VXAPL"): na
IV := ticker == "AMZN"? vix("VXAZN"): IV
IV := ticker == "IBM"? vix("VXIBM"): IV
IV := ticker == "GOOG"? vix("VXGOG"): IV
IV := ticker == "GS"? vix("VXGS"): IV
vix_security = security(IV_src, timeframe.period, src)
if ticker == "SPY" or (na(IV) and default_to_vix9d)
IV := vix_security // vix("VIX")
vix_security1 = security(IV_src, resCustom1, src)
if ticker == "SPY" or (na(IV) and default_to_vix9d)
IV := vix_security1 // vix("VIX")
vix_security2 = security(IV_src, resCustom2, src)
if ticker == "SPY" or (na(IV) and default_to_vix9d)
IV := vix_security2 // vix("VIX")
vix_security3 = security(IV_src, resCustom3, src)
if ticker == "SPY" or (na(IV) and default_to_vix9d)
IV := vix_security3 // vix("VIX")
vix_security4 = security(IV_src, resCustom4, src)
if ticker == "SPY" or (na(IV) and default_to_vix9d)
IV := vix_security4 // vix("VIX")
vix_security5 = security(IV_src, resCustom5, src)
if ticker == "SPY" or (na(IV) and default_to_vix9d)
IV := vix_security5 // vix("VIX")
vix_security6 = security(IV_src, resCustom6, src)
if ticker == "SPY" or (na(IV) and default_to_vix9d)
IV := vix_security6 // vix("VIX")
vix_security7 = security(IV_src, resCustom7, src)
if ticker == "SPY" or (na(IV) and default_to_vix9d)
IV := vix_security7 // vix("VIX")
vix_security8 = security(IV_src, resCustom8, src)
if ticker == "SPY" or (na(IV) and default_to_vix9d)
IV := vix_security8 // vix("VIX")
vix_security9 = security(IV_src, resCustom9, src)
if ticker == "SPY" or (na(IV) and default_to_vix9d)
IV := vix_security9 // vix("VIX")
// Standard deviation (expected 1 SD move) calculation from VIX (equivalent to 30 days Implied Volatility)
// Example: https://money.stackexchange.com/questions/81494/how-to-calculate-the-expected-move-of-a-stock
sd = (src * (IV / 100.0) * sqrt(d2exp/365.0)) / d2exp
sd1 = (src * (vix_security1 / 100) * sqrt(d2exp/365.0))/ d2exp
sd2 = (src * (vix_security2 / 100) * sqrt(d2exp/365.0))/ d2exp
sd3 = (src * (vix_security3 / 100) * sqrt(d2exp/365.0))/ d2exp
sd4 = (src * (vix_security4 / 100) * sqrt(d2exp/365.0))/ d2exp
sd5 = (src * (vix_security5 / 100) * sqrt(d2exp/365.0))/ d2exp
sd6 = (src * (vix_security6 / 100) * sqrt(d2exp/365.0))/ d2exp
sd7 = (src * (vix_security7 / 100) * sqrt(d2exp/365.0))/ d2exp
sd8 = (src * (vix_security8 / 100) * sqrt(d2exp/365.0))/ d2exp
sd9 = (src * (vix_security9 / 100) * sqrt(d2exp/365.0))/ d2exp
// UI code starts here --
// Dynamically shows the lines if price gets close to reduce the clutters
show_if_close_by_percentage = 99
keep_showing_once_shown = 25
is_near(l, lb) => abs(l[lb]-src[lb]) < (show_if_close_by_percentage/100.0)*sd
is_near_since_back(l, far) =>
is_n = false
for i = 0 to far
is_n := is_n or is_near(l, i)
is_n
show(l) => is_near_since_back(l, keep_showing_once_shown)? l: na
// Style & Colors
_style=plot.style_linebr
_up_color = #F86161 // red
_down_color = #B7FFBF // green
_up_color2 = #75C2E1
_down_color2 = #75C2E1
_up_color3 = #E1E100
_down_color3 = #E1E100
_up_color4 = #ADE199
_down_color4 = #ADE199
_up_color5 = #E16E74
_down_color5 = #E16E74
_up_color6 = #DFDFE1
_down_color6 = #DFDFE1
_up_color7 = #E10400
_down_color7 = #08E100
_up_color8 = #E10400
_down_color8 = #08E100
_up_color9 = #E10400
_down_color9 = #08E100
_up_color10 = #E10400
_down_color10 = #08E100
nySessionStart = #FFFDF2
nySession = #FFFDF2
nySessionEnd = #FFFDF2
australiaSessionStart = #A9A9A9
australiaSession = #A9A9A9
australiaSessionEnd = #A9A9A9
asiaSessionStart = #DAA520
asiaSession = #DAA520
asiaSessionEnd = #DAA520
berlinSessionStart = #C414FF
berlinSession = #C414FF
berlinSessionEnd = #C414FF
europeSessionStart = #1E90FF
europeSession = #1E90FF
europeSessionEnd = #1E90FF
OptimalESessionStart = #00FF22
OptimalESession = #00FF22
OptimalESessionEnd = #00FF22
start_transp = 99
start_transp_line = 20
transp_delta = 5
vwapc = color.new(color.white, start_transp_line - 3 * transp_delta)
uc1 = color.new(_up_color, start_transp_line)
uc2 = color.new(_up_color, start_transp_line - 1 * transp_delta)
uc3 = color.new(_up_color, start_transp_line - 2 * transp_delta)
uc3_2 = color.new(_up_color2, start_transp_line - 2 * transp_delta)
uc3_3 = color.new(_up_color3, start_transp_line - 2 * transp_delta)
uc3_4 = color.new(_up_color4, start_transp_line - 2 * transp_delta)
uc3_5 = color.new(_up_color5, start_transp_line - 2 * transp_delta)
uc3_6 = color.new(_up_color6, start_transp_line - 2 * transp_delta)
uc3_7 = color.new(_up_color7, start_transp_line - 2 * transp_delta)
uc3_8 = color.new(_up_color8, start_transp_line - 2 * transp_delta)
uc3_9 = color.new(_up_color9, start_transp_line - 2 * transp_delta)
uc3_10 = color.new(_up_color10, start_transp_line - 2 * transp_delta)
dc1 = color.new(_down_color, start_transp_line)
dc2 = color.new(_down_color, start_transp_line - 1 * transp_delta)
dc3 = color.new(_down_color, start_transp_line - 2 * transp_delta)
dc3_2 = color.new(_down_color2, start_transp_line - 2 * transp_delta)
dc3_3 = color.new(_down_color3, start_transp_line - 2 * transp_delta)
dc3_4 = color.new(_down_color4, start_transp_line - 2 * transp_delta)
dc3_5 = color.new(_down_color5, start_transp_line - 2 * transp_delta)
dc3_6 = color.new(_down_color6, start_transp_line - 2 * transp_delta)
dc3_7 = color.new(_down_color7, start_transp_line - 2 * transp_delta)
dc3_8 = color.new(_down_color8, start_transp_line - 2 * transp_delta)
dc3_9 = color.new(_down_color9, start_transp_line - 2 * transp_delta)
dc3_10 = color.new(_down_color10, start_transp_line - 2 * transp_delta)
ufc1 = color.new(_up_color, start_transp)
ufc2 = color.new(_up_color, start_transp - 1 * transp_delta)
ufc3 = color.new(_up_color, start_transp - 2 * transp_delta)
dfc1 = color.new(_down_color, start_transp)
dfc2 = color.new(_down_color, start_transp - 1 * transp_delta)
dfc3 = color.new(_down_color, start_transp - 2 * transp_delta)
bgcolor(doNYOpen and timeinrange(timeframe.period, "0930-0940") ? nySessionStart : na, transp=10)
bgcolor(doNYSession and timeinrange(timeframe.period, "0930-1700") ? nySession : na, transp=75)
bgcolor(doNYClose and timeinrange(timeframe.period, "1650-1700") ? nySessionEnd : na, transp=20)
bgcolor(doAussieOpen and timeinrange(timeframe.period, "1700-1710") ? australiaSessionStart : na, transp=10)
bgcolor(doAussieSession and timeinrange(timeframe.period, "1700-0200") ? australiaSession : na, transp=75)
bgcolor(doAussieClose and timeinrange(timeframe.period, "0150-0200") ? australiaSessionEnd : na, transp=20)
bgcolor(doAsiaOpen and timeinrange(timeframe.period, "1900-1910") ? asiaSessionStart : na, transp=10)
bgcolor(doAsiaSession and timeinrange(timeframe.period, "1900-0400") ? asiaSession : na, transp=75)
bgcolor(doAsiaClose and timeinrange(timeframe.period, "0350-0400") ? asiaSessionEnd : na, transp=20)
bgcolor(doBerOpen and timeinrange(timeframe.period, "0200-0210") ? europeSessionStart : na, transp=10)
bgcolor(doBerSession and timeinrange(timeframe.period, "0200-1200") ? europeSession : na, transp=75)
bgcolor(doBerClose and timeinrange(timeframe.period, "1150-1200") ? europeSessionEnd : na, transp=20)
bgcolor(doEurOpen and timeinrange(timeframe.period, "0300-0310") ? europeSessionStart : na, transp=10)
bgcolor(doEurSession and timeinrange(timeframe.period, "0300-1200") ? europeSession : na, transp=75)
bgcolor(doEurClose and timeinrange(timeframe.period, "1150-1200") ? europeSessionEnd : na, transp=20)
bgcolor(doOptimalEOpen and timeinrange(timeframe.period, "1900-1910") ? OptimalESessionStart : na, transp=10)
bgcolor(doOptimalESession and timeinrange(timeframe.period, "1900-1200") ? OptimalESession : na, transp=75)
bgcolor(doOptimalEClose and timeinrange(timeframe.period, "1150-1200") ? OptimalESessionEnd : na, transp=20)
// Plots
day_start = time - time[1] > 25000000
vw1 = vwap(src)
vw = day_start? na: vw1
v = plot(vw, title="VWAP", color=vwapc, style=_style, transp = 50)
s1 = plot(show(vw + sd), title="+1 Std Dev", color=uc1, style=_style, transp = 50, display=display.none)
s_1 = plot(show(vw - sd), title="-1 Std Dev", color=dc1, style=_style, transp = 50, display=display.none)
s2 = plot(show(vw + 2*sd), title="+2 Std Dev", color=uc2, style=_style, transp = 50, display=display.none)
s_2 = plot(show(vw - 2*sd), title="-2 Std Dev", color=dc2, style=_style, transp = 50, display=display.none)
s3 = plot(show(vw + 3*sd), title="+3 Std Dev", color=uc3, style=_style, transp = 50)
s_3 = plot(show(vw - 3*sd), title="-3 Std Dev", color=dc3, style=_style, transp = 50)
ss3 = plot(show(vw + 3*sd1), title="resCust1 +3 Std Dev", color=uc3_2, style=_style, transp=50)
ss_3 = plot(show(vw - 3*sd1), title="resCust1 -3 Std Dev", color=dc3_2, style=_style, transp = 50)
sss3 = plot(show(vw + 3*sd2), title="resCust2 +3 Std Dev", color=uc3_3, style=_style, transp=50)
sss_3 = plot(show(vw - 3*sd2), title="resCust2 -3 Std Dev", color=dc3_3, style=_style, transp = 50)
ssss3 = plot(show(vw + 3*sd3), title="resCust3 +3 Std Dev", color=uc3_4, style=_style, transp=50)
ssss_3 = plot(show(vw - 3*sd3), title="resCust3 -3 Std Dev", color=dc3_4, style=_style, transp = 50)
sssss3 = plot(show(vw + 3*sd4), title="resCust4 +3 Std Dev", color=uc3_5, style=_style, transp=50)
sssss_3 = plot(show(vw - 3*sd4), title="resCust4 -3 Std Dev", color=dc3_5, style=_style, transp = 50)
ssssss3 = plot(show(vw + 3*sd5), title="resCust5 +3 Std Dev", color=uc3_6, style=_style, transp=50)
ssssss_3 = plot(show(vw - 3*sd5), title="resCust5 -3 Std Dev", color=dc3_6, style=_style, transp = 50)
sssssss3 = plot(show(vw + 3*sd6), title="resCust6 +3 Std Dev", color=uc3_7, linewidth=2, style=_style, transp=50)
sssssss_3 = plot(show(vw - 3*sd6), title="resCust6 -3 Std Dev", color=dc3_7, linewidth=2, style=_style, transp = 50)
ssssssss3 = plot(show(vw + 3*sd7), title="resCust7 +3 Std Dev", color=uc3_8, linewidth=3, style=_style, transp=50)
ssssssss_3 = plot(show(vw - 3*sd7), title="resCust7 -3 Std Dev", color=dc3_8, linewidth=3, style=_style, transp = 50)
sssssssss3 = plot(show(vw + 3*sd8), title="resCust8 +3 Std Dev", color=uc3_9, linewidth=4, style=_style, transp=50)
sssssssss_3 = plot(show(vw - 3*sd8), title="resCust8 -3 Std Dev", color=dc3_9, linewidth=4, style=_style, transp = 50)
ssssssssss3 = plot(show(vw + 3*sd9), title="resCust9 +3 Std Dev", color=uc3_10, linewidth=1, style=_style, transp=50)
ssssssssss_3 = plot(show(vw - 3*sd9), title="resCust9 -3 Std Dev", color=dc3_10, linewidth=1, style=_style, transp = 50)
optimalTime = time(timeframe.period, optimalEntryTimes)
n1 = low < vw-3*sd
n_1 = high > vw+3*sd
n2 = low < vw-3*sd1
n_2 = high > vw+3*sd1
n3 = low < vw-3*sd2
n_3 = high > vw+3*sd2
n4 = low < vw-3*sd3
n_4 = high > vw+3*sd3
n5 = low < vw-3*sd4
n_5 = high > vw+3*sd4
n6 = low < vw-3*sd5
n_6 = high > vw+3*sd5
if (high > vw+3*sd5)
var label1 = label.new(bar_index, high, text="Price breached +3std on: \n" + resCustom1 + ", " + resCustom2 + ", " + resCustom3 + ", " + resCustom4 + ", " + resCustom5 + ", " + resCustom9, yloc=yloc.abovebar, style=label.style_label_down)
label.set_x(label1, 0)
label.set_xloc(label1, time, xloc.bar_time)
label.set_color(label1, color.red)
label.set_size(label1, size.normal)
if (low < vw-3*sd5)
var label1 = label.new(bar_index, low, text="Price breached -3std on: \n" + resCustom1 + ", " + resCustom2 + ", " + resCustom3 + ", " + resCustom4 + ", " + resCustom5 + ", " + resCustom9, yloc=yloc.belowbar, style=label.style_label_up)
label.set_x(label1, 0)
label.set_xloc(label1, time, xloc.bar_time)
label.set_color(label1, color.lime)
label.set_size(label1, size.normal)
plotshape(n1 and optimalTime and noPlotOutside, "n1 current res", style = shape.arrowup, color = #FF00FF, location = location.belowbar)
plotshape(n_1 and optimalTime and noPlotOutside, "n_1 current res", style = shape.arrowdown, color = #FF00FF, location = location.abovebar)
plotshape(n2 and optimalTime and noPlotOutside, "n2 rescust1", style = shape.arrowup, color = #75C2E1, location = location.belowbar)
plotshape(n_2 and optimalTime and noPlotOutside, "n_2 rescust1", style = shape.arrowdown, color = #75C2E1, location = location.abovebar)
plotshape(n3 and optimalTime and noPlotOutside, "n3 rescust2", style = shape.arrowup, color = #E1E100, location = location.belowbar)
plotshape(n_3 and optimalTime and noPlotOutside, "n_3 rescust2", style = shape.arrowdown, color = #E1E100, location = location.abovebar)
plotshape(n4 and optimalTime and noPlotOutside, "n4 rescust3", style = shape.arrowup, color = #ADE199, location = location.belowbar)
plotshape(n_4 and optimalTime and noPlotOutside, "n_4 rescust3", style = shape.arrowdown, color = #ADE199, location = location.abovebar)
plotshape(n5 and optimalTime and noPlotOutside, "n5 rescust4", style = shape.arrowup, color = #E16E74, location = location.belowbar)
plotshape(n_5 and optimalTime and noPlotOutside, "n_5 rescust4", style = shape.arrowdown, color = #E16E74, location = location.abovebar)
plotshape(n6 and optimalTime and noPlotOutside, "n6 rescust5", style = shape.arrowup, color = #DFDFE1, location = location.belowbar)
plotshape(n_6 and optimalTime and noPlotOutside, "n_6 rescust5", style = shape.arrowdown, color = #DFDFE1, location = location.abovebar)
alertcondition(n1 and optimalTime and noPlotOutside, title='Long Entry', message='-3std crossing down at {{timenow}} at price {{low}}')
alertcondition(n_1 and optimalTime and noPlotOutside, title='Short Entry', message='+3std crossing up at {{timenow}} at price {{high}}')
Last edited by a moderator: