I am trying to just make a simple trading decision......whoever has the biggest DIFFERENCE from the CustomFactor output I want to buy 20 shares of that stock (its either AAPL or IBM) and If I own one i want to sell the other and I want to make this decision every day. Where am I going wrong??
class Last_Close_30(CustomFactor):
# output (yesterdays price, average projection of tomorrows price, difference of yesterdays and average, standard deviation)
out.yesterday[:] = close[-1]
out.projection[:] = average
out.difference[:] = difference
out.standard[:] = std_mc
def initialize(context):
# Rebalance every day
schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open())
# Record tracking variables at the end of each day.
schedule_function(my_record_vars, date_rules.every_day(), time_rules.market_close())
# Create our dynamic stock selector.
attach_pipeline(make_pipeline(), 'my_pipeline')
def make_pipeline():
pipe = Pipeline()
#base_universe = Q500US()
# make mask to have desired stocks we want
aapl = StaticAssets(symbols('AAPL','IBM'))
#call Last_Close_30 Class
projection_price = Last_Close_30(window_length = 30, mask = aapl)
# top 10 stocks with highest positive difference and low std
# highest = projection_price.difference.top(10)
# Add the desired values to our pipe.
#pipe.add(highest, 'highest 10')
pipe.add(projection_price, 'projection')
pipe.set_screen(aapl)
#pipe.set_screen(base_universe)
return pipe
def before_trading_start(context, data):
# Called every day before market open.
context.output = pipeline_output('my_pipeline')
print context.output
def my_assign_weights(context, data):
"""
Assign weights to securities that we want to order.
"""
pass
def my_rebalance(context,data):
context.aapl = sid(24)
context.ibm = sid(37)
# if ibm better then apple hold one share vice
if aapl.difference > ibm.difference
order_target(context.aapl, 20)
order_target(context.ibm, 0)
else
order_target(context.aapl, 0)
order_target(context.ibm, 20)
"""
Execute orders according to our schedule_function() timing.
"""
pass