I need to sell stocks only once they are above a certain price for longs and below a certain price for shorts. I am trying to implement this code in my algorithm to determine when to sell for each stock in my list of open orders.
This code starts at line 172 and ends at line 181
for position in context.portfolio.positions:
print(stock)
if(context.portfolio.positions[stock].amount > 0):
print(" long " + str(context.portfolio.positions[stock].amount))
if(data.current(stock, 'price') > context.longprices[stock]+context.threshold):
order_target_percent(stock, 0)
if(context.portfolio.positions[stock].amount < 0):
print(" short " + str(context.portfolio.positions[stock].amount))
if(data.current(stock, 'price') < context.shortprices[stock]-context.threshold):
order_target_percent(stock, 0)
context.longprices and context.shortprices are empty Pandas Series I made to store the price when I bought a stock so I can compare the price of when I bought it to it's current price to determine when I should sell
e.x.) If a long position is 0.2 dollars above the price when I bought it, sell
e.x.) If a short position is 0.2 dollars below the price when I bought it, sell
The issue I am experiencing is that it throws an error:
IndexError: index out of bounds
There was a runtime error on line 180.
I don't understand why this is happening, any help would be great,
Thanks!