I am getting the following error when I try to launch my algorithm live, which did not occur when backtesting.
TypeError: unsupported operand type(s) for -: 'Timestamp' and 'NoneType'
...
USER ALGORITHM:113, in before_trading_start
time_owned_raw = now - bought
Further, the code causing the issue is in 'before trading start', and I have only tried to start the code in the middle of the day, while I already own securities. My guess is that the code is identifying the shares from my broker and adding to my owned stocks list, but not to the degree where all the details are there (such as cost basis, or last sale date, etc). Continuing with this assumption, I'd have to guess that the formula is processing with empty information... because technically, the code throwing the error should only be executed on owned securities.
for stock in context.owned:
avg_price = context.portfolio.positions[stock].cost_basis
shares_qty = context.portfolio.positions[stock].amount
total_cost = avg_price * shares_qty
#print context.portfolio.positions[stock]
curr_price = data.current(stock, "price")
curr_value = curr_price * shares_qty
#print "Total cost %s %s" % (stock, total_cost)
#print "Current Value %s %s" % (stock, curr_value)
##
now = get_datetime()
bought = context.portfolio.positions[stock].last_sale_date
#next line is where the issue was.
time_owned_raw = now - bought
days_owned = time_owned_raw.days + 1
context.do[stock] = days_owned