I followed the syntax on the docs for the stop-order but its still not working, don't know if its because how implemented it or my syntax?
def initialize(context):
context.stock = sid(35260)
# Algorithm will call myfunc once per week, on the second day of the week,
# 1 hours and 5 minutes after the market opens
schedule_function(
trade,
date_rules.week_start(),
time_rules.market_open(hours=1, minutes=5)
)
#Set execution cost assumptions. For live trading with Interactive Brokers we #will assume a $1.00 minimum
# per trade fee, with a per share cost of $0.0075.
#set_commission(commission.PerShare(cost=0.0075, min_trade_cost=1.00))
#Set market impact assumptions. We will limit our simulation to allow us to be #up to 2.5% of
# the traded volume for any one minute, and that our price impact constant is #0.1.
set_slippage(slippage.VolumeShareSlippage(volume_limit=0.025, price_impact=0.10))
set_max_order_size(sid(35260), max_notional=3000.0)
# Will be called on every trade event for the securities you specify.
def handle_data(context, data):
# Implement your algorithm logic here.
pass
# data[sid(X)] holds the trade event data for that security.
# context.portfolio holds the current portfolio state.
# TODO: implement your own logic here.
def trade(context, data):
ma_long = data[context.stock].mavg(9)
ma_short = data[context.stock].mavg(4)
current_price = data[context.stock].price
current_positions = context.portfolio.positions[sid(35260)].amount
cash = context.portfolio.cash
if (ma_long > (ma_short*1.02)) and ma_long > ma_short and current_positions == 0:
order_target_percent(context.stock,.50, style=StopOrder(stop_price = current_price*0.95))
#print get_order(order_id)
log.info("Buying shares at price %s" %(current_price))
elif (ma_short > ma_long) and (current_price > context.portfolio.positions[sid(35260)].cost_basis) and current_positions != 0:
order_target_percent(context.stock, 0)
log.info("Selling shares at price %s" %(current_price))
record(MAShort = ma_short, MALong = ma_long, Price = current_price)