I'm trying to implement an algorithm that buys and sells on specific dates, but with some days of offset. I cannot seem to find a way to implement this in a way that uses trading days instead of calendar days for the offset. Here's what I've got now:
-----------------------------------------------------------------------
stock = symbol('SPY')
-----------------------------------------------------------------------
import datetime
from datetime import timedelta
def initialize(context):
context.delay = 0
schedule_function(trade,date_rules.every_day(),time_rules.market_open(minutes = 1))
def before_trading_start(context, data):
pass
def trade(context,data):
if str(get_datetime().date()) == str(datetime.date(2017,4,11)+datetime.timedelta(days=context.delay)):
order_target_percent(stock,1.0)
if str(get_datetime().date()) == str(datetime.date(2017,4,18)+datetime.timedelta(days=context.delay)):
order_target_percent(stock,0.0)
Can anyone offer suggestions?