簡體   English   中英

Python盈透證券ibapi

[英]Python Interactive Brokers ibapi

我在使用修改后的IB示例代碼為每個庫存訂單使用和增加“ orderId”時遇到麻煩。 我有一個在類中生成nextValidId並將其打印到stdout的方法,但是我不確定如何訪問在main()程序主體中創建的已定義屬性(self.nextValidOrderId)。 我能夠實例化EWrapper和EClient類並下訂單(如果我手動輸入orderId)。 在示例代碼中,我將其硬編碼為126。我想我可以在main()程序主體中使用以下代碼。 “ orderId = app.nextValidOrderId”,但是它不起作用。

`__author__ = 'noone'

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

class OrderApp(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self,self)

    def error(self,reqId:TickerId, errorCode:int, errorString:str):
        print("Error:",reqId," ", errorCode, " ", errorString)

    # This is the initial response after connection to TS from the API providing next available OrderID
    @iswrapper        
    def nextValidId(self, orderId: int):
    super().nextValidId(orderId)

    print("setting nextValidOrderId: %d", orderId)
    self.nextValidOrderId = orderId

    def contractDetails(self, reqId:int, contractDetails:ContractDetails):
        print("contractDetails: ", reqId, " ", contractDetails)

def main():
    app=OrderApp()
    app.connect("127.0.0.1",7497,0)

    ## Build the contract object to be passed to the order Method

    stock_contract = Contract()
    stock_contract.symbol = 'AAPL'
    stock_contract.secType = 'STK'
    stock_contract.exchange = 'SMART'
    stock_contract.currency = 'USD'
    stock_contract.primaryExchange = 'NASDAQ'

    # reqID must be provided to the Order. This method gets the reqID from IB DB's
    app.reqContractDetails(10, stock_contract)

    try:
        # Now place the order in Paper Money Account
        app.placeOrder(126, stock_contract, OrderSamples.LimitOrder("BUY", 50, 12))

    except:
        raise

    app.run()

if __name__ == "__main__":
    main()`

您的方法在語法上不正確。 它應該具有以下縮進形式

@iswrapper        
def nextValidId(self, orderId: int):
    super().nextValidId(orderId)

    print("setting nextValidOrderId: %d", orderId)
    self.nextValidOrderId = orderId

另外,您不導入功能裝飾器iswrapper

from ibapi.utils import iswrapper

希望這可以幫助

暫無
暫無

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

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