Hello All,
I am trying to deal with leverage, and in setting up the order logic to do so I am running into some issues around NaN values.
What I am trying to do is simply base the order value on the available funds in the portfolio divided by the stock price. The issue was the math with a NaN object. I initially tried to just skip over NaN values:
Here is the code (I have not been able to run a back test because of said errors):
def handle_data(context, data):
results = pipeline_output('my_pipeline')
context.pipeline_results = results.sort_values('SMARatio', ascending=False).iloc[:300]
for stocks in context.pipeline_results:
cash = context.portfolio.cash
stock_value = context.pipeline_results.Close
if np.isnan(stock_value) == False:
try:
#Order Logic
except:
return
The Error (if np.isnan(stock_value) == False:):
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I get the same error this way:
def handle_data(context, data):
results = pipeline_output('my_pipeline')
context.pipeline_results = results.sort_values('SMARatio', ascending=False).iloc[:300]
for stocks in context.pipeline_results:
cash = context.portfolio.cash
stock_value = context.pipeline_results.Close
stock_value[np.isnan(stock_value)] = 0
if stock_value != 0.0:
purchase_target = (cash/stock_value)
I can convert the NaN values to zeros before any math, but then I have an issue with infinities. So I would prefer to handle NaNs within an IF Statement.