简体   繁体   English

将所有未结订单记录从 IB 交易站导出到 csv 文件

[英]Export all open order record from IB trader station to csv file

I am working on the python program for Ibapi which I can automatedly place order.我正在为 Ibapi 开发 python 程序,我可以自动下订单。 Moreover, I would like to export the current open orders from the trader station by the program.此外,我想通过程序从交易者站导出当前未结订单。

self.reqAllOpenOrders() (Ref: https://interactivebrokers.github.io/tws-api/open_orders.html ) self.reqAllOpenOrders() (参考: https ://interactivebrokers.github.io/tws-api/open_orders.html)

I am using this command to get all the open orders.我正在使用此命令获取所有未结订单。 It gives all the record in the terminal, and the type of the records are none type.它给出了终端中的所有记录,记录的类型为无类型。 So, I wonder how could I export that record to a csv file.所以,我想知道如何将该记录导出到 csv 文件。

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.order import *

import threading
import time


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

    def openOrder(self, orderId, contract, order, orderState):
        print('openOrder id:', orderId, contract.symbol, contract.secType, '@', contract.exchange,
              ':', order.action, order.orderType, order.totalQuantity, orderState.status)

        self.reqAllOpenOrders()

def run_loop():
    app.run()

app = IBapi()
app.connect('127.0.0.1', 7497, 123)

# Start the socket in a thread
api_thread = threading.Thread(target=run_loop, daemon=True)
api_thread.start()

time.sleep(3)
app.disconnect()
class IBapi(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self, self)
        self.file = open('file.csv', 'w')

    def openOrder(self, orderId, contract, order, orderState):
        mywriter = csv.writer(
            self.file, lineterminator='\n')
        mywriter.writerows([df])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM