SlideShare a Scribd company logo
1 of 67
introduction
               Connection and data
                          The quest
                  Final Comments




             Trading Strategies using R
              The quest for the holy grail

                            Eran Raviv
              Econometric Institute - Erasmus University,
                       http://eranraviv.com


                          April 02, 2012



Eran Raviv              Trading Strategies using R          April 02, 2012
introduction
                     Connection and data
                                The quest
                        Final Comments


Outline for section 1

  1   introduction

  2   Connection and data

  3   The quest
       Sign Prediction
       Filtering
       Time Series Analysis
       Pairs Trading

  4   Final Comments


      Eran Raviv              Trading Strategies using R   April 02, 2012
Cumulative Returns
                            introduction
                    Connection and data
                               The quest
             0.2       Final Comments


(very) Limited Success
                                                           Decision was
            0.15                                          made to reduce
                                                             volume



             0.1




   cumsum   0.05




               0
                      1
                      5
                      9
                     13
                     17
                     21
                     25
                     29
                     33
                     37
                     41
                     45
                     49
                     53
                     57
                     61
                     65
                     69
                     73
                     77
                     81
                     85
                     89
                     93
                     97
                    101
                    105
                    109
            -0.05


    Eran Raviv               Trading Strategies using R                    April 02, 2012
introduction
                     Connection and data
                                The quest
                        Final Comments


Outline for section 2

  1   introduction

  2   Connection and data

  3   The quest
       Sign Prediction
       Filtering
       Time Series Analysis
       Pairs Trading

  4   Final Comments


      Eran Raviv              Trading Strategies using R   April 02, 2012
introduction
                      Connection and data
                                 The quest
                         Final Comments


For Inter-day, yahoo is fine

 nam = c ( 'AON ' , 'M C' , ' AKS ' , 'BAC ' , . . . ) ; t c k r = s o r t (nam)
                            M
 # Most r e c e n t 252 days :
 end<− f o r m a t ( Sys . Date ( ) , ”%Y m     −% −%d” ) # yyyy− −ddmm
 s t a r t<−f o r m a t ( Sys . Date ( ) − 3 6 5 , ”%Y m−% −%d” )
 l = length ( tckr )
 dat = a r r a y ( dim = c ( 2 5 2 , 6 , l ) )
 for ( i in 1: l ){
 dat0 = ( getSymbols ( t c k r [ i ] , s r c=” yahoo ” , from=s t a r t , t o=end ,
 auto . a s s i g n = FALSE) ) # C anc e l auto . a s s i g n i f you want t o
         manipulate the o b j e c t
 dat [ 1 : l e n g t h ( dat0 [ , 2 ] ) , , i ] = dat0 [ , 2 : 6 ]
 }
 dat = dat [ 1 : l e n g t h ( na . omit ( dat [ , 1 , 1 ] ) ) , , ]




     Eran Raviv                Trading Strategies using R                  April 02, 2012
introduction
                Connection and data
                           The quest
                   Final Comments


For Intra-day use IB


     IB has extensive API. Connect to their trading
     platform (TWS) using Java and C among others.




   Eran Raviv            Trading Strategies using R   April 02, 2012
introduction
                Connection and data
                           The quest
                   Final Comments


For Intra-day use IB


     IB has extensive API. Connect to their trading
     platform (TWS) using Java and C among others.




   Eran Raviv            Trading Strategies using R   April 02, 2012
introduction
                Connection and data
                           The quest
                   Final Comments


For Intra-day use IB


     IB has extensive API. Connect to their trading
     platform (TWS) using Java and C among others.

     Account is not that easy to set up, many forms to fill
     out and hefty sum to transfer, especially if you would
     like to day trade.




   Eran Raviv            Trading Strategies using R   April 02, 2012
introduction
                Connection and data
                           The quest
                   Final Comments


For Intra-day use IB


     IB has extensive API. Connect to their trading
     platform (TWS) using Java and C among others.

     Account is not that easy to set up, many forms to fill
     out and hefty sum to transfer, especially if you would
     like to day trade.




   Eran Raviv            Trading Strategies using R   April 02, 2012
introduction
                Connection and data
                           The quest
                   Final Comments


For Intra-day use IB


     IB has extensive API. Connect to their trading
     platform (TWS) using Java and C among others.

     Account is not that easy to set up, many forms to fill
     out and hefty sum to transfer, especially if you would
     like to day trade.

     Jeffrey A. Ryan did outstanding work, we can now
     trade via R.



   Eran Raviv            Trading Strategies using R   April 02, 2012
introduction
                    Connection and data
                               The quest
                       Final Comments


For Intra-day use IB
     Easy:
     l i b r a r y ( IBrokers )
     I B r o k e r s v e r s i o n 0 . 9 − 1 : Implementing API V e r s i o n 9 . 6 4
     This s o f t w a r e comes with NO WARRANTY. Not i n t e n d e d f o r
             p r o d u c t i o n u s e ! See ? I B r o k e r s f o r d e t a i l s }
     con = twsConnect ( c l i e n t I d = 1 , h o s t = ' l o c a l h o s t ' , p o r t
              = 7 4 9 6 , v e r b o s e = TRUE, t i m e o u t = 5 , f i l e n a m e =
             NULL)




   Eran Raviv                 Trading Strategies using R                      April 02, 2012
introduction
                    Connection and data
                               The quest
                       Final Comments


For Intra-day use IB
     Easy:
     l i b r a r y ( IBrokers )
     I B r o k e r s v e r s i o n 0 . 9 − 1 : Implementing API V e r s i o n 9 . 6 4
     This s o f t w a r e comes with NO WARRANTY. Not i n t e n d e d f o r
             p r o d u c t i o n u s e ! See ? I B r o k e r s f o r d e t a i l s }
     con = twsConnect ( c l i e n t I d = 1 , h o s t = ' l o c a l h o s t ' , p o r t
              = 7 4 9 6 , v e r b o s e = TRUE, t i m e o u t = 5 , f i l e n a m e =
             NULL)

     High frequency data if you have the patience to
     program it.




   Eran Raviv                 Trading Strategies using R                      April 02, 2012
introduction
                    Connection and data
                               The quest
                       Final Comments


For Intra-day use IB
     Easy:
     l i b r a r y ( IBrokers )
     I B r o k e r s v e r s i o n 0 . 9 − 1 : Implementing API V e r s i o n 9 . 6 4
     This s o f t w a r e comes with NO WARRANTY. Not i n t e n d e d f o r
             p r o d u c t i o n u s e ! See ? I B r o k e r s f o r d e t a i l s }
     con = twsConnect ( c l i e n t I d = 1 , h o s t = ' l o c a l h o s t ' , p o r t
              = 7 4 9 6 , v e r b o s e = TRUE, t i m e o u t = 5 , f i l e n a m e =
             NULL)

     High frequency data if you have the patience to
     program it.
     Limitation on the number of requests.




   Eran Raviv                 Trading Strategies using R                      April 02, 2012
introduction
                    Connection and data
                               The quest
                       Final Comments


For Intra-day use IB
     Easy:
     l i b r a r y ( IBrokers )
     I B r o k e r s v e r s i o n 0 . 9 − 1 : Implementing API V e r s i o n 9 . 6 4
     This s o f t w a r e comes with NO WARRANTY. Not i n t e n d e d f o r
             p r o d u c t i o n u s e ! See ? I B r o k e r s f o r d e t a i l s }
     con = twsConnect ( c l i e n t I d = 1 , h o s t = ' l o c a l h o s t ' , p o r t
              = 7 4 9 6 , v e r b o s e = TRUE, t i m e o u t = 5 , f i l e n a m e =
             NULL)

     High frequency data if you have the patience to
     program it.
     Limitation on the number of requests.
     In any case not more than one year, but you can store
     it.


   Eran Raviv                 Trading Strategies using R                      April 02, 2012
introduction
                    Connection and data
                               The quest
                       Final Comments


For Intra-day use IB
     Easy:
     l i b r a r y ( IBrokers )
     I B r o k e r s v e r s i o n 0 . 9 − 1 : Implementing API V e r s i o n 9 . 6 4
     This s o f t w a r e comes with NO WARRANTY. Not i n t e n d e d f o r
             p r o d u c t i o n u s e ! See ? I B r o k e r s f o r d e t a i l s }
     con = twsConnect ( c l i e n t I d = 1 , h o s t = ' l o c a l h o s t ' , p o r t
              = 7 4 9 6 , v e r b o s e = TRUE, t i m e o u t = 5 , f i l e n a m e =
             NULL)

     High frequency data if you have the patience to
     program it.
     Limitation on the number of requests.
     In any case not more than one year, but you can store
     it.
     Professional yahoo group at:
     http://finance.groups.yahoo.com/group/TWSAPI/
   Eran Raviv                 Trading Strategies using R                      April 02, 2012
introduction    Sign Prediction
                     Connection and data     Filtering
                                The quest    Time Series Analysis
                        Final Comments       Pairs Trading


Outline for section 3

  1   introduction

  2   Connection and data

  3   The quest
       Sign Prediction
       Filtering
       Time Series Analysis
       Pairs Trading

  4   Final Comments


      Eran Raviv              Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                 Connection and data     Filtering
                            The quest    Time Series Analysis
                    Final Comments       Pairs Trading


Selected Ideas


  Over the years I have backtested many ideas, among others:

      Sign Prediction




    Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                 Connection and data     Filtering
                            The quest    Time Series Analysis
                    Final Comments       Pairs Trading


Selected Ideas


  Over the years I have backtested many ideas, among others:

      Sign Prediction
      Filtering




    Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                 Connection and data     Filtering
                            The quest    Time Series Analysis
                    Final Comments       Pairs Trading


Selected Ideas


  Over the years I have backtested many ideas, among others:

      Sign Prediction
      Filtering
      Multivariate time series modelling




    Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                 Connection and data     Filtering
                            The quest    Time Series Analysis
                    Final Comments       Pairs Trading


Selected Ideas


  Over the years I have backtested many ideas, among others:

      Sign Prediction
      Filtering
      Multivariate time series modelling
      Pairs trading




    Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                 Connection and data     Filtering
                            The quest    Time Series Analysis
                    Final Comments       Pairs Trading


Selected Ideas


  Over the years I have backtested many ideas, among others:

      Sign Prediction
      Filtering
      Multivariate time series modelling
      Pairs trading




    Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                 Connection and data     Filtering
                            The quest    Time Series Analysis
                    Final Comments       Pairs Trading


Selected Ideas


  Over the years I have backtested many ideas, among others:

      Sign Prediction
      Filtering
      Multivariate time series modelling
      Pairs trading

  Born to trade, forced to work.



    Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                     Connection and data     Filtering
                                The quest    Time Series Analysis
                        Final Comments       Pairs Trading


Table of Contents

  1   introduction

  2   Connection and data

  3   The quest
       Sign Prediction
       Filtering
       Time Series Analysis
       Pairs Trading

  4   Final Comments


      Eran Raviv              Trading Strategies using R            April 02, 2012
introduction     Sign Prediction
                  Connection and data      Filtering
                             The quest     Time Series Analysis
                     Final Comments        Pairs Trading


Sign Prediction

  Sign prediction using:

      Logistic Regression (glm)
      Support Vector Machine (svm)
         ♣ library(e1071)
      K-Nearest Neighbour (knn)
         ♣ library(class)
      Neural Networks (nnet)
         ♣ library(nnet)



    Eran Raviv              Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                 Connection and data     Filtering
                            The quest    Time Series Analysis
                    Final Comments       Pairs Trading


Sign Prediction - continued


      Working with daily returns, so target is to predict
      tomorrow’s move. (Avoid overnight)
      Explanatory variables considered:
          I five lags (one week)
         II Spread between the volume and the rolling average of
            most recent 5 days.
        III Volatility - average of the last five days.




    Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                     Connection and data     Filtering
                                The quest    Time Series Analysis
                        Final Comments       Pairs Trading


Sign Prediction - continued
  Volatility is measured as the average of three different
  intra-day volatility measures which are more efficient
  (converge faster) than the standard ”sd” estimate:

      Parkinson (1980):
                   1       N       hi 2
      σ=         4N ln2    i=1 (ln li )




    Eran Raviv                Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                     Connection and data     Filtering
                                The quest    Time Series Analysis
                        Final Comments       Pairs Trading


Sign Prediction - continued
  Volatility is measured as the average of three different
  intra-day volatility measures which are more efficient
  (converge faster) than the standard ”sd” estimate:

      Parkinson (1980):
                   1       N       hi 2
      σ=         4N ln2    i=1 (ln li )

      German Klass (1980):
                 1    N 1       hi 2        1      N                    ci
      σ=         N    i=1 2 (ln li )    −   N      i=1 (2ln2   − 1)(ln ci−1 )2




    Eran Raviv                Trading Strategies using R                  April 02, 2012
introduction    Sign Prediction
                     Connection and data     Filtering
                                The quest    Time Series Analysis
                        Final Comments       Pairs Trading


Sign Prediction - continued
  Volatility is measured as the average of three different
  intra-day volatility measures which are more efficient
  (converge faster) than the standard ”sd” estimate:

      Parkinson (1980):
                   1       N       hi 2
      σ=         4N ln2    i=1 (ln li )

      German Klass (1980):
                 1    N 1       hi 2        1      N                    ci
      σ=         N    i=1 2 (ln li )    −   N      i=1 (2ln2   − 1)(ln ci−1 )2
      Rogers and satchell (1991):
                 1    N       hi      hi           l        l
      σ=         N    i=1 (ln li )(ln oi )   + (ln cii )(ln oii )

    Eran Raviv                Trading Strategies using R                  April 02, 2012
introduction        Sign Prediction
                           Connection and data         Filtering
                                      The quest        Time Series Analysis
                              Final Comments           Pairs Trading


Sign Prediction - continued
  dat0 = ( getSymbols ( t c k r [ 1 ] , s r c=” yahoo ” , from=s t a r t , t o=end ,
            auto . a s s i g n = FALSE) )
  l = l e n g t h ( dat0 [ , 1 ] )
  d a t e s 0 = ( i n d e x ( dat0 ) ) # t r i c k t o g e t t r a d i n g d a t e s
  t t = NULL # we now p a r s e i t i n t o IB mode
                      #
  for ( i in 1: l ){
  tt [ i ] = paste ( substr ( dates0 [ i ] , 1 , 4 ) , substr ( dates0 [ i ] , 6 , 7 ) ,
          s u b st r ( dates0 [ i ] , 9 , 1 0 ) , sep = ”” )
  t t [ i ] = p a s t e ( t t [ i ] , ” 2 3 : 0 0 : 0 0 GMT” )
  }
  c o n t=t ws Eq ui ty ( ' p l u g your f a v o u r i t e symbol ' , 'SMART ' , ' NYSE ' )
  mat1 = a r r a y ( dim = c ( l , 4 0 0 , 8 ) )#T y p i c a l day s h o u l d have 390
          mins
  for ( i in 1: l ){
  m1 = a s . m a t r i x ( r e q H i s t o r i c a l D a t a ( con , cont , t t [ i ] , b a r S i z e =
          ” 1 min” ,
  d u r a t i o n = ” 1 d” , useRTH = ” 1 ” , whatToShow = ”TRADES” , time .
          f o r m a t = ” 1 ” , v e r b o s e = TRUE) )
  mat1 [ i , 1 : l e n g t h (m1 [ , 1 ] ) , ] = m1
  Sys . s l e e p ( 1 4 ) # IB r e s t r i c t i o n , WAIT.
                             #
  }
      Eran Raviv                      Trading Strategies using R                             April 02, 2012
introduction     Sign Prediction
                        Connection and data      Filtering
                                   The quest     Time Series Analysis
                           Final Comments        Pairs Trading


Sign Prediction - continued
  Sample code:
 l o g i t 1 = glm ( y˜ l a g y+v o l a t+volume , data=dat [ 1 : t1 , ] , f a m i l y=
         b i n o m i a l ( l i n k = ” l o g i t ” ) , na . a c t i o n=na . p a s s )
 summary ( l o g i t 1 ) #t 1 i s end o f t r a i n i n g , TT i s f u l l l e n g t h .
 l i b r a r y ( nnet )
 nnet1 = nnet ( a s . f a c t o r ( y ) ˜ l a g y+v o l a t+volume , data=dat [ 1 : t1 , ] ,
         s i z e =1 , t r a c e=T)
 summary ( nnet1 )
 library ( class )
 knn1 = knn ( dat [ 1 : t1 , ] , dat [ ( t 1 +1) : TT, ] , c l = dat $ y [ 1 : t 1 ] , k=25 ,
         prob=F)
 sum ( knn1==dat $ y [ ( t 1 +1) ] ) / (TT 1 +1)#H i t r a t i o
                                                    −t
 l i b r a r y ( e1071 )
 svm1 = svm ( dat [ 1 : t1 , 2 : 4 ] , y=dat [ 1 : t1 , 1 ] , t y p e = ”C” )
 # I n sample :
 sum ( svm1 $ f i t==dat $ y [ ( 1 ) : t 1 ] ) / t 1
 # out o f sample :
 svmpred=p r e d i c t ( svm1 , newdata = dat [ ( t 1 +1) : TT, 2 : 4 ] )
 sum ( svmpred==dat $ y [ ( t 1 +1) :TT ] ) / (TT 1 +1)#H i t r a t i o
                                                             −t

     Eran Raviv                   Trading Strategies using R                       April 02, 2012
introduction    Sign Prediction
                     Connection and data     Filtering
                                The quest    Time Series Analysis
                        Final Comments       Pairs Trading


Table of Contents

  1   introduction

  2   Connection and data

  3   The quest
       Sign Prediction
       Filtering
       Time Series Analysis
       Pairs Trading

  4   Final Comments


      Eran Raviv              Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                Connection and data     Filtering
                           The quest    Time Series Analysis
                   Final Comments       Pairs Trading


Deviation from the mean




     Motivation =⇒ Disposition effect, the Voodoo of
     financial markets.
     Standardise the deviation from the (rolling) mean.




   Eran Raviv            Trading Strategies using R            April 02, 2012
introduction                                    Sign Prediction
                                                   Connection and data                                     Filtering
                                                              The quest                                    Time Series Analysis
                                                      Final Comments                                       Pairs Trading


Deviation from the mean
                                                                                                 Google
                                                                                                                                                                 qqq
              650




                                                                                                                                                                 q
                                                                                                                                                                   q
                                                                                                                                                               q
                                                                                                                                                               qq      q
                                                                                                                                                                    qqq
                                                                                                                                                                   qqq qq
                                                                                                                                                                   qqq q
                     qq                                                                                                                              q     qqq q
                                                                                                                                                             q
                                                                                                                                                         q qq qqqq
                                                                                                                                                                 q   qq   qq
                    qqq                                                                                                                              q qq
                                                                                                                                                      q q
                                                                                                                                                       q
                                                                                                                                                          q    q
                                                                                                                                                               q     q q   q
                                                                                                                                                                            q            qq
                        q q                                                               q
                                                                                          q                                                         q q       q              q
                                                                                                                                                                             q          qq     q
                    q    q                                                               q q                                                 q
                                                                                                                                             q               q
                                                                                                                                                             q                     q
                                                                                                                                                                                 qq q         q
                         q                                                                                                                                                    q qq         qqq
                        qq q                                                                                                              q qq              q                  q q qqqqqqq  q q
                                                                                                                                                                                          qqq
              600




                               q                                                                                                                           q                    q     q    qq
                           q    qq                                                     qq qqq                                             qq
                                                                                                                                           q q          qq
                                                                                                                                                       qq
                                                                                                                                                       qq
                                                                                                                                                          qq                   qqq q q qq
                                                                                                                                                                                        q
                                                                                                                                                                                        q
                           qq    q                                                                                            q                qq q q
                                                                                                                                              qqqqq                              q
                                                                                                                                                                                 qq    q     q
                             q
                                  qq     q
                                                                                     qq
                                                                                      qq      q q
                                                                                              q qqq
                                                                                                                                    q
                                                                                                                                      q q q
                                                                                                                                   qq q qq  qqqqqqqqq                              qqq
                                                                                                                                                                               q qqqq
                                                                                                                                                                                      q
                             q     qq                                                           q q                               qq                                    q      q
                              q      q q q
                                     q q
                                     qq
                                      qq                                                      qqq   qq                          qq
                                                                                                                               qq
                                                                                                                                        q q
                                                                                                                                       q q     q q                       q
                                                                                                                                                                         q   qq
                                                                                               q                                                                             q
  Price




                                      qqq
                                       qq                                                     q      q                          q     q q       qq
                               q   q qqqqqq
                                    q      qqq
                                         qqqq
                                            qq                                               q                                          q       q                         qq
                               q q        q q   q
                                                q                                           q
                                                                                                  q
                                                                                                      q                                 q                                  q
                                                                                                                                                                           q
                                 q               q                                          q          q                               q         q
                                 qq               q
                                                  q                                        q    qq     q                              q
                                                   q                                            q                                     q
              550




                                q                   q                                     q             q
                                                    qq                                            qq
                                                                                                   q     q                           q
                                                      q                                   q              q         q          q     q
                                                                                                                                    q
                                                    qq                                                                       q
                                              q        qq
                                                        q                                q
                                                                                                          qq
                                                                                                           q     qq               q
                                                                                                                                   q
                                                                                 q q                       q      q    q
                                                  q qqq
                                                    q
                                                   q qqqq q
                                                          q q                       q               q     qq          q          qq
                                                      q   qqqq
                                                           qqqq
                                                            qqq                  qq     q                q qqq qqq
                                                                                                              q q qqq
                                                                                                             q q
                                                                                                              q      qqq
                                                                                                                       qq q q   q
                                                                                                                                q
                                               q            q    q q                    q               q    qq    q      q q
                                                                                                                          qqq
                                              qqqq
                                                 q         q     qqqq
                                                                  q q
                                                                  q q
                                                                   q qq
                                                                    q           q qq
                                                                                       q             q      q   qq q
                                                                                                                qq q
                                                                                                                 q         q q
                                                                                                                            q
                                                              qq q qqq
                                                               qq       q             q                q q     q        q
                                                            q q     q q   q
                                                                          q                                          q     q
                                                                           qq         q                      q
                                                                      qqq q q        q                                   qq
              500




                                                                             q      q
                                                                        q
                                                                        q     qqqq
                                                                              q
                                                                               qqq
                                                                                qq
                                                                                   q                  qq
                                                                              q                      q                    q
                                                                           q                                             q
                                                                          q
                                                                          qqq
                                                                            q
                                                                               q



                    0                           50                             100                            150                             200                                  250
                                                                                                     Days


                                   Histogram for Z
              20




                                                                                                                                                                                             q

                                                                                                                                                         q                                    q
                                                                                                                                                                            q
                                                                                                                                                      q                                       q
                                                                                                                                                        q                          q
                                                                                                                                                                                   q         q
                                                                                                                                                     q q
                                                                                                                                                      q



                                                                                                              2
                                                                                                                                                                             q
                                                                                                                                                                             q     q                       q
                                                                                                                                                         q                        q          qq            q
              15




                                                                                                                                                         q
                                                                                                                                                         qq                 q           q    q
                                                                                                                                                     qq q
                                                                                                                                                      qq
                                                                                                                                                                      q
                                                                                                                                                                      q
                                                                                                                                                                      qq     q qq q
                                                                                                                                                                              q
                                                                                                                                                                              q
                                                                                                                                                                              q         q                 qq q
                                                                                                                                  q                                         q q        qq
                                                                                                                                                                                        q                q
                                                                                                                                                       q q q               q qq q q      q q q
                                                                                                                                                                                          qq
                                                                                                                                  q                     q q                    q           q            q     q
                                                                                                                                                           q                    qq       qqq
  Frequency




                                                                                                                                                                                          qq
                                                                                                                                                                                           q            qq
                                                                                                                                                                                                       qq
                                                                                                                                                                                                        q
                                                                                                                                 q
                                                                                                                                   q                        q        qq q
                                                                                                                                                                        q
                                                                                                                                                                                 q                     q qq
                                                                                                                                                                                                        q
                                                                                                                                                                                                      q q
                                                                                                                                                     q      q        q          q qq q                q
                                                                                                                                                                                                      qq
                                                                                                                                                                    qq
                                                                                                                                q q
                                                                                                                                 q q
                                                                                                                                 q q
                                                                                                                                          q                         q                             q
                                                                                                              0
                                                                                                                                          q q  q                  q
                                                                                                                                                                  q
                                                                                                                               q qq      qq                       q q q
              10




                                                                                                                                q           q        q       q
                                                                                                                                                             q     q                qq           q q
                                                                                                      Z


                                                                                                                                q      qq q q                      q     q                      q
                                                                                                                               q        q q q
                                                                                                                                        qq          q              q     qq                      q q        q
                                                                                                                               q        q
                                                                                                                                       q q                    q q       q q           q         q           q
                                                                                                                                  qq q         q
                                                                                                                                                q                q
                                                                                                                                                                 q                             qqq q
                                                                                                                                                                                                            q
                                                                                                                                                                                                             q
                                                                                                                                      q       qq                                                    q
                                                                                                                                                q q            qq
                                                                                                                                                               q       q q
                                                                                                                                                                                                    q
                                                                                                                                                                                                    q
                                                                                                                              q       q     qq
                                                                                                                                             q     q         qq                      q
                                                                                                                              q       q                                  q           q
                                                                                                                                                                                                             q
                                                                                                              −2


                                                                                                                             q                   qqq          qq
                                                                                                                                                               qq                                  q
                                                                                                                                     q       q      q                     q
                                                                                                                             q
                                                                                                                             qq                   q                                                q
                                                                                                                                                                                                   q
                                                                                                                                           qq    q                                   q
              5




                                                                                                                                                                q
                                                                                                                                                                q                     q           q
                                                                                                                                     q           qq
                                                                                                                                                 q                                               q
                                                                                                                                     q                                   q
                                                                                                                                                 q
                                                                                                              −4
              0




                                                                                                                                    q



                    −4              −2                  0                  2                                           0            50            100             150             200             250
                                                                                                                                                             Days


               Eran Raviv                                                Trading Strategies using R                                                                                              April 02, 2012
introduction    Sign Prediction
                     Connection and data     Filtering
                                The quest    Time Series Analysis
                        Final Comments       Pairs Trading


Table of Contents

  1   introduction

  2   Connection and data

  3   The quest
       Sign Prediction
       Filtering
       Time Series Analysis
       Pairs Trading

  4   Final Comments


      Eran Raviv              Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                Connection and data     Filtering
                           The quest    Time Series Analysis
                   Final Comments       Pairs Trading


Motivation

     Momentum in Microstructure - Dermot Murphy and
     Ramabhadran S. Thirumalai (Job Market Paper -
     2011)
     Are You Trading Predictably? -Steven L. Heston
     ,Robert A. Korajczyk ,Ronnie Sadka, Lewis D.
     Thorson. (2010)




   Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                Connection and data     Filtering
                           The quest    Time Series Analysis
                   Final Comments       Pairs Trading


Motivation

     Momentum in Microstructure - Dermot Murphy and
     Ramabhadran S. Thirumalai (Job Market Paper -
     2011)
     Are You Trading Predictably? -Steven L. Heston
     ,Robert A. Korajczyk ,Ronnie Sadka, Lewis D.
     Thorson. (2010)
     We find predictable patterns in stock returns. Stocks
     whose relative returns are high in a given half-hour
     interval today exhibit similar outperformance in the
     same half-hour period on subsequent days. The effect
     is stronger at the beginning and end of the trading
     day. These results suggest...
   Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                     Connection and data     Filtering
                                The quest    Time Series Analysis
                        Final Comments       Pairs Trading


VAR models

 For each day t = {1, ..., T }, the return of half an hour
 k = {1, ..., 13} , and the lag number p = {1, ..., P }:
      1
                      a1,1 a1 1,2 · · · a1
                                                     
   y1,t       c1                         1,k    y1,t−1
 y2,t  c2  a1            1          1 
      2,1 a2,2 · · · a2,k  y2,t−1 
                                                       
  . = . + .              .    ..    .  .  + ··· +
  .  .  .
    .          .        .     .
                              .       .  .  . 
                                         .         .
  yk,t     ck           a1      1         1
                          k,1 ak,2 · · · ak,k  yk,t−1
  p    p               p 
  a1,1 a1,2 · · ·      a1,k
                                         
                                y1,t−p    e1,t
 ap    p
                       ap  y2,t−p  e2,t 
  2,1 a2,2 · · ·       2,k  
                         .  .  +  . 
                                         
  .    .     ..
  .
   .    .
        .        .       .  .   . 
                         .         .        .
   p    p
  ak,1 ak,2 · · ·      ap
                        k,k
                                 yk,t−p          ek,t



    Eran Raviv                Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                     Connection and data     Filtering
                                The quest    Time Series Analysis
                        Final Comments       Pairs Trading


VAR models

 For each day t = {1, ..., T }, the return of half an hour
 k = {1, ..., 13} , and the lag number p = {1, ..., P }:
      1
                      a1,1 a1 1,2 · · · a1
                                                     
   y1,t       c1                         1,k    y1,t−1
 y2,t  c2  a1            1          1 
      2,1 a2,2 · · · a2,k  y2,t−1 
                                                       
  . = . + .              .    ..    .  .  + ··· +
  .  .  .
    .          .        .     .
                              .       .  .  . 
                                         .         .
  yk,t     ck           a1      1         1
                          k,1 ak,2 · · · ak,k  yk,t−1
  p    p               p 
  a1,1 a1,2 · · ·      a1,k
                                         
                                y1,t−p    e1,t
 ap    p
                       ap  y2,t−p  e2,t 
  2,1 a2,2 · · ·       2,k  
                         .  .  +  . 
                                         
  .    .     ..
  .
   .    .
        .        .       .  .   . 
                         .         .        .
   p    p
  ak,1 ak,2 · · ·      ap
                        k,k
                                 yk,t−p          ek,t
 Problem: for P = 1, how many parameters?

    Eran Raviv                Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                Connection and data     Filtering
                           The quest    Time Series Analysis
                   Final Comments       Pairs Trading


VAR models (cont’d)


     Possible solution =⇒ Dimension Reduction.




   Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                Connection and data     Filtering
                           The quest    Time Series Analysis
                   Final Comments       Pairs Trading


VAR models (cont’d)


     Possible solution =⇒ Dimension Reduction.

     Stepwise Regression, Lasso, Variable selection
     (according to some Information Criteria), Principal
     Component Regression, Ridge Regression, Bayesian
     VAR and many more.
     Very nice vars package to start you off, though as most
     built-ins, not flexible enough. (e.g. rolling windows
     and/or shrinking)



   Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                     Connection and data     Filtering
                                The quest    Time Series Analysis
                        Final Comments       Pairs Trading


Table of Contents

  1   introduction

  2   Connection and data

  3   The quest
       Sign Prediction
       Filtering
       Time Series Analysis
       Pairs Trading

  4   Final Comments


      Eran Raviv              Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                Connection and data     Filtering
                           The quest    Time Series Analysis
                   Final Comments       Pairs Trading


Pairs Trading


     Well known and widely used. (e.g. Statistical Arbitrage
     in the U.S. Equities Market, Marco Avellaneda and
     Jeong-Hyun Lee (2008))




   Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                Connection and data     Filtering
                           The quest    Time Series Analysis
                   Final Comments       Pairs Trading


Pairs Trading


     Well known and widely used. (e.g. Statistical Arbitrage
     in the U.S. Equities Market, Marco Avellaneda and
     Jeong-Hyun Lee (2008))

     Suitable for the conservative mind. (we see why in a
     minute..)




   Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                Connection and data     Filtering
                           The quest    Time Series Analysis
                   Final Comments       Pairs Trading


Pairs Trading


     Well known and widely used. (e.g. Statistical Arbitrage
     in the U.S. Equities Market, Marco Avellaneda and
     Jeong-Hyun Lee (2008))

     Suitable for the conservative mind. (we see why in a
     minute..)




   Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                      Connection and data     Filtering
                                 The quest    Time Series Analysis
                         Final Comments       Pairs Trading


Pairs Trading (cont’d)

  The Idea:

                  ra = βa rm + ea
                  rb = βb rm + eb
                 rab = wa (βa rm + ea ) + wb (βb rm + ea )
                     = rm (wa βa + wb βb ) + noise


  and so with weights wa = − βaβb b and wb = 1 − wa we can
                               −β
  net out the market. (and other factors if you will)


    Eran Raviv                 Trading Strategies using R            April 02, 2012
introduction      Sign Prediction
                          Connection and data       Filtering
                                     The quest      Time Series Analysis
                             Final Comments         Pairs Trading


Pairs Trading (cont’d)
        Choose symbols with similar properties.
        Net out the market and create the spread:
 # sp1 = s t o c k p r i c e 1 , g=s i z e o f moving window ,
  #
 # n = l e n g t h ( sp1 )
  #
     for ( i in g : n){
   b e t 0 [ i ]=lm ( sp1 [ ( i −g+1) : ( i −1) ] ˜ sp2 [ ( i −g+1) : ( i −1) ] ) $ c o e f [ 1 ] #
          # n o t e −> i −1
   b e t 1 [ i ]=lm ( sp1 [ ( i −g+1) : ( i −1) ] ˜ sp2 [ ( i −g+1) : ( i −1) ] ) $ c o e f [ 2 ]
 s p r e a d [ , i ]= sp1 [ ( i −g+1) : i ]− r e p ( b e t 0 [ i ] , g )−b e t 1 [ i ] *
 sp2 [ ( i −g+1) : i ]
     }

        Text book example (actually from: Quantitative
        Trading: How to Build Your Own Algorithmic Trading
        Business )

     Eran Raviv                     Trading Strategies using R                          April 02, 2012
introduction      Sign Prediction
                          Connection and data       Filtering
                                     The quest      Time Series Analysis
                             Final Comments         Pairs Trading


Pairs Trading (cont’d)
        Choose symbols with similar properties.
        Net out the market and create the spread:
 # sp1 = s t o c k p r i c e 1 , g=s i z e o f moving window ,
  #
 # n = l e n g t h ( sp1 )
  #
     for ( i in g : n){
   b e t 0 [ i ]=lm ( sp1 [ ( i −g+1) : ( i −1) ] ˜ sp2 [ ( i −g+1) : ( i −1) ] ) $ c o e f [ 1 ] #
          # n o t e −> i −1
   b e t 1 [ i ]=lm ( sp1 [ ( i −g+1) : ( i −1) ] ˜ sp2 [ ( i −g+1) : ( i −1) ] ) $ c o e f [ 2 ]
 s p r e a d [ , i ]= sp1 [ ( i −g+1) : i ]− r e p ( b e t 0 [ i ] , g )−b e t 1 [ i ] *
 sp2 [ ( i −g+1) : i ]
     }

        Text book example (actually from: Quantitative
        Trading: How to Build Your Own Algorithmic Trading
        Business )
        The GLD and GDX spread
     Eran Raviv                     Trading Strategies using R                          April 02, 2012
introduction    Sign Prediction
                 Connection and data     Filtering
                            The quest    Time Series Analysis
                    Final Comments       Pairs Trading


Pairs Trading (cont’d)
  The GLD and GDX spread:




    Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                 Connection and data     Filtering
                            The quest    Time Series Analysis
                    Final Comments       Pairs Trading


Pairs Trading Issues


  Estimation of the market neutral portfolio is tricky:
      Price levels or price changes?




    Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                 Connection and data     Filtering
                            The quest    Time Series Analysis
                    Final Comments       Pairs Trading


Pairs Trading Issues


  Estimation of the market neutral portfolio is tricky:
      Price levels or price changes?

      Stability over time




    Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                 Connection and data     Filtering
                            The quest    Time Series Analysis
                    Final Comments       Pairs Trading


Pairs Trading Issues


  Estimation of the market neutral portfolio is tricky:
      Price levels or price changes?

      Stability over time

      Errors on both sides. (both y and x are measured with
      errors)




    Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                 Connection and data     Filtering
                            The quest    Time Series Analysis
                    Final Comments       Pairs Trading


Pairs trading issues
  Stability over time:




    Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                 Connection and data     Filtering
                            The quest    Time Series Analysis
                    Final Comments       Pairs Trading


Pairs trading issues

  Errors on both sides:

                          sta = αstb + ea
                          stb = βsta + eb
                                1
                           α =
                                β
                              ⇓

  Portfolio is different and will depend on which instrument
  goes on the LHS and which on the RHS.


    Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                 Connection and data     Filtering
                            The quest    Time Series Analysis
                    Final Comments       Pairs Trading


Pairs trading - possible solutions


  Price levels or price changes?




    Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                 Connection and data     Filtering
                            The quest    Time Series Analysis
                    Final Comments       Pairs Trading


Pairs trading - possible solutions


  Price levels or price changes?


      flip a coin (solid option)
      average the estimates




    Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                 Connection and data     Filtering
                            The quest    Time Series Analysis
                    Final Comments       Pairs Trading


Pairs trading - possible solutions


  Price levels or price changes?


      flip a coin (solid option)
      average the estimates




    Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                 Connection and data     Filtering
                            The quest    Time Series Analysis
                    Final Comments       Pairs Trading


Pairs trading - possible solutions (cont’d)


  Stability over time




    Eran Raviv            Trading Strategies using R            April 02, 2012
introduction    Sign Prediction
                    Connection and data     Filtering
                               The quest    Time Series Analysis
                       Final Comments       Pairs Trading


Pairs trading - possible solutions (cont’d)


  Stability over time

      Choose window length that fits your style, the shorter
      the more you trade.
      Recent paper (though in different context) suggests to
      average estimates across different windows to partially
      hedge out uncertainty. (M. Hashem Pesaran, Andreas Pick.
      Journal of Business and Economic Statistics. April 1, 2011)

      Kalman filter the coefficients.



    Eran Raviv               Trading Strategies using R             April 02, 2012
introduction    Sign Prediction
                 Connection and data     Filtering
                            The quest    Time Series Analysis
                    Final Comments       Pairs Trading


Pairs trading - possible solutions (cont’d)

  Errors on both sides, two highly correlated possible
  solutions:
       Demming regression (1943). (Total least squares - just
       minimize numerically both sides simultaneously)
       Geometric Mean Regression - force coherence through:

                             sta = αstb + ea
                             stb = βsta + eb
                                                       1
                               γ =           α×
                                                       β


    Eran Raviv            Trading Strategies using R            April 02, 2012
introduction
                     Connection and data
                                The quest
                        Final Comments


Outline for section 4

  1   introduction

  2   Connection and data

  3   The quest
       Sign Prediction
       Filtering
       Time Series Analysis
       Pairs Trading

  4   Final Comments


      Eran Raviv              Trading Strategies using R   April 02, 2012
introduction
                Connection and data
                           The quest
                   Final Comments


Miscellaneous remarks
     Trading costs!, consider it when backtesting.




   Eran Raviv            Trading Strategies using R   April 02, 2012
introduction
                Connection and data
                           The quest
                   Final Comments


Miscellaneous remarks
     Trading costs!, consider it when backtesting.
     You cannot be too careful, stay pessimistic.




   Eran Raviv            Trading Strategies using R   April 02, 2012
introduction
                Connection and data
                           The quest
                   Final Comments


Miscellaneous remarks
     Trading costs!, consider it when backtesting.
     You cannot be too careful, stay pessimistic.
     Adopt rigorous robustness checks, different
     instruments, different time frames and even different
     markets.




   Eran Raviv            Trading Strategies using R   April 02, 2012
introduction
                Connection and data
                           The quest
                   Final Comments


Miscellaneous remarks
     Trading costs!, consider it when backtesting.
     You cannot be too careful, stay pessimistic.
     Adopt rigorous robustness checks, different
     instruments, different time frames and even different
     markets.
     Use paper money for at least a full quarter, it will help
     you handle operational problems. (e.g. outages and
     time zones issues)




   Eran Raviv            Trading Strategies using R    April 02, 2012
introduction
                Connection and data
                           The quest
                   Final Comments


Miscellaneous remarks
     Trading costs!, consider it when backtesting.
     You cannot be too careful, stay pessimistic.
     Adopt rigorous robustness checks, different
     instruments, different time frames and even different
     markets.
     Use paper money for at least a full quarter, it will help
     you handle operational problems. (e.g. outages and
     time zones issues)
     It is (very) stressing work, know it before you start.



   Eran Raviv            Trading Strategies using R     April 02, 2012
introduction
                Connection and data
                           The quest
                   Final Comments


Miscellaneous remarks
     Trading costs!, consider it when backtesting.
     You cannot be too careful, stay pessimistic.
     Adopt rigorous robustness checks, different
     instruments, different time frames and even different
     markets.
     Use paper money for at least a full quarter, it will help
     you handle operational problems. (e.g. outages and
     time zones issues)
     It is (very) stressing work, know it before you start.
     Know what you are doing, what is your edge? why it is
     (not) there?
   Eran Raviv            Trading Strategies using R     April 02, 2012
introduction
             Connection and data
                        The quest
                Final Comments




        THANKS
and good luck at the tables..



Eran Raviv            Trading Strategies using R   April 02, 2012

More Related Content

Similar to Backtesting Trading Strategies with R

Lulu.com.java.j2 ee.job.interview.companion.2nd.edition.apr.2007
Lulu.com.java.j2 ee.job.interview.companion.2nd.edition.apr.2007Lulu.com.java.j2 ee.job.interview.companion.2nd.edition.apr.2007
Lulu.com.java.j2 ee.job.interview.companion.2nd.edition.apr.2007Arun Kumar
 
Java j2ee job interview companion ebook
Java j2ee job interview companion ebookJava j2ee job interview companion ebook
Java j2ee job interview companion ebookVijay Ekkaladevi
 
Ebs architecture con9036_pdf_9036_0001
Ebs architecture con9036_pdf_9036_0001Ebs architecture con9036_pdf_9036_0001
Ebs architecture con9036_pdf_9036_0001jucaab
 
Jeet Shah - CV
Jeet Shah - CVJeet Shah - CV
Jeet Shah - CVjeet shah
 
Presentation Summarizer: A Full-Fledged NLP Service
Presentation Summarizer: A Full-Fledged NLP ServicePresentation Summarizer: A Full-Fledged NLP Service
Presentation Summarizer: A Full-Fledged NLP ServiceIRJET Journal
 
SAP Business Objects Planning and Consolidaton, Version for SAP NetWeaver
SAP Business Objects Planning and Consolidaton, Version for SAP NetWeaverSAP Business Objects Planning and Consolidaton, Version for SAP NetWeaver
SAP Business Objects Planning and Consolidaton, Version for SAP NetWeaverIBM India Smarter Computing
 
Sentiment Analysis using Naïve Bayes, CNN, SVM
Sentiment Analysis using Naïve Bayes, CNN, SVMSentiment Analysis using Naïve Bayes, CNN, SVM
Sentiment Analysis using Naïve Bayes, CNN, SVMIRJET Journal
 
Named Entity Recognition (NER) Using Automatic Summarization of Resumes
Named Entity Recognition (NER) Using Automatic Summarization of ResumesNamed Entity Recognition (NER) Using Automatic Summarization of Resumes
Named Entity Recognition (NER) Using Automatic Summarization of ResumesIRJET Journal
 
Growing Intelligence by Properly Storing and Mining Call Center Data
Growing Intelligence by Properly Storing and Mining Call Center DataGrowing Intelligence by Properly Storing and Mining Call Center Data
Growing Intelligence by Properly Storing and Mining Call Center DataBay Bridge Decision Technologies
 
The proper analysis approach for life data
The proper analysis approach for life dataThe proper analysis approach for life data
The proper analysis approach for life dataASQ Reliability Division
 
PAC 2020 Santorin - Vasilis Chatzinasios
PAC 2020 Santorin - Vasilis ChatzinasiosPAC 2020 Santorin - Vasilis Chatzinasios
PAC 2020 Santorin - Vasilis ChatzinasiosNeotys
 
Redis rise of Dataops
Redis rise of DataopsRedis rise of Dataops
Redis rise of Dataopslandoop
 
PAC 2020 Santorin - Stijn Schepers
PAC 2020 Santorin - Stijn SchepersPAC 2020 Santorin - Stijn Schepers
PAC 2020 Santorin - Stijn SchepersNeotys
 

Similar to Backtesting Trading Strategies with R (18)

Lulu.com.java.j2 ee.job.interview.companion.2nd.edition.apr.2007
Lulu.com.java.j2 ee.job.interview.companion.2nd.edition.apr.2007Lulu.com.java.j2 ee.job.interview.companion.2nd.edition.apr.2007
Lulu.com.java.j2 ee.job.interview.companion.2nd.edition.apr.2007
 
One java book
One java bookOne java book
One java book
 
Java j2ee job interview companion ebook
Java j2ee job interview companion ebookJava j2ee job interview companion ebook
Java j2ee job interview companion ebook
 
Workshop APM in a Cloud & Virtualized environment
Workshop APM in a Cloud & Virtualized environmentWorkshop APM in a Cloud & Virtualized environment
Workshop APM in a Cloud & Virtualized environment
 
Ebs architecture con9036_pdf_9036_0001
Ebs architecture con9036_pdf_9036_0001Ebs architecture con9036_pdf_9036_0001
Ebs architecture con9036_pdf_9036_0001
 
Jeet Shah - CV
Jeet Shah - CVJeet Shah - CV
Jeet Shah - CV
 
Presentation Summarizer: A Full-Fledged NLP Service
Presentation Summarizer: A Full-Fledged NLP ServicePresentation Summarizer: A Full-Fledged NLP Service
Presentation Summarizer: A Full-Fledged NLP Service
 
SAP Business Objects Planning and Consolidaton, Version for SAP NetWeaver
SAP Business Objects Planning and Consolidaton, Version for SAP NetWeaverSAP Business Objects Planning and Consolidaton, Version for SAP NetWeaver
SAP Business Objects Planning and Consolidaton, Version for SAP NetWeaver
 
Sentiment Analysis using Naïve Bayes, CNN, SVM
Sentiment Analysis using Naïve Bayes, CNN, SVMSentiment Analysis using Naïve Bayes, CNN, SVM
Sentiment Analysis using Naïve Bayes, CNN, SVM
 
DevOps Challenges and Mitigation
DevOps Challenges and MitigationDevOps Challenges and Mitigation
DevOps Challenges and Mitigation
 
Named Entity Recognition (NER) Using Automatic Summarization of Resumes
Named Entity Recognition (NER) Using Automatic Summarization of ResumesNamed Entity Recognition (NER) Using Automatic Summarization of Resumes
Named Entity Recognition (NER) Using Automatic Summarization of Resumes
 
Growing Intelligence by Properly Storing and Mining Call Center Data
Growing Intelligence by Properly Storing and Mining Call Center DataGrowing Intelligence by Properly Storing and Mining Call Center Data
Growing Intelligence by Properly Storing and Mining Call Center Data
 
The proper analysis approach for life data
The proper analysis approach for life dataThe proper analysis approach for life data
The proper analysis approach for life data
 
PAC 2020 Santorin - Vasilis Chatzinasios
PAC 2020 Santorin - Vasilis ChatzinasiosPAC 2020 Santorin - Vasilis Chatzinasios
PAC 2020 Santorin - Vasilis Chatzinasios
 
R tutorial
R tutorialR tutorial
R tutorial
 
Redis rise of Dataops
Redis rise of DataopsRedis rise of Dataops
Redis rise of Dataops
 
Sreerag dot net vs j2ee
Sreerag   dot net vs j2eeSreerag   dot net vs j2ee
Sreerag dot net vs j2ee
 
PAC 2020 Santorin - Stijn Schepers
PAC 2020 Santorin - Stijn SchepersPAC 2020 Santorin - Stijn Schepers
PAC 2020 Santorin - Stijn Schepers
 

Recently uploaded

Dharavi Russian callg Girls, { 09892124323 } || Call Girl In Mumbai ...
Dharavi Russian callg Girls, { 09892124323 } || Call Girl In Mumbai ...Dharavi Russian callg Girls, { 09892124323 } || Call Girl In Mumbai ...
Dharavi Russian callg Girls, { 09892124323 } || Call Girl In Mumbai ...Pooja Nehwal
 
02_Fabio Colombo_Accenture_MeetupDora&Cybersecurity.pptx
02_Fabio Colombo_Accenture_MeetupDora&Cybersecurity.pptx02_Fabio Colombo_Accenture_MeetupDora&Cybersecurity.pptx
02_Fabio Colombo_Accenture_MeetupDora&Cybersecurity.pptxFinTech Belgium
 
(ANIKA) Budhwar Peth Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANIKA) Budhwar Peth Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANIKA) Budhwar Peth Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANIKA) Budhwar Peth Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
VIP Call Girls LB Nagar ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With Room...
VIP Call Girls LB Nagar ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With Room...VIP Call Girls LB Nagar ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With Room...
VIP Call Girls LB Nagar ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With Room...Suhani Kapoor
 
(DIYA) Bhumkar Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(DIYA) Bhumkar Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(DIYA) Bhumkar Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(DIYA) Bhumkar Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
00_Main ppt_MeetupDORA&CyberSecurity.pptx
00_Main ppt_MeetupDORA&CyberSecurity.pptx00_Main ppt_MeetupDORA&CyberSecurity.pptx
00_Main ppt_MeetupDORA&CyberSecurity.pptxFinTech Belgium
 
Quarter 4- Module 3 Principles of Marketing
Quarter 4- Module 3 Principles of MarketingQuarter 4- Module 3 Principles of Marketing
Quarter 4- Module 3 Principles of MarketingMaristelaRamos12
 
Best VIP Call Girls Noida Sector 18 Call Me: 8448380779
Best VIP Call Girls Noida Sector 18 Call Me: 8448380779Best VIP Call Girls Noida Sector 18 Call Me: 8448380779
Best VIP Call Girls Noida Sector 18 Call Me: 8448380779Delhi Call girls
 
Q3 2024 Earnings Conference Call and Webcast Slides
Q3 2024 Earnings Conference Call and Webcast SlidesQ3 2024 Earnings Conference Call and Webcast Slides
Q3 2024 Earnings Conference Call and Webcast SlidesMarketing847413
 
Independent Call Girl Number in Kurla Mumbai📲 Pooja Nehwal 9892124323 💞 Full ...
Independent Call Girl Number in Kurla Mumbai📲 Pooja Nehwal 9892124323 💞 Full ...Independent Call Girl Number in Kurla Mumbai📲 Pooja Nehwal 9892124323 💞 Full ...
Independent Call Girl Number in Kurla Mumbai📲 Pooja Nehwal 9892124323 💞 Full ...Pooja Nehwal
 
Andheri Call Girls In 9825968104 Mumbai Hot Models
Andheri Call Girls In 9825968104 Mumbai Hot ModelsAndheri Call Girls In 9825968104 Mumbai Hot Models
Andheri Call Girls In 9825968104 Mumbai Hot Modelshematsharma006
 
The Economic History of the U.S. Lecture 17.pdf
The Economic History of the U.S. Lecture 17.pdfThe Economic History of the U.S. Lecture 17.pdf
The Economic History of the U.S. Lecture 17.pdfGale Pooley
 
Log your LOA pain with Pension Lab's brilliant campaign
Log your LOA pain with Pension Lab's brilliant campaignLog your LOA pain with Pension Lab's brilliant campaign
Log your LOA pain with Pension Lab's brilliant campaignHenry Tapper
 
The Economic History of the U.S. Lecture 30.pdf
The Economic History of the U.S. Lecture 30.pdfThe Economic History of the U.S. Lecture 30.pdf
The Economic History of the U.S. Lecture 30.pdfGale Pooley
 
Malad Call Girl in Services 9892124323 | ₹,4500 With Room Free Delivery
Malad Call Girl in Services  9892124323 | ₹,4500 With Room Free DeliveryMalad Call Girl in Services  9892124323 | ₹,4500 With Room Free Delivery
Malad Call Girl in Services 9892124323 | ₹,4500 With Room Free DeliveryPooja Nehwal
 
20240429 Calibre April 2024 Investor Presentation.pdf
20240429 Calibre April 2024 Investor Presentation.pdf20240429 Calibre April 2024 Investor Presentation.pdf
20240429 Calibre April 2024 Investor Presentation.pdfAdnet Communications
 
Vip Call US 📞 7738631006 ✅Call Girls In Sakinaka ( Mumbai )
Vip Call US 📞 7738631006 ✅Call Girls In Sakinaka ( Mumbai )Vip Call US 📞 7738631006 ✅Call Girls In Sakinaka ( Mumbai )
Vip Call US 📞 7738631006 ✅Call Girls In Sakinaka ( Mumbai )Pooja Nehwal
 
Call US 📞 9892124323 ✅ Kurla Call Girls In Kurla ( Mumbai ) secure service
Call US 📞 9892124323 ✅ Kurla Call Girls In Kurla ( Mumbai ) secure serviceCall US 📞 9892124323 ✅ Kurla Call Girls In Kurla ( Mumbai ) secure service
Call US 📞 9892124323 ✅ Kurla Call Girls In Kurla ( Mumbai ) secure servicePooja Nehwal
 
OAT_RI_Ep19 WeighingTheRisks_Apr24_TheYellowMetal.pptx
OAT_RI_Ep19 WeighingTheRisks_Apr24_TheYellowMetal.pptxOAT_RI_Ep19 WeighingTheRisks_Apr24_TheYellowMetal.pptx
OAT_RI_Ep19 WeighingTheRisks_Apr24_TheYellowMetal.pptxhiddenlevers
 

Recently uploaded (20)

Dharavi Russian callg Girls, { 09892124323 } || Call Girl In Mumbai ...
Dharavi Russian callg Girls, { 09892124323 } || Call Girl In Mumbai ...Dharavi Russian callg Girls, { 09892124323 } || Call Girl In Mumbai ...
Dharavi Russian callg Girls, { 09892124323 } || Call Girl In Mumbai ...
 
02_Fabio Colombo_Accenture_MeetupDora&Cybersecurity.pptx
02_Fabio Colombo_Accenture_MeetupDora&Cybersecurity.pptx02_Fabio Colombo_Accenture_MeetupDora&Cybersecurity.pptx
02_Fabio Colombo_Accenture_MeetupDora&Cybersecurity.pptx
 
(ANIKA) Budhwar Peth Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANIKA) Budhwar Peth Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANIKA) Budhwar Peth Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANIKA) Budhwar Peth Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
VIP Call Girls LB Nagar ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With Room...
VIP Call Girls LB Nagar ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With Room...VIP Call Girls LB Nagar ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With Room...
VIP Call Girls LB Nagar ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With Room...
 
(DIYA) Bhumkar Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(DIYA) Bhumkar Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(DIYA) Bhumkar Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(DIYA) Bhumkar Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
00_Main ppt_MeetupDORA&CyberSecurity.pptx
00_Main ppt_MeetupDORA&CyberSecurity.pptx00_Main ppt_MeetupDORA&CyberSecurity.pptx
00_Main ppt_MeetupDORA&CyberSecurity.pptx
 
Quarter 4- Module 3 Principles of Marketing
Quarter 4- Module 3 Principles of MarketingQuarter 4- Module 3 Principles of Marketing
Quarter 4- Module 3 Principles of Marketing
 
Best VIP Call Girls Noida Sector 18 Call Me: 8448380779
Best VIP Call Girls Noida Sector 18 Call Me: 8448380779Best VIP Call Girls Noida Sector 18 Call Me: 8448380779
Best VIP Call Girls Noida Sector 18 Call Me: 8448380779
 
Q3 2024 Earnings Conference Call and Webcast Slides
Q3 2024 Earnings Conference Call and Webcast SlidesQ3 2024 Earnings Conference Call and Webcast Slides
Q3 2024 Earnings Conference Call and Webcast Slides
 
Independent Call Girl Number in Kurla Mumbai📲 Pooja Nehwal 9892124323 💞 Full ...
Independent Call Girl Number in Kurla Mumbai📲 Pooja Nehwal 9892124323 💞 Full ...Independent Call Girl Number in Kurla Mumbai📲 Pooja Nehwal 9892124323 💞 Full ...
Independent Call Girl Number in Kurla Mumbai📲 Pooja Nehwal 9892124323 💞 Full ...
 
Andheri Call Girls In 9825968104 Mumbai Hot Models
Andheri Call Girls In 9825968104 Mumbai Hot ModelsAndheri Call Girls In 9825968104 Mumbai Hot Models
Andheri Call Girls In 9825968104 Mumbai Hot Models
 
Veritas Interim Report 1 January–31 March 2024
Veritas Interim Report 1 January–31 March 2024Veritas Interim Report 1 January–31 March 2024
Veritas Interim Report 1 January–31 March 2024
 
The Economic History of the U.S. Lecture 17.pdf
The Economic History of the U.S. Lecture 17.pdfThe Economic History of the U.S. Lecture 17.pdf
The Economic History of the U.S. Lecture 17.pdf
 
Log your LOA pain with Pension Lab's brilliant campaign
Log your LOA pain with Pension Lab's brilliant campaignLog your LOA pain with Pension Lab's brilliant campaign
Log your LOA pain with Pension Lab's brilliant campaign
 
The Economic History of the U.S. Lecture 30.pdf
The Economic History of the U.S. Lecture 30.pdfThe Economic History of the U.S. Lecture 30.pdf
The Economic History of the U.S. Lecture 30.pdf
 
Malad Call Girl in Services 9892124323 | ₹,4500 With Room Free Delivery
Malad Call Girl in Services  9892124323 | ₹,4500 With Room Free DeliveryMalad Call Girl in Services  9892124323 | ₹,4500 With Room Free Delivery
Malad Call Girl in Services 9892124323 | ₹,4500 With Room Free Delivery
 
20240429 Calibre April 2024 Investor Presentation.pdf
20240429 Calibre April 2024 Investor Presentation.pdf20240429 Calibre April 2024 Investor Presentation.pdf
20240429 Calibre April 2024 Investor Presentation.pdf
 
Vip Call US 📞 7738631006 ✅Call Girls In Sakinaka ( Mumbai )
Vip Call US 📞 7738631006 ✅Call Girls In Sakinaka ( Mumbai )Vip Call US 📞 7738631006 ✅Call Girls In Sakinaka ( Mumbai )
Vip Call US 📞 7738631006 ✅Call Girls In Sakinaka ( Mumbai )
 
Call US 📞 9892124323 ✅ Kurla Call Girls In Kurla ( Mumbai ) secure service
Call US 📞 9892124323 ✅ Kurla Call Girls In Kurla ( Mumbai ) secure serviceCall US 📞 9892124323 ✅ Kurla Call Girls In Kurla ( Mumbai ) secure service
Call US 📞 9892124323 ✅ Kurla Call Girls In Kurla ( Mumbai ) secure service
 
OAT_RI_Ep19 WeighingTheRisks_Apr24_TheYellowMetal.pptx
OAT_RI_Ep19 WeighingTheRisks_Apr24_TheYellowMetal.pptxOAT_RI_Ep19 WeighingTheRisks_Apr24_TheYellowMetal.pptx
OAT_RI_Ep19 WeighingTheRisks_Apr24_TheYellowMetal.pptx
 

Backtesting Trading Strategies with R

  • 1. introduction Connection and data The quest Final Comments Trading Strategies using R The quest for the holy grail Eran Raviv Econometric Institute - Erasmus University, http://eranraviv.com April 02, 2012 Eran Raviv Trading Strategies using R April 02, 2012
  • 2. introduction Connection and data The quest Final Comments Outline for section 1 1 introduction 2 Connection and data 3 The quest Sign Prediction Filtering Time Series Analysis Pairs Trading 4 Final Comments Eran Raviv Trading Strategies using R April 02, 2012
  • 3. Cumulative Returns introduction Connection and data The quest 0.2 Final Comments (very) Limited Success Decision was 0.15 made to reduce volume 0.1 cumsum 0.05 0 1 5 9 13 17 21 25 29 33 37 41 45 49 53 57 61 65 69 73 77 81 85 89 93 97 101 105 109 -0.05 Eran Raviv Trading Strategies using R April 02, 2012
  • 4. introduction Connection and data The quest Final Comments Outline for section 2 1 introduction 2 Connection and data 3 The quest Sign Prediction Filtering Time Series Analysis Pairs Trading 4 Final Comments Eran Raviv Trading Strategies using R April 02, 2012
  • 5. introduction Connection and data The quest Final Comments For Inter-day, yahoo is fine nam = c ( 'AON ' , 'M C' , ' AKS ' , 'BAC ' , . . . ) ; t c k r = s o r t (nam) M # Most r e c e n t 252 days : end<− f o r m a t ( Sys . Date ( ) , ”%Y m −% −%d” ) # yyyy− −ddmm s t a r t<−f o r m a t ( Sys . Date ( ) − 3 6 5 , ”%Y m−% −%d” ) l = length ( tckr ) dat = a r r a y ( dim = c ( 2 5 2 , 6 , l ) ) for ( i in 1: l ){ dat0 = ( getSymbols ( t c k r [ i ] , s r c=” yahoo ” , from=s t a r t , t o=end , auto . a s s i g n = FALSE) ) # C anc e l auto . a s s i g n i f you want t o manipulate the o b j e c t dat [ 1 : l e n g t h ( dat0 [ , 2 ] ) , , i ] = dat0 [ , 2 : 6 ] } dat = dat [ 1 : l e n g t h ( na . omit ( dat [ , 1 , 1 ] ) ) , , ] Eran Raviv Trading Strategies using R April 02, 2012
  • 6. introduction Connection and data The quest Final Comments For Intra-day use IB IB has extensive API. Connect to their trading platform (TWS) using Java and C among others. Eran Raviv Trading Strategies using R April 02, 2012
  • 7. introduction Connection and data The quest Final Comments For Intra-day use IB IB has extensive API. Connect to their trading platform (TWS) using Java and C among others. Eran Raviv Trading Strategies using R April 02, 2012
  • 8. introduction Connection and data The quest Final Comments For Intra-day use IB IB has extensive API. Connect to their trading platform (TWS) using Java and C among others. Account is not that easy to set up, many forms to fill out and hefty sum to transfer, especially if you would like to day trade. Eran Raviv Trading Strategies using R April 02, 2012
  • 9. introduction Connection and data The quest Final Comments For Intra-day use IB IB has extensive API. Connect to their trading platform (TWS) using Java and C among others. Account is not that easy to set up, many forms to fill out and hefty sum to transfer, especially if you would like to day trade. Eran Raviv Trading Strategies using R April 02, 2012
  • 10. introduction Connection and data The quest Final Comments For Intra-day use IB IB has extensive API. Connect to their trading platform (TWS) using Java and C among others. Account is not that easy to set up, many forms to fill out and hefty sum to transfer, especially if you would like to day trade. Jeffrey A. Ryan did outstanding work, we can now trade via R. Eran Raviv Trading Strategies using R April 02, 2012
  • 11. introduction Connection and data The quest Final Comments For Intra-day use IB Easy: l i b r a r y ( IBrokers ) I B r o k e r s v e r s i o n 0 . 9 − 1 : Implementing API V e r s i o n 9 . 6 4 This s o f t w a r e comes with NO WARRANTY. Not i n t e n d e d f o r p r o d u c t i o n u s e ! See ? I B r o k e r s f o r d e t a i l s } con = twsConnect ( c l i e n t I d = 1 , h o s t = ' l o c a l h o s t ' , p o r t = 7 4 9 6 , v e r b o s e = TRUE, t i m e o u t = 5 , f i l e n a m e = NULL) Eran Raviv Trading Strategies using R April 02, 2012
  • 12. introduction Connection and data The quest Final Comments For Intra-day use IB Easy: l i b r a r y ( IBrokers ) I B r o k e r s v e r s i o n 0 . 9 − 1 : Implementing API V e r s i o n 9 . 6 4 This s o f t w a r e comes with NO WARRANTY. Not i n t e n d e d f o r p r o d u c t i o n u s e ! See ? I B r o k e r s f o r d e t a i l s } con = twsConnect ( c l i e n t I d = 1 , h o s t = ' l o c a l h o s t ' , p o r t = 7 4 9 6 , v e r b o s e = TRUE, t i m e o u t = 5 , f i l e n a m e = NULL) High frequency data if you have the patience to program it. Eran Raviv Trading Strategies using R April 02, 2012
  • 13. introduction Connection and data The quest Final Comments For Intra-day use IB Easy: l i b r a r y ( IBrokers ) I B r o k e r s v e r s i o n 0 . 9 − 1 : Implementing API V e r s i o n 9 . 6 4 This s o f t w a r e comes with NO WARRANTY. Not i n t e n d e d f o r p r o d u c t i o n u s e ! See ? I B r o k e r s f o r d e t a i l s } con = twsConnect ( c l i e n t I d = 1 , h o s t = ' l o c a l h o s t ' , p o r t = 7 4 9 6 , v e r b o s e = TRUE, t i m e o u t = 5 , f i l e n a m e = NULL) High frequency data if you have the patience to program it. Limitation on the number of requests. Eran Raviv Trading Strategies using R April 02, 2012
  • 14. introduction Connection and data The quest Final Comments For Intra-day use IB Easy: l i b r a r y ( IBrokers ) I B r o k e r s v e r s i o n 0 . 9 − 1 : Implementing API V e r s i o n 9 . 6 4 This s o f t w a r e comes with NO WARRANTY. Not i n t e n d e d f o r p r o d u c t i o n u s e ! See ? I B r o k e r s f o r d e t a i l s } con = twsConnect ( c l i e n t I d = 1 , h o s t = ' l o c a l h o s t ' , p o r t = 7 4 9 6 , v e r b o s e = TRUE, t i m e o u t = 5 , f i l e n a m e = NULL) High frequency data if you have the patience to program it. Limitation on the number of requests. In any case not more than one year, but you can store it. Eran Raviv Trading Strategies using R April 02, 2012
  • 15. introduction Connection and data The quest Final Comments For Intra-day use IB Easy: l i b r a r y ( IBrokers ) I B r o k e r s v e r s i o n 0 . 9 − 1 : Implementing API V e r s i o n 9 . 6 4 This s o f t w a r e comes with NO WARRANTY. Not i n t e n d e d f o r p r o d u c t i o n u s e ! See ? I B r o k e r s f o r d e t a i l s } con = twsConnect ( c l i e n t I d = 1 , h o s t = ' l o c a l h o s t ' , p o r t = 7 4 9 6 , v e r b o s e = TRUE, t i m e o u t = 5 , f i l e n a m e = NULL) High frequency data if you have the patience to program it. Limitation on the number of requests. In any case not more than one year, but you can store it. Professional yahoo group at: http://finance.groups.yahoo.com/group/TWSAPI/ Eran Raviv Trading Strategies using R April 02, 2012
  • 16. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Outline for section 3 1 introduction 2 Connection and data 3 The quest Sign Prediction Filtering Time Series Analysis Pairs Trading 4 Final Comments Eran Raviv Trading Strategies using R April 02, 2012
  • 17. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Selected Ideas Over the years I have backtested many ideas, among others: Sign Prediction Eran Raviv Trading Strategies using R April 02, 2012
  • 18. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Selected Ideas Over the years I have backtested many ideas, among others: Sign Prediction Filtering Eran Raviv Trading Strategies using R April 02, 2012
  • 19. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Selected Ideas Over the years I have backtested many ideas, among others: Sign Prediction Filtering Multivariate time series modelling Eran Raviv Trading Strategies using R April 02, 2012
  • 20. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Selected Ideas Over the years I have backtested many ideas, among others: Sign Prediction Filtering Multivariate time series modelling Pairs trading Eran Raviv Trading Strategies using R April 02, 2012
  • 21. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Selected Ideas Over the years I have backtested many ideas, among others: Sign Prediction Filtering Multivariate time series modelling Pairs trading Eran Raviv Trading Strategies using R April 02, 2012
  • 22. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Selected Ideas Over the years I have backtested many ideas, among others: Sign Prediction Filtering Multivariate time series modelling Pairs trading Born to trade, forced to work. Eran Raviv Trading Strategies using R April 02, 2012
  • 23. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Table of Contents 1 introduction 2 Connection and data 3 The quest Sign Prediction Filtering Time Series Analysis Pairs Trading 4 Final Comments Eran Raviv Trading Strategies using R April 02, 2012
  • 24. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Sign Prediction Sign prediction using: Logistic Regression (glm) Support Vector Machine (svm) ♣ library(e1071) K-Nearest Neighbour (knn) ♣ library(class) Neural Networks (nnet) ♣ library(nnet) Eran Raviv Trading Strategies using R April 02, 2012
  • 25. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Sign Prediction - continued Working with daily returns, so target is to predict tomorrow’s move. (Avoid overnight) Explanatory variables considered: I five lags (one week) II Spread between the volume and the rolling average of most recent 5 days. III Volatility - average of the last five days. Eran Raviv Trading Strategies using R April 02, 2012
  • 26. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Sign Prediction - continued Volatility is measured as the average of three different intra-day volatility measures which are more efficient (converge faster) than the standard ”sd” estimate: Parkinson (1980): 1 N hi 2 σ= 4N ln2 i=1 (ln li ) Eran Raviv Trading Strategies using R April 02, 2012
  • 27. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Sign Prediction - continued Volatility is measured as the average of three different intra-day volatility measures which are more efficient (converge faster) than the standard ”sd” estimate: Parkinson (1980): 1 N hi 2 σ= 4N ln2 i=1 (ln li ) German Klass (1980): 1 N 1 hi 2 1 N ci σ= N i=1 2 (ln li ) − N i=1 (2ln2 − 1)(ln ci−1 )2 Eran Raviv Trading Strategies using R April 02, 2012
  • 28. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Sign Prediction - continued Volatility is measured as the average of three different intra-day volatility measures which are more efficient (converge faster) than the standard ”sd” estimate: Parkinson (1980): 1 N hi 2 σ= 4N ln2 i=1 (ln li ) German Klass (1980): 1 N 1 hi 2 1 N ci σ= N i=1 2 (ln li ) − N i=1 (2ln2 − 1)(ln ci−1 )2 Rogers and satchell (1991): 1 N hi hi l l σ= N i=1 (ln li )(ln oi ) + (ln cii )(ln oii ) Eran Raviv Trading Strategies using R April 02, 2012
  • 29. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Sign Prediction - continued dat0 = ( getSymbols ( t c k r [ 1 ] , s r c=” yahoo ” , from=s t a r t , t o=end , auto . a s s i g n = FALSE) ) l = l e n g t h ( dat0 [ , 1 ] ) d a t e s 0 = ( i n d e x ( dat0 ) ) # t r i c k t o g e t t r a d i n g d a t e s t t = NULL # we now p a r s e i t i n t o IB mode # for ( i in 1: l ){ tt [ i ] = paste ( substr ( dates0 [ i ] , 1 , 4 ) , substr ( dates0 [ i ] , 6 , 7 ) , s u b st r ( dates0 [ i ] , 9 , 1 0 ) , sep = ”” ) t t [ i ] = p a s t e ( t t [ i ] , ” 2 3 : 0 0 : 0 0 GMT” ) } c o n t=t ws Eq ui ty ( ' p l u g your f a v o u r i t e symbol ' , 'SMART ' , ' NYSE ' ) mat1 = a r r a y ( dim = c ( l , 4 0 0 , 8 ) )#T y p i c a l day s h o u l d have 390 mins for ( i in 1: l ){ m1 = a s . m a t r i x ( r e q H i s t o r i c a l D a t a ( con , cont , t t [ i ] , b a r S i z e = ” 1 min” , d u r a t i o n = ” 1 d” , useRTH = ” 1 ” , whatToShow = ”TRADES” , time . f o r m a t = ” 1 ” , v e r b o s e = TRUE) ) mat1 [ i , 1 : l e n g t h (m1 [ , 1 ] ) , ] = m1 Sys . s l e e p ( 1 4 ) # IB r e s t r i c t i o n , WAIT. # } Eran Raviv Trading Strategies using R April 02, 2012
  • 30. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Sign Prediction - continued Sample code: l o g i t 1 = glm ( y˜ l a g y+v o l a t+volume , data=dat [ 1 : t1 , ] , f a m i l y= b i n o m i a l ( l i n k = ” l o g i t ” ) , na . a c t i o n=na . p a s s ) summary ( l o g i t 1 ) #t 1 i s end o f t r a i n i n g , TT i s f u l l l e n g t h . l i b r a r y ( nnet ) nnet1 = nnet ( a s . f a c t o r ( y ) ˜ l a g y+v o l a t+volume , data=dat [ 1 : t1 , ] , s i z e =1 , t r a c e=T) summary ( nnet1 ) library ( class ) knn1 = knn ( dat [ 1 : t1 , ] , dat [ ( t 1 +1) : TT, ] , c l = dat $ y [ 1 : t 1 ] , k=25 , prob=F) sum ( knn1==dat $ y [ ( t 1 +1) ] ) / (TT 1 +1)#H i t r a t i o −t l i b r a r y ( e1071 ) svm1 = svm ( dat [ 1 : t1 , 2 : 4 ] , y=dat [ 1 : t1 , 1 ] , t y p e = ”C” ) # I n sample : sum ( svm1 $ f i t==dat $ y [ ( 1 ) : t 1 ] ) / t 1 # out o f sample : svmpred=p r e d i c t ( svm1 , newdata = dat [ ( t 1 +1) : TT, 2 : 4 ] ) sum ( svmpred==dat $ y [ ( t 1 +1) :TT ] ) / (TT 1 +1)#H i t r a t i o −t Eran Raviv Trading Strategies using R April 02, 2012
  • 31. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Table of Contents 1 introduction 2 Connection and data 3 The quest Sign Prediction Filtering Time Series Analysis Pairs Trading 4 Final Comments Eran Raviv Trading Strategies using R April 02, 2012
  • 32. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Deviation from the mean Motivation =⇒ Disposition effect, the Voodoo of financial markets. Standardise the deviation from the (rolling) mean. Eran Raviv Trading Strategies using R April 02, 2012
  • 33. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Deviation from the mean Google qqq 650 q q q qq q qqq qqq qq qqq q qq q qqq q q q qq qqqq q qq qq qqq q qq q q q q q q q q q q qq q q q q q q q q q qq q q q q q q q q q q qq q q q q qq qqq qq q q qq q q q qqqqqqq q q qqq 600 q q q q qq q qq qq qqq qq q q qq qq qq qq qqq q q qq q q qq q q qq q q qqqqq q qq q q q qq q qq qq q q q qqq q q q q qq q qq qqqqqqqqq qqq q qqqq q q qq q q qq q q q q q q q q qq qq qqq qq qq qq q q q q q q q q qq q q Price qqq qq q q q q q qq q q qqqqqq q qqq qqqq qq q q q qq q q q q q q q q q q q q q q q q q q qq q q q qq q q q q q 550 q q q q qq qq q q q q q q q q q q qq q q qq q q qq q qq q q q q q q q q qqq q q qqqq q q q q q qq q qq q qqqq qqqq qqq qq q q qqq qqq q q qqq q q q qqq qq q q q q q q q q q q qq q q q qqq qqqq q q qqqq q q q q q qq q q qq q q q qq q qq q q q q q qq q qqq qq q q q q q q q q q q q q q q qq q q qqq q q q qq 500 q q q q qqqq q qqq qq q qq q q q q q q qqq q q 0 50 100 150 200 250 Days Histogram for Z 20 q q q q q q q q q q q q q 2 q q q q q q qq q 15 q qq q q q qq q qq q q qq q qq q q q q q qq q q q q qq q q q q q q qq q q q q q qq q q q q q q q q qq qqq Frequency qq q qq qq q q q q qq q q q q qq q q q q q q q qq q q qq qq q q q q q q q q q 0 q q q q q q qq qq q q q 10 q q q q q q qq q q Z q qq q q q q q q q q q qq q q qq q q q q q q q q q q q q q q qq q q q q q qqq q q q q qq q q q qq q q q q q q q qq q q qq q q q q q q −2 q qqq qq qq q q q q q q qq q q q qq q q 5 q q q q q qq q q q q q −4 0 q −4 −2 0 2 0 50 100 150 200 250 Days Eran Raviv Trading Strategies using R April 02, 2012
  • 34. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Table of Contents 1 introduction 2 Connection and data 3 The quest Sign Prediction Filtering Time Series Analysis Pairs Trading 4 Final Comments Eran Raviv Trading Strategies using R April 02, 2012
  • 35. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Motivation Momentum in Microstructure - Dermot Murphy and Ramabhadran S. Thirumalai (Job Market Paper - 2011) Are You Trading Predictably? -Steven L. Heston ,Robert A. Korajczyk ,Ronnie Sadka, Lewis D. Thorson. (2010) Eran Raviv Trading Strategies using R April 02, 2012
  • 36. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Motivation Momentum in Microstructure - Dermot Murphy and Ramabhadran S. Thirumalai (Job Market Paper - 2011) Are You Trading Predictably? -Steven L. Heston ,Robert A. Korajczyk ,Ronnie Sadka, Lewis D. Thorson. (2010) We find predictable patterns in stock returns. Stocks whose relative returns are high in a given half-hour interval today exhibit similar outperformance in the same half-hour period on subsequent days. The effect is stronger at the beginning and end of the trading day. These results suggest... Eran Raviv Trading Strategies using R April 02, 2012
  • 37. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading VAR models For each day t = {1, ..., T }, the return of half an hour k = {1, ..., 13} , and the lag number p = {1, ..., P }:      1 a1,1 a1 1,2 · · · a1   y1,t c1 1,k y1,t−1 y2,t  c2  a1 1 1       2,1 a2,2 · · · a2,k  y2,t−1    . = . + . . .. .  .  + ··· +  .  .  . . . . . . . .  .  . . yk,t ck a1 1 1 k,1 ak,2 · · · ak,k yk,t−1  p p p  a1,1 a1,2 · · · a1,k    y1,t−p e1,t ap p ap  y2,t−p  e2,t   2,1 a2,2 · · · 2,k   .  .  +  .      . . ..  . . . . . .  .   .  . . . p p ak,1 ak,2 · · · ap k,k yk,t−p ek,t Eran Raviv Trading Strategies using R April 02, 2012
  • 38. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading VAR models For each day t = {1, ..., T }, the return of half an hour k = {1, ..., 13} , and the lag number p = {1, ..., P }:      1 a1,1 a1 1,2 · · · a1   y1,t c1 1,k y1,t−1 y2,t  c2  a1 1 1       2,1 a2,2 · · · a2,k  y2,t−1    . = . + . . .. .  .  + ··· +  .  .  . . . . . . . .  .  . . yk,t ck a1 1 1 k,1 ak,2 · · · ak,k yk,t−1  p p p  a1,1 a1,2 · · · a1,k    y1,t−p e1,t ap p ap  y2,t−p  e2,t   2,1 a2,2 · · · 2,k   .  .  +  .      . . ..  . . . . . .  .   .  . . . p p ak,1 ak,2 · · · ap k,k yk,t−p ek,t Problem: for P = 1, how many parameters? Eran Raviv Trading Strategies using R April 02, 2012
  • 39. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading VAR models (cont’d) Possible solution =⇒ Dimension Reduction. Eran Raviv Trading Strategies using R April 02, 2012
  • 40. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading VAR models (cont’d) Possible solution =⇒ Dimension Reduction. Stepwise Regression, Lasso, Variable selection (according to some Information Criteria), Principal Component Regression, Ridge Regression, Bayesian VAR and many more. Very nice vars package to start you off, though as most built-ins, not flexible enough. (e.g. rolling windows and/or shrinking) Eran Raviv Trading Strategies using R April 02, 2012
  • 41. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Table of Contents 1 introduction 2 Connection and data 3 The quest Sign Prediction Filtering Time Series Analysis Pairs Trading 4 Final Comments Eran Raviv Trading Strategies using R April 02, 2012
  • 42. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Pairs Trading Well known and widely used. (e.g. Statistical Arbitrage in the U.S. Equities Market, Marco Avellaneda and Jeong-Hyun Lee (2008)) Eran Raviv Trading Strategies using R April 02, 2012
  • 43. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Pairs Trading Well known and widely used. (e.g. Statistical Arbitrage in the U.S. Equities Market, Marco Avellaneda and Jeong-Hyun Lee (2008)) Suitable for the conservative mind. (we see why in a minute..) Eran Raviv Trading Strategies using R April 02, 2012
  • 44. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Pairs Trading Well known and widely used. (e.g. Statistical Arbitrage in the U.S. Equities Market, Marco Avellaneda and Jeong-Hyun Lee (2008)) Suitable for the conservative mind. (we see why in a minute..) Eran Raviv Trading Strategies using R April 02, 2012
  • 45. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Pairs Trading (cont’d) The Idea: ra = βa rm + ea rb = βb rm + eb rab = wa (βa rm + ea ) + wb (βb rm + ea ) = rm (wa βa + wb βb ) + noise and so with weights wa = − βaβb b and wb = 1 − wa we can −β net out the market. (and other factors if you will) Eran Raviv Trading Strategies using R April 02, 2012
  • 46. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Pairs Trading (cont’d) Choose symbols with similar properties. Net out the market and create the spread: # sp1 = s t o c k p r i c e 1 , g=s i z e o f moving window , # # n = l e n g t h ( sp1 ) # for ( i in g : n){ b e t 0 [ i ]=lm ( sp1 [ ( i −g+1) : ( i −1) ] ˜ sp2 [ ( i −g+1) : ( i −1) ] ) $ c o e f [ 1 ] # # n o t e −> i −1 b e t 1 [ i ]=lm ( sp1 [ ( i −g+1) : ( i −1) ] ˜ sp2 [ ( i −g+1) : ( i −1) ] ) $ c o e f [ 2 ] s p r e a d [ , i ]= sp1 [ ( i −g+1) : i ]− r e p ( b e t 0 [ i ] , g )−b e t 1 [ i ] * sp2 [ ( i −g+1) : i ] } Text book example (actually from: Quantitative Trading: How to Build Your Own Algorithmic Trading Business ) Eran Raviv Trading Strategies using R April 02, 2012
  • 47. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Pairs Trading (cont’d) Choose symbols with similar properties. Net out the market and create the spread: # sp1 = s t o c k p r i c e 1 , g=s i z e o f moving window , # # n = l e n g t h ( sp1 ) # for ( i in g : n){ b e t 0 [ i ]=lm ( sp1 [ ( i −g+1) : ( i −1) ] ˜ sp2 [ ( i −g+1) : ( i −1) ] ) $ c o e f [ 1 ] # # n o t e −> i −1 b e t 1 [ i ]=lm ( sp1 [ ( i −g+1) : ( i −1) ] ˜ sp2 [ ( i −g+1) : ( i −1) ] ) $ c o e f [ 2 ] s p r e a d [ , i ]= sp1 [ ( i −g+1) : i ]− r e p ( b e t 0 [ i ] , g )−b e t 1 [ i ] * sp2 [ ( i −g+1) : i ] } Text book example (actually from: Quantitative Trading: How to Build Your Own Algorithmic Trading Business ) The GLD and GDX spread Eran Raviv Trading Strategies using R April 02, 2012
  • 48. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Pairs Trading (cont’d) The GLD and GDX spread: Eran Raviv Trading Strategies using R April 02, 2012
  • 49. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Pairs Trading Issues Estimation of the market neutral portfolio is tricky: Price levels or price changes? Eran Raviv Trading Strategies using R April 02, 2012
  • 50. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Pairs Trading Issues Estimation of the market neutral portfolio is tricky: Price levels or price changes? Stability over time Eran Raviv Trading Strategies using R April 02, 2012
  • 51. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Pairs Trading Issues Estimation of the market neutral portfolio is tricky: Price levels or price changes? Stability over time Errors on both sides. (both y and x are measured with errors) Eran Raviv Trading Strategies using R April 02, 2012
  • 52. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Pairs trading issues Stability over time: Eran Raviv Trading Strategies using R April 02, 2012
  • 53. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Pairs trading issues Errors on both sides: sta = αstb + ea stb = βsta + eb 1 α = β ⇓ Portfolio is different and will depend on which instrument goes on the LHS and which on the RHS. Eran Raviv Trading Strategies using R April 02, 2012
  • 54. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Pairs trading - possible solutions Price levels or price changes? Eran Raviv Trading Strategies using R April 02, 2012
  • 55. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Pairs trading - possible solutions Price levels or price changes? flip a coin (solid option) average the estimates Eran Raviv Trading Strategies using R April 02, 2012
  • 56. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Pairs trading - possible solutions Price levels or price changes? flip a coin (solid option) average the estimates Eran Raviv Trading Strategies using R April 02, 2012
  • 57. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Pairs trading - possible solutions (cont’d) Stability over time Eran Raviv Trading Strategies using R April 02, 2012
  • 58. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Pairs trading - possible solutions (cont’d) Stability over time Choose window length that fits your style, the shorter the more you trade. Recent paper (though in different context) suggests to average estimates across different windows to partially hedge out uncertainty. (M. Hashem Pesaran, Andreas Pick. Journal of Business and Economic Statistics. April 1, 2011) Kalman filter the coefficients. Eran Raviv Trading Strategies using R April 02, 2012
  • 59. introduction Sign Prediction Connection and data Filtering The quest Time Series Analysis Final Comments Pairs Trading Pairs trading - possible solutions (cont’d) Errors on both sides, two highly correlated possible solutions: Demming regression (1943). (Total least squares - just minimize numerically both sides simultaneously) Geometric Mean Regression - force coherence through: sta = αstb + ea stb = βsta + eb 1 γ = α× β Eran Raviv Trading Strategies using R April 02, 2012
  • 60. introduction Connection and data The quest Final Comments Outline for section 4 1 introduction 2 Connection and data 3 The quest Sign Prediction Filtering Time Series Analysis Pairs Trading 4 Final Comments Eran Raviv Trading Strategies using R April 02, 2012
  • 61. introduction Connection and data The quest Final Comments Miscellaneous remarks Trading costs!, consider it when backtesting. Eran Raviv Trading Strategies using R April 02, 2012
  • 62. introduction Connection and data The quest Final Comments Miscellaneous remarks Trading costs!, consider it when backtesting. You cannot be too careful, stay pessimistic. Eran Raviv Trading Strategies using R April 02, 2012
  • 63. introduction Connection and data The quest Final Comments Miscellaneous remarks Trading costs!, consider it when backtesting. You cannot be too careful, stay pessimistic. Adopt rigorous robustness checks, different instruments, different time frames and even different markets. Eran Raviv Trading Strategies using R April 02, 2012
  • 64. introduction Connection and data The quest Final Comments Miscellaneous remarks Trading costs!, consider it when backtesting. You cannot be too careful, stay pessimistic. Adopt rigorous robustness checks, different instruments, different time frames and even different markets. Use paper money for at least a full quarter, it will help you handle operational problems. (e.g. outages and time zones issues) Eran Raviv Trading Strategies using R April 02, 2012
  • 65. introduction Connection and data The quest Final Comments Miscellaneous remarks Trading costs!, consider it when backtesting. You cannot be too careful, stay pessimistic. Adopt rigorous robustness checks, different instruments, different time frames and even different markets. Use paper money for at least a full quarter, it will help you handle operational problems. (e.g. outages and time zones issues) It is (very) stressing work, know it before you start. Eran Raviv Trading Strategies using R April 02, 2012
  • 66. introduction Connection and data The quest Final Comments Miscellaneous remarks Trading costs!, consider it when backtesting. You cannot be too careful, stay pessimistic. Adopt rigorous robustness checks, different instruments, different time frames and even different markets. Use paper money for at least a full quarter, it will help you handle operational problems. (e.g. outages and time zones issues) It is (very) stressing work, know it before you start. Know what you are doing, what is your edge? why it is (not) there? Eran Raviv Trading Strategies using R April 02, 2012
  • 67. introduction Connection and data The quest Final Comments THANKS and good luck at the tables.. Eran Raviv Trading Strategies using R April 02, 2012