簡體   English   中英

互動經紀人 API 執行細節,我如何獲得我得到的期貨的入場價格?

[英]interactive brokers API execute details, how I get the entry price of a Future that i got?

我想得到我實際購買的未來的價格。 我越來越瘋狂地試圖讓它發揮作用。 我也想知道如何實時獲得 PnL。 當我嘗試這樣做時,更改值需要將近 5 分鍾。 這是我在這里使用的一些代碼,它確實有效,但我無法獲得執行細節。

進口時間

def read_positions(): # 讀取所有賬戶頭寸並返回 DataFrame 信息

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.common import TickerId
import pandas as pd
import time

class ib_class(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self, self)
        self.all_positions = pd.DataFrame([], columns=['Account', 'Symbol', 'Quantity', 'Average Cost', 'Sec Type'])

    def error(self, reqId: TickerId, errorCode: int, errorString: str):
        if reqId > -1:
            print("Error. Id: ", reqId, " Code: ", errorCode, " Msg: ", errorString)

    def position(self, account, contract, pos, avgCost):
        index = str(account) + str(contract.symbol)
        self.all_positions.loc[index] = account, contract.symbol, pos, avgCost, contract.secType

    def positionEnd(self):
        self.disconnect()

ib_api = ib_class()
ib_api.connect("127.0.0.1", 7496, 10)
ib_api.reqPositions()
current_positions = ib_api.all_positions
ib_api.run()

return current_positions

def read_navs(): # 讀取所有賬戶的 NAV

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.common import TickerId
import pandas as pd
import time

class ib_class(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self, self)
        self.all_accounts = pd.DataFrame([], columns=['reqId', 'Account', 'Tag', 'Value', 'Currency'])

    def error(self, reqId: TickerId, errorCode: int, errorString: str):
        if reqId > -1:
            print("Error. Id: ", reqId, " Code: ", errorCode, " Msg: ", errorString)

    def accountSummary(self, reqId, account, tag, value, currency):
        #if tag == 'NetLiquidationByCurrency':
            index = str(account)
            self.all_accounts.loc[index] = reqId, account, tag, value, currency


    def accountSummaryEnd(self, reqId: int):
        self.disconnect()

    def execDetails(self, reqId, contract, execution):
        print('Order Executed: ', reqId, contract.symbol, contract.secType, contract.currency, execution.execId,
              execution.orderId, execution.shares, execution.lastLiquidity)

ib_api = ib_class()
ib_api.connect("127.0.0.1", 7496, 10)
ib_api.reqAccountSummary(9001, "All", "$LEDGER")
current_nav = ib_api.all_accounts
ib_api.run()

return current_nav

而 True: time.sleep(0.1); 打印(讀取位置())

我記得這是一個新的 function reqPnLSingle所以我寫了這個來測試。

有一些奇怪的數據,但值似乎正確,並且不斷更新。

我在代碼中添加注釋來解釋。

import ibapi
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.common import *
from ibapi.contract import *

import pandas as pd
import collections 
import time

class TestApp(EClient, EWrapper):
    accts = []
    orderId = 0
    posns = collections.defaultdict(list)
    start = time.time()
    
    def __init__(self):
        EClient.__init__(self, self)
        
    def error(self, reqId:TickerId, errorCode:int, errorString:str):
        if (reqId > -1):
            # ignore many 2150 Invalid position trade derived value
            if (errorCode == 2150): return
            print("Error:", reqId, errorCode, errorString)
        else :
            print("Info: ", errorCode, errorString)
        
    def managedAccounts(self, accountsList: str):
        # the accounts are sent when a connection is made
        # only needed to reqPosns for sub acct
        self.accts = accountsList.split(",")# probably just 1
        print("accts", self.accts)
        
    def nextValidId(self, orderId:int):
        # this is called when a connection is made
        self.orderId = orderId
        
        # to make sure the version is >= min 9.73.06, server > ~127?
        print(ibapi.VERSION, self.serverVersion())

        #use this as a signal to start making requests
        self.reqPositions()
    
    def position(self, account: str, contract: Contract, position: float, avgCost: float):
        self.posns["account"].append(account)
        self.posns["conId"].append(contract.conId)
        self.posns["symbol"].append(contract.localSymbol)
        self.posns["avgCost"].append(avgCost)
        self.posns["posn"].append(position)
        
    def positionEnd(self):
        self.df = pd.DataFrame.from_dict(self.posns) #will make an automatic int index
        
        if (self.df.empty):
            self.disconnect()
            return
        
        # make req for each posn, use index for reqId
        for index, row in self.df.iterrows():
            self.reqPnLSingle(index, row.account, "", row.conId)
    
    def pnlSingle(self, reqId: int, pos: int, dailyPnL: float, unrealizedPnL: float, realizedPnL: float, value: float):
        row = self.df.iloc[reqId]
        # all PnL's are maxint for me at night so I calc the right amount
        print(row.symbol, "PnL", unrealizedPnL, "calc", value - row.avgCost * row.posn)
        
        #just run for ~10 secs
        if (time.time() - self.start > 10): 
            print(self.df)
            self.disconnect()
    
def main():
    app = TestApp()
    app.connect("127.0.0.1", 7497, 123)
    app.run()
    
if __name__ == "__main__":
    main()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM