IB証券 API接続で商品情報の証拠金や手数料などの詳細情報を取得

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

前提

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

取得できる商品情報

商品情報は購読しているマーケット・データによって取得できないものがあります。

IB証券のマーケットデータの購読は以下を参照してください。
IB証券 マーケットデータの購読

商品情報の取得

単一の商品情報を取得(補完)

ib.qualifyContracts(contract)

指定した商品情報に、足りない情報を補足します。特に重要なのがconIdになります。

contract = Contract(
    secType  = 'FUT',
    symbol   = 'ES',
    exchange = 'GLOBEX',
    lastTradeDateOrContractMonth = '20200918',
    tradingClass = 'ES',
)
print(contract)
# Contract(secType='FOP', symbol='ES', lastTradeDateOrContractMonth='20200918', strike='3300', right='P', exchange='GLOBEX', tradingClass='ES')

ib.qualifyContracts(contract)

print(contract)
# Contract(secType='FOP', conId=384750208, symbol='ES', lastTradeDateOrContractMonth='20200918', strike=3300.0, right='P', multiplier='50', exchange='GLOBEX', currency='USD', localSymbol='ESU0 P3300', tradingClass='ES')

条件に一致する複数の商品の詳細情報を取得

ib.reqContractDetails(contract)

指定した商品情報の詳細情報をリスト型で取得することができます。

contract = Contract(
    secType  = 'FUT',
    symbol   = 'ES',
    exchange = 'GLOBEX',
    lastTradeDateOrContractMonth = '20200918',
    tradingClass = 'ES',
)

details = ib.reqContractDetails(contract)

print(details)
# [ContractDetails(contract=Contract(secType='FUT', conId=371749798, symbol='ES', lastTradeDateOrContractMonth='20200918', multiplier='50', exchange='GLOBEX', currency='USD', localSymbol='ESU0', tradingClass='ES'), marketName='ES', minTick=0.25, orderTypes='ACTIVETIM,AD,ADJUST,ALERT,ALGO,ALLOC,AVGCOST,BASKET,BENCHPX,COND,CONDORDER,DAY,DEACT,DEACTDIS,DEACTEOD,GAT,GTC,GTD,GTT,HID,ICE,IOC,LIT,LMT,LTH,MIT,MKT,MTL,NGCOMB,NONALGO,OCA,PEGBENCH,SCALE,SCALERST,SNAPMID,SNAPMKT,SNAPREL,STP,STPLMT,TRAIL,TRAILLIT,TRAILLMT,TRAILMIT,WHATIF', validExchanges='GLOBEX,QBALGO', priceMagnifier=1, underConId=11004968, longName='E-mini S&P 500', contractMonth='202009', timeZoneId='CST (中部標準時)', tradingHours='20200827:1700-20200828:1515;20200828:1530-20200828:1600;20200829:CLOSED;20200830:1700-20200831:1515;20200831:1530-20200831:1600;20200831:1700-20200901:1515;20200901:1530-20200901:1600;20200901:1700-20200902:1515;20200902:1530-20200902:1600;20200902:1700-20200903:1515;20200903:1530-20200903:1600', liquidHours='20200828:0830-20200828:1515;20200828:1530-20200828:1600;20200829:CLOSED;20200830:CLOSED;20200831:0830-20200831:1515;20200831:1530-20200831:1600;20200901:0830-20200901:1515;20200901:1530-20200901:1600;20200902:0830-20200902:1515;20200902:1530-20200902:1600;20200902:1700-20200903:1515;20200903:1530-20200903:1600', mdSizeMultiplier=1, aggGroup=2147483647, underSymbol='ES', underSecType='IND', marketRuleIds='67,67', realExpirationDate='20200918', lastTradeTime='08:30')]

print(details[0].contract)
# Contract(secType='FUT', conId=371749798, symbol='ES', lastTradeDateOrContractMonth='20200918', multiplier='50', exchange='GLOBEX', currency='USD', localSymbol='ESU0', tradingClass='ES')

detailsの内容を見やすいようにしました。
contract以外に可能な注文種別や取引時間などを取得することができます。

[
    ContractDetails(
        contract=Contract(
            secType='FUT', conId=371749798, symbol='ES', lastTradeDateOrContractMonth='20200918', multiplier='50', exchange='GLOBEX', currency='USD', localSymbol='ESU0', tradingClass='ES'
        ), 
        marketName='ES', 
        minTick=0.25, 
        orderTypes='ACTIVETIM,AD,ADJUST,ALERT,ALGO,ALLOC,AVGCOST,BASKET,BENCHPX,COND,CONDORDER,DAY,DEACT,DEACTDIS,DEACTEOD,GAT,GTC,GTD,GTT,HID,ICE,IOC,LIT,LMT,LTH,MIT,MKT,MTL,NGCOMB,NONALGO,OCA,PEGBENCH,SCALE,SCALERST,SNAPMID,SNAPMKT,SNAPREL,STP,STPLMT,TRAIL,TRAILLIT,TRAILLMT,TRAILMIT,WHATIF', 
        validExchanges='GLOBEX,QBALGO', 
        priceMagnifier=1, 
        underConId=11004968, 
        longName='E-mini S&P 500', 
        contractMonth='202009', 
        timeZoneId='CST (中部標準時)', 
        tradingHours='20200827:1700-20200828:1515;20200828:1530-20200828:1600;20200829:CLOSED;20200830:1700-20200831:1515;20200831:1530-20200831:1600;20200831:1700-20200901:1515;20200901:1530-20200901:1600;20200901:1700-20200902:1515;20200902:1530-20200902:1600;20200902:1700-20200903:1515;20200903:1530-20200903:1600', 
        liquidHours='20200828:0830-20200828:1515;20200828:1530-20200828:1600;20200829:CLOSED;20200830:CLOSED;20200831:0830-20200831:1515;20200831:1530-20200831:1600;20200901:0830-20200901:1515;20200901:1530-20200901:1600;20200902:0830-20200902:1515;20200902:1530-20200902:1600;20200902:1700-20200903:1515;20200903:1530-20200903:1600', 
        mdSizeMultiplier=1, 
        aggGroup=2147483647, 
        underSymbol='ES', 
        underSecType='IND', 
        marketRuleIds='67,67', 
        realExpirationDate='20200918', 
        lastTradeTime='08:30'
    )
]

たとえば、以下のように先物の限月を指定せずに実行した場合は、限月以外の条件にあった商品情報を取得することができます。これで複数の限月の商品情報を得ることができます。

contract = Contract(
    secType  = 'FUT',
    symbol   = 'ES',
    exchange = 'GLOBEX',
#     lastTradeDateOrContractMonth = '20200918',
    tradingClass = 'ES',
)
contracts = [cd.contract for cd in ib.reqContractDetails(contract)]

for contract in contracts:
    print(contract)
    
# Contract(secType='FUT', conId=371749798, symbol='ES', lastTradeDateOrContractMonth='20200918', multiplier='50', exchange='GLOBEX', currency='USD', localSymbol='ESU0', tradingClass='ES')
# Contract(secType='FUT', conId=383974339, symbol='ES', lastTradeDateOrContractMonth='20201218', multiplier='50', exchange='GLOBEX', currency='USD', localSymbol='ESZ0', tradingClass='ES')
# Contract(secType='FUT', conId=396336017, symbol='ES', lastTradeDateOrContractMonth='20210319', multiplier='50', exchange='GLOBEX', currency='USD', localSymbol='ESH1', tradingClass='ES')
# Contract(secType='FUT', conId=412889032, symbol='ES', lastTradeDateOrContractMonth='20210617', multiplier='50', exchange='GLOBEX', currency='USD', localSymbol='ESM1', tradingClass='ES')
# Contract(secType='FUT', conId=428520022, symbol='ES', lastTradeDateOrContractMonth='20210916', multiplier='50', exchange='GLOBEX', currency='USD', localSymbol='ESU1', tradingClass='ES')

限月が終了した先物の情報を取得する場合

以下のように商品情報にincludeExpired=Trueを追加することで、過去の限月を取得することが可能になります。

ただし、これで先物のデータは取得できますが、オプション、先物オプション、ワラント、ストラクチャード商品は取得できません。

contract = Contract(
    secType  = 'FUT',
    symbol   = 'ES',
    exchange = 'GLOBEX',
#     lastTradeDateOrContractMonth = '20200918',
    tradingClass = 'ES',
    includeExpired=True
)
contracts = [cd.contract for cd in ib.reqContractDetails(contract)]

for contract in contracts:
    print(contract)
    
# Contract(secType='FUT', conId=371749798, symbol='ES', lastTradeDateOrContractMonth='20200918', multiplier='50', exchange='GLOBEX', currency='USD', localSymbol='ESU0', tradingClass='ES')
# Contract(secType='FUT', conId=383974339, symbol='ES', lastTradeDateOrContractMonth='20201218', multiplier='50', exchange='GLOBEX', currency='USD', localSymbol='ESZ0', tradingClass='ES')
# Contract(secType='FUT', conId=396336017, symbol='ES', lastTradeDateOrContractMonth='20210319', multiplier='50', exchange='GLOBEX', currency='USD', localSymbol='ESH1', tradingClass='ES')
# Contract(secType='FUT', conId=412889032, symbol='ES', lastTradeDateOrContractMonth='20210617', multiplier='50', exchange='GLOBEX', currency='USD', localSymbol='ESM1', tradingClass='ES')
# Contract(secType='FUT', conId=428520022, symbol='ES', lastTradeDateOrContractMonth='20210916', multiplier='50', exchange='GLOBEX', currency='USD', localSymbol='ESU1', tradingClass='ES')
# Contract(secType='FUT', conId=279396694, symbol='ES', lastTradeDateOrContractMonth='20180921', multiplier='50', exchange='GLOBEX', currency='USD', localSymbol='ESU8', tradingClass='ES')
# Contract(secType='FUT', conId=289128563, symbol='ES', lastTradeDateOrContractMonth='20181221', multiplier='50', exchange='GLOBEX', currency='USD', localSymbol='ESZ8', tradingClass='ES')
# Contract(secType='FUT', conId=299552802, symbol='ES', lastTradeDateOrContractMonth='20190315', multiplier='50', exchange='GLOBEX', currency='USD', localSymbol='ESH9', tradingClass='ES')
# Contract(secType='FUT', conId=310629209, symbol='ES', lastTradeDateOrContractMonth='20190621', multiplier='50', exchange='GLOBEX', currency='USD', localSymbol='ESM9', tradingClass='ES')
# Contract(secType='FUT', conId=321454763, symbol='ES', lastTradeDateOrContractMonth='20190920', multiplier='50', exchange='GLOBEX', currency='USD', localSymbol='ESU9', tradingClass='ES')
# Contract(secType='FUT', conId=334144679, symbol='ES', lastTradeDateOrContractMonth='20191220', multiplier='50', exchange='GLOBEX', currency='USD', localSymbol='ESZ9', tradingClass='ES')
# Contract(secType='FUT', conId=346577697, symbol='ES', lastTradeDateOrContractMonth='20200320', multiplier='50', exchange='GLOBEX', currency='USD', localSymbol='ESH0', tradingClass='ES')
# Contract(secType='FUT', conId=357283364, symbol='ES', lastTradeDateOrContractMonth='20200619', multiplier='50', exchange='GLOBEX', currency='USD', localSymbol='ESM0', tradingClass='ES')

オプションチェーンを取得

ib.reqSecDefOptParams(contract.symbol, contract.exchange, contract.secType, contract.conId)

オプションチェーンとは限月別の権利行使価格の一覧の画面を表しますが、その元となるデータを取得することができます。

データはTWSの画面と同じく、取引所別、トレーディングクラス別、限月別に権利行使価格の一覧を取得できます。

chains = ib.reqSecDefOptParams(contract.symbol, contract.exchange, contract.secType, contract.conId)

print(util.df(chains))
#   exchange underlyingConId tradingClass multiplier expirations                                            strikes
# 0   GLOBEX       371749798          E1A         50  [20200908]  [1700.0, 1800.0, 1900.0, 2000.0, 2050.0, 2100....
# 1   GLOBEX       371749798          E3C         50  [20200916]  [1700.0, 1800.0, 1900.0, 2000.0, 2050.0, 2100....
# 2   GLOBEX       371749798          E2A         50  [20200914]  [1700.0, 1800.0, 1900.0, 2000.0, 2050.0, 2100....
# 3   GLOBEX       371749798          E1C         50  [20200902]  [1700.0, 1800.0, 1900.0, 2000.0, 2050.0, 2100....
# 4   GLOBEX       371749798          E2C         50  [20200909]  [1700.0, 1800.0, 1900.0, 2000.0, 2050.0, 2100....
# 5   GLOBEX       371749798          EW2         50  [20200911]  [1000.0, 1050.0, 1100.0, 1150.0, 1200.0, 1250....
# 6   GLOBEX       371749798           EW         50  [20200831]  [100.0, 200.0, 300.0, 400.0, 500.0, 600.0, 700...
# 7   GLOBEX       371749798          EW1         50  [20200904]  [1000.0, 1050.0, 1100.0, 1150.0, 1200.0, 1250....
# 8   GLOBEX       371749798           ES         50  [20200918]  [100.0, 200.0, 300.0, 400.0, 500.0, 600.0, 700...

注文する商品にかかる証拠金や手数料などを取得

ib.whatIfOrder(contract, order)

商品情報と注文内容から証拠金や手数料などを取得することができます。これによりAPIからの注文前に証拠金や手数料をシステムで確認することが可能になります。

取得できる情報は以下の通りです。

  • commission:手数料と手数料の通貨
  • equity With Loan:ローン付き株式と、アカウントの取引前の委託証拠金と取引後の委託証拠金
  • Initial Margin:委託証拠金と、アカウントの取引前の委託証拠金と取引後の委託証拠金
  • Maintenance Margin:維持証拠金と、アカウントの取引前の維持証拠金と取引後の維持証拠金
contract = Contract(
    secType  = 'FUT',
    symbol   = 'ES',
    exchange = 'GLOBEX',
    lastTradeDateOrContractMonth = '20200918',
    tradingClass = 'ES'
)

ib.qualifyContracts(contract)

order = MarketOrder('BUY', 1)

orderstate = ib.whatIfOrder(contract, order)

print(orderstate)
# OrderState(status='PreSubmitted', initMarginBefore='13058.7', maintMarginBefore='11877.67', equityWithLoanBefore='721618.62', initMarginChange='9743.71', maintMarginChange='8857.92', equityWithLoanChange='-1.6999999999534339', initMarginAfter='22802.41', maintMarginAfter='20735.59', equityWithLoanAfter='721616.92', commission=1.7, commissionCurrency='USD')

orderstates = {
    "initMarginBefore" : orderstate.initMarginBefore,
    "maintMarginBefore" : orderstate.maintMarginBefore,
    "equityWithLoanBefore" : orderstate.equityWithLoanBefore,
    "initMarginChange" : orderstate.initMarginChange,
    "maintMarginChange" : orderstate.maintMarginChange,
    "equityWithLoanChange" : orderstate.equityWithLoanChange,
    "initMarginAfter" : orderstate.initMarginAfter,
    "maintMarginAfter" : orderstate.maintMarginAfter,
    "equityWithLoanAfter" : orderstate.equityWithLoanAfter,
    "commission" : orderstate.commission,
    "commissionCurrency" : orderstate.commissionCurrency
}
pprint.pprint(orderstates)
# {'commission': 1.7,
#  'commissionCurrency': 'USD',
#  'equityWithLoanAfter': '721616.92',
#  'equityWithLoanBefore': '721618.62',
#  'equityWithLoanChange': '-1.6999999999534339',
#  'initMarginAfter': '22802.41',
#  'initMarginBefore': '13058.7',
#  'initMarginChange': '9743.71',
#  'maintMarginAfter': '20735.59',
#  'maintMarginBefore': '11877.67',
#  'maintMarginChange': '8857.92'}

コンボ注文

コンボ注文でも同じように取得することができます。

symbol       = 'ES'
exchange     = 'GLOBEX'
tradingClass = 'ES'
expiration   = '20200918'
strike       = ['3270', '3620']

contracts = [
    Contract(
        secType  = 'FOP',
        symbol   = symbol,
        exchange = exchange,
        lastTradeDateOrContractMonth = expiration,
        right    = "P",
        strike   = strike[0],
        tradingClass = tradingClass
    ),
    Contract(
        secType  = 'FOP',
        symbol   = symbol,
        exchange = exchange,
        lastTradeDateOrContractMonth = expiration,
        right    = "C",
        strike   = strike[1],
        tradingClass = tradingClass
    ),
]

conid = []
for contract in contracts:
    ib.qualifyContracts(contract)
    conid.append(contract.conId)
    
contract = Contract(
    symbol=symbol, secType='BAG', exchange=exchange, currency='USD', 
    comboLegs=[
        ComboLeg(conId=conid[0],ratio=1, action='SELL', exchange=exchange),
        ComboLeg(conId=conid[1],ratio=1, action='SELL', exchange=exchange)
    ]
)
order = MarketOrder('BUY', 1)

orderstate = ib.whatIfOrder(contract, order)

orderstates = {
    "initMarginBefore" : orderstate.initMarginBefore,
    "maintMarginBefore" : orderstate.maintMarginBefore,
    "equityWithLoanBefore" : orderstate.equityWithLoanBefore,
    "initMarginChange" : orderstate.initMarginChange,
    "maintMarginChange" : orderstate.maintMarginChange,
    "equityWithLoanChange" : orderstate.equityWithLoanChange,
    "initMarginAfter" : orderstate.initMarginAfter,
    "maintMarginAfter" : orderstate.maintMarginAfter,
    "equityWithLoanAfter" : orderstate.equityWithLoanAfter,
    "commission" : orderstate.commission,
    "commissionCurrency" : orderstate.commissionCurrency
}
pprint.pprint(orderstates)
# {'commission': 2.04,
#  'commissionCurrency': 'USD',
#  'equityWithLoanAfter': '721616.58',
#  'equityWithLoanBefore': '721618.62',
#  'equityWithLoanChange': '-2.040000000037253',
#  'initMarginAfter': '25461.57',
#  'initMarginBefore': '13058.7',
#  'initMarginChange': '12402.869999999999',
#  'maintMarginAfter': '23153.01',
#  'maintMarginBefore': '11877.67',
#  'maintMarginChange': '11275.339999999998'}
# {'commission': 2.04,
#  'commissionCurrency': 'USD',
#  'equityWithLoanAfter': '724346.1',
#  'equityWithLoanBefore': '724391.32',
#  'equityWithLoanChange': '-45.21999999997206',
#  'initMarginAfter': '149071.54',
#  'initMarginBefore': '136665.34',
#  'initMarginChange': '12406.200000000012',
#  'maintMarginAfter': '135525.24',
#  'maintMarginBefore': '124246.88',
#  'maintMarginChange': '11278.359999999986'}

IFO注文(ブラケット注文)Bracket Orders

IFO注文ではエラーになるようです。考えると、IFO注文は新規注文と決済注文になりますから、新規の情報を取得する場合は、一旦、MarketOrderで確認すればいいですね。

contract = Future(symbol='ES', lastTradeDateOrContractMonth='202009', exchange='GLOBEX', currency='USD')
limitPrice = 3220
takeProfitPrice = limitPrice + 5
stopLossPrice = limitPrice - 5
bracket = ib.bracketOrder(
    action = 'BUY', 
    quantity = 1.0, 
    limitPrice = limitPrice, 
    takeProfitPrice = takeProfitPrice, 
    stopLossPrice = stopLossPrice
)

orderstate = ib.whatIfOrder(contract, bracket)
# AttributeError: 'BracketOrder' object has no attribute 'dict'

 

コメント

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