【问题标题】:reqPostions() help disconnecting TWS / Interactive brokers / apireqPostions() 帮助断开 TWS / Interactive brokers / api
【发布时间】:2019-07-26 21:17:05
【问题描述】:

以下是我如何检查未平仓头寸的示例。如果不存在头寸,则创建一个 txt 文件,如果存在未平仓头寸,则删除 .txt 文件。 我的问题是我不知道如何断开它。它也输出错误 -1 见下面的脚本

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.common import *
from ibapi.contract import *
from ib.opt import ibConnection
import os.path
from os import path


class CheckPos(EClient, EWrapper):
            def __init__(self):
                EClient.__init__(self, self)
            def nextValidId(self, orderId:int):
                self.reqPositions()
            def position(self, account: str, contract: Contract, position: float,
                avgCost: float):
                super().position(account, contract, position, avgCost)

                if position >0:
                    try:
                        os.remove("noposition.txt")

                    except:
                       print("Open Positons")



                else:
                    try:
                        open("noposition.txt","w+")
                        print("File Created Sucessfully")

                    except:
                        print("No Open Positions")







def main():
    app = CheckPos()
    app.connect("127.0.0.1", 7497,421 )
    app.run()


if __name__ == "__main__":
    main()    

输出是 错误 -1 2104 市场数据农场连接正常:usfuture 错误 -1 2104 市场数据农场连接正常:usfarm 错误 -1 2106 HMDS 数据场连接正常:ushmds 文件创建成功

【问题讨论】:

  • 哥们,首先是逗号。它们有助于更好地表达。其次,您是否有任何可重现的代码、输入、预期输出等?
  • 好的,我们所说的可重现代码的意思是我们可以看看你的代码来调试哪里出了问题吗?
  • 你真的想用 IbPy 代替 IB interactivebrokers.ca/en/index.php?f=5041 的新 api 吗?
  • 我愿意使用较新的 API 并已安装,但无法理解 Ewapper 和 Eclient。
  • stackoverflow.com/a/56917618/2855515 是我使用 ib api 的最新答案,但使用的是 contractDetails 而不是职位。在这里你可以看到它几乎与位置相同。 interactivebrokers.github.io/tws-api/… 。尝试使用我的代码作为模板编写一些代码,并将合约方法替换为位置方法。编辑您的问题并发布您的代码,如果它不起作用。如果它确实有效,您可以将其发布为答案。

标签: python api tws


【解决方案1】:

您删除了您的 positionEnd() 方法。那是你应该断开连接的时候。

from ib.opt import ibConnection 这行来自 IbPy,删除它。

我不确定您要对文件做什么,但您可能会在回调中返回许多位置。最好将它们添加到列表中,并在程序完成所有位置后对它们进行处理。我在 positionEnd() 方法中写了一些东西,断开连接然后保存数据。

ERROR -1 2104 Market data farm connection is OK:usfuture 这样的行只是信息。你可以忽略它们,除非他们说它坏了,但这是另一个错误代码。

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

import os

class TestApp(EClient, EWrapper):
    posns = []
    fname = 'fname.txt'

    def __init__(self):
        EClient.__init__(self, self)

    def nextValidId(self, orderId:int):
        print("id", orderId)
        print("starting program")
        self.reqPositions()

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

    def position(self, account: str, contract: Contract, position: float, avgCost: float):
        self.posns.append((account, contract.symbol, position, avgCost))
        print(contract.symbol, position)

    def positionEnd(self):
        print("end, disconnecting")
        self.disconnect()

        #write posns to file or delete file if no positions
        if self.posns: #means not empty
            with open(self.fname, "w") as outfile:
                outfile.write('\n'.join(str(posn) for posn in self.posns))
        else: # no posns so delete file
            os.remove(self.fname)

def main():
    app = TestApp()
    app.connect("127.0.0.1", 7497, 421)
    app.run()

if __name__ == "__main__":
    main()

【讨论】:

  • 非常感谢您抽出宝贵时间。使用文件的原因是我不太擅长这个,但我正在努力至少让它发挥作用。我有一个 python 文件,它从 tradingview 获取警报(在 pine 中为警报编码指标要好得多),然后进行交易,并向我发送一条短信。我只想定义一个函数来将 var 存储在我的非循环脚本顶部的未平仓头寸,以便可以在我的逻辑中使用它 if then else 语句稍后在代码中。
  • 这行得通。这很容易,我可以看到我的投资组合。感谢您的辛勤工作!
猜你喜欢
  • 1970-01-01
  • 2020-01-26
  • 1970-01-01
  • 2013-12-31
  • 1970-01-01
  • 2012-12-11
  • 1970-01-01
  • 1970-01-01
  • 2018-07-11
相关资源
最近更新 更多