简体   繁体   中英

Interactive Brokers Python API - Executing multiple trades

I'm trying to create a program for a Python API to place multiple trades/market orders at once. I used a tutorial online to get some of this code and made a few changes. But, I am not able to place multiple orders at once. I am using 2 lists 1 is for symbols and the other is for their quantity. (Ex: Buy 3 Apple stocks). My code is executing only the last order: which is "Buy 3 CRM stocks". Can anyone help me figure out how to place multiple orders?

Here is my Python code:

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.order import *
from threading import Timer
import pandas as pd

t = ['AAPL', 'CRM']

n = [2, 3]

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

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

def nextValidId(self, orderId ):
    self.nextOrderId = orderId
    self.start()

def orderStatus(self, orderId , status, filled, remaining, avgFillPrice, permId, parentId, lastFillPrice, clientId, whyHeld, mktCapPrice):
    print("OrderStatus. Id: ", orderId, ", Status: ", status, ", Filled: ", filled, ", Remaining: ", remaining, ", LastFillPrice: ", lastFillPrice)

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)

def execDetails(self, reqId, contract, execution):
    print("ExecDetails. ", reqId, contract.symbol, contract.secType, contract.currency, execution.execId,
          execution.orderId, execution.shares, execution.lastLiquidity)

def start(self):
    for i in t:
        contract = Contract()
        contract.symbol = i
        contract.secType = "STK"
        contract.exchange = "SMART"
        contract.currency = "USD"
        contract.primaryExchange = "NASDAQ"

    for j in n:
        order = Order()
        order.action = "BUY"
        order.totalQuantity = j
        order.orderType = "MKT"

    self.placeOrder(self.nextOrderId, contract, order)

def stop(self):
    self.done = True
    self.disconnect()

def main():
   app = TestApp()
   app.nextOrderId = 0
   app.connect("127.0.0.1", 7497, 9)

   Timer(3, app.stop).start()
   app.run()

if __name__ == "__main__":
main()

The problem is with your for loop:

for j in n:
    order = Order()
    order.action = "BUY"
    order.totalQuantity = j
    order.orderType = "MKT"

self.placeOrder(self.nextOrderId, contract, order)

This code creates order n times and then submits one order to IB. If you want to submit n orders, you need to call self.placeOrder inside the for loop:

for j in n:
    order = Order()
    order.action = "BUY"
    order.totalQuantity = j
    order.orderType = "MKT"
    self.placeOrder(self.nextOrderId, contract, order)

Make sure you increment self.nextOrderId after placing each order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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