<jupyter_start><jupyter_text>Ordering Warning: This notebook will place live ordersUse a paper trading account (during market hours).<jupyter_code>from ib_insync import *
util.startLoop()

ib = IB()
ib.connect('127.0.0.1', 7497, clientId=13)<jupyter_output><empty_output><jupyter_text>Create a contract and a market order:<jupyter_code>contract = Stock('AAPL', 'SMART', 'USD')
ib.qualifyContracts(contract)

order = MarketOrder('BUY', 100)<jupyter_output><empty_output><jupyter_text>placeOrder will place the order order and return a ``Trade`` object right away (non-blocking):<jupyter_code>trade = ib.placeOrder(contract, order)

trade<jupyter_output><empty_output><jupyter_text>``trade`` contains the order and everything related to it, such as order status, fills and a log.It will be live updated with every status change or fill of the order.<jupyter_code>ib.sleep(1)
trade.log<jupyter_output><empty_output><jupyter_text>``trade`` will also available from ``ib.trades()``:<jupyter_code>assert trade in ib.trades()<jupyter_output><empty_output><jupyter_text>Likewise for ``order``:<jupyter_code>assert order in ib.orders()<jupyter_output><empty_output><jupyter_text>Now let's create a limit order with an unrealistic limit:<jupyter_code>limitOrder = LimitOrder('BUY', 100, 0.05)
limitTrade = ib.placeOrder(contract, limitOrder)

limitTrade<jupyter_output><empty_output><jupyter_text>``status`` will change from "PendingSubmit" to "Submitted":<jupyter_code>ib.sleep(1)
assert limitTrade.orderStatus.status == 'Submitted'
assert limitTrade in ib.openTrades()<jupyter_output><empty_output><jupyter_text>Let's modify the limit price and resubmit:<jupyter_code>limitOrder.lmtPrice = 0.10

ib.placeOrder(contract, limitOrder)<jupyter_output><empty_output><jupyter_text>And now cancel it:<jupyter_code>ib.cancelOrder(limitOrder)
limitTrade.log<jupyter_output><empty_output><jupyter_text>placeOrder is not blocking and will not wait on what happens with the order.To make the order placement blocking, that is to wait until the order is eitherfilled or canceled, consider the following:<jupyter_code>%%time
order = MarketOrder('BUY', 100)

trade = ib.placeOrder(contract, order)
while not trade.isDone():
    ib.waitOnUpdate()<jupyter_output>CPU times: user 6.28 ms, sys: 783 µs, total: 7.06 ms
Wall time: 147 ms<jupyter_text>What are our positions?<jupyter_code>ib.positions()<jupyter_output><empty_output><jupyter_text>What's the total of commissions paid today?<jupyter_code>sum(fill.commissionReport.commission for fill in ib.fills())<jupyter_output><empty_output><jupyter_text>whatIfOrder can be used to see the commission and the margin impact of an order without actually sending the order:<jupyter_code>order = MarketOrder('SELL', 200)
ib.whatIfOrder(contract, order)
ib.disconnect()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11075612353567625, 'fraction_numerical': 0.01561945331913383, 'mean_word_length': 8.683848797250858, 'pattern_counts': {'":': 1, '<': 56, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '102050', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 1004, 'n_tokens_neox': 945, 'n_words': 230, 'path': 'notebooks/ordering.ipynb', 'repo_name': 'mikethoun/ib_insync', 'size': '2817'}