如何使用Web API实施简单的IBKR买卖

发布于 2025-02-03 03:18:19 字数 1531 浏览 4 评论 0 原文

我目前正在Python开发一个简单的IBKR交易机器人。
我希望该机器人每天进行这些操作:

  1. 出售我所有的股票(当市场开放

时 't有一个帐户,但是为了最大程度地减少我在IBKR平台上的不活动时间,我想知道如何事先实施我想要的东西。

从我有限的理解中,我拥有的是:

def main():
    sell_stock(previous_day_stock)
    buy_stock(daily_recommended_stock)

def get_available_money():
    return requests.get(f"https://localhost:5000/v1/api/portfolio/{account_id}/summary").json()["availablefunds"]["amount"]

def get_owned_stocks_value():
    return requests.get(f"https://localhost:5000/v1/api/portfolio/{account_id}/positions/").json()["avgPrice"]

def buy_stock(symbol):
    account_id = 1234
    payload = {
        "conid": get_contract_id(symbol),
        "secType": "STK",
        "orderType": "MKT",
        "cashQty": get_available_money(),
        "side": "BUY",
        "tif": "DAY"
    }

    return requests.post(f"https://localhost:5000/v1/api/iserver/account/{account_id}/orders", payload)

def sell_stock(symbol):
    account_id = 1234
    payload = {
        "conid": get_contract_id(symbol),
        "secType": "STK",
        "orderType": "MKT",
        "cashQty": get_owned_stocks_value(),
        "side": "SELL",
        "tif": "DAY"
    }

    return requests.post(f"https://localhost:5000/v1/api/iserver/account/{account_id}/orders", payload)

def get_contract_id(symbol):
    return requests.get(f"https://localhost:5000/v1/api/trsrv/stocks/symbols={symbol}").json()["contracts"]["conid"]

我觉得自己的操作方式是不正确的,而且我无法访问API来了解我应该做什么,并且我的代码甚至有意义。我很想知道做我想做的正确的方法。

I am currently developing a simple IBKR trading bot in Python.
I want the bot to do these operations daily:

  1. sell all of my owned stocks (when market opens)
  2. use the entire money to buy a recommended symbol that I generate (after I sell everything)

I don't have an account yet, but to minimize my inactivity time on the IBKR platform, I want to know how to implement what I want beforehand.

What I have now from my limited understanding:

def main():
    sell_stock(previous_day_stock)
    buy_stock(daily_recommended_stock)

def get_available_money():
    return requests.get(f"https://localhost:5000/v1/api/portfolio/{account_id}/summary").json()["availablefunds"]["amount"]

def get_owned_stocks_value():
    return requests.get(f"https://localhost:5000/v1/api/portfolio/{account_id}/positions/").json()["avgPrice"]

def buy_stock(symbol):
    account_id = 1234
    payload = {
        "conid": get_contract_id(symbol),
        "secType": "STK",
        "orderType": "MKT",
        "cashQty": get_available_money(),
        "side": "BUY",
        "tif": "DAY"
    }

    return requests.post(f"https://localhost:5000/v1/api/iserver/account/{account_id}/orders", payload)

def sell_stock(symbol):
    account_id = 1234
    payload = {
        "conid": get_contract_id(symbol),
        "secType": "STK",
        "orderType": "MKT",
        "cashQty": get_owned_stocks_value(),
        "side": "SELL",
        "tif": "DAY"
    }

    return requests.post(f"https://localhost:5000/v1/api/iserver/account/{account_id}/orders", payload)

def get_contract_id(symbol):
    return requests.get(f"https://localhost:5000/v1/api/trsrv/stocks/symbols={symbol}").json()["contracts"]["conid"]

I feel like the way I'm doing it is not correct, and I don't have access to the API yet to understand what I should do, and if my code even makes sense. I'd love to know the correct way to do what I want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人的歌 2025-02-10 03:18:19

首先,您可以无需拥有IB帐户即可访问IBAPI库 - 它只是从PYPI下载的Python库()或互动经纪人()。您应该这样做,因为它提供了与API/TWS交互所需的所有对象,函数和回调。

您概述的任务将需要进行大量的编程工作,但是在粗略的轮廓中,您需要的步骤是:

  1. 建立与TWS的连接
  2. 通过API请求当前位置信息使用 reqacccountupdates reqpositions
  3. 通过位置接收位置信息
  4. 通过位置迭代并生成合同 <代码>订单对象放置您的出口为每个位置订购。
  5. 使用 plotorder
  6. 等待位置退出,并通过 reqopenorder openordorder 回调
  7. 确定您的可用资金,请确认您的可用资金 updateAccountValue 回调
  8. 使用另一个合同订单 in ploteorder

所有这些详细信息的详细信息此处的API文档 https://interactivebrokers.github.github.io/tws-api/

应仔细注意,即使对这组简单的程序进行编程也将是一项非平凡的练习,而且您编程的越多,您就会发现您从未想到的事情就越需要额外的时间/编程。

我不确定这是在设置帐户之前尝试对所有编码进行编码的正确方法。只有在与TWS一起使用代码时,您才会发现大多数问题。

Firstly, you can access the ibapi library without having an IB account - it's just a python library downloaded from pypi (https://pypi.org/project/ibapi/) or interactive brokers (https://interactivebrokers.github.io). You should do this as it provides all the objects, functions and callbacks you will need to interact with the API/TWS.

The tasks you outline will require quite a bit of work to program, but in rough outline the steps you will need are:

  1. Establish a connection to the TWS via the API
  2. Request current position information using reqAccountUpdates or reqPositions
  3. Receive position information via the position callback
  4. Iterate through the positions and generate a Contract and an Order object to place your exit order for each position.
  5. Place the exit orders using placeOrder
  6. Wait for positions to exit, and confirm by viewing order status via reqopenOrder and openOrder callback
  7. Determine your available funds using the updateAccountValue callback
  8. Place your entry order using another Contract and Order objects in placeOrder

Details on all of these are available in the API documentation here https://interactivebrokers.github.io/tws-api/

You should note carefully that it will very much be a non-trivial exercise to program even this simple set of procedures, and the more you program the more you will discover things you hadn't thought of that will require additional time/programming.

I'm not sure it's the right approach to try to code it all before setting up an account. You will only discover most of the issues when you start using your code with the TWS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文