erdewit / ib_insync

Python sync/async framework for Interactive Brokers API
BSD 2-Clause "Simplified" License
2.75k stars 723 forks source link

can not place order with float quantity #705

Closed ctu1121 closed 4 months ago

ctu1121 commented 4 months ago

Hello, I wrote stock, vol, stop sell price on excel as below. stock.xlsx

Then I use the code below to transmit order for me. The code below can not place order on ib tws. I think the problem might be: 'vol': float

if I change 'vol': float to 'vol': int, the code will place order to ib tws correctly. Please advice how could I place with float quantity to ib tws. Thanks in advnace.


from ib_insync import * import pandas as pd

Connect to Interactive Brokers (IB)

ib = IB() ib.connect('127.0.0.1', 7496, clientId=1)

df = pd.read_excel('/Users/ctu1121/Downloads/stock.xlsx', dtype={'stock': str, 'vol': float, 'price': float}) #Error,might be 'vol': float

Iterate over each row in the DataFrame

for index, row in df.iterrows():

Create the contract object for the stock

stock = Stock(row['stock'], 'SMART', 'USD')

# Create a market buy order and set FA information
buy_order = MarketOrder("BUY", row['vol'])

buy_order.faGroup = '20240302'  # Specify the FA portfolio name

# Place the market buy order
buy_trade = ib.placeOrder(stock, buy_order)

# Create a stop sell order
stop_price = row['price']  # Get the stop sell price from the Excel file
sell_order = StopOrder("SELL", row['vol'], stop_price)

sell_order.faGroup = '20240302'  # Use the same FA portfolio name

# Place the stop sell order
sell_trade = ib.placeOrder(stock, sell_order)

# Print trade information
print(buy_trade)
print(sell_trade)

Disconnect from IB

ib.disconnect()