Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
WaveTrend Oscillator

Hi Quantopian Community,
I am a student and this is one of my first experiments with signals on Quantopian. I write because I was trying to implement the WaveTrend Oscillator strategy: https://www.tradingview.com/script/2KE8wTuF-Indicator-WaveTrend-Oscillator-WT/

This is my code and I'm kind of stuck at the moment because it keeps giving me error and I'm not able to run a backtest.

The main idea is to sell AAPL when the indicator is high and buy when it's low and closing all the positions 30 minutes before the market close, so I could go to bed with happy dreams.

import talib  
import numpy as np  
import scipy  
import math

def initialize(context):  
    context.stock = sid(24)  
    schedule_function(open_positions, date_rules.week_start(), time_rules.market_open())  
    schedule_function(close_positions, date_rules.week_end(), time_rules.market_close(minutes=30))  
def handle_data(context, data):  
    n1 = 10  #"Channel Length"  
    n2 = 21  #"Average Length"  
    period=12  
    if get_open_orders(): return

    close = data.history(context.stock, 'close', period + 1, '1d')  
    low = data.history(context.stock, 'low', period + 1, '1d')  
    high = data.history(context.stock, 'high', period + 1, '1d')  
    ap = (high+low+close)/3  
    esa = talib.EMA(ap, n1)  
    d = talib.EMA(abs(ap - esa), n1)  
    ci = (ap - esa) / (0.015 * d)  
    tci = talib.EMA(ci, n2)  
    wt1 = tci  
    wt2 = talib.SMA(wt1,4)  
def open_positions(context, data):  
    if data.can_trade(context.stock <= wt1):  
        order_target_percent(context.stock, 2)  
    elif data.can_trade(context.stock >= wt2):  
        order_target_percent(context.stock, -1)

def close_positions(context, data):  
        order_target_percent(context.stock, 0)  

Can anyone help me?
Thanks a lot in advance,
Mattia

3 responses

Now it is working properly =)

Wow, neat. Thanks for sharing! Is there any intuition about what this algorithm is doing? The video was not very clear. What's with all these exponential moving averages and how do you set the alpha smoothing factor?

man, I'm new to Q. can you let me know how to start with python in my computer ?