Can someone tell me why the context.tgtamt returns '0' in the second 'percent to hold' step below code? I am a newbie Python programmer and I am confused about this. The num holdings returns 2. 1/2 seems like it should return .5? The variable is defined as type float? So what am I missing? Thanks.
def initialize(context):
context.sb = symbols('SPY', 'IEF')
schedule_function(rebal,
date_rule=date_rules.week_start(), time_rule=time_rules.market_close(minutes=1))
context.tgtamt = float(.5)
def handle_data(context, data):
pass
def rebal (context, data):
for s in data:
print ('value of cntxt.tgtamt step1 : %s' %context.tgtamt)
numholdings = (len(context.sb))
log.info("number of holding %s" % (numholdings))
context.tgtamt = 1/numholdings
log.info("percent to hold - tgtamtstep2 : %s" % (context.tgtamt))
order_target_percent(s,context.tgtamt)
log.info("buying %s" % (s.symbol))
record(leverage = context.account.leverage)