Hey!
I am having some trouble.
My goal is to purchase a stock, and as soon as I do that, to place a limit order for it at 2% above it's current price.
The below code doesn't seem to work.
My thought is that when I place the buy order, it isn't completing by the time I place the sell order, however, I'm not sure how to handle that or wait for that event.
Thanks for your help!
# This is what interactive brokers charges us
set_commission(commission.PerShare(cost=0.0075, min_trade_cost=1.00))
# Put any initialization logic here. The context object will be passed to
# the other methods in your algorithm.
def initialize(context):
# I want this to run every trading day, a couple minutes after we start trading
schedule_function(
func=myDailyTradingFunction,
date_rule=date_rules.every_day(),
time_rule=time_rules.market_open(minutes=60),
half_days=False
)
def myDailyTradingFunction(context,data):
# These are the stocks I want to trade between
oilUp = symbol('UWTI')
oilDown = symbol('DWTI')
# Figure out which stock to buy
stockToBuy = oilDown
# Place the Buy order
buyOrderId = order_percent(stockToBuy, 1)
# Place the Sell order
limitPrice = data[stockToBuy].price*1.02
stopPrice = data[stockToBuy].price*0.99
order_percent(stockToBuy, -1, style=StopLimitOrder(limit_price=limitPrice,stop_price=stopPrice))
return True