47
Python in algotrading lectures Hugo E. Ramirez [email protected] Facultad de Econom´ ıa. Universidad del Rosario Scipy Latam 2019 Hugo E. Ramirez [email protected] Python in algotrading lectures 1 / 47

Hugo E. Ramirez [email protected] › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Python in algotrading lectures

Hugo E. [email protected]

Facultad de Economıa. Universidad del RosarioScipy Latam

2019

Hugo E. Ramirez [email protected] Python in algotrading lectures 1 / 47

Page 2: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Outline

1 Why Python?

2 In the classroom

3 Trading/FinanceData CollectionTechnical AnalysisStrategiesBacktesting

4 Some Projects

Hugo E. Ramirez [email protected] Python in algotrading lectures 2 / 47

Page 3: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Why Python? I

Free and open sourceWidely used in Quant community. Python has an increasinguse among scientist and trading communities.High-level and general purpose programming language.Extensible and interoperable with other languages (Gnuplot).Has standard free libraries for finance (Ready to use!).Since it is not as complex as more structured languages (C++or Java) enables us to focus on the specific topic rather thanthe programming. Learning curve is much faster than otherused languages.Allows Object-Oriented, procedural and functionalprogramming.It offers the right balance between programming complexityand computational speed.Can be thought for many levels of computational knowledge.

Hugo E. Ramirez [email protected] Python in algotrading lectures 3 / 47

Page 4: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Some definitions I

Algorithmic trading: Means turning a trading idea into a tradingstrategy via an algorithm. The algorithmic trading strategy can beexecuted either manually or in an automated way.Backtesting: Test a trading strategy with historical data to checkwhether it will give good returns in real markets.Quantitative trading: This involves using advanced mathematicaland statistical models for creating and executing trading strategies.Automated trading: Completely automating the ordergeneration, submission, and the order execution process.HFT or high-frequency trading: Trading strategies can becategorized as low-frequency, medium-frequency andhigh-frequency strategies as per the holding time of the trades.High-frequency strategies are algorithmic strategies which getexecuted in an automated way in quick time, usually on asub-second time scale.

Hugo E. Ramirez [email protected] Python in algotrading lectures 4 / 47

Page 5: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Some definitions II

In Algorithmic trading, we follow four “simple” steps:1 Hypothesis. Market insight.2 Model. Mathematics.3 Validation. Backtesting and calibration.4 Analysis. Why it works

There are some known strategies: Portfolio optimisation, Pairstrading, Market making, trend following, Moving averagecrossover, HJB’s, Kalman filter signalling, and else.Some known algorithms to “make” money: Moving averages,Knight-Satchell-Tran process, Bull/Bear, Co-integration, Meanreverting, factor models, HJB mean reverting, stochastic volatility,and else.

Hugo E. Ramirez [email protected] Python in algotrading lectures 5 / 47

Page 6: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Financial/Computational motivation I

Capability to process Data: Nowadays it is not enough toconsider the end-of-day quotes for financial instruments andthere is a lot of information during the day (High-frequencytrading)Speed in analytics: The decisions for investors have to bemade very fast and thus large amounts of data have to beanalysed in short periods of time.Numerical solutions instead of analytical solutions: Since aremore readable and sometimes more accessible.Difficult or not possible analytical solutions to problems:∫ b

ae−x2dx , for a < b ∈ R

Trading is still very much an art rather than a science.

Hugo E. Ramirez [email protected] Python in algotrading lectures 6 / 47

Page 7: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Financial/Computational motivation II

Electronic trading has transformed the markets (more controland accessibility).A computerised system is now responsible for executingorders, by following a set of rules.The famous Capital Asset Pricing Model that measures astock’s beta is really just a linear regression equation. Noreason for human power to do this.In trading, behavioural variables that can affect securityvalues. Such as herding, overconfidence, overreaction toearnings announcements, and momentum trading can all leadto (human) errors.

Hugo E. Ramirez [email protected] Python in algotrading lectures 7 / 47

Page 8: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Use in the classroom I

Anaconda + Spyder. Must be installed in your computer.Standard programming interaction.Anaconda + Jupyter. Must be installed in your computer.Allows to create documents executable with text/equations,code and graphs.Jupyter notebooks (jupyter.org, cocalc.com).Open-source web application that allows to create and sharedocuments that contain live code. You can also share andpublish online your documents, for public to view.kaggle (www.kaggle.com). Collaboration community withcustomisable Jupyter Notebooks environment that access freeonline GPU’s. Contains public notebooks with codes anddata. Courses and even competitions.

Hugo E. Ramirez [email protected] Python in algotrading lectures 8 / 47

Page 9: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Use in the classroom II

google Colaboratory (colab.research.google.com). Freeenvironment of Jupyter Notebook. Does not require muchconfiguration. Executed on the cloud with powerfulcomputational resources (GPU and TPU).

Hugo E. Ramirez [email protected] Python in algotrading lectures 9 / 47

Page 10: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Pandas I

It is always important to import the desired libraries, we mostly usepandas (“PANel DAta”) and numpy (“NUMerical PYthon”) Infinance data comes in forms of time series and/or tables (Similarto DB tables or excel spreadsheets). In Python pandas:

The first corresponds to Series. The primary building blockof pandas and represents a one-dimensional array, which maybe indexed.The second is DataFrame. Usually in finance these areindexed by dates.

The to create an object of type Series we may use an array,dictionary or else, or create it as an empty object. For example:

Hugo E. Ramirez [email protected] Python in algotrading lectures 10 / 47

Page 11: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Pandas II

1 impor t pandas as pd2 impor t numpy as np3 # Create a S e r i e s w i th d e f a u l t i n d e x system4 s = pd . S e r i e s ( np . random . randn (20) )5 # Create a S e r i e s w i th d i f f e r e n t i n d e x than d e f a u l t6 s2 = pd . S e r i e s ( [ 3 . 2 , 2 . 7 , 4 . 5 ] , i n d ex =[ ’ Juan ’ , ’ Pedro ’ , ’

Maria ’ ] )7 #Or cou ld be c r e a t e d as d i c t i o n a r y8 s3 = pd . S e r i e s ({ ’ Juan ’ : 3 . 5 , ’ Andrea ’ : 3 . 5 , ’ Pedro ’ : 4 . 0 , ’

Maria ’ : 4 . 3 , ’ d e f a u l t ’ : np . nan })

These objects are always indexed. and operations are performedover indices.Series can only associate a single value with any given indexlabel, it has only limited ability to model multiple variables.DataFrame may be compared to a relational database table. Anexample of a DataFrame is:

Hugo E. Ramirez [email protected] Python in algotrading lectures 11 / 47

Page 12: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Pandas IIIName Age Height · · · Salary

index1 0.8 0.8 0.0 · · · 0.9index2 0.4 0.8 0.3 · · · 0.1index3 0.7 0.3 0.7 · · · 0.2

......

...... · · ·

...

and the next code creates some random Dataframe with five rowand four columns A, B, C, D

1 #Crea te a DataFrame2 np . random . seed (456)3 d f = pd . DataFrame ( np . random . randn (5 , 4) , columns =[ ’A ’ ,

’B ’ , ’C ’ , ’D ’ ] )4 p r i n t d f

Arithmetic operations may be made row-by-row orcolumn-by-column, and also many database operations such asSubFrames, re-indexation, group by, join, merge, progressive anddefault filling.Hugo E. Ramirez [email protected] Python in algotrading lectures 12 / 47

Page 13: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Data from .csv I

It is possible to read the data from a .csv file (with columns:Contract Name, Last Trade Date, Strike, Last Price, Bid, Ask,Change, % Change, Volume, Open Interest, Implied Volatility)

1 s p 5 0 0 o p t i o n s = pd . r e a d c s v ( ” sp500 OP . c sv ” , i n d e x c o l=’Contract Name ’ )

2 p r i n t s p 5 0 0 o p t i o n s [ [ ’ La s t P r i c e ’ , ’ Bid ’ , ’ Ask ’ , ’ S t r i k e ’] ] . head (3 )

Finally we show some useful examples on how to obtain some datafrom the Dataframe. Specifically, note the use of loc, iloc,ix, at, iat.

Hugo E. Ramirez [email protected] Python in algotrading lectures 13 / 47

Page 14: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Data from .csv II

1 ## A s p e c i f i c row By name2 s p 5 0 0 o p t i o n s . l o c [ ’ SPX180216C01840000 ’ ]3 #By i n d e x4 s p 5 0 0 o p t i o n s . i l o c [ [ 0 , 2 ] ]5 s p 5 0 0 o p t i o n s . a t [ ’ SPX180216C02590000 ’ , ’ Volume ’ ]6 s p 5 0 0 o p t i o n s . i a t [ 1 0 , 2 ]7 # They a l s o accep t boo l ean s e l e c t i o n s e n t e n c e s .8 s p 5 0 0 o p t i o n s . Volume>1009 # or f i l t r a t e by the e x p r e s i o n

10 s p 5 0 0 o p t i o n s [ s p 5 0 0 o p t i o n s . Volume>100]11 # More complex e x p r e s i o n s a l s o ho ld12 s p 5 0 0 o p t i o n s [ ( s p 5 0 0 o p t i o n s . Volume>100) & ( s p 5 0 0 o p t i o n s .

Ask<500) ] [ [ ’ Volume ’ , ’ La s t P r i c e ’ , ’ I m p l i e dV o l a t i l i t y ’ ] ]

Hugo E. Ramirez [email protected] Python in algotrading lectures 14 / 47

Page 15: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Online data I

Among the various ways to do so, we will use a framework createdfor python. Specifically, we use Pandas-DataReader because of itssimplicity, and because is similar to others. Our main source ofdata will be yahoo finance. Although there are many others, evenBloomberg and Eikon provide API’s to connect with python.In our setting the library we use to retrieve information is thepandas-Datareader. This library does not come included inpython’s standard installation, so you may need to install it. Youcan install it via the command line or terminal by typing

>> pip install pandas-datareader

Or if you have anaconda installed, in the command line or terminaltype:

>> conda install -c anaconda pandas-reader

Hugo E. Ramirez [email protected] Python in algotrading lectures 15 / 47

Page 16: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Online data II

Remark. If you use codes from internet please be aware that thereis a deprecated pandas-datareader, i.e. import pandas.io.dataas web. For more information go to the url: https://pydata.github.io/pandas-datareader/devel/remote_data.html.So the next thing to do is import the datareader library. There is avery useful library when it comes to manipulating dates and is thedatetime.

1 impor t p a n d a s d a t a r e a d e r . data as data2 impor t da te t ime3 # We s e t an i n i t i a l an t e r m i n a l date f o r our

i n f o r m a t i o n4 s t a r t = date t ime . da te t ime (2015 , 1 , 1)5 end = date t ime . da te t ime (2015 , 12 , 30)

So to retrieve the financial information of Microsoft between thedates start and end, we type

Hugo E. Ramirez [email protected] Python in algotrading lectures 16 / 47

Page 17: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Online data III

1 msft = data . DataReader ( ”MSFT” , ’ yahoo ’ , s t a r t , end )2 p r i n t msft [ : 3 ]

Similarly for Apple1 a ap l = data . DataReader ( ”AAPL” , ’ yahoo ’ , s t a r t , end )2 p r i n t aa p l [ : 3 ]

You may check the datareader documentation:Google finance does not work, it seems an update of the url,xml or cvs structure is needed.Quandl. The symbol names consist of two parts: DB nameand symbol name

1 symbol = ’ WIKI/AAPL ’ # or ’AAPL . US ’2 d f = data . DataReader ( symbol , ’ quand l ’ , ”2015−01−01”

, ”2015−01−05” )3 p r i n t d f . l o c [ ’ 2015−01−02 ’ ]

Hugo E. Ramirez [email protected] Python in algotrading lectures 17 / 47

Page 18: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Online data IVFRED (Federal Reserve Economic Data). We check the GrossDomestic Product

1 gdp = data . DataReader ( ”GDP” , ” f r e d ” , s t a r t , end )2 p r i n t gdp . i x [ ’ 2013−01−01 ’ ]3 # M u l t i p l e s e r i e s i n v o l v i n g Consumer P r i c e I ndex (

A l l I tems , Le s s Food and Energy ) :4 i n f l a t i o n = data . DataReader ( [ ”CPIAUCSL” , ”CPILFESL”

] , ” f r e d ” , s t a r t , end )5 i n f l a t i o n . head ( )

More information and specific API’s may be found in https://pydata.github.io/pandas-datareader/devel/readers/There are also alternative ways to fetch data, such as:

googlefinance.client, a python client for google finance api.Google Finance GET. Uses googlefinance.client.Quandl. Directly using quandl api (quandl.get(...)).IEX Finance.

Hugo E. Ramirez [email protected] Python in algotrading lectures 18 / 47

Page 19: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Online data V

Alpha vantage. Minute level datarsvp/fecon235. Searches in yahoo finance, and backups withgoogle finance.

Hugo E. Ramirez [email protected] Python in algotrading lectures 19 / 47

Page 20: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Graphical analysis I

In finance we are often interested in the prices of assets and itsreturns. One way to see the data is graphically. We crate afunction oget that retrieves all the tickers in a list and returns adataframe with the data indexed by (tckr,date).

1 t c k r s = [ ’AAPL ’ , ’MSFT ’ , ’ IBM ’ , ’F ’ , ’GM’ , ’TM’ , ’PEP ’ , ’KO ’]

2 t c k d a t a = oget ( t c k r s , ’ 2016−1−1 ’ , ’ 2018−1−1 ’ )

For example, to visualize the data using (importmatplotlib.pyplot as plt):

1 Close prices.

Hugo E. Ramirez [email protected] Python in algotrading lectures 20 / 47

Page 21: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Graphical analysis II

1 #Get the c l o s e p r i c e s2 a l l c l s p x = t c k d a t a [ [ ’ Adj C l o s e ’ ] ] . r e s e t i n d e x ( )3 # Organ i ze the data4 d a i l y p x = a l l c l s p x . p i v o t ( ’ Date ’ , ’ T i c k e r ’ , ’ Adj

C l o s e ’ )5 #p l o t j u s t one t i c k e r6 d a i l y p x [ ’AAPL ’ ] . p l o t ( )7 p l t . show ( )

Hugo E. Ramirez [email protected] Python in algotrading lectures 21 / 47

Page 22: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Graphical analysis III

2 Returns. We calculate the return as the percentage change:

Hugo E. Ramirez [email protected] Python in algotrading lectures 22 / 47

Page 23: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Graphical analysis IV

1 d r e t = d a i l y p x . pc t change ( )2 # Rep lace the NaN by 03 d r e t . f i l l n a (0 , i n p l a c e=True )4 d r e t [ ’MSFT ’ ] . p l o t ( )5 p l t . l e g end ( l o c =2)6 p l t . t i t l e ( ’ D a i l y r e t u r n s ’ )7 p l t . show ( )

Hugo E. Ramirez [email protected] Python in algotrading lectures 23 / 47

Page 24: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Graphical analysis V

3 Volume. We plot the prices and volumes trades

Hugo E. Ramirez [email protected] Python in algotrading lectures 24 / 47

Page 25: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Graphical analysis VI

1 top = p l t . s u b p l o t 2 g r i d ( ( 4 , 1 ) , (0 , 0) , rowspan =3,c o l s p a n =1)

2 top . p l o t ( d a i l y p x . index , d a i l y p x [ ’KO ’ ] , l a b e l=’ AdjC l o s e ’ )

3 p l t . t i t l e ( ’ Coca Cola Ad jus ted C lo s e P r i c e from 2016− 2018 ’ )

4 p l t . l e g end ( l o c =2)5 bottom = p l t . s u b p l o t 2 g r i d ( ( 4 , 1 ) , ( 3 , 0 ) , rowspan =1,

c o l s p a n =1)6 bottom . bar ( KOvol . index , KOvol )7 p l t . t i t l e ( ’ Coca Cola D a i l y Trad ing Volume ’ )8 p l t . g c f ( ) . s e t s i z e i n c h e s (12 ,8 )9 p l t . s u b p l o t s a d j u s t ( hspace =0.75)

10 p l t . show ( )

Hugo E. Ramirez [email protected] Python in algotrading lectures 25 / 47

Page 26: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Graphical analysis VII

Hugo E. Ramirez [email protected] Python in algotrading lectures 26 / 47

Page 27: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Graphical analysis VIII

4 Candlesticks. The candlestick charts are useful and mostlyused in finance to represent the open-high-low-close plots, andillustrates movements in the price over time.(frommatplotlib.finance import candlestick ohlc)

1 f i g , ax = p l t . s u b p l o t s ( )2 ax . x a x i s . s e t m a j o r l o c a t o r ( mondays )3 #ax . x a x i s . s e t m i n o r l o c a t o r ( a l l d a y s )4 ax . x a x i s . s e t m a j o r f o r m a t t e r ( w eek f o rm a t t e r )5 #ax . x a x i s . s e t m i n o r f o r m a t t e r ( dayFormatte r )6 c a n d l e s t i c k o h l c ( ax , tupleKO , width =0.6 , c o l o r u p=’ g ’

, co lo rdown=’ r ’ )7 p l t . show ( )

Hugo E. Ramirez [email protected] Python in algotrading lectures 27 / 47

Page 28: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Graphical analysis IX

There are many other ways to visualise the returns, among these:cumulative returns, histograms, Q-Q plots, Box and Whiskersand scatter plots.

Hugo E. Ramirez [email protected] Python in algotrading lectures 28 / 47

Page 29: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Strategies I

There are two broad categories for “predicting” movements in themarket: momentum strategies and mean-reversion strategies.Here we only show an example of:

Momentum. This type of trading focuses on stocks that aremoving in a specific direction on high volume, measuring therate of change in price changes. We continuously computeprice differences at fixed time intervals. Is known to workbetter in Bull (or rising) markets.

These strategies utilise moving averages (rolling means) of theclosing price of stocks. Notice that if moving averages (with largerwindows):

Are bellow the actual stock price, then it means a bull marketand the large windows rolling average acts a a floor for theprice.

Hugo E. Ramirez [email protected] Python in algotrading lectures 29 / 47

Page 30: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Strategies II

Are above the actual stock price, represents a resilience tokeep the price up.

There are some negative point when using the rolling windows tohave in mind:

It only tell you things about the past (If only the past couldpredict the future).With shorter windows we add more the noise into the signal.

1 t c k d a t a = odata ( ’SEP ’ , da t e t ime (2016 , 1 , 1) , da t e t ime(2018 , 1 , 1) )

2 c l s p x = t c k d a t a [ [ ’ Adj C l o s e ’ ] ]3 ##### C a l c u l a t e the moving a v e r a g e s ################4 c l s p x [ ’MA7 ’ ] = pd . r o l l i n g m e a n ( c l s p x [ ’ Adj C l o s e ’ ] , 7)5 c l s p x [ ’MA30 ’ ] = pd . r o l l i n g m e a n ( c l s p x [ ’ Adj C l o s e ’ ] , 30)6 c l s p x [ ’MA90 ’ ] = pd . r o l l i n g m e a n ( c l s p x [ ’ Adj C l o s e ’ ] , 90)7 c l s p x [ ’EWMA90 ’ ] = pd . ewma( c l s p x [ ’ Adj C l o s e ’ ] , span =90)

Hugo E. Ramirez [email protected] Python in algotrading lectures 30 / 47

Page 31: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Crossovers I

A crossover is a simple strategy that changes its position wheneverthe moving average crosses the price of the asset.Let us explain with the following example:

2017-022017-03

2017-042017-05

2017-062017-07

2017-082017-09

2017-102017-11

2017-12

Date

38

39

40

41

42

43

44 Adj CloseMA90EWMA90

Sell

Buy

Hugo E. Ramirez [email protected] Python in algotrading lectures 31 / 47

Page 32: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Crossovers II

Let pt be the price at t and et be its EWMA and notice that:If pt crosses et from below, we close any short position and golong (buy) one unit of the asset.If pt crosses et from above, we close any existing long positionand go short (sell) one unit of the asset.

This is translated into a strategy in that:

wt ={

1 If pt > et ≡ pt − et > 0−1 If pt < et ≡ pt − et < 0

and therefore, we may assign

wt = sgn(pt − et)

That is

Hugo E. Ramirez [email protected] Python in algotrading lectures 32 / 47

Page 33: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Crossovers III

1 c l o s e p x = c l s p x [ ’ Adj C l o s e ’ ]2 ewma px = c l s p x [ ’EWMA90 ’ ]3 s i g n a l p x = ewma px − c l o s e p x4 t r a d e p o s = s i g n a l p x . app l y ( np . s i g n )

Since we calculate the signal for the same day at close (i.e. closingprice) the execution of the signal is going to happen the next dayand therefore we shift the data one day.

1 t r a d e p o s = t r a d e p o s . s h i f t ( 1 )

The next step is to calculate the daily (log-)returns and apply ourtrading strategy:

logrt = ln( pt

pt−1

)1 l o g r e t = np . l o g ( c l o s e p x ) . d i f f ( )2 r s = t r a d e p o s ∗ l o g r e t

Hugo E. Ramirez [email protected] Python in algotrading lectures 33 / 47

Page 34: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Crossovers IV

And calculate the cumulative returns

crtk = exp{ k∑

i=1logrti

}− 1

1 cum ret = r s . cumsum ( )2 n o s t c u m r e t = l o g r e t . cumsum ( )3 # And r e l a t i v e r e t u r n s4 c u m r e l r e t = np . exp ( cum ret ) − 15 n o s t c u m r e l r e t = np . exp ( n o s t c u m r e t ) − 1

Finally we plot the returns and check

Hugo E. Ramirez [email protected] Python in algotrading lectures 34 / 47

Page 35: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Crossovers V

1 f i g = p l t . f i g u r e ( )2

3 ax = f i g . a d d s u b p l o t ( 2 , 1 , 1 )4 ax . p l o t ( cum ret . index , cum ret , l a b e l=’Momentum ’ )5 ax . p l o t ( n o s t c u m r e t . index , no s t cum re t , l a b e l=’ buy

and ho ld ’ )6 ax . s e t y l a b e l ( ’ Cumulat i ve log−r e t u r n s ’ )7 ax . l e g end ( l o c=’ b e s t ’ )8 ax . g r i d ( )9

10 ax = f i g . a d d s u b p l o t ( 2 , 1 , 2 )11 ax . p l o t ( c u m r e l r e t . index , 100∗ c u m r e l r e t , l a b e l= ’

Momentum ’ )12 ax . p l o t ( n o s t c u m r e l r e t . index , 100∗ n o s t c u m r e l r e t ,

l a b e l=’ buy and ho ld ’ )13 ax . s e t y l a b e l ( ’ Tota l r e l a t i v e r e t u r n s (%) ’ )14 ax . l e g end ( l o c=’ b e s t ’ )15 ax . g r i d ( )16 p l t . show ( )

Hugo E. Ramirez [email protected] Python in algotrading lectures 35 / 47

Page 36: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Crossovers VI

Hugo E. Ramirez [email protected] Python in algotrading lectures 36 / 47

Page 37: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Crossovers VII

Hugo E. Ramirez [email protected] Python in algotrading lectures 37 / 47

Page 38: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Zipline I

Previously we simulated trading based on a historical stock data,but these examples were naive in that they omit many features ofreal trading such as transaction fees, commissions,slippage(Different transaction price than quoted price) and manyothers.There is a library called Zipline, which provides many of thisfeatures into our model and is used in the web based algotradingplatform quantopian. This library provides the backtestingfeature useful to determine the effectiveness of the strategy on theactual market data.Here we present the basics for backtesting. Remember that classicinstallation of python complements is made by:

> pip install zipline

or

Hugo E. Ramirez [email protected] Python in algotrading lectures 38 / 47

Page 39: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Zipline II

> conda install -c Quantopian zipline

More information may be found at:http://www.zipline.io/install.html

To use the zipline library we usually create a Class that inheritsof the TradingAlgorithm subclass of zipline. We have threeways of running programs having inherited the TradingAlgorithmclass:

Directly by command line. If we want to run our program fromcommand line first we need to ingest the data to back-test ouralgorithm, and then run using the zipline command, that is> zipline ingest -b quantopian-quandl> zipline run -f my_zipline_algorithm.py ...--start 2017-1-1 --end 2017-10-10 -o myrecords.pickle

Hugo E. Ramirez [email protected] Python in algotrading lectures 39 / 47

Page 40: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Zipline III

This command tells zipline to run the filemy zipline algorithm.py from 2017− 1− 1 to 2017− 10− 10and record the output in myrecords.pickle. Off course, youwill need a pickle reader to check this data.other option to run the algorithm may be consulted by using> zipline run --help

In IPython (via Jupyter or else) just by adding the command1 %%z i p l i n e −−s t a r t 2017−1−1 −−end 2018−1−1

Which tells IPython this is a zipline algorithm, and that itshould run it from 2017− 1− 1 to 2018− 1− 1.With python normally, we must explicitly create an object andcall the run function. Although since there have been changesin the API we must tweak somewhat the traditional ingestingof data.

Hugo E. Ramirez [email protected] Python in algotrading lectures 40 / 47

Page 41: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

TradingAlgorithm I

Every zipline algorithm consists of at least two functions:1 initialize(context). This method is called to start the

trading algorithm2 handle data(context, data). This method is called at

each simulated trading period (i.e. day-to-day). Here we mustanalyse and decide how to trade.

wherecontext is a namespace used to store variables needed fromiteration to iteration (i.e. day-to-day trading).data contains the current trading bar with ohlc and volume inour setting.

Note. This functions are mandatory to implement since these areinherited from TradingAlgorithm.The basic structure of the .py file is

Hugo E. Ramirez [email protected] Python in algotrading lectures 41 / 47

Page 42: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

TradingAlgorithm II

1 from z i p l i n e . a p i impor t o rde r , r e co rd , symbol2

3 de f i n i t i a l i z e ( c o n t e x t ) :4 # Here you w r i t e your i n i t i a l i s i n g code5 pas s6

7 de f h a n d l e d a t a ( contex t , data ) :8 # Here you w r i t e your hand l e code9 pas s

There are some important functions to have in mind:1 order(asset, amount,...). Place an order of amount of

shares of the asset asset. If amount is positive means buyand negative sell. (Similar to order value)

2 order target(asset, target). Place an order to adjust aposition to a target number of shares, so the final amount ofshares after the order is executed is target. (Similarorder target value)

Hugo E. Ramirez [email protected] Python in algotrading lectures 42 / 47

Page 43: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

TradingAlgorithm III

3 symbol(symbol str). Lookup an Equity by its ticker symbol.4 record(self, *args, **kwargs). Track and record values

each day.More functions and more detailed explanation may be found in theAPI documentation: http://www.zipline.io/

Hugo E. Ramirez [email protected] Python in algotrading lectures 43 / 47

Page 44: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Zipline simple Example I

As a simplest example we create an algorithm to buy 1 asset ofApple constantly during the period of trading.In this example we first create our Class and to do so, we mustfirst import some libraries

1 from z i p l i n e . a p i impor t o rde r , r e co rd , symbol2 from z i p l i n e impor t Trad ingA lgo r i thm

To create the Class we use the class command and call itBuyApple, Next we add our two functions and tell them to printthe activity.

1 c l a s s BuyApple ( Trad ingA lgo r i thm ) :2

3 de f i n i t i a l i z e ( c o n t e x t ) :4 p r i n t ( ”−−−> i n i t i a l i z e ” )5 c o n t e x t . a s s e t = ’AAPL ’6 p r i n t ( c o n t e x t )7 p r i n t ( ”<−−− i n i t i a l i z e ” )8

Hugo E. Ramirez [email protected] Python in algotrading lectures 44 / 47

Page 45: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Zipline simple Example II

9 de f h a n d l e d a t a ( s e l f , c o n t e x t ) :10 p r i n t ( ”−−−> h a n d l e d a t a ” )11 p r i n t ( c o n t e x t )12 s e l f . o r d e r ( symbol ( s e l f . a s s e t ) , 1)13 p r i n t ( ”<−− h a n d l e d a t a ” )

Next we must create the a program that ingest some data to feedthe trading algorithm, and then creates an object of typeBuyApple to run it.The first part is to ingest the data, this was usually done with

1 impor t z i p l i n e . data . l o a d e r as zp f2 data = zp f . l o ad f r om yahoo ( s t o c k s =[ ’AAPL ’ ] , i n d e x e s ={} ,

s t a r t=date t ime (2010 , 1 , 1) , end=date t ime (2014 , 1 ,1) , a d j u s t e d=F a l s e )

3 c l s p x = data [ [ ’ C l o s e ’ ] ]4 c l s p x . columns = [ [ ’AAPL ’ ] ]

This may be unstable in Windows.Next, we instanciate an object and run the trading algorithm

Hugo E. Ramirez [email protected] Python in algotrading lectures 45 / 47

Page 46: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Zipline simple Example III

1 B A r e s u l t = BuyApple ( ) . run ( c l s p x [ ’ 2013−01−03 ’ : ’2013−01−15 ’ ] )

Note that we use just a smaller period of time of that digestedpreviously.For this strategy you may have the starting cash,ending cash and ending value

1 B A r e s u l t [ [ ’ s t a r t i n g c a s h ’ , ’ e n d i n g c a s h ’ , ’e n d i n g v a l u e ’ ] ]

Finally we could check our results for a larger (and/or more recent)period of time, the next example runs for all 2011

1 r e s u l t f o r 2 0 1 1 = BuyApple ( ) . run ( c l s p x [ ’ 2011 ’ ] )

Hugo E. Ramirez [email protected] Python in algotrading lectures 46 / 47

Page 47: Hugo E. Ramirez hugoedu.ramirez@urosario.edu › charlas › python-en-clases-de-comercio... · Numerical solutions instead of analytical solutions: Since are more readable and sometimes

Projects

Neural Networks to give trading signals. (sklearn).Google trends trading. (Using sentiment from google trends)Twiter sentiment trading. (tweepy).Crypto currencies trading. using quandl.Trading in Quantopian.Balancing an optimal portfolio.

Hugo E. Ramirez [email protected] Python in algotrading lectures 47 / 47