I have one more question. I'm trying to test this strategy on a number of stocks in a csv file. I've read as much about fetcher as I can but still can't these stocks to be included in my universe for the backtest. Any help with this would be greatly appreciated.
Thanks
Tim
def initialize(context):
#set_do_not_order_list(security_lists.leveraged_etf_list)
fetch_csv("https://dl.dropboxusercontent.com/s/q7i5k3453io9xlo/SPNQ_TickersQ.csv",universe_func=my_universe,date_column='Date',symbol='Ticker')
schedule_function(count_triggers_function, date_rules.every_day(), time_rules.market_open(hours=0,minutes=1))
schedule_function(order_entry_function, date_rules.every_day(), time_rules.market_open(hours=0,minutes=1))
schedule_function(order_exit_function, date_rules.every_day(), time_rules.market_close(hours=0,minutes=2))
def preview(df):
log.info(' %s ' % df.head())
return df
def my_universe(context, fetcher_data):
my_stocks=set(fetcher_data[symbol])
return my_stocks
def count_triggers_function(context,data):
triggers=0
for stock in data:
print stock
current_price = data[stock].close_price
yc = context.yc[stock].values.tolist()[0] # Unsure about these, check them
od = context.od[stock].values.tolist()[0] # in the debugger for example.
gap_size = (od - yc) / yc
if gap_size < -0.05:
triggers=triggers+1
elif gap_size>.05:
triggers=triggers+1
return triggers
def order_entry_function(context, data):
for stock in data:
current_price = data[stock].close_price
yc = context.yc[stock].values.tolist()[0] # Unsure about these, check them
od = context.od[stock].values.tolist()[0] # in the debugger for example.
gap_size = (od - yc) / yc
if gap_size <- 0.05:
print gap_size
triggers=count_triggers_function(context,data)
cash = (context.portfolio.cash / triggers)
print cash
number_of_shares = int((cash *.04)/(current_price-0.9*current_price))
order(stock, +number_of_shares)
log.info('Buying {} {}'.format(number_of_shares, stock.symbol))
log.info(triggers)
elif gap_size>0.05:
print gap_size
triggers=count_triggers_function(context,data)
cash=context.portfolio.cash/triggers
number_of_shares = int((cash *.04)/(current_price-0.9*current_price))
order(stock,-number_of_shares)
log.info('Shorting {}{}'.format(number_of_shares,stock.symbol))
log.info(triggers)
def order_exit_function(context, data):
for stock in data:
print stock
#record(cash = cash)
current_price = data[stock].close_price
#record(**{stock.symbol: current_price})
yc = context.yc[stock].values.tolist()[0]
od = context.od[stock].values.tolist()[0]
gap_size = (od - yc) / yc
if gap_size <-0.05:
shrs = context.portfolio.positions[stock].amount
if shrs:
order_target(stock, 0)
log.info('Selling Longs{} {}'.format(shrs, stock.symbol))
elif gap_size>0.05:
shrs = context.portfolio.positions[stock].amount
if shrs:
order_target(stock, 0)
log.info('Buying to Cover{} {}'.format(shrs, stock.symbol))
def handle_data(context, data):
# Only need to run history once per bar/frame
context.yc = history(2, "1d", 'close_price')
context.od = history(1, "1d", 'open_price')