Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.87 KB | None | 0 0
  1. from quantopian.algorithm import attach_pipeline, pipeline_output
  2. from quantopian.pipeline import Pipeline
  3. from quantopian.pipeline import CustomFactor
  4. from quantopian.pipeline.data.builtin import USEquityPricing
  5. from quantopian.pipeline.factors import SimpleMovingAverage
  6. from quantopian.pipeline.data import morningstar
  7. import numpy as np
  8.  
  9. def offset_sma_factor(days, **kwargs):  
  10.     kwargs['window_length'] += days  
  11.     class OffsetSMA(CustomFactor):  
  12.         def compute(self, today, assets, out, values):  
  13.             out[:] = np.nanmean(values[:-days], axis=0)  
  14.     return OffsetSMA(**kwargs)  
  15.  
  16. def initialize(context):
  17.     context.security = symbol('SPY')
  18.     pipe = Pipeline()
  19.     pipe = attach_pipeline(pipe, name='co_pipeline')
  20.     sma_50 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=50)
  21.     sma_200 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=200)
  22.     sma_50_1day_ago = offset_sma_factor(1, inputs=[USEquityPricing.close], window_length=50)
  23.     sma_200_1day_ago = offset_sma_factor(1, inputs=[USEquityPricing.close], window_length=200)
  24.    
  25.     current_co = (sma_50 > sma_200)
  26.     past_not_co = (sma_50_1day_ago < sma_200_1day_ago);
  27.    
  28.     pipe.add(sma_50, 'sma_50')
  29.     pipe.add(sma_200, 'sma_200')
  30.     pipe.add(sma_50_1day_ago, 'sma_50_1day_ago')
  31.     pipe.add(sma_200_1day_ago, 'sma_200_1day_ago')
  32.     pipe.set_screen(current_co & past_not_co)
  33.    
  34. def before_trading_start(context, data):
  35.     # get the output of your pipeline
  36.     context.output = pipeline_output('co_pipeline').fillna(0)
  37.    
  38.     context.etf_list = [sid(24), sid(5061)]
  39.  
  40.     list = context.output[context.output.index.isin(context.etf_list)]
  41.     context.stock_list = list.sort(ascending=True).iloc[:2]
  42.     context.assets = [context.stock_list]  
  43.  
  44. def handle_data(context, data):
  45.    
  46.     co_output = context.assets
  47.     log.info(co_output)
  48.    
  49.     average_price = data[context.security].mavg(5)
  50.     current_price = data[context.security].price
  51.    
  52.     cash = context.portfolio.cash
  53.    
  54.     if current_price > 1.01*average_price and cash > current_price:
  55.        
  56.         # Need to calculate how many shares we can buy
  57.         number_of_shares = int(cash/current_price)
  58.        
  59.         # Place the buy order (positive means buy, negative means sell)
  60.         order(context.security, +number_of_shares)
  61.         log.info("Buying %s" % (context.security.symbol))
  62.        
  63.  
  64.     elif current_price < average_price:
  65.        
  66.         # Sell all of our shares by setting the target position to zero
  67.         order_target(context.security, 0)
  68.         log.info("Selling %s" % (context.security.symbol))
  69.    
  70.     # You can use the record() method to track any custom signal.
  71.     # The record graph tracks up to five different variables.
  72.     # Here we record the Apple stock price.
  73.     record(stock_price=data[context.security].price)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement