Здесь это мой код:
Код: Выделить всё
from ib_insync import *
# Create a connection to the IB gateway or TWS
ib = IB()
ib.connect('127.0.0.1', 7497, clientId=1) # adjust these as needed
# Define the contract and order
contract = Forex('GBPUSD')
ib.qualifyContracts(contract)
order = MarketOrder('BUY', 25000)
# Submit the order
trade = ib.placeOrder(contract, order)
print("Order submitted.")
ib.sleep(30)
# Wait for the order to fill
while trade.orderStatus.status != 'Filled':
ib.waitOnUpdate()
print(f"Order status: {trade.orderStatus.status}")
if trade.orderStatus.status == 'Filled':
print("Order filled, placing child orders.")
# Define the prices for the child orders
stop_loss_price = trade.orderStatus.avgFillPrice - 0.00150
profit_target_price = trade.orderStatus.avgFillPrice + 0.00200
print(f"Stop loss price: {stop_loss_price}, Profit target price: {profit_target_price}")
ib.sleep(30)
# If the order is filled, place a stop loss order
stop_loss_order = StopOrder('SELL', 25000, stop_loss_price)
stop_loss_order.orderId = ib.client.getReqId() # get a new order id
stop_loss_trade = ib.placeOrder(contract, stop_loss_order)
ib.sleep(30)
print(f"Stop loss order placed, status: {stop_loss_trade.orderStatus.status}")
ib.sleep(30)
# And place a profit taking order
profit_taking_order = LimitOrder('SELL', 25000, profit_target_price)
profit_taking_order.orderId = ib.client.getReqId() # get a new order id
profit_taking_trade = ib.placeOrder(contract, profit_taking_order)
ib.sleep(30)
print(f"Profit taking order placed, status: {profit_taking_trade.orderStatus.status}")
ib.sleep(30)
ib.disconnect()
#ib.run()
Подробнее здесь: https://stackoverflow.com/questions/767 ... -ib-insync
Мобильная версия