I am hoping to use the Average True Range to set sell trigger like this:
high = history(30, '1d', 'high').dropna(axis=1)
low = history(30, '1d', 'low').dropna(axis=1)
close = history(30, '1d', 'close_price').dropna(axis=1)
try:
atr = talib.ATR(high[stock],
low[stock],
close[stock],
timeperiod=14)[-1]
except:
return
price = data[stock].price
stop_loss_price = price - (atr*2)
ideal_sell_price = price + (atr*2)
if current_positions != 0:
if (price >= ideal_sell_price):
order_target(stock, 0)
print('SELL', stock.symbol, 'above ATR', price)
Above is just a small sample of my code. My problem is I have everything logged so I know when a stock is bought or sold, and what triggered it. However the ATR above never seems to come up. Am I doing something wrong with regard to how I am setting the ideal_sell_price and stop_loss_price?