Error: inputs are all NaN

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):
order_target_percent(context.stock, 2)
order_target_percent(context.stock, -1)


Mattia

2 responses

The variables defined in one function are not visible in other functions, but you can save wt1 and wt2 in context and access them in open_positions function. Also I fixed few small things but still esa ad d are mostly nan, you have to double check the logic of your computation.

import talib
import numpy as np
# ---------------------------------------------------
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(np.abs(ap.values - esa), timeperiod=n1)
ci = (ap - esa) / (0.015 * d)
wt1 = talib.EMA(ci, timeperiod=n2)
wt2 = talib.SMA(wt1, timeperiod=4)
context.wt1 = wt1
context.wt2 = wt2

def open_positions(context, data):
order_target_percent(context.stock, 2)
order_target_percent(context.stock, -1)


Consider the line :

esa = talib.EMA(ap, timeperiod=n1)


With n1= 10 len(ap)= 13, you get 9 NaNs:

[ nan nan nan nan nan nan nan nan nan 323.49433333 323.29718182 324.20175482 325.64628425]


With n1=4 you get 3 NaNs

[ nan nan nan 321.70833333 322.961 323.36193333 323.57182667 324.435096 324.90772427 324.57396789 323.70838074 325.53396177 328.17904373]


This is because the period tells the function how many items you need before computing a value, so if n1 is 10, you need at least 10 elements before computing the value, that's the reason why the first 9 elements are NaNs