Re: Sell orders executing as soon as I buy
You need to add lines for?
triggerPrice and triggerMethod for stop order. I think that is unset.?
And check the prices again.
Last resort, place an order manually from IBKR TWS and then check the order object from API to know what exactly is needed in the order that you send.
That works
Pratik Babhulkar
toggle quoted message
Show quoted text
Hi all,
I am running a strategy, but my sell orders are being executed as soon as
the buy orders are placed.
I am trying to use trailing stop orders, but suspect am making an error
somewhere.
I did read the thread at
/g/twsapi/topic/trail_limit_orders/34461744 but it did not
help. Yes, the problem was different, but I was hoping I would get some
ideas.
Here is the order placing code.
while not output_queue.empty():
? ? ? ? ? ? ? ? strategy_outputs.append(output_queue.get())
? ? ? ? for output in strategy_outputs:
? ? ? ? ? ? ? ? contract = output["contract"]
? ? ? ? ? ? ? ? trigger_price = round_to_multiple(output["stop_loss_price"],
broker_multiple, "nearest")
? ? ? ? ? ? ? ? price_upper_limit = 800? # Avoid buying too expensive stocks
? ? ? ? ? ? ? ? order_quantity = 5
? ? ? ? ? ? ? ? ltp = output["last_closing_price"]
? ? ? ? ? ? ? ? current_cash = getCash("U8034364")
? ? ? ? ? ? ? ? amount_required_for_transaction = order_quantity * ltp
? ? ? ? ? ? ? ? if output["current_signal"] == 1 and contract not in
[i.contract for i in ib.openTrades()] and contract not in [i.contract for i
in ib.positions()]:
? ? ? ? ? ? ? ? ? ? ? ? if current_cash > amount_required_for_transaction
and ltp < price_upper_limit:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # We have the money, so buy the stock
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? parent_order = LimitOrder("BUY",
order_quantity, ltp, outsideRth=True, transmit=False)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? parent_order.orderId = ib.client.getReqId()
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? stop_loss_order = StopOrder("SELL",
order_quantity, trigger_price, parentId=parent_order.orderId, transmit=True)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? stop_loss_order.tif = "GTC"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? stop_loss_order.orderId =
ib.client.getReqId()
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? stop_loss_order.adjustedOrderType = "TRAIL"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? trail_amount = 0.08
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? stop_loss_order.adjustedTrailingAmount =
trail_amount
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? stop_loss_order.adjustedStopPrice =
trigger_price
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? parent_trade = ib.placeOrder(contract,
parent_order)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? stop_trade = ib.placeOrder(contract,
stop_loss_order)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print("bought ",contract.symbol)
? ? ? ? ? ? ? ? elif output["current_signal"] == -1 and contract in
[i.contract for i in ib.positions()]:
? ? ? ? ? ? ? ? ? ? ? ? # Time to sell
? ? ? ? ? ? ? ? ? ? ? ? positions = ib.positions()
? ? ? ? ? ? ? ? ? ? ? ? for p in positions:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if p.contract == contract:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? order_quantity = p.position
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? order_price =
output["last_closing_price"]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? parent_order = LimitOrder("SELL",
order_quantity, order_price, outsideRth=True)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? parent_trade =
ib.placeOrder(contract, parent_order)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print("sold ",contract.symbol)
? ? ? ? ? ? ? ? ib.sleep(5)? # Cater to IB data downloading restrictions
? ? ? ? ib.disconnect()
? ? ? ? with
open('reports/signal_file_daily_kalman_crossover_long_only_runner.csv', 'w',
newline='') as f:
? ? ? ? ? ? ? ? writer = csv.writer(f)
? ? ? ? ? ? ? ? if strategy_outputs:
? ? ? ? ? ? ? ? ? ? ? ? writer.writerow(strategy_outputs[0].keys())
? ? ? ? ? ? ? ? ? ? ? ? for dictionary in strategy_outputs:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? writer.writerow(dictionary.values())
? ? ? ? print("Kalman crossover run complete")
Pranav
Are you sure you wish to delete this message from the message archives of
[email protected] ?
This cannot be undone.
Are you sure you wish to repost this message?