Don’t bother just to be better than your contemporaries or predecessors. Try to be better than yourself.
Basic Concepts
- Algorithm Trading: A trading algorithm is a computer program that defines a set of rules for buying and selling assets.
Research Environment: A Jupyter Notebook Environment in Quantopian. And you can use this environment to access and analyze historical datasets available in the platform.
1
from quantopian.research import prices, symbols,returns
Alternative Data: In Quantopian, besides pricing and volume data, it also includes fundamental, stock sentiment analysis, macroeconomic indicators and so on. For the stock sentiment analysis, you can use the sentimental data from the platform called PsychSignal
1
2
3
4
5
6from quantopian.pipeline.data.psychsignal import (
aggregated_twitter_withretweets_stocktwits, #Psychsignal data from Twitter and Stocktwits.
stocktwits, #Psychsignal data from Stocktwits.
twitter_noretweets, #Psychsignal data from Twitter (no retweets).
twitter_withretweets, #Psychsignal data from Twitter (with retweets).
)Backtesting: Measure the algorithm’s performance by simulating the realistic conditions over historical data.
API Glossary (API Reference)
Pipeline API: A tool for wrangling data from multiple sources. You can select assets, rank assets as well as get the allocation of the portfolio.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40# Pipeline class
from quantopian.pipeline import Pipeline
# include a reference to a dataset
from quantopian.pipeline.data import USEquityPricing
# include the sentimental data set
from quantopian.pipeline.data.psychsignal import stocktwits
# include a build in moving average
from quantopian.pipeline.factors import SimpleMovingAverage
# Import built-in trading universe for screening and selecting proper assets.
from quantopian.pipeline.experimental import QTradableStocksUS
# Import run_pipeline method
from quantopian.research import run_pipeline
def make_pipeline():
# Create a reference to our trading universe
base_universe = QTradableStocksUS()
# Create and return an empty Pipeline
close_price = USEquityPricing.close.latest
# Calculate 3 day average of bull_minus_bear scores
sentiment_score = SimpleMovingAverage(
inputs=[stocktwits.bull_minus_bear],
window_length=3,
)
# Return Pipeline containing latest closing price
return Pipeline(
columns={
'close_price': close_price,
'sentiment_score': sentiment_score,
},
screen=base_universe,
)
# The run_pipeline method takes three arguments: function: make_pipeline, and time_start, time_end
pipeline_output = run_pipeline(
make_pipeline(),
start_date='2013-01-01',
end_date='2013-12-31'
)Algorithm API and Core Functions: use to facilitate order scheduling and execution and also manage parameters in the trading algorithm. Below are some examples of the algorithm APIs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45# initialize(context)
# before_trading_start(context, data)
# schedule_function(func, day_rule, time_rule)
# The pipeline is able to process data streams and generate an output before market opened
# Attach a pipeline to an algorithm
# The argument of attach_pipeline is Pipeline object and the name of the pipeline
import quantopian.algorithm as algo
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.psychsignal import stocktwits
from quantopian.pipeline.factors import SimpleMovingAverage
from quantopian.pipeline.filters import QTradableStocksUS
def initialize(context):
algo.attach_pipeline( make_pipeline(), 'data_pipe')
algo.schedule_function(
rebalance,
date_rule=algo.date_rules.week_start(),
time_rule=algo.time_rules.market_open()
)
def before_trading_start(context, data):
# The pipeline_output function will take the pipeline name specify in the initialize stage
context.pipeline_data = algo.pipeline_output('data_pipe')
def make_pipeline():
'''
1. the notnull method and intersection with the tradable universe to get all the tradable assets
'''
base_universe = QTradableStocksUS()
sentiment_score = SimpleMovingAverage(
inputs=[stocktwits.bull_minus_bear],
window_length=3,
)
return Pipeline(
columns={
'sentiment_score': sentiment_score,
},
screen=(
base_universe
& sentiment_score.notnull()
)
)Optimize API: The Optimize API hides the complexity of optimization problems and just provide a high level concepts such as “maximize expected returns” and “constrain sector exposure”. You can use the Optimize API by providing Objective and Constraint objects to the API function.
1
2
3
4
5
6
7
8
9# Import Optimize API module
import quantopian.optimize as opt
# Provide the Objective and Constraint objects to the calculate_optimal_portfolio function
objective = opt.MaximizeAlpha(expected_returns)
constraints = [
opt.MaxGrossExposure(W_max),
opt.PositionConcentration(min_weights, max_weights),
]
optimal_weights = opt.calculate_optimal_portfolio(objective, constraints)There are three functions:
- calculate_optimal_portfolio(): calculate the optimization, but not place orders.
- order_optimal_portfolio(): on top of the calculate_optimal_portfolio function, it can place orders.
- run_optimization(): return an OptimizationResult object.
- Risk API: