IB証券 APIで注文内容の変更

スポンサーリンク
スポンサーリンク

前提

このページでは接続後のみのサンプルを記載しています。接続方法は以下を参照してください。
IB証券 APIの接続方法

金融商品

contracts = Forex('USDJPY', exchange='IDEALPRO')で注文、決済する金融商品を設定します。
詳しくは以下を参照してください。
IB証券 API接続での商品情報の設定

注文内容の変更方法

当然だと思いますが、約定する前に変更を実行する必要があり、約定前に実行しても先に約定される可能性もあります。

注文内容の例

本ページでは、以下のように注文した場合の、例を上げます。

米国株のエクスペディア(EXPE)の買いを90ドルの指値で、1枚を発注しています。

contract = Stock('EXPE', 'SMART', 'USD')
ib.qualifyContracts(contract)
limitOrder = LimitOrder('BUY', 1, 90)
limitTrade = ib.placeOrder(contract, limitOrder)
print(limitTrade)
# Trade(contract=Stock(conId=98542021, symbol='EXPE', exchange='SMART', primaryExchange='NASDAQ', currency='USD', localSymbol='EXPE', tradingClass='NMS'), order=LimitOrder(orderId=25, clientId=2002, action='BUY', totalQuantity=1, lmtPrice=90), orderStatus=OrderStatus(orderId=25, status='PendingSubmit', filled=0, remaining=0, avgFillPrice=0.0, permId=0, parentId=0, lastFillPrice=0.0, clientId=0, whyHeld='', mktCapPrice=0.0), fills=[], log=[TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 12, 36, 378587, tzinfo=datetime.timezone.utc), status='PendingSubmit', message='')])

発注数量の変更

発注数量を1から2に変更します。

発注時のib.placeOrder()の返値であるlimitTradeorder.totalQuantityで数量を設定します。

limitTrade.order.totalQuantity = 2
limitTradeUpdate = ib.placeOrder(contract, limitTrade.order)
print(limitTradeUpdate)

# Trade(contract=Stock(conId=98542021, symbol='EXPE', exchange='SMART', primaryExchange='NASDAQ', currency='USD', localSymbol='EXPE', tradingClass='NMS'), order=LimitOrder(orderId=25, clientId=2002, permId=338873485, action='BUY', totalQuantity=2, lmtPrice=90), orderStatus=OrderStatus(orderId=25, status='PreSubmitted', filled=0.0, remaining=1.0, avgFillPrice=0.0, permId=338873485, parentId=0, lastFillPrice=0.0, clientId=2002, whyHeld='', mktCapPrice=0.0), fills=[], log=[TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 12, 36, 378587, tzinfo=datetime.timezone.utc), status='PendingSubmit', message=''), TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 12, 36, 739895, tzinfo=datetime.timezone.utc), status='PreSubmitted', message=''), TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 13, 4, 204721, tzinfo=datetime.timezone.utc), status='PreSubmitted', message='Modify')])

指値の価格を変更

指値を90ドルから85ドルに変更します。

発注時のib.placeOrder()の返値であるlimitTradeorder.lmtPriceで指値の価格を設定します。

limitTrade.order.lmtPrice = 85
limitTradeUpdate = ib.placeOrder(contract, limitTrade.order)
print(limitTradeUpdate)
# Trade(contract=Stock(conId=98542021, symbol='EXPE', exchange='SMART', primaryExchange='NASDAQ', currency='USD', localSymbol='EXPE', tradingClass='NMS'), order=LimitOrder(orderId=25, clientId=2002, permId=338873485, action='BUY', totalQuantity=2, lmtPrice=85), orderStatus=OrderStatus(orderId=25, status='PreSubmitted', filled=0.0, remaining=2.0, avgFillPrice=0.0, permId=338873485, parentId=0, lastFillPrice=0.0, clientId=2002, whyHeld='', mktCapPrice=0.0), fills=[], log=[TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 12, 36, 378587, tzinfo=datetime.timezone.utc), status='PendingSubmit', message=''), TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 12, 36, 739895, tzinfo=datetime.timezone.utc), status='PreSubmitted', message=''), TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 13, 4, 204721, tzinfo=datetime.timezone.utc), status='PreSubmitted', message='Modify'), TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 13, 4, 655854, tzinfo=datetime.timezone.utc), status='PreSubmitted', message=''), TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 13, 17, 749828, tzinfo=datetime.timezone.utc), status='PreSubmitted', message='Modify')])

指値を成行に変更

指値から成行に変更します。

発注時のib.placeOrder()の返値であるlimitTradeorder.orderTypeで成行を設定します。

limitTrade.order.orderType = "MKT"
limitTradeUpdate = ib.placeOrder(contract, limitTrade.order)
print(limitTradeUpdate)
# Trade(contract=Stock(conId=98542021, symbol='EXPE', exchange='SMART', primaryExchange='NASDAQ', currency='USD', localSymbol='EXPE', tradingClass='NMS'), order=LimitOrder(orderId=25, clientId=2002, permId=338873485, action='BUY', totalQuantity=2, lmtPrice=85), orderStatus=OrderStatus(orderId=25, status='PreSubmitted', filled=0.0, remaining=2.0, avgFillPrice=0.0, permId=338873485, parentId=0, lastFillPrice=0.0, clientId=2002, whyHeld='', mktCapPrice=0.0), fills=[], log=[TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 12, 36, 378587, tzinfo=datetime.timezone.utc), status='PendingSubmit', message=''), TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 12, 36, 739895, tzinfo=datetime.timezone.utc), status='PreSubmitted', message=''), TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 13, 4, 204721, tzinfo=datetime.timezone.utc), status='PreSubmitted', message='Modify'), TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 13, 4, 655854, tzinfo=datetime.timezone.utc), status='PreSubmitted', message=''), TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 13, 17, 749828, tzinfo=datetime.timezone.utc), status='PreSubmitted', message='Modify'), TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 13, 33, 806209, tzinfo=datetime.timezone.utc), status='PreSubmitted', message='Modify')])

成行から指値に変更

成行から指値に変更することはできません。エラーになります。

limitTrade.order.orderType = "LMT"
limitTrade.order.lmtPrice = 85
limitTradeUpdate = ib.placeOrder(contract, limitTrade.order)
print(limitTradeUpdate)
# Error 329, reqId 27: 注文変更に失敗しました。新しい注文方法に変更できません。:LMT
# Canceled order: Trade(contract=Stock(conId=98542021, symbol='EXPE', exchange='SMART', primaryExchange='NASDAQ', currency='USD', localSymbol='EXPE', tradingClass='NMS'), order=LimitOrder(orderId=27, clientId=2002, permId=338873486, action='BUY', totalQuantity=1, lmtPrice=85), orderStatus=OrderStatus(orderId=27, status='Cancelled', filled=0.0, remaining=1.0, avgFillPrice=0.0, permId=338873486, parentId=0, lastFillPrice=0.0, clientId=2002, whyHeld='', mktCapPrice=0.0), fills=[], log=[TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 26, 24, 90813, tzinfo=datetime.timezone.utc), status='PendingSubmit', message=''), TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 26, 24, 210956, tzinfo=datetime.timezone.utc), status='PreSubmitted', message=''), TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 26, 37, 604946, tzinfo=datetime.timezone.utc), status='PreSubmitted', message='Modify'), TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 26, 43, 521713, tzinfo=datetime.timezone.utc), status='PreSubmitted', message='Modify'), TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 26, 43, 541653, tzinfo=datetime.timezone.utc), status='Cancelled', message='Error 329, reqId 27: 注文変更に失敗しました。新しい注文方法に変更できません。:LMT')])

買いから売りに変更

買いから売りに変更することはできません。エラーになります。

limitTrade.order.orderType = "SELL"
limitTradeUpdate = ib.placeOrder(contract, limitTrade.order)
print(limitTradeUpdate)
# Error 321, reqId 25: リクエスト検証のエラー:-'bO': 原因- Invalid order type was entered
# Canceled order: Trade(contract=Stock(conId=98542021, symbol='EXPE', exchange='SMART', primaryExchange='NASDAQ', currency='USD', localSymbol='EXPE', tradingClass='NMS'), order=LimitOrder(orderId=25, clientId=2002, permId=338873485, action='BUY', totalQuantity=2, lmtPrice=85), orderStatus=OrderStatus(orderId=25, status='Cancelled', filled=0.0, remaining=2.0, avgFillPrice=0.0, permId=338873485, parentId=0, lastFillPrice=0.0, clientId=2002, whyHeld='', mktCapPrice=0.0), fills=[], log=[TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 12, 36, 378587, tzinfo=datetime.timezone.utc), status='PendingSubmit', message=''), TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 12, 36, 739895, tzinfo=datetime.timezone.utc), status='PreSubmitted', message=''), TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 13, 4, 204721, tzinfo=datetime.timezone.utc), status='PreSubmitted', message='Modify'), TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 13, 4, 655854, tzinfo=datetime.timezone.utc), status='PreSubmitted', message=''), TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 13, 17, 749828, tzinfo=datetime.timezone.utc), status='PreSubmitted', message='Modify'), TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 13, 33, 806209, tzinfo=datetime.timezone.utc), status='PreSubmitted', message='Modify'), TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 14, 44, 957761, tzinfo=datetime.timezone.utc), status='PreSubmitted', message='Modify'), TradeLogEntry(time=datetime.datetime(2020, 9, 13, 4, 14, 44, 987986, tzinfo=datetime.timezone.utc), status='Cancelled', message="Error 321, reqId 25: リクエスト検証のエラー:-'bO': 原因- Invalid order type was entered")])

 

コメント

タイトルとURLをコピーしました