Advertisement
Guest User

Untitled

a guest
Jan 15th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.77 KB | None | 0 0
  1. """
  2. This is a template algorithm on Quantopian for you to adapt and fill in.
  3. """
  4. from quantopian.algorithm                    import attach_pipeline, pipeline_output
  5. from quantopian.pipeline                     import Pipeline
  6. from quantopian.pipeline.data.builtin        import USEquityPricing
  7. from quantopian.pipeline.factors             import AverageDollarVolume, SimpleMovingAverage, RSI, BollingerBands, MovingAverageConvergenceDivergenceSignal
  8. from quantopian.pipeline.filters.morningstar import Q500US, Q1500US
  9.  
  10. import talib
  11. import numpy  as np
  12. import pandas as pd
  13.  
  14. def initialize(context):
  15.     """
  16.    Called once at the start of the algorithm.
  17.    """  
  18.     context.toTradeLong  = []
  19.     context.toTradeShort = []
  20.     # Rebalance every day, 1 hour after market open.
  21.     schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(minutes=10))
  22.      
  23.     # Record tracking variables at the end of each day.
  24.     #schedule_function(my_record_vars, date_rules.every_day(), time_rules.market_close())
  25.      
  26.     # Create our dynamic stock selector.
  27.     attach_pipeline(make_pipeline(), 'my_pipeline')
  28.          
  29. def make_pipeline():
  30.     """
  31.    A function to create our dynamic stock selector (pipeline). Documentation on
  32.    pipeline can be found here: https://www.quantopian.com/help#pipeline-title
  33.    """
  34.    
  35.     # Base universe set to the Q500US
  36.     base_universe = Q1500US()
  37.  
  38.     # Factor of yesterday's close price.
  39.     lower, middle, upper = BollingerBands(mask=base_universe, window_length=22, k=3)  
  40.    
  41.     bbScreen = USEquityPricing.close.latest > upper or USEquityPricing.close.latest < lower
  42.    
  43.     upperFilter = (USEquityPricing.close.latest > upper)
  44.    
  45.     pipe = Pipeline(
  46.         screen = bbScreen,
  47.         columns = {
  48.             'upper': upper,
  49.             'lower': lower,
  50.             'upFilter': upperFilter
  51.         }
  52.     )
  53.     return pipe
  54.  
  55. def before_trading_start(context, data):
  56.     """
  57.    Called every day before market open.
  58.    """
  59.     context.output = pipeline_output('my_pipeline')
  60.  
  61.     # These are the securities that we are interested in trading each day.
  62.     context.security_list = context.output.index
  63.     i = 0
  64.     for index in context.security_list:
  65.         if context.output[index]['upFilter'] == True:
  66.             i = i+1
  67.    
  68.     print i
  69.      
  70. def my_assign_weights(context, data):
  71.     """
  72.    Assign weights to securities that we want to order.
  73.    """
  74.     pass
  75.  
  76. def my_rebalance(context,data):
  77.     """
  78.    Execute orders according to our schedule_function() timing.
  79.    """
  80.     pass
  81.  
  82. def my_record_vars(context, data):
  83.     """
  84.    Plot variables at the end of each day.
  85.    """
  86.     pass
  87.  
  88. def handle_data(context,data):
  89.     """
  90.    Called every minute.
  91.    """
  92.     pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement