I would like to use 'context.portfolio.positions' function to get the positions information but the result is 0, Code as below:
def initialize(context):
pass
def handle_data(context,data):
expiry_d ='20240627'
call_price= float(19900)
Call_CD = "F4"
C_opt = superSymbol(secType='OPT', symbol='HSI',currency='HKD', exchange='HKFE', strike=call_price, right='C', multiplier='50', localSymbol='HSI19900F4',expiry=expiry_d)
C_opt_Position = context.portfolio.positions[C_opt]
for security, position in context.portfolio.positions.items( ):
print(security,position,C_opt_Position)
end()
Result as below:
Try to get connected to Interactive Brokers
Connected to Interactive Brokers
IBridgePy version 20.5.2
fileName = untitled52.py
Starting to initialize trader ####
ACCOUNT Balance DU2350282 ##
CASH=1984048.93
portfolio_value=1984985.92
positions_value=980.2
POSITIONS DU2350282 ##
Symbol Amount Cost_basis
OPT,HSI,HKD, 19100.00,C,50,HSI19100F4W21 1 469.04
OPT,HSI,HKD, 19900.00,C,50,HSI19900F4 1 269.04
NO any order ##
Initialize trader COMPLETED ####
OPT,HSI,HKD,19100.0,C,50 {security=OPT,HSI,HKD, 19100.00,C,50,HSI19100F4W21 amount=1 cost_basis=469.04} {security=OPT,HSI,HKD,20240627, 19900.00,C,50,HSI19900F4 amount=0 cost_basis=0.0}
OPT,HSI,HKD,19900.0,C,50 {security=OPT,HSI,HKD, 19900.00,C,50,HSI19900F4 amount=1 cost_basis=269.04} {security=OPT,HSI,HKD,20240627, 19900.00,C,50,HSI19900F4 amount=0 cost_basis=0.0}
untitled52.py END
How can I get the HSI19900F4 positions by using context.portfolio.positions Function?