def initialize(context): # get data from pipeline data_pull = Data_Pull() attach_pipeline(data_pull, 'Data') # filter out bad stocks for universe mask = filter_universe() data_pull.set_screen(mask) # set leverage ratios for longs and shorts context.long_leverage = 1.3 context.short_leverage = -0.3 # at the start of each moth, run the rebalancing function schedule_function(rebalance, date_rules.month_start(), time_rules.market_open(minutes=30)) # clean untradeable securities daily schedule_function(daily_clean, date_rule=date_rules.every_day(), time_rule=time_rules.market_close(minutes=30)) # record variables schedule_function(record_vars, date_rule=date_rules.every_day(), time_rule=time_rules.market_close()) pass
def initialize(context): """ Called once at the start of the algorithm. """ context.counter = 0 set_benchmark(sid(8554)) # Rebalance at the #end of every month, 1 hour after market open. schedule_function(my_assign_weights_p98, date_rules.month_end(), time_rules.market_open()) schedule_function(my_rebalance, date_rules.month_end(), time_rules.market_open(hours=1)) # 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. context.return_period = 252 # SPY context.mom1 = mom1 = sid(8554) # VEU context.mom2 = mom2 = sid(33486) # SHV context.tbill = tbill = sid(33154) # BND context.agg = agg = sid(33652) # QQQ - NASDAQ context.tech = tech = sid(39214) context.stock_leverage = 1 sec_list = [tech, mom1, mom2, tbill, agg] attach_pipeline(make_pipeline(sec_list, context), 'my_pipeline')
def initialize(context): set_long_only() context.MaxCandidates = 100 context.MaxBuyOrdersAtOnce = 30 context.MyLeastPrice = 3.00 context.MyMostPrice = 25.00 context.MyFireSalePrice = context.MyLeastPrice context.MyFireSaleAge = 6 # hold 期間を確認するための辞書 context.age = {} # rebalance schedule start_min = 1 end_min = int(6.5 * 60) # =取引時間6.5時間*60分 everyminute = 10 # 毎10分ごとにリバランス。 for m in range(start_min, end_min, everyminute): schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(minutes=m)) # 描画 schedule_function(my_record_vars, date_rules.every_day(), time_rules.market_close()) my_pipe = make_pipeline(context) algo.attach_pipeline(my_pipe, 'pipeline')
def initialize(context): set_commission(commission.PerShare(cost=0.000, min_trade_cost=0)) schedule_function(func=monthly_rebalance, date_rule=date_rules.month_start(days_offset=5), time_rule=time_rules.market_open(), half_days=True) schedule_function(func=daily_rebalance, date_rule=date_rules.every_day(), time_rule=time_rules.market_open(hours=1)) set_do_not_order_list(security_lists.leveraged_etf_list) context.acc_leverage = 1.00 context.holdings = 30 context.profit_taking_factor = 0.5 #0.01,27.14 context.profit_target = {} context.profit_taken = {} context.entry_date = {} context.stop_pct = 0.75 context.stop_price = defaultdict(lambda: 0) context.stock = sid(49139) pipe = Pipeline() attach_pipeline(pipe, 'ranked_stocks') factor1 = momentum_factor_1() pipe.add(factor1, 'factor_1') factor2 = momentum_factor_2() pipe.add(factor2, 'factor_2') factor3 = momentum_factor_3() pipe.add(factor3, 'factor_3') factor4 = momentum_factor_4() pipe.add(factor4, 'factor_4') factor5 = efficiency_ratio() pipe.add(factor5, 'factor_5') #3000 rsi_screen = MyTalibRSI(window_length=120) stocks = rsi_screen.bottom(421) #421 #high_dollar_volume_screen = high_dollar_volume(window_length = 63) #liquid_filter = high_dollar_volume_screen.percentile_between(95,100) #mkt_screen = market_cap() #stocks = mkt_screen.top(3000) factor_5_filter = factor5 > 0.031 total_filter = (factor_5_filter) pipe.set_screen(total_filter) factor1_rank = factor1.rank(mask=total_filter, ascending=False) pipe.add(factor1_rank, 'f1_rank') factor2_rank = factor2.rank(mask=total_filter, ascending=False) pipe.add(factor2_rank, 'f2_rank') factor3_rank = factor3.rank(mask=total_filter, ascending=False) pipe.add(factor3_rank, 'f3_rank') factor4_rank = factor4.rank(mask=total_filter, ascending=False) pipe.add(factor4_rank, 'f4_rank') combo_raw = (factor1_rank + factor2_rank + factor3_rank + factor4_rank) / 4 pipe.add(combo_raw, 'combo_raw') pipe.add(combo_raw.rank(mask=total_filter), 'combo_rank')
def initialize(context): """ Called once at the start of the algorithm. """ set_benchmark(symbol('SPYV')) set_commission(commission.PerTrade(cost=0.0)) # set_slippage(slippage.VolumeShareSlippage(volume_limit=1, price_impact=0)) set_long_only() # set_max_leverage(1.1) # Rebalance every day, 1 hour after market open. algo.schedule_function( rebalance, algo.date_rules.every_day(), algo.time_rules.market_open(hours=1), ) # Record tracking variables at the end of each day. algo.schedule_function( record_vars, algo.date_rules.every_day(), algo.time_rules.market_close(), ) # Create our dynamic stock selector. algo.attach_pipeline(make_pipeline(), 'piotroski') algo.attach_pipeline(risk_loading_pipeline(), 'risk_factors')
def initialize(context): my_pipe = make_pipeline() attach_pipeline(my_pipe, 'my_pipeline') context.recent_prices = {} context.window_length = 1 # Amount of prior bars to study context.spy = deque(maxlen=context.window_length+1) #context.classifier = RandomForestClassifier() # Use a random forest classifier context.classifier = DecisionTreeClassifier(max_depth=5, max_leaf_nodes=10) # my training data #context.features = ['RSI','EMA','MACD','SMA_5','SMA_10','bb_lower','bb_middle','bb_upper', 'diluted_eps','growth_score','tangible_bv'] context.features = ['RSI','EMA','MACD','SMA_5','SMA_10','bb_lower','bb_middle','bb_upper'] context.response = ['Class'] context.X = pd.DataFrame(columns = context.features) # Independent, or input variables context.Y = pd.DataFrame(columns = context.response) # Dependent, or output variable context.prediction = {} # Stores most recent prediction context.count = 0 context.activate = False context.no_of_stocks = 0 schedule_function(rebalance, date_rules.every_day(), time_rules.market_open(minutes=10)) schedule_function(record_vars, date_rules.every_day(), time_rules.market_close()) set_benchmark(symbol('QQQ'))
def initialize(context): schedule_function(my_rebalance, date_rules.week_start(), time_rules.market_open(hours=1)) my_pipeline = make_pipeline() attach_pipeline(my_pipeline, 'my_pipeline')
def initialize(context): context.sound_tribe_sector_9 = 1.0 context.target_qty = 5 context.spy = sid(8554) set_long_only() # context.rebalance_int = 1 # context.month_count = context.rebalance_int # here is my schedule function # schedule_function(buy_stocks,date_rules.every_day(), time_rules.market_open(minutes=60)) # schedule_function(sell_signal,date_rules.every_day(), time_rules.market_open()) # set_commission(commission.PerShare(cost=0, min_trade_cost=0)) schedule_function(func=allocate_1, date_rule=date_rules.every_day(), time_rule=time_rules.market_open(minutes=60), half_days=True) schedule_function(func=allocate_2, date_rule=date_rules.every_day(), time_rule=time_rules.market_open(minutes=90), half_days=True) schedule_function(func=allocate_3, date_rule=date_rules.every_day(), time_rule=time_rules.market_open(minutes=120), half_days=True) schedule_function(func=record_vars, date_rule=date_rules.every_day(), time_rule=time_rules.market_open(minutes=1), half_days=True) attach_pipeline(make_pipeline(context), 'myPipe')
def initialize(context): # Set slippage and commission to zero to evaulate the signal generating # ability of the algorithm set_commission(commission.PerShare(cost=0.0075, min_trade_cost=1.0)) set_slippage(slippage.VolumeShareSlippage(volume_limit=0.025, price_impact=0.1)) context.long_leverage = 0.50 context.short_leverage = -0.50 context.spy = sid(8554) attach_pipeline(make_pipeline(), 'ranking_example') # Used to avoid purchasing any leveraged ETFs context.dont_buys = security_lists.leveraged_etf_list # Schedule my rebalance function schedule_function(func=rebalance, date_rule=date_rules.month_start(days_offset=0), time_rule=time_rules.market_open(hours=0,minutes=30), half_days=True) # clean untradeable securities daily schedule_function(daily_clean, date_rule=date_rules.every_day(), time_rule=time_rules.market_close(minutes=30))
def initialize(context): NCAV = NCAVRatio() NCAVINV = InverseNCAV() schedule_function(rebalance, date_rules.week_start(), time_rules.market_open()) # Create and apply a filter representing the top 500 equities by MarketCap # every day. # Construct an average dollar volume factor PERatio = PE() Solid_PE = PERatio < 10 Short_PE = PERatio > 50 div = Dividends() Solid_Div = div >= 0.02 Short_Div = PERatio >100 LongsUniverse = Q1500US() & Solid_PE & Solid_Div#& Not_Bio#& high_dollar_volume ShortsUniverse = Q1500US() & Short_PE & Short_Div # Create filters for our long and short portfolios. longs = NCAV.top(15, mask=LongsUniverse) shortsTest = NCAVINV.top(15) shorts = NCAVINV.top(15, mask=ShortsUniverse) print "List of stocksTEST for short position : {}".format(shortsTest) print "List of stocks for short position : {}".format(shorts) Universe= (longs|shorts|shortsTest) pipe = Pipeline(columns = {'longs': longs, 'shorts': shorts, 'shortsTest': shortsTest}, screen = Universe) attach_pipeline(pipe, 'Value_Investing')
def initialize(context): print("Attempting To Initialize") context.oo = 0 context.max_leverage = 0.0 context.securities_in_results = [] attach_pipeline(custom_pipe(context), 'custom_pipe') ''' Hypothesis: small cap equities with a bullish sentiment pop will continue upward overnight Initialize Schedule: --------:---------------:------------ time : function : description --------:---------------:------------ 9:30 am : sell : place limit orders for all positions in pipeline (.98 threshold) 2:29 pm : cancel_orders : cancels all open orders that have not been filled 2:30 pm : buy : place proportional orders (1/n) for each security in your current pipeline universe 3:29 pm : cancel_orders : 3:30 pm : buy_2 : retry to place proportional orders (1/n) for each position in positions held with new price data 4:00 pm : cancel_orders : --------:---------------:------------ Possible improvements: - is as_of date for sentiment utilized correctly? - Long only (meant for use on RH), could long/short lower beta? - Use limit orders? ''' # run sell function everyday at 9:30 am schedule_function(sell, date_rules.every_day(), time_rules.market_open()) #open sell orders # run cancel_order function everyday at 2:29 pm (91 min before market close) schedule_function(cancel_orders, date_rules.every_day(), time_rules.market_close(minutes=91)) # run buy function everyday at 2:30 pm schedule_function(buy, date_rules.every_day(), time_rules.market_close(minutes=90)) #open buy orders # run cancel_order function everyday at 3:29 pm schedule_function(cancel_orders, date_rules.every_day(), time_rules.market_close(minutes=31)) #close buy orders # run buy_2 function everyday at 3:30 pm schedule_function(buy_2, date_rules.every_day(), time_rules.market_close(minutes=30)) #open buy # run cancel_order function everyday at 4:00 pm schedule_function(cancel_orders, date_rules.every_day(), time_rules.market_close()) # no commission on RH set_commission(commission.PerShare(cost=0.0, min_trade_cost=0.0)) context.longs = [] context.S = [] context.B = [] print("Initialization Successful")
def initialize(context): # Rebalance every day, 1 hour after market open. schedule_function(my_rebalance, date_rules.week_start(), time_rules.market_open(hours=3)) # Create our dynamic stock selector. attach_pipeline(make_pipeline(), 'pipe')
def initialize(context): context.gapdowns = [] context.gapups = [] context.sids = None context.bar_count = 40 context.turnover_threshold = 0.0 #0.0 42.8% context.gapup_threshold = 0.04 #0.04 schedule_function(find_gapup, date_rules.every_day(), time_rules.market_open(minutes=1)) schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(minutes=1)) schedule_function(close_orders, date_rules.every_day(), time_rules.market_open(minutes=context.bar_count)) schedule_function(my_record_vars, date_rules.every_day(), time_rules.market_open(minutes=context.bar_count)) attach_pipeline(make_pipeline(), 'pipe') fetch_csv( 'http://www.cboe.com/publish/scheduledtask/mktdata/datahouse/vixcurrent.csv', symbol='vix', skiprows=1, date_column='Date', pre_func=addFieldsVIX)
def initialize(context): """ Called once at the start of the algorithm. """ # Create our dynamic stock selector. attach_pipeline(make_pipeline(), 'my_pipeline')
def initialize(context): """ A core function called automatically once at the beginning of a backtest. Use this function for initializing state or other bookkeeping. Parameters ---------- context : AlgorithmContext An object that can be used to store state that you want to maintain in your algorithm. context is automatically passed to initialize, before_trading_start, handle_data, and any functions run via schedule_function. context provides the portfolio attribute, which can be used to retrieve information about current positions. """ algo.attach_pipeline(make_pipeline(), 'long_short_equity_template') # Attach the pipeline for the risk model factors that we # want to neutralize in the optimization step. The 'risk_factors' string is # used to retrieve the output of the pipeline in before_trading_start below. algo.attach_pipeline(risk_loading_pipeline(), 'risk_factors') # Schedule our rebalance function algo.schedule_function(func=rebalance, date_rule=algo.date_rules.week_start(), time_rule=algo.time_rules.market_open(hours=0, minutes=30), half_days=True) # Record our portfolio variables at the end of day algo.schedule_function(func=record_vars, date_rule=algo.date_rules.every_day(), time_rule=algo.time_rules.market_close(), half_days=True)
def initialize(context): """ The initialize function is the place to create your pipeline (security selector), and set trading conditions such as commission and slippage. It is called once at the start of the simulation and also where context variables can be set. """ # Define context variables that can be accessed in other methods of # the algorithm. context.long_leverage = 0.5 context.short_leverage = -0.5 context.returns_lookback = 5 # Rebalance on the first trading day of each week at 11AM. schedule_function(rebalance, date_rules.week_start(days_offset=0), time_rules.market_open(hours = 1, minutes = 30)) # Record tracking variables at the end of each day. schedule_function(record_vars, date_rules.every_day(), time_rules.market_close(minutes=1)) # Create and attach our pipeline (dynamic security selector), defined below. attach_pipeline(make_pipeline(context), 'mean_reversion_example')
def initialize(context): """ Called once at the start of the algorithm. """ #Generic name for equity traded, simply change the ticker #symbol to get any stock. context.stock_traded = symbol('ZUMZ') #To set the benchmark simply change the ticker symbol and #simply comment this line out for the SPX benchmark. set_benchmark(symbol('ZUMZ')) context.invested = False context.leverage = 1.0 context.short = False # Rebalance every day, 1 hour after market open. schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(hours=1)) # Record tracking variables at the end of each day. schedule_function(my_record_vars, date_rules.every_day(), time_rules.market_close()) schedule_function(trade, date_rules.every_day(), time_rules.market_open()) # Create our dynamic stock selector. attach_pipeline(make_pipeline(context), 'my_pipeline')
def initialize(context): """ Called once at the start of the algorithm. """ set_slippage(slippage.FixedSlippage(spread=0.00)) set_commission(commission.PerShare(cost=0, min_trade_cost=0)) schedule_function(rebalance_portfolio, TRADE_FREQ, time_rules.market_open(minutes=1)) # Record tracking variables at the end of each day. schedule_function(log_metrics, date_rules.every_day(), time_rules.market_close()) # Set up universe # base_universe = AverageDollarVolume(window_length=63, mask=QTradableStocksUS()).percentile_between(80, 100) universe = AverageDollarVolume( window_length=63, mask=QTradableStocksUS()).percentile_between(40, 60) # create alpha factors and machine learning pipline ml_pipeline = make_ml_pipeline(alpha_factors=make_alpha_factors(), universe=universe, lookback=TRAINING_PERIOD, lookahead=HOLDING_PERIOD) attach_pipeline(ml_pipeline, 'alpha_model') attach_pipeline(risk_loading_pipeline(), 'risk_loading_pipeline') context.past_predictions = {} context.realized_rmse = 0 context.realized_ic = 0 context.long_short_spread = 0
def initialize(context): """ Called once at the start of the algorithm. """ # Create a dic to hold "days held" value for each security in our current portfolio context.held_days = {} # Attach the pipeline defined in my_pipe so we have data to use attach_pipeline(make_pipeline(), 'my_pipeline') # Schedule when to log highs schedule_function(log_highs, date_rules.every_day(), time_rules.market_close()) # Schedule when to trade schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(hours=1)) # Schedule when to record any variables one wants to watch #schedule_function(my_record_vars, date_rules.every_day(), time_rules.market_close()) schedule_function(record_high, date_rules.every_day(), time_rules.market_close()) context.day_count = 0 context.high_sids = [] context.high_sids_exclude = [] set_slippage( slippage.VolumeShareSlippage(volume_limit=0.025, price_impact=0.1)) # Default set_commission(commission.PerShare(cost=0.005, min_trade_cost=1.0)) # FSC for IB
def initialize(context): # 空のパイプラインを作ります pipe = Pipeline() # research では run_pipeline # algorithm では,initilize 内で attach_pipeline を実行して,パイプラインを毎日実行するように設定します. # ほしいデータを定義 # pipeline 用に定義された 移動平均 SimpleMovingAverage を使う SMA10 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=10) SMA30 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=30) pipe.add(SMA10, 'SMA10') pipe.add(SMA30, 'SMA30') ## raw_weights raw_weights = (SMA10 - SMA30) / SMA30 abs_raw_weights = raw_weights.abs() pipe.add(raw_weights, 'raw_weights') pipe.add(abs_raw_weights, 'abs_raw_weights') pipe.set_screen(Q500US()) attach_pipeline(pipe, "my_pipeline") # 毎週月曜日にどの銘柄を保有するか決定する schedule_function(rebalance, date_rules.week_start(), time_rules.market_open()) # 毎週金曜日の取引時間終了時に持っている銘柄を全部クローズする. schedule_function(all_position_close, date_rule = date_rules.week_end(days_offset=0), time_rule = time_rules.market_close())
def initialize(context): # Set benchmark to short-term Treasury note ETF (SHY) since strategy is dollar neutral set_benchmark(sid(23911)) # Schedule our rebalance function to run at the end of each day. schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_close()) # Record variables at the end of each day. schedule_function(my_record_vars, date_rules.every_day(), time_rules.market_close()) # Get intraday prices today before the close if you are not skipping the most recent data schedule_function(get_prices, date_rules.every_day(), time_rules.market_close(minutes=5)) # Set commissions and slippage to 0 to determine pure alpha set_commission(commission.PerShare(cost=0, min_trade_cost=0)) set_slippage(slippage.FixedSlippage(spread=0)) # Number of quantiles for sorting returns for mean reversion context.nq = 5 # Number of quantiles for sorting volatility over five-day mean reversion period context.nq_vol = 3 # Create our pipeline and attach it to our algorithm. my_pipe = make_pipeline() attach_pipeline(my_pipe, 'my_pipeline')
def initialize(context): """ Called once at the start of the algorithm. """ # Set commision model set_commission(commission.PerShare(cost=0.005, min_trade_cost=1.0)) # Ensure no short trading (just a precaution) set_long_only() # Create and attach pipeline to get data attach_pipeline(my_pipeline(context), name='my_pipeline') # Place orders once a day schedule_function(enter_trades, 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()) set_slippage( slippage.VolumeShareSlippage(volume_limit=0.025, price_impact=0.1)) # Default set_commission(commission.PerShare(cost=0.005, min_trade_cost=1.0)) # FSC for IB
def initialize(context): context.bar_count = 60 context.turnover_threshold = 0.00 context.gapdown_threshold_upper = 0.00 context.gapdown_threshold_lower = 0.00 context.wincnt = 0 context.losscnt = 0 schedule_function(find_gapdown, date_rules.every_day(), time_rules.market_open(minutes=1)) schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(minutes=1)) schedule_function(all_open_position_close, date_rules.every_day(), time_rules.market_open(minutes=context.bar_count)) schedule_function(my_record_vars, date_rules.every_day(), time_rules.market_open(minutes=context.bar_count)) attach_pipeline(make_pipeline(), 'pipe') fetch_csv( 'http://www.cboe.com/publish/scheduledtask/mktdata/datahouse/vixcurrent.csv', symbol='vix', skiprows=1, date_column='Date', pre_func=addFieldsVIX)
def initialize(context): set_commission(commission.PerShare(cost=0.0075, min_trade_cost=1.00)) context.daycounttot = 0 #count of total days since start context.in_out = 1 #start with being 'in' the market context.waitdays = 15 #min days of being out; 3 trading weeks context.outday = 0 #daycounttot when in_out=0 context.spy_in = 0 #when SPY drops 30% go 'in' no matter what context.spyinday = 0 #daycounttot when spy_in=1 context.SPY = symbol('SPY') context.XLI = symbol('XLI') #to capture drop in industrials context.DBB = symbol('DBB') #to capture drop in materials context.BIL = symbol('SHY') #to capture interest rate incr. context.alternative = [symbol('IEF'), symbol('TLT')] #investment if 'out' of the market algo.attach_pipeline(make_pipeline(context), 'pipeline') # Schedule functions schedule_function( #daily check re getting 'out' of market check_out, date_rules.every_day(), time_rules.market_open(minutes = 75) ) schedule_function( #weekly regular trading (getting 'in') trade, date_rules.week_start(days_offset=4), time_rules.market_open(minutes = 75) )
def initialize(context): # будем ребалансировать наш портфель каждый январь в начале, через 1.5 часа после открытия attach_pipeline(make_pipeline(context), 'my_pipeline') schedule_function(rebalance, date_rules.month_start(days_offset=0), time_rules.market_open(hours=1, minutes=30))
def initialize(context): """ Called once at the start of the algorithm. """ set_slippage(slippage.FixedSlippage(spread=0.00)) set_commission(commission.PerShare(cost=0, min_trade_cost=0)) schedule_function( rebalance, TRADE_FREQ, time_rules.market_open(minutes=1), ) # Record tracking variables at the end of each day. schedule_function( record_vars, date_rules.every_day(), time_rules.market_close(), ) # Set up universe, alphas and ML pipline context.universe = QTradableStocksUS() ml_pipeline = make_ml_pipeline( context.universe, n_forward_days=PRED_N_FORWARD_DAYS, window_length=ML_TRAINING_WINDOW, ) # Create our dynamic stock selector. attach_pipeline(ml_pipeline, 'alpha_model') context.past_predictions = {} context.hold_out_accuracy = 0 context.hold_out_log_loss = 0 context.hold_out_returns_spread_bps = 0
def initialize(context): pipe = Pipeline() attach_pipeline(pipe, 'my_pipeline') close = Latest(inputs=[USEquityPricing.close], window_length=1) pipe.add(close, 'Close') # cboe_vix, yahoo_index_vix でどれだけデータが違っているか,確認してみる pipe.add(GetVIX(inputs=[cboe_vix.vix_close]), 'QuandleVIXClose') pipe.add(GetVIX(inputs=[cboe_vxv.close]), 'QuandleVXVClose') context.vxx = sid(38054) ## これは,日付index用につかう (下記の”⇐ここ”のところ) # fetch_csv したデータは, pd.DataFrame になり, symbol= でつけた名前で, data.current('symbol名', 'ファイル内のコラム') でアクセスできる # 参照 https://www.quantopian.com/help#overview-fetcher fetch_csv( 'http://www.cboe.com/publish/scheduledtask/mktdata/datahouse/vxvdailyprices.csv', symbol='vxv', skiprows=2, date_column='Date', pre_func=addFieldsVXV) fetch_csv( 'http://www.cboe.com/publish/scheduledtask/mktdata/datahouse/vixcurrent.csv', symbol='vix', skiprows=1, date_column='Date', pre_func=addFieldsVIX) schedule_function(outputlog, date_rules.every_day(), time_rules.market_open(minutes=5))
def initialize(context): #Called once at the start of the algorithm. # Nominal Leverage = Maximum Gross Exposure = 1.00, but re-set this to 0.90 to avoid risk of exceeding hard leverage limit of 1.10 context.leverage_buffer = 0.90 # Set slippage & commission as per Quantopian Open rules. # For competition use, assume $0.001/share # Can take up to 2.5% of 1 minute's trade volume. set_commission(commission.PerShare(cost=0.001, min_trade_cost=0)) set_slippage( slippage.VolumeShareSlippage(volume_limit=0.025, price_impact=0.1)) context.spy = sid(8554) attach_pipeline(make_pipeline(), 'long_short_equity_template') # Schedule my rebalance function. #------------------------------- #Changed from monthly to weekly and then daily rebal, 45 mins after market open. schedule_function(func=rebalance, date_rule=date_rules.week_start(days_offset=1), time_rule=time_rules.market_open(hours=0, minutes=45), half_days=True) # Record tracking variables at the end of each day. #------------------------------------------------- schedule_function(func=recording_statements, date_rule=date_rules.every_day(), time_rule=time_rules.market_close(), half_days=True) #Initialize a dictionary to store the entry dates for each stock held context.entry_dates = dict()
def initialize(context): """ Called once at the start of the algorithm. """ # referencing SPY so the 'handle_data' function will be called every minute #context.spy = sid(8554) context.intraday_bars = {} context.target_weights = {} context.daily_stat_history = [] context.positions_stop_loss = {} context.positions_max_gain = {} # schedule_function( # open_positions, # date_rules.every_day(), # time_rules.market_open() # ) # schedule_function( # close_positions, # date_rules.every_day(), # time_rules.market_close(hours=0.5) # ) # Create our dynamic stock selector. attach_pipeline(make_pipeline(), 'my_pipeline')
def initialize(context): #: Set commissions and slippage to 0 to determine pure alpha set_commission(commission.PerShare(cost=0, min_trade_cost=0)) set_slippage(slippage.FixedSlippage(spread=0)) #: Declaring the days to hold, change this to what you want context.days_to_hold = 3 #: Declares which stocks we currently held and how many days we've held them dict[stock:days_held] context.stocks_held = {} #: Declares the minimum magnitude of percent surprise context.min_surprise = .00 context.max_surprise = .05 #: OPTIONAL - Initialize our Hedge # See order_positions for hedging logic # context.spy = sid(8554) # Make our pipeline attach_pipeline(make_pipeline(context), 'earnings') # Log our positions at 10:00AM schedule_function(func=log_positions, date_rule=date_rules.every_day(), time_rule=time_rules.market_close(minutes=30)) # Order our positions schedule_function(func=order_positions, date_rule=date_rules.every_day(), time_rule=time_rules.market_open())
def initialize(context): """ Called once at the start of the algorithm. """ context.capacity = 30 context.annual_add = 10000 # must be >0 #schedule for buying a week after the year start algo.schedule_function(func=schedule_task_a, date_rule=date_rules.month_start(4), time_rule=time_rules.market_open()) #schedule for selling losers a week before the year start algo.schedule_function(func=schedule_task_b, date_rule=date_rules.month_end(4), time_rule=time_rules.market_open()) ''' #schedule for selling winners on the 7th day of year start algo.schedule_function(func=schedule_task_c, date_rule=date_rules.month_start(3), time_rule=time_rules.market_close()) ''' algo.schedule_function( record_vars, algo.date_rules.month_start(), algo.time_rules.market_close(), ) # Create our dynamic stock selector. algo.attach_pipeline(make_pipeline(), 'pipeline')
def initialize(context): set_benchmark(symbol('SHY')) set_commission( commission.PerTrade(cost=0.001)) #comment out to use default attach_pipeline(make_pipeline(), 'pipeline') schedule_function(close, date_rules.every_day(), time_rules.market_open()) schedule_function(shrts, date_rules.every_day(), time_rules.market_open(minutes=30)) schedule_function(longs, date_rules.every_day(), time_rules.market_open(hours=1)) schedule_function(record_vars, date_rules.every_day(), time_rules.market_close()) # Take profit several times a day context.profit_threshold = .05 context.profit_logging = 1 for i in range(20, 390, 60): # (low, high, every i minutes) #break # uncomment to deactivate schedule_function(take_profit, date_rules.every_day(), time_rules.market_open(minutes=i)) for i in range(1, 391): schedule_function(pvr, date_rules.every_day(), time_rules.market_open(minutes=i)) set_slippage( slippage.VolumeShareSlippage(volume_limit=0.025, price_impact=0.1)) # Default set_commission(commission.PerShare(cost=0.005, min_trade_cost=1.0)) # FSC for IB
def initialize(context): use_beta_hedging = True # Set to False to trade unhedged # Initialize a universe of liquid assets schedule_function(rebalance, date_rule=date_rules.month_start(), time_rule=time_rules.market_open(hours=1)) # Plot leverage and positions at the end of each day. schedule_function(record_vars, date_rule=date_rules.every_day(), time_rule=time_rules.market_close()) if use_beta_hedging: # Call the beta hedging function 30 mins after ordering to # make sure all of our orders have gone through. schedule_function(hedge_portfolio, date_rule=date_rules.month_start(), time_rule=time_rules.market_open(hours=1.5)) # Used to aviod purchasing any leveraged ETFs context.dont_buys = security_lists.leveraged_etf_list # trading days used for beta calculation context.lookback = 150 # Current allocation per asset context.pct_per_asset = 0 context.index = symbol('SPY') # attach the pipeline attach_pipeline(make_pipeline(context), 'pipeline')
def initialize(context): """ Called once at the start of the algorithm. """ # current position, 0 stands for empty exposure, 1 stands for full exposure context.pos = 0 # Rebalance every day, 1 hour after market open. schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(hours=2)) # 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(my_pipeline(context), 'my_pipeline')
def initialize(context): # Define the instruments in the portfolio: context.sidsLongVol = {sid(38054): +1.0} context.sidsShortVol = {sid(40516): +1.0} context.sidsShortSPY = {sid(22887): +1.0} context.sidsLongSPY = {sid(8554): +1.0} context.spy = symbol('SPY') context.hedge = symbol('IWM') context.vxx = symbol('VXX') context.epsilon = .01 context.ivts=[] context.ivts_medianfiltered = [] pipe = Pipeline() attach_pipeline(pipe, 'vix_pipeline') pipe.add(GetVol(inputs=[cboe_vix.vix_close]), 'vix') pipe.add(GetVol(inputs=[cboe_vxv.close]), 'vxv') vxstUrl = 'http://www.cboe.com/publish/scheduledtask/mktdata/datahouse/vxstcurrent.csv' vx1Url = 'http://www.quandl.com/api/v1/datasets/CHRIS/CBOE_VX1.csv' vx2Url = 'http://www.quandl.com/api/v1/datasets/CHRIS/CBOE_VX2.csv' fetch_csv(vxstUrl, symbol='VXST', skiprows=3,date_column='Date', pre_func=addFieldsVXST) fetch_csv(vx1Url, date_column='Trade Date',date_format='%Y-%m-%d', symbol='v1', post_func=rename_col) fetch_csv(vx2Url, date_column='Trade Date',date_format='%Y-%m-%d', symbol='v2', post_func=rename_col) # Calculating the contango ratio of the front and second month VIX Futures settlements context.threshold = 0.90 #contango ratio threshold schedule_function(ordering_logic,date_rule=date_rules.every_day(),time_rule=time_rules.market_open(hours=0, minutes=1)) # Rebalance every day, 1 hour after market open. context.wait_trigger=False context.vixpipe = None start_date = context.spy.security_start_date end_date = context.spy.security_end_date context.algo_hist={} context.returns_df = pd.DataFrame() # Get the dates when the market closes early: context.early_closes = get_early_closes(start_date, end_date).date context.slopes = Series() context.betas = Series()
def initialize(context): log.info("here") context.x = 100 # Create, register and name a pipeline in initialize. pipe = Pipeline() attach_pipeline(pipe, 'example') # Construct a simple moving average factor and add it to the pipeline. simple_return = Returns(inputs=[USEquityPricing.close], window_length=365) pipe.add(simple_return, 'simple_return') # Set a screen on the pipelines to filter out securities. pipe.set_screen(simple_return > 1.0) schedule_function(func=rebalance, date_rule = date_rules.every_day(), time_rule = time_rules.market_open(hours = 1))
def initialize(context): set_slippage(slippage.VolumeShareSlippage(volume_limit=0.025, price_impact=0.1)) set_commission(commission.PerShare(cost=0.0075, min_trade_cost=1)) attach_pipeline(make_pipeline(), 'long_short_factors') # Schedule my rebalance function schedule_function(func=rebalance, date_rule=date_rules.month_start(), time_rule=time_rules.market_open(hours=1,minutes=30), half_days=True) # record my portfolio variables at the end of day schedule_function(func=recording_statements, date_rule=date_rules.every_day(), time_rule=time_rules.market_close(), half_days=True)
def initialize(context): # Create, register and name a pipeline in initialize. pipe = Pipeline() attach_pipeline(pipe, 'dollar_volume_10m_pipeline') # Construct a 100-day average dollar volume factor and add it to the pipeline. dollar_volume = AverageDollarVolume(window_length=100) pipe.add(dollar_volume, 'dollar_volume') #Create high dollar-volume filter to be the top 2% of stocks by dollar volume. high_dollar_volume = dollar_volume.percentile_between(99, 100) # Set the screen on the pipelines to filter out securities. pipe.set_screen(high_dollar_volume) context.dev_multiplier = 2 context.max_notional = 1000000 context.min_notional = -1000000 context.days_traded = 0 schedule_function(func=process_data_and_order, date_rule=date_rules.every_day())
def initialize(context): context.long_leverage = 1.0 context.short_leverage = -1.0 context.spy = sid(8554) attach_pipeline(make_pipeline(), 'ranking_example') # Used to avoid purchasing any leveraged ETFs context.dont_buys = security_lists.leveraged_etf_list # Schedule my rebalance function schedule_function(func=rebalance, date_rule=date_rules.month_start (days_offset=0), time_rule=time_rules.market_open(hours=0,minutes=30), half_days=True) # Schedule a function to plot leverage and position count schedule_function(func=record_vars, date_rule=date_rules.every_day(), time_rule=time_rules.market_close(), half_days=True)
def initialize(context): # Define context variables that can be accessed in other methods of # the algorithm. context.long_leverage = 0.5 context.short_leverage = -0.5 context.returns_lookback = 5 # Rebalance every Monday (or the first trading day if it's a holiday). # At 11AM ET, which is 1 hour and 30 minutes after market open. schedule_function(rebalance, date_rules.week_start(days_offset=0), time_rules.market_open(hours = 1, minutes = 30)) # Record tracking variables at the end of each day. schedule_function(record_vars, date_rules.every_day(), time_rules.market_close(minutes=1)) # Create and attach our pipeline (dynamic stock selector), defined below. attach_pipeline(make_pipeline(context), 'mean_reversion_example')
def initialize(context): context.lookback = 300 context.return_window = 60 context.long_leverage = 0.5 context.short_leverage = -0.5 context.reduce_correlation = True # There's bad data for this security so I ignore it context.ignores = [sid(7143)] schedule_function(rebalance, date_rule=date_rules.month_start(), time_rule=time_rules.market_open(minutes=20)) # Record tracking variables at the end of each day. schedule_function(record_vars, date_rules.every_day(), time_rules.market_close()) # Create our dynamic stock selector. attach_pipeline(pipeline(context), 'pipeline')
def initialize(context): # get data from pipeline data_pull = Data_Pull() attach_pipeline(data_pull, "Data") # filter out bad stocks for universe mask = filter_universe() data_pull.set_screen(mask) # set leverage ratios for longs and shorts context.long_leverage = 1.3 context.short_leverage = -0.3 # at the start of each moth, run the rebalancing function schedule_function(rebalance, date_rules.month_start(), time_rules.market_open(minutes=30)) # clean untradeable securities daily schedule_function(daily_clean, date_rule=date_rules.every_day(), time_rule=time_rules.market_close(minutes=30)) # record variables schedule_function(record_vars, date_rule=date_rules.every_day(), time_rule=time_rules.market_close()) pass
'shorts': shorts, 'factor_rank': factor_rank, 'reversion': reversion, 'sector': sector, 'market_beta': beta }, screen=long_short_screen ) return pipe def initialize(context): set_commission(commission.PerShare(cost=0.0, min_trade_cost=0)) set_slippage(slippage.VolumeShareSlippage(volume_limit=1, price_impact=0)) context.spy = sid(8554) attach_pipeline(make_pipeline(), 'long_short_equity_template') schedule_function(func=rebalance, date_rule=date_rules.month_start(), time_rule=time_rules.market_open(hours=0, minutes=30), half_days=True) schedule_function(func=recording_statements, date_rule=date_rules.every_day(), time_rule=time_rules.market_close(), half_days=True) def before_trading_start(context, data): context.pipeline_data = pipeline_output('long_short_equity_template') def recording_statements(context, data): record(num_positions=len(context.portfolio.positions))
def initialize(context): context.lookback = 252 # Period to calculate slope and draw down context.maxlever = 1.0 # Leverage context.profittake = 1.96 # 95% bollinger band for profit take context.minimumreturn = 0.1 # Entry if annualized slope is over this level context.maxdrawdown = 0.12 # Avoid security with too much drawdown context.market_impact = 0.2 # Max order is 10% of market trading volume context.weights = {} # Slope at the time of entry. 0 if not to trade context.drawdown = {} # Draw down at the time of entry context.shares = {} # Daily target share schedule_function(regression, date_rules.every_day(), time_rules.market_open(minutes=45)) schedule_function(trade, date_rules.every_day(), time_rules.market_open(minutes=90) schedule_function(trail_stop, date_rules.every_day(), time_rules.market_open(minutes=30)) # Run trailing stop and execution every 30 minutes. for m in range(1, 391): if m % 30 == 0: schedule_function(execute, date_rules.every_day(), time_rules.market_open(minutes = m)) # Create and attach our pipeline (top dollar-volume selector), defined below. attach_pipeline(high_dollar_volume_pipeline(), 'top_dollar_volume') def high_dollar_volume_pipeline(): # Create a pipeline object. pipe = Pipeline() # Create a factor for average dollar volume over the last 62 day (1 quarter equivalent). dollar_volume = AverageDollarVolume(window_length=62) pipe.add(dollar_volume, 'dollar_volume') # Define high dollar-volume filter to be the top 5% of stocks by dollar volume. high_dollar_volume = dollar_volume.percentile_between(95, 100) pipe.set_screen(high_dollar_volume) return pipe def before_trading_start(context, data): context.pipe_output = pipeline_output('top_dollar_volume') context.security_list = context.pipe_output.index # Calculate the slopes for different assetes def regression(context, data): prices = data.history(context.security_list, 'open', context.lookback, '1d') X=range(len(prices)) # Add column of ones so we get intercept A=sm.add_constant(X) for s in context.security_list: # Price movement sd = prices[s].std() # Price points to run regression Y = prices[s].values # If all empty, skip if isnan(Y).any(): continue # Run regression y = ax + b results = sm.OLS(Y,A).fit() (b, a) =results.params # a is daily return. Multiply by 252 to get annualized trend line slope slope = a / Y[-1] * 252 # Daily return regression * 1 year if slope > 0: dd = drawdown(Y) if slope < 0: dd = drawdown(-Y) # Currently how far away from regression line? delta = Y - (dot(a,X) + b) # Don't trade if the slope is near flat slope_min = max(dd, context.minimumreturn) # Max drawdown and minimum return # Current gain if trading gain = get_gain(context, s) # Exits if s in context.weights and context.weights[s] != 0: # Long but slope turns down, then exit if context.weights[s] > 0 and slope < 0: context.weights[s] = 0 log.info('v %+2d%% Slope turn bull %3s - %s' %(gain*100, s.symbol, s.security_name)) # Short but slope turns upward, then exit if context.weights[s] < 0 and 0 < slope: context.weights[s] = 0 log.info('^ %+2d%% Slope turn bear %3s - %s' %(gain*100, s.symbol, s.security_name)) # Profit take, reaches the top of 95% bollinger band if delta[-1] > context.profittake * sd and s in context.weights and context.weights[s] > 0: context.weights[s] = 0 log.info('//%+2d%% Long exit %3s - %s'%(gain*100, s.symbol, s.security_name)) # Profit take, reaches the top of 95% bollinger band if delta[-1] < - context.profittake * sd and context.weights[s] < 0: context.weights[s] = 0 log.info('\\%+2d%% Short exit %3s - %s' %(gain*100, s.symbol, s.security_name)) # Entry else: # Trend is up and price crosses the regression line if slope > slope_min and delta[-1] > 0 and delta[-2] < 0 and dd < context.maxdrawdown: context.weights[s] = slope context.drawdown[s] = slope_min log.info('/ Long a = %+.2f%% %3s - %s' %(slope*100, s.symbol, s.security_name),gain) # Trend is down and price crosses the regression line if slope < -slope_min and delta[-1] < 0 and delta[-2] > 0 and dd < context.maxdrawdown: context.weights[s] = slope context.drawdown[s] = slope_min log.info('\ Short a = %+.2f%% %3s - %s' %(slope*100, s.symbol, s.security_name)) def get_gain(context, s): if s in context.portfolio.positions: cost = context.portfolio.positions[s].cost_basis amount = context.portfolio.positions[s].amount price = context.portfolio.positions[s].last_sale_price if cost == 0: return 0 if amount > 0: gain = price/cost - 1 elif amount < 0: gain = 1 - price/cost else: gain = 0 return gain def trade(context, data): w = context.weights record(leverage_pct = context.account.leverage*100.) record(longs = sum(context.portfolio.positions[s].amount > 0 for s in context.portfolio.positions)) record(shorts = sum(context.portfolio.positions[s].amount < 0 for s in context.portfolio.positions)) positions = sum(w[s] != 0 for s in w) held_positions = [p for p in context.portfolio.positions if context.portfolio.positions[p].amount != 0] context.securities = context.security_list.tolist() + held_positions for s in context.securities: if s not in w: context.shares.pop(s,0) context.drawdown.pop(s,0) elif w[s] == 0: context.shares.pop(s,0) context.drawdown.pop(s,0) context.weights.pop(s,0) elif w[s] > 0: context.shares[s] = context.maxlever/positions elif w[s] < 0: context.shares[s] = -context.maxlever/positions def execute(context,data): open_orders = get_open_orders() for s in context.shares: if not data.can_trade(s) or s in open_orders: continue order_target_percent(s, context.shares[s]) # We are entering into position when slope exceeds the drawdown # If we experience the drawdown again, stop loss def trail_stop(context, data): print get_datetime() print 'Positions: %s' % str(context.portfolio.positions.keys()) prices = data.history(context.portfolio.positions.keys(), 'price', context.lookback, '1d') for s in context.portfolio.positions: if s not in context.weights or context.weights[s] == 0: context.shares[s] = 0 continue if s not in prices or s in get_open_orders(): continue gain = get_gain(context, s) if context.portfolio.positions[s].amount > 0: if drawdown(prices[s].values) > context.drawdown[s]: log.info('x %+2d%% Long stop loss %3s - %s' %(gain * 100, s.symbol, s.security_name)) context.weights[s] = 0 context.shares[s] = 0 elif context.portfolio.positions[s].amount < 0: if drawdown(-prices[s].values) > context.drawdown[s]: log.info('x %+2d%% Short stop loss %3s - %s' %(gain * 100, s.symbol, s.security_name)) context.weights[s] = 0 context.shares[s] = 0 # Reference http://stackoverflow.com/questions/22607324/start-end-and-duration-of-maximum-drawdown-in-python def drawdown(xs): if len(xs) == 0: return 0. i = np.argmax(np.maximum.accumulate(xs) - xs) # end of the period if len(xs[:i]) == 0: return 0. j = np.argmax(xs[:i]) # start of period return abs((xs[i] - xs[j]) / xs[j])