Hello,
I have a code to get out of the market (sell it from a long position once it reaches a certain return, say 10%).
for sid in context.portfolio.positions.items():
costBasis = context.portfolio.positions[sid].cost_basis
marketPrice = context.portfolio.positions[sid].last_sale_price
if (float(marketPrice) / max(float(costBasis),0.01)) > 1.1:
sell(context, sid)
of course ...
def sell(context, sid):
if sid not in get_open_orders():
order_target_percent(sid, 0)
However , it is not working. I feel like there is something going on with "sid" and the way "context.portfolio.positions[sid].cost_basis" is supposed to work. Than you very much for your help.