想問下,如果你IB户口未執行的限價盤金額超過你戶口有既cash,IB會唔會自動將超出既金額變為孖展?
問題係個d只係限價盤,可能永遠都唔會實現到,所以超出部分唔應該計我孖展!實際超出但未實現既金額IB到底計本金定計孖展?
```python
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.order import Order
class IBWrapper(EWrapper):
def __init__(self):
self.next_order_id = None
def nextValidId(self, orderId: int):
self.next_order_id = orderId
class IBClient(EClient):
def __init__(self, wrapper):
EClient.__init__(self, wrapper)
def create_contract(symbol: str, sec_type: str, exchange: str, currency: str):
contract = Contract()
contract.symbol = symbol
contract.secType = sec_type
contract.exchange = exchange
contract.currency = currency
return contract
def create_order(action: str, quantity: float):
order = Order()
order.action = action
order.orderType = "MKT" # 使用市價單
order.totalQuantity = quantity
return order
if __name__ == "__main__":
# 建立 IBWrapper 和 IBClient
wrapper = IBWrapper()
client = IBClient(wrapper)
client.connect("127.0.0.1", 7497, clientId=1) # 連接到 IB 交易平台
# 等待連接成功
while not client.isConnected():
pass
# 等待接收下一個訂單 ID
while wrapper.next_order_id is None:
pass
# 建立合約
qqq_contract = create_contract("QQQ", "STK", "SMART", "USD")
spy_contract = create_contract("SPY", "STK", "SMART", "USD")
aapl_contract = create_contract("AAPL", "STK", "SMART", "USD")
# 設置訂單
qqq_order = create_order("BUY", 10) # 購買 10 股 QQQ
spy_order = create_order("BUY", 5) # 購買 5 股 SPY
aapl_order = create_order("BUY", 20) # 購買 20 股 AAPL
# 下單
client.placeOrder(wrapper.next_order_id, qqq_contract, qqq_order)
client.placeOrder(wrapper.next_order_id + 1, spy_contract, spy_order)
client.placeOrder(wrapper.next_order_id + 2, aapl_contract, aapl_order)
# 斷開連接
client.disconnect()