I need help specific to my code that you guys KNOW will work pls.
I need help specific to my code that you guys KNOW will work pls.
You need this:
for stock in stocks:
last_close = context.output.get_value(stock, 'close')
min_close = context.output.get_value (stock, 'min_close')
max_close = context.output.get_value (stock, 'max_close')
number_of_open_positions= len(openorders)
currentcash= context.portfolio.cash
buyprice= context.portfolio.positions[stock].cost_basis
curprice= data.current(stock, 'price')
# for stuff last_close >= max_close
#for stock in stocks:
#percentgain= and currentcash>50000
if data.can_trade(stock):
if (last_close < min_close):
if get_open_orders():
return
else:
order_target_percent(stock, 0.5)
elif (last_close >= max_close or (buyprice!=0 and (buyprice-curprice)/buyprice<=-.05) and leverage<2.95):
if get_open_orders():
return
else:
order_target_percent(stock, 0)
to be all one For Loop. This shouldn't be separate and also you should redo it like this:
open_orders = get_open_orders()
if data.can_trade(stock) & (stock not in open_orders):
#Since parent if-statement will be inside for loop (as stated above)
if (stock not in context.security_list):
if (last_close < min_close):
order_target_percent(stock, 0.5)
elif (last_close >= max_close or (buyprice!=0 and (buyprice-curprice)/buyprice<=-.05) and leverage<2.95):
rder_target_percent(stock, 0)
This should solve some of what you're experiencing
But leverage. You will want to use a set weight like this:
open_orders = get_open_orders()
if (len(securities_long_or_short) > 0):
wgt = 1. / len(securities_long_or_short)
for (stock in securities_long_or_short):
if data.can_trade(stock) & (stock not in open_orders):
order_target_percent(stock, wgt)
thanks for spending the time on a post...
I implemented it, and quantopian freezes, never finishes the pipeline...