Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def initialize(context):
- schedule_function(trade, date_rules.every_day(), time_rules.market_close(minutes=5))
- schedule_function(time_stop, date_rules.every_day(), time_rules.market_close(minutes=10))
- context.spy = sid(8554)
- context.counter = 0
- context.number_of_days_for_time_stop = 5 #We sell 5 days after we enter
- def trade(context, data):
- #This function simply buys SPY
- order_target_percent(context.spy, 1.0)
- def time_stop(context, data):
- #This function checks if we are long SPY,
- #If so it adds "1" to context.counter
- if context.portfolio.positions[context.spy].amount > 0:
- context.counter = context.counter + 1
- print(context.counter)
- #When context.counter equals "context.number_of_days_for_time_stop" we sell
- #Sets context.counter back to 0
- if context.counter == context.number_of_days_for_time_stop:
- order_target_percent(context.spy, 0.0)
- print('we are selling after X days')
- context.counter = 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement