Good afternoon,
I am a student and 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. It seems like it doesn't recognise the local variables wt1 and wt2 even though I use them in the "open_positions" function right after. what I wanted to do is selling AAPL when the indicator is high and buying it when is low. Can anyone help me?
import talib
# ---------------------------------------------------
n1, n2, period = 10, 21, 12
# ---------------------------------------------------
def initialize(context):
context.stock = sid(24)
schedule_function(open_positions, date_rules.week_start(), time_rules.market_open())
def handle_data(context, data):
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, timeperiod=n1)
d = talib.EMA(abs(ap - esa), timeperiod=n1)
ci = (ap - esa) / (0.015 * d)
wt1 = talib.EMA(ci, timeperiod=n2)
wt2 = talib.SMA(wt1, timeperiod=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)
Thanks in advance,
Mattia