0% found this document useful (0 votes)
223 views

First Project Report Algorithmic Trading Strategy

This document provides a summary of a trading algorithm developed by Juan Camilo Monterrosa Sanchez. The algorithm uses Quantopian libraries and APIs to define constraints, pipelines for data analysis, and portfolio optimization. Key aspects of the algorithm include rebalancing daily based on the previous day's closing prices, managing risk exposure using the Quantopian risk model, and backtesting to evaluate performance against contest criteria.

Uploaded by

juans_502
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
223 views

First Project Report Algorithmic Trading Strategy

This document provides a summary of a trading algorithm developed by Juan Camilo Monterrosa Sanchez. The algorithm uses Quantopian libraries and APIs to define constraints, pipelines for data analysis, and portfolio optimization. Key aspects of the algorithm include rebalancing daily based on the previous day's closing prices, managing risk exposure using the Quantopian risk model, and backtesting to evaluate performance against contest criteria.

Uploaded by

juans_502
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

First project report - algorithmic trading strategy

Juan Camilo Monterrosa Sanchez


November 2018

1 Description of the trading algorithm:


Below you can find the different libraries and classes extracted from Quantopia
for the execution of the algorithm on the datasets available in the platform:
import quantopian . a l g o r i t h m as a l g o
import quantopian . o p t i m i z e as opt
from quantopian . p i p e l i n e import P i p e l i n e
from quantopian . p i p e l i n e . data . b u i l t i n import USEquityPricing
from quantopian . p i p e l i n e . f i l t e r s import QTradableStocksUS
from quantopian . p i p e l i n e . e x p e r i m e n t a l
import r i s k l o a d i n g p i p e l i n e
The Algorithm API provides functions that facilitates the scheduling and exe-
cution of orders, and allow to initialize and manage parameters in the algorithms.

As part of the functions that Algorithm API incorporates, ’initialize’ is called


exactly once when the algorithm starts running and requires ’context’ as input.
Any parameter initialization and one-time startup logic should go here.

The Algorithm need to specify the list of constraints to satisfy the target
portfolio. Defining some threshold values in ’initialize’ and storing them in the
’context’ variable.

On Quantopian, algorithms can trade equities between 9:30AM-4PM Eastern


Time on regular trading days, following the NYSE trading calendar. The algo-
rithm will run much faster if it does not have to work every single minute for
which schedule function allows to execute custom functions at specified dates
and times.

Also, the Pipeline API is a powerful tool for cross-sectional analysis of asset
data. It allows us to define a set of calculations on multiple data inputs and
analyze a large amount of assets at a time. By using the functions contained
within the quantopian.pipeline class.

1
To use the data pipeline in an algorithm, must add a reference to it in the
algorithm’s ’initialize’ function. This is done using the attach pipeline method,
which requires two inputs: a reference to the Pipeline object (which is con-
structed using make pipeline), and a String name to identify it.

def i n i t i a l i z e ( context ) :
##### C o n s t r a i n t s t o s a t i s f y t he t a r g e t p o r t f o l i o #####
context . max leverage = 1.0
context . max pos size = 0.01
c o n t e x t . max turnover = 0.95

##### Rebalance e v e r y day , 1 hour a f t e r market open . #####


algo . schedule function (
rebalance ,
algo . d a t e r u l e s . every day ( ) ,
a l g o . t i m e r u l e s . market open ( hours =1) ,
)

##### Create a dynamic s t o c k s e l e c t o r . ######


algo . attach pipeline (
make pipeline () ,
’ data pipe ’
)

algo . attach pipeline (


risk loading pipeline () ,
’ risk pipe ’
)

Having a universe is fundamental for the development of the algorithm, which is


the foundation of the trading strategy. The universe has to be as large as possible,
but contain only assets that match the platform capabilities. The QTradable-
StocksUS is the largest universe that satisfies these requirements on Quantopian.
In addition, all of the constituents of the QTradableStocksUS are covered by the
risk model.

In this algorithm, the trading strategy uses the US equity pricing taken from
the last entry of the previous day’s portfolio as the inputs for the following
computational calculations. Through which the algorithm can obtain the alpha
scores for the corresponding optimization of the strategy.

def make pipeline ( ) :


##### Base u n i v e r s e s e t t o t h e QTradableStocksUS
− excludes s t o c k s that are l i q u i d or

2
d i f f i c u l t t o n e g o t i a t e #####

b a s e u n i v e r s e = QTradableStocksUS ( )

#s e n t i m e n t s c o r e = SimpleMovingAverage (
# i n p u t s =[ s t o c k t w i t s . b u l l m i n u s b e a r ] ,
# window length =3,
# mask=QTradableStocksUS ( )
#)

# Factor of yesterday ’ s c l o s e p r i c e .
y e s t e r d a y c l o s e = USEquityPricing . c l o s e . l a t e s t

return Pipeline (
columns={
’ close ’ : yesterday close ,
},
s c r e e n=b a s e u n i v e r s e
)

The algorithm gets the pipeline’s output in before trading start using the pipeline output
function, which takes the pipeline name that was specified in ’initialize’, and re-
turns the pandas DataFrame generated by the pipeline.

In addition to setting constraints on the target portfolio’s structure, wants


to limit its exposure to common risk factors that could have an impact on its
performance. Using the Quantopian’s Risk Model to manage the portfolio’s ex-
posure to common risk factors. The Risk Model calculates asset exposure to 16
different risk factors: 11 sector factors and 5 style factors.

d e f b e f o r e t r a d i n g s t a r t ( c o n t e x t , data ) :
##### Get p i p e l i n e and r i s k f a c t o r s o u t p u t s
and s t o r e them i n ’ c o n t e x t ’ #####

c o n t e x t . output = a l g o . p i p e l i n e o u t p u t ( ’ d a t a p i p e ’ )

context . r i s k f a c t o r b e t a s = algo . pipeline output ( ’ risk pipe ’ )

The Optimize API is defined in the Quantopian.optimize module: This is the


best way to move a portfolio from one state to another on Quantopian, which is
a suite of tools for defining and solving portfolio optimization problems directly
in terms of financial domain concepts. The Optimize API hides most of the
complex mathematics of portfolio optimization, allowing users to think in terms
of high-level concepts like “maximize expected returns” and “constrain sector

3
exposure” instead of abstract matrix products.

Having add a RiskModelExposure constraint to the portfolio optimization


logic. The constraint takes the data generated by the Risk Model and sets
a limit on the overall exposure of the target portfolio to each of the factors
included in the model.
d e f r e b a l a n c e ( c o n t e x t , data ) :

o b j e c t i v e = opt . MaximizeAlpha (
c o n t e x t . output . c l o s e
)

c o n s t r a i n p o s s i z e = opt . P o s i t i o n C o n c e n t r a t i o n . w i t h e q u a l b o u n d s (
−c o n t e x t . m a x p o s s i z e ,
context . max pos size
)

f a c t o r r i s k c o n s t r a i n t s = opt . e x p e r i m e n t a l . RiskModelExposure (
context . r i s k f a c t o r b e t a s ,
v e r s i o n=opt . Newest
)

##### Ensure l o n g and s h o r t books


a r e r o u g h l y t he same s i z e #####
d o l l a r n e u t r a l = opt . D o l l a r N e u t r a l ( )

m a x l e v e r a g e = opt . MaxGrossExposure ( c o n t e x t . m a x l e v e r a g e )

max turnover = opt . MaxTurnover ( c o n t e x t . max turnover )

##### Rebalance p o r t f o l i o u s i n g o b j e c t i v e
and l i s t o f c o n s t r a i n t s #####

order optimal portfolio (


o b j e c t i v e=o b j e c t i v e ,
c o n s t r a i n t s =[
max leverage ,
dollar neutral ,
constrain pos size ,
max turnover ,
factor risk constraints
]
)

4
2 Backtesting
In order to participate in the contest, algorithms need to meet a special set
of criteria. These criteria select for cross-sectional, long-short equity strategies
and are aligned with our allocation process. Algorithms are required to have the
following properties in order to participate in the contest:

• Positive returns.

• Leverage between 0.8x-1.1x.

• Low position concentration.

• Low beta-to-SPY.

• Mid-range turnover.

• Long and short positions.

• Trades liquid stocks.

• Low exposure to sector risk.

• Low exposure to style risk.

• Place orders with Optimize API.

5
6
3 Contest position:

You might also like