Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- This is a template algorithm on Quantopian for you to adapt and fill in.
- """
- from quantopian.algorithm import attach_pipeline, pipeline_output
- from quantopian.pipeline import Pipeline
- from quantopian.pipeline.data.builtin import USEquityPricing
- from quantopian.pipeline.factors import AverageDollarVolume, SimpleMovingAverage, RSI, BollingerBands, MovingAverageConvergenceDivergenceSignal
- from quantopian.pipeline.filters.morningstar import Q500US, Q1500US
- import talib
- import numpy as np
- import pandas as pd
- def initialize(context):
- """
- Called once at the start of the algorithm.
- """
- context.toTradeLong = []
- context.toTradeShort = []
- # Rebalance every day, 1 hour after market open.
- schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(minutes=10))
- # 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(make_pipeline(), 'my_pipeline')
- def make_pipeline():
- """
- A function to create our dynamic stock selector (pipeline). Documentation on
- pipeline can be found here: https://www.quantopian.com/help#pipeline-title
- """
- # Base universe set to the Q500US
- base_universe = Q1500US()
- # Factor of yesterday's close price.
- lower, middle, upper = BollingerBands(mask=base_universe, window_length=22, k=3)
- bbScreen = USEquityPricing.close.latest > upper or USEquityPricing.close.latest < lower
- upperFilter = (USEquityPricing.close.latest > upper)
- pipe = Pipeline(
- screen = bbScreen,
- columns = {
- 'upper': upper,
- 'lower': lower,
- 'upFilter': upperFilter
- }
- )
- return pipe
- def before_trading_start(context, data):
- """
- Called every day before market open.
- """
- context.output = pipeline_output('my_pipeline')
- # These are the securities that we are interested in trading each day.
- context.security_list = context.output.index
- i = 0
- for index in context.security_list:
- if context.output[index]['upFilter'] == True:
- i = i+1
- print i
- def my_assign_weights(context, data):
- """
- Assign weights to securities that we want to order.
- """
- pass
- def my_rebalance(context,data):
- """
- Execute orders according to our schedule_function() timing.
- """
- pass
- def my_record_vars(context, data):
- """
- Plot variables at the end of each day.
- """
- pass
- def handle_data(context,data):
- """
- Called every minute.
- """
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement