Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Troubles trying to implement my first signal

Hi everybody,

Recently I found this signal: https://www.tradingview.com/script/og7JPrRA-CM-Williams-Vix-Fix-Finds-Market-Bottoms/
which looked pretty cool and I have been trying to make it work on Quantopian to do some backtests for a university project.

study("CM_Williams_Vix_Fix", overlay=false)  
pd = input(22, title="LookBack Period Standard Deviation High")  
bbl = input(20, title="Bolinger Band Length")  
mult = input(2.0    , minval=1, maxval=5, title="Bollinger Band Standard Devaition Up")  
lb = input(50  , title="Look Back Period Percentile High")  
ph = input(.85, title="Highest Percentile - 0.90=90%, 0.95=95%, 0.99=99%")  
pl = input(1.01, title="Lowest Percentile - 1.10=90%, 1.05=95%, 1.01=99%")  
hp = input(false, title="Show High Range - Based on Percentile and LookBack Period?")  
sd = input(false, title="Show Standard Deviation Line?")

wvf = ((highest(close, pd)-low)/(highest(close, pd)))*100

sDev = mult * stdev(wvf, bbl)  
midLine = sma(wvf, bbl)  
lowerBand = midLine - sDev  
upperBand = midLine + sDev

rangeHigh = (highest(wvf, lb)) * ph  
rangeLow = (lowest(wvf, lb)) * pl


col = wvf >= upperBand or wvf >= rangeHigh ? lime : gray


plot(hp and rangeHigh ? rangeHigh : na, title="Range High Percentile", style=line, linewidth=4, color=orange)  
plot(hp and rangeLow ? rangeLow : na, title="Range High Percentile", style=line, linewidth=4, color=orange)  
plot(wvf, title="Williams Vix Fix", style=histogram, linewidth = 4, color=col)  
plot(sd and upperBand ? upperBand : na, title="Upper Band", style=line, linewidth = 3, color=aqua)  

This code should be in C++, I believe, a language that I never used before and I don't think I translated properly in Python, since I kept messing up.
Could anyone help me? Any suggestions on how to implement it for example in the AAPL stock?
Thanks in advance,
Mattia

4 responses

Mattia,

May be this William's Volatility Fix (WVF) indicator from my library will help you.

# -----------------------------------------------  
stock, period, UB, DB  = symbol('AAPL'), 12, 3, 1  
# -----------------------------------------------

def initialize(context):  
    schedule_function(trade, date_rules.every_day(), time_rules.market_open(minutes = 65))  

def trade(context, data):  
    LB = UB - DB  
    prices = data.history(stock, 'close', period + 1, '1d')  
    lows = data.history(stock, 'low', period + 1, '1d')  
    highest_close = prices.rolling(period, center = False).max()

    WVF = ((highest_close - lows[-1])/(highest_close)) * 100  # William's Volatility Fix  

    record(WVF = WVF[-1], UB = UB, LB = LB )  

Hi Vladimir,
Thanks for the help! The signal works here
Unfortunately, when I tried to apply it for a buy/sell strategy it kept giving me error.
What I wanted to do is to buy when the signal is high (for example bigger than 5, which could represent the lowest point for the stock) and sell when the signal is low (for example lower than 1.5, that could represent the moment in which the stock is about to drop because it's overvalued).

This is the code I tried to use

# -----------------------------------------------  
stock, period, UB, DB  = symbol('AAPL'), 12, 3, 1  
# -----------------------------------------------  
def initialize(context):  
    schedule_function(open_positions, date_rules.every_day(), time_rules.market_open(minutes = 65))  
    schedule_function(close_positions, date_rules.every_day(), time_rules.market_open(minutes = 65)) 

def open_positions(context, data):  
    LB = UB - DB  
    prices = data.history(stock, 'close', period + 1, '1d')  
    lows = data.history(stock, 'low', period + 1, '1d')  
    highest_close = prices.rolling(period, center = False).max()  
    WVF = ((highest_close - lows[-1])/(highest_close)) * 100  # William's Volatility Fix  
    record(WVF = WVF[-1], UB = UB, LB = LB )  
    current_positions = context.portfolio.positions[stock].amount  
    if (WVF > 5) and current_positions == 0:  
        order_target_percent(context.aapl, 2.0)  
def close_positions(context, data):  
    LB = UB - DB  
    prices = data.history(stock, 'close', period + 1, '1d')  
    lows = data.history(stock, 'low', period + 1, '1d')  
    highest_close = prices.rolling(period, center = False).max()

    WVF = ((highest_close - lows[-1])/(highest_close)) * 100  # William's Volatility Fix  

    record(WVF = WVF[-1], UB = UB, LB = LB )  
    current_positions = context.portfolio.positions[stock].amount  
    if (WVF < 1.5) and current_positions != 0:  
        order_target_percent(context.aapl, 0)  

Do you have any idea, where I am mistaking?
Thanks a lot,
Mattia

Mattia,

Try this it should work.

# ---------------------------------------------------  
stock, period, UB, DB  = symbol('AAPL'), 12, 2.0, 0.5  
# ---------------------------------------------------  
def initialize(context):  
    schedule_function(trade, date_rules.every_day(), time_rules.market_open(minutes = 65))  

def trade(context, data):  
    LB = UB - DB  
    if get_open_orders(): return

    prices = data.history(stock, 'close', period + 1, '1d')  
    lows = data.history(stock, 'low', period + 1, '1d')  
    highest_close = prices.rolling(period, center = False).max()

    WVF = ((highest_close - lows[-1])/(highest_close)) * 100  # William's Volatility Fix  
    record(WVF = WVF[-1], UB = UB, LB = LB )

    if data.can_trade(stock):  
        if WVF[-1] < LB :  
            order_target_percent(stock, 1.0)  
        elif WVF[-1] > UB :  
            order_target(stock, 0) 

Vladimir,
Now it works, thanks so much!