i keep getting the following error
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
There was a runtime error on line 26.
LINE 26 - is the beginning of the if statement and is highlighted in bold. thank you in advance!!!!
import numpy as np
def initialize(context):
context.cost = sid(1787)
schedule_function(compute_z_score,date_rules.every_day(), time_rules.market_close(minutes=60))
# Flags to tell us if we're currently in a trade
context.long_on_spread = False
context.shorting_spread = False
def compute_z_score(context, data):
#for conveniece
costco = context.cost
#historical data
prices = data.history(context.cost,"price",1,"1d")
#create our cumulative moving average
cumulative_moving_average = prices.expanding(min_periods=1).mean()
#compute z score components
std = prices.expanding(min_periods=1).std()
#compute z score
**if std > 0:**
z_score = (prices - cumulative_moving_average) / std
#entry case 1
if z_score > 1 and not context.shorting_spread:
order_target_percent(costco, -1)
context.shorting_spread = True
context.long_on_spread = False
elif z_score < -1 and not context.long_on_spread:
order_target_percent(costco, 1)
context.shorting_spread = False
context.long_on_spread = True
elif -0.5 < z_score < 0.5:
order_target_percent(costco, 0)
context.shorting_spread = False
context.long_on_spread = False
record('zscore', z_score)