Strategy Oil Z Score
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jroche1973


//@version=4
strategy("Strategy Oil Z Score", precision=6, overlay=false, pyramiding=100, calc_on_order_fills=true)

f_print(_text) =>
    // Create label on the first bar.
    var _label = label.new(bar_index, na, _text, xloc.bar_index, yloc.price, color(na), label.style_none, color.gray, size.large, text.align_left)
    // On next bars, update the label's x and y position, and the text it displays.
    label.set_xy(_label, bar_index, highest(10)[1])
    label.set_text(_label, _text)
    
Length      = input(252) //252

o_StdDevs   = input(defval = 2.0, title="Oil Z Std Dev")
use_what    = input(title="Use underlying", defval="Oil", options=["Oil", "Move", "Vix"])


_bb_mult    = input(title="BB mult", type=input.float, defval=2)
_bb_range   = input(title="BB mean range", type=input.integer, defval=20)

//src = input(close)
MOVE        = security("TVC:MOVE", timeframe.period, close)
VIX         = security("VIX", timeframe.period, close)
OILVIX      = security("CBOE:OVX", timeframe.period, close)
USO         = security("TVC:USOIL", timeframe.period, close)
show_ema    = input(type=input.bool, title="Apply EMA", defval=true)
l_ema       = input( title="EMA Length", defval=100)

startDateTime = input(type=input.time, defval=timestamp("15 July 2019 09:30"), 
     title="Start Date", group="Strategy Date Range", 
     tooltip="Specifies the start date and time from which " + 
     "the strategy simulates buy and sell trades.")    


[_bb_mid, _bb_hig, _bb_low] = bb(close, _bb_range, _bb_mult)

_bb_percent = (close - _bb_low) / (_bb_hig - _bb_low)
s_ema = ema(close, l_ema)

iOILVIX = 1/OILVIX
divO = USO/iOILVIX
//Proxy of M of Move ie alt VVIX
if use_what == "Move"
    divO := MOVE/iOILVIX
else if use_what == "Vix"
    divO := VIX/iOILVIX
else
    divO := USO/iOILVIX

m_basis = sma(divO, Length)

m_zscore = (divO - m_basis) / stdev(divO, Length)
show_m = true

hline(o_StdDevs, color=color.white, linestyle=hline.style_dotted)
hline(-1 * o_StdDevs, color=color.white, linestyle=hline.style_dotted)


color m_color = m_zscore < 0 ? color.new(color.white, 70): color.new(color.blue, 70) //we want white if not active

color mb_color = m_zscore < 0 and _bb_percent > 0.5 ? color.new(color.white, 70): color.new(color.blue, 70) //we want white if not active

use_b = _bb_percent < 0.5 ? true: false


bgcolor(show_m and use_b ? color.new(m_color,70) : color.new(color.white, 70)) // we want whatever color our status stipulates if 'on' or white if 'off'

//Strategy Testing

long_move =     m_zscore < 0    and     _bb_percent > 0.5

short_move =    m_zscore > 0    and     _bb_percent < 0.5

if show_ema
    if time >= startDateTime and time <= timenow 
        if m_zscore > 0 and _bb_percent < 0.5 and close < s_ema //short
            strategy.entry("sell", strategy.short, 100,  when=strategy.position_size > 0)
            strategy.close("close long", when=long_move)
        else
            strategy.entry("buy", strategy.long, 100, when=strategy.position_size <= 0)
            strategy.close("close short", when=short_move)
else
    if time >= startDateTime and time <= timenow 
        if m_zscore > 0 and _bb_percent < 0.5//short
            strategy.entry("sell", strategy.short, 100,  when=strategy.position_size > 0)
            strategy.close("close long", when=long_move)
        else
            strategy.entry("buy", strategy.long, 100, when=strategy.position_size <= 0)
            strategy.close("close short", when=short_move)



