https://wiki.pairtradinglab.com/api.php?action=feedcontributions&user=Admin&feedformat=atomPair Trading Lab WIKI - User contributions [en]2023-12-02T07:45:14ZUser contributionsMediaWiki 1.20.5https://wiki.pairtradinglab.com/wiki/Pair_Trading_ModelsPair Trading Models2022-07-20T11:09:53Z<p>Admin: /* Kalman-grid Model (v2) */</p>
<hr />
<div>Pair Trading Lab offers pair trading algorithms based on various mathematical models. These are models currently supported in PTL:<br />
== Ratio Model ==<br />
[[File:Ratio-illustration.png|right|650px|thumb|Ratio Uptick Example]]<br />
This is one of the standard pair trading models described in literature. It is based in ratio of instrument prices, [http://en.wikipedia.org/wiki/Moving_average moving average] and [http://en.wikipedia.org/wiki/Standard_deviation standard deviation]. In other words, it is based on [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator.<br />
<br />
Since Nov 27th 2014, this model also supports additional [http://www.investopedia.com/terms/r/rsi.asp RSI filter] you can use in addition to Bollinger Bands method.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (RSI supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.5, 2.5>, 2.0 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values are allowed (to exit on other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical values is <0, 1>, 0 is used most often...only used for downtick entry mode as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* moving average period P<sub>m</sub> (typical range <10, 100>), default = 15<br />
* moving average type T (algorithm), default = exponential<br />
* standard deviation period P<sub>s</sub> (typical range <10, 100>), default = 15<br />
* entry mode (simple, uptick, downtick)<br />
* RSI period and threshold (optional RSI filtering)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* we need to calculate ratio time series ''R(t)'' = ''A(t)'' / ''B(t)''<br />
* let's apply moving average of type T with period P<sub>m</sub> on ''R(t)'' to get time series ''M(t)''<br />
* let's apply standard deviation with period P<sub>s</sub> on ''R(t)'' to get time series ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = (''R(t)'' - ''M(t)'') / ''S(t)'', this time series can give us z-score to signal trading decision directly (in reality we have two Z-scores: Z-score<sub>ask</sub> and Z-score<sub>bid</sub> as they are calculated using different prices, but for the sake of simplicity let's now pretend we don't pay bid-ask spread and we have just one Z-score)<br />
* another common approach (to visualize) is to create bands and put it above the moving average ''M(t)'':<br />
** upper entry band ''U<sub>n</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>n</sub><br />
** lower entry band ''L<sub>n</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>n</sub><br />
** upper exit band ''U<sub>x</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>x</sub><br />
** lower exit band ''L<sub>x</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>x</sub><br />
** upper downtick band ''U<sub>d</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** lower downtick band ''L<sub>d</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** these bands are actually the same bands as in [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator and we can use crossing of ''R(t)'' and bands as trade signals<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'')<br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'')<br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' < E<sub>n</sub> (same as ''R(t-1)'' < ''U<sub>n</sub>(t-1)'')<br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' > -E<sub>n</sub> (same as ''R(t-1)'' > ''L<sub>n</sub>(t-1)'')<br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside (but it must stay above the downtick band):<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' < ''U<sub>n</sub>(t)'' '''and''' ''R(t-1)'' >= ''U<sub>n</sub>(t-1)'' '''and''' ''R(t)'' > ''U<sub>d</sub>(t)''<br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' > ''L<sub>n</sub>(t)'' '''and''' ''R(t-1)'' <= ''L<sub>n</sub>(t-1)'' '''and''' ''R(t)'' < ''L<sub>d</sub>(t)''<br />
<br />
Why do we have the '''simple entry mode'''? In normal situations and backtests, it gives same results as the '''uptick mode'''. But the difference comes up while trading multiple pairs in portfolio. The simple mode allows you to jump in the position immediately after a new slot is freed, regardless of the previous Z-scores.<br />
<br />
'''Which entry mode is better?''' Hard to tell, sometimes the uptick, sometimes the downtick. You have to do your homework and decide, which idea suits your trading style better. In general, uptick/simple mode is more aggressive, as it does not wait for first signs of spread mean reversion.<br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub> (equivalent to ''R(t)'' <= ''U<sub>x</sub>(t)'')<br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub> (equivalent to ''R(t)'' >= ''L<sub>x</sub>(t)'')<br />
<br />
=== Moving Average Types ===<br />
[[File:Rsi-illustration.png|right|650px|thumb|Ratio RSI Trading Example]]<br />
You can choose from these moving average algorithms:<br />
* [http://www.investopedia.com/terms/s/sma.asp Simple Moving Average] (SMA)<br />
* [http://www.investopedia.com/terms/e/ema.asp Exponential Moving Average] (EMA)<br />
* [http://fxtrade.oanda.com/learn/forex-indicators/weighted-moving-average Weighted Moving Average] (WMA)<br />
* [http://www.investopedia.com/terms/d/double-exponential-moving-average.asp Double Exponential Moving Average] (DEMA)<br />
* [http://www.investopedia.com/terms/t/triple-exponential-moving-average.asp Triple Exponential Moving Average] (TEMA)<br />
* [http://daytrading.about.com/od/indicators/a/Triangular.htm Triangular Moving Average] (TMA)<br />
* [http://etfhq.com/blog/2011/11/07/adaptive-moving-average-ama-aka-kaufman-adaptive-moving-average-kama Kaufman Adaptive Moving Average] (KAMA)<br />
* [http://www.mesasoftware.com/Papers/MAMA.pdf MESA Adaptive Moving Average] (MAMA)<br />
* [http://www.tradeescort.com/AgenaIndicatorHelp/AgenaIndicator.html?T3TripleExponentialMovingAverage.html Triple Exponential T3 Moving Average]<br />
<br />
Which one is the best? It depends, you have to test for yourself. They mostly differ in the term of memory and how fast they react to changes. Industry standard and the default is EMA. We suggest to try all of them on some sample pair to see how they work.<br />
<br />
=== RSI Filtering ===<br />
You can combine the Z-score/Bollinger Band model with [http://www.investopedia.com/terms/r/rsi.asp RSI indicator] applied on the ratio ''R(t)''. RSI filter will be automatically enabled if you set RSI threshold to other value than zero. RSI filter is combined with the Z-score rules using '''AND''' operator ('''both''' entry rule and RSI entry rule must be true to open a position).<br />
<br />
You can also change the RSI period if you want (default period = 15).<br />
<br />
RSI Threshold is value between 0 and 50. Because RSI indicator value oscillates between 0 and 100 (where 50 = mean), your threshold value is just used to set real thresholds for RSI:<br />
* let's assume RSI Period P<sub>r</sub> was entered<br />
* effective RSI value threshold is then 50+P<sub>r</sub> (upper) and 50-P<sub>r</sub> (lower)<br />
* you can see both thresholds in the example image at the right side<br />
<br />
Example:<br />
* RSI Threshold entered is 15<br />
* then, short positions are only opened, if RSI >= 65 (50+15)<br />
* long positions are only opened, if RSI <=35 (50-15)<br />
<br />
Useful hint: if you want to control entry rules just by RSI, you can set Entry Mode to simple and Entry Threshold to some low value.<br />
<br />
== Residual Model ==<br />
[[File:Residual-illustration2.png|right|650px|thumb|Residual Downtick Example]]<br />
Residual mode is based on [http://en.wikipedia.org/wiki/Linear_regression linear regression]. In literature it has been also referred to as the '''cointegration approach'''. Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the OLS method (Ordinary Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
This particular implementation is very simple. The regression is constructed using floating window of a fixed period, the same period is used for calculating the standard deviation.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (uptick and downtick modes are supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <15, 300><br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* first we need to construct a linear regression between ''A(t)'', ''B(t)'' using OLS, where ''A(t)'' = ''&beta;'' * ''B(t)'' + ''&alpha;'' + ''R(t)''<br />
* because we use floating window of period P (we calculate new regression each day), we actually get new series ''&beta;(t)'', ''&alpha;(t)'', ''R(t)'', where ''&beta;(t)'', ''&alpha;(t)'' are series of regression coefficients and ''R(t)'' are residuals (prediction errors)<br />
* ''R(t)'' = ''A(t)'' - (''&beta;(t)'' * ''B(t)'' + ''&alpha;(t)'')<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly (in reality we have two Z-scores: Z-score<sub>ask</sub> and Z-score<sub>bid</sub> as they are calculated using different prices, but for the sake of simplicity let's now pretend we don't pay bid-ask spread and we have just one Z-score)<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside (while we must stay above the downtick band):<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>d</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>d</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman Model ==<br />
This model is based on [http://en.wikipedia.org/wiki/Kalman_filter Kalman Filter]. In this case, the filter is used here instead of linear regression to determine proper hedge ratio, deviation from the mean and the standard deviation of the spread. The advantage is that this filter is superior when dealing with noise compared to OLS or TLS methods, also it does not have any lookback period to optimize.<br />
The disadvantage is that is has another parameters (like &delta;) to find out.<br />
<br />
We strongly recommend to read more details about Kalman filter applications in pair trading in [http://www.amazon.com/Algorithmic-Trading-Winning-Strategies-Rationale/dp/1118460146 this book].<br />
<br />
While Kalman Filter also estimates the standard deviation on the fly, our implementation also supports a possibility of having an auxiliary standard deviation indicator applied directly on the forecast error time series. The advantage is that you get less sensitivity on the &delta; parameter, but on the other hand a new lookback parameter is introduced.<br />
<br />
'''Kalman Model has too sensitive parameters which prevents it to be traded in reality. This model is provided only for education purposes - to be able to see how parameters affects its performance. Use Kalman Grid model for any serious trading.'''<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not planned'''<br />
* in PTL Trader: '''not planned'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter transition covariance &delta;, typical value is 0.0001 - '''unfortunately this model is very sensitive to this parameter especially when using standard deviation estimate coming from the Kalman filter itself'''<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* auxiliary standard deviation period - if equal to zero, Kalman filter is used to estimate standard deviation, if non-zero, auxiliary standard deviation indicator with this period is used (an the estimate from Kalman filter is ignored)<br />
* unstable period = how many Kalman filter observations are ignored at the beginning (first observations are quite unstable)<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1, 2><br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-1, 0>...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >10 if used)<br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model.<br />
<br />
'''This model is provided for education purposes only.'''<br />
<br />
== Kalman-grid Model (v2) ==<br />
<br />
After one year of experience with the [[Pair Trading Models#Kalman-grid Model (v1)|Kalman-grid Model (v1)]] and researching [https://en.wikipedia.org/wiki/Machine_learning machine learning principles], we have spent weeks of additional work on developing new version of Kalman Grid model, redesigned from scratch. Kalman Grid v2 is now the most advanced model PTL offers.<br />
<br />
This model brings the superior performance of Kalman filter design, but it also deals with all the flaws of Kalman and old Kalman Grid v1:<br />
* all parameters of Kalman filter have been eliminated (&delta;, V<sub>e</sub>) - they are auto-estimated by the grid logic and updated with each price sample<br />
* increased average performance compared to v1<br />
* fixed issues of old version which prevented model to trade too often (biggest flaw of v1)<br />
<br />
This model rendered the version 1 obsolete and completely replaces it.<br />
<br />
How does it work? Instead of single strategy (and single Kalman filter), the whole grid of Kalman filters is evaluated by a proprietary system at the same time. Then, machine learning principles are applied to rank particular Kalman filter performances. Another algorithm is then used to figure filter outputs (slope, intercept, standard deviation) from the whole grid. The whole system actually behaves like a single Kalman filter, but with no need to provide any parameters.<br />
<br />
Main advantages of this model:<br />
* it squeezes all profit from the pair<br />
* beats any other models in performance (in average) and also in performance per day-in-position<br />
* no risk of over-fitting in parameter optimization (nothing to optimize, except thresholds)<br />
<br />
Issues of this model:<br />
* you still need to set entry/exit thresholds - basically this affect number of trades and slot usage...exit threshold is usually 0, try entry threshold from range <2,4><br />
* the simple A.I. inside is profit-based - so not-so-good pairs with waves in profit can "fool" the algorithm so it falls from the optimum<br />
* model performance is proportional to margin usage<br />
* evaluating this model needs a lot of memory and processing resources<br />
<br />
Specialties:<br />
* if you set entry threshold to 1, exit threshold to -1, model to "simple" and if you allow reversals, the model will be "always-in-position" squeezing the most profit from good pairs<br />
<br />
To give some insight on performance: we took 300 best performing pairs from PTL database in period Jan 2013 - Jan 2016. Then we backtested all 300 pairs using '''out-of-sample''' period (Jan 2016 - Sep 2016) and we compared their performance. Models used: Ratio(per 14), Residual(20), Kalman Grid v1, Kalman Grid v2 using exit threshold of 0 (normal) and -1 (aggressive). Margin 50% (Req-T). Results here:<br />
{|<br />
|'''Model'''<br />
|'''Median CAGR %'''<br />
|'''Mean CAGR %'''<br />
|-<br />
|Kalman-grid v2 (normal)<br />
|'''7.206409'''<br />
|9.538509<br />
|-<br />
|Kalman-grid v2 (aggressive)<br />
|7.088950<br />
|'''11.433199'''<br />
|-<br />
|Kalman-grid v1<br />
|4.877391<br />
|10.794117<br />
|-<br />
|Residual(20)<br />
|3.596212<br />
|6.631089<br />
|-<br />
|Ratio(14)<br />
|3.590883<br />
|8.954484 <br />
|}<br />
<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''since v1.4.0'''<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1, 4><br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-1, 00>...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band (not used often)<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >10 if used)<br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model.<br />
<br />
== Kalman-Auto Model ==<br />
This model is very similar to Kalman-grid model, it is like his younger brother. The main difference is that the simple A.I. of this model is not trying to work with profit (which is problem with certain kind of pairs). Instead, it justs tries to find optimum &delta; (transition covariance) of the Kalman filter together with the entry threshold to match the '''margin usage target'''. An example: you can set this model to try to optimize everything so the strategy spends 50% time in position (so if you traded one year, the model spent only half a year in positions). Or you can say the average slot usage (margin usage) is 50%. This is useful for trading whole portfolios of Kalman-Auto based strategies, so you can fine tune the diversification inside the portfolio correctly.<br />
<br />
Advantages of this model:<br />
* the only parameter to fine-tune of this model is (apart from the usage target) V<sub>e</sub> (Kalman filter observation covariance), which is luckily not sensitive<br />
* the model is much faster to evaluate than Kalman-Grid<br />
* the simple A.I. of this model is robust and not-so easy to fool<br />
* entry threshold is side-product of the self-optimization!<br />
<br />
This is the model you need if you don't want to spent hours of optimizing parameters. You just tell it to trade and stay in position 60% of the time and that's it. On the other hands, it does not match in profits with Kalman-Grid for very good pairs (but actually it has similar performance when you divide the profit to the margin usage).<br />
<br />
Please note there is no regulation feedback loop inside, the self optimization works just with ring-buffer with follows the current point and it is used for finding best parameters. So do not expect you set the target to 80% and at the end you get 80% portfolio usage. The good working range is <40, 60>.<br />
<br />
Example: [https://www.pairtradinglab.com/backtests/WCGoZyG8rDVDQnY1 Visa/Mastercard backtest] with Usage Target set to 60%. Notice the '''% Days In Trade''' resulted in 64.9%. <br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''since v1.4.0'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman-Auto Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* desired margin usage threshold in %, allowed range <20, 80>, the range <40,60> works the best in terms of how precise is the margin usage matched to the target<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model, but:<br />
* entry threshold is figured automatically by the model<br />
* exit threshold is always 0<br />
* entry mode is always "simple"<br />
<br />
= Deprecated Models =<br />
* Orthogonal Model<br />
* Kalman-grid Model (v1)</div>Adminhttps://wiki.pairtradinglab.com/wiki/PTL_Trader_ChangelogPTL Trader Changelog2021-07-02T09:38:10Z<p>Admin: /* 1.6.0 */</p>
<hr />
<div>== 1.6.0 ==<br />
Since 1.6.0 this project has been released as open-source and hosted on GitHub. Visit this page for further release notes: https://github.com/quantverse/ptltrader/releases<br />
<br />
Released 2021-07-02.<br />
<br />
== 1.5.0 ==<br />
* CFD trading is supported<br />
* fixed missing z-scores in history for Kalman-Grid and Kalman-Auto models<br />
* fixed race condition with portfolio updates shortly after positions are closed (leading to "position mismatch" error and marking the pair as "requiring intervention")<br />
<br />
Released 2017-11-09.<br />
<br />
== 1.4.0 ==<br />
* Kalman-Grid (v2) model fully supported<br />
* Kalman-Auto model fully supported<br />
* removed forgotten last price constraint (causing ''suspicious market data'' state in markets missing last price quotes)<br />
<br />
Released 2016-12-21.<br />
<br />
== 1.3.1 ==<br />
* fixed serious bug causing strategy thread to crash completely if an incomplete historical data are provided from IB API (bug in IB API side)- if a strategy is in ''wait for hist_data'' state for few hours, you can treat it like crashed because of this issue<br />
* profit potential is indicated even in ''no new positions'' and ''fully inactive'' states (for manual traders)<br />
* added ''Reset Last Opened'' button for maintenance operations and manual traders<br />
* removed last price constraints (causing ''suspicious market data'' state in markets missing last price quotes)<br />
<br />
Released 2016-11-23.<br />
<br />
== 1.3.0 ==<br />
* multi-platform - builds for win32, win64, linux64 and macOS now available<br />
* indicating both z-score bid and z-score ask separately<br />
* more effective margin allocation algorithm (only differs when margins differ per leg)<br />
* profit potential is now calculated from bid-ask quotes (instead of trades)<br />
* custom thresholds for downtick mode supported<br />
* allow-reversals constraint supported<br />
* copyright and license update<br />
* cosmetic changes<br />
* updated 3rd party components<br />
* migrated to Gradle build system<br />
<br />
Released 2016-11-08.<br />
<br />
== 1.2.4 ==<br />
Minor bugfix release.<br />
* fix for incorrect handling of some time zones (Moscow, etc...)<br />
* Z-score and RSI is now being indicated in more situations, including "No new positions"<br />
<br />
Released 2015-12-21.<br />
<br />
== 1.2.3 ==<br />
* fix for duplicate order ID issues occurring when many orders are being sent at the same time<br />
* Open Long, Open Short and Close buttons can be used in more situations<br />
* Close button can be used to manually resolve "one leg opened" situation<br />
* cosmetic UI changes<br />
* copyright and license update<br />
<br />
Released 2015-07-23.<br />
<br />
== 1.2.2 ==<br />
* added some price event sanity checks and constraints<br />
* other minor fixes / changes<br />
<br />
Released 2015-04-18.<br />
<br />
== 1.2.1 ==<br />
* application JAR is now wrapped in EXE - fix for shortcut/installer issues with Java 1.8<br />
* fixed authentication fail message<br />
* more precise z-score indication (now also the indicated z-score is calculated using bid/ask prices)<br />
* cooldown after external close detected<br />
* "last" prices are no longer used for entry/exit signals (only ask/bid prices are used now)<br />
* redesigned exchange activity detection (to detect holidays / days with no activity)<br />
* trading engine should be more friendly to low liquidity stocks (removed some unnecessary constraints in signal evaluation)<br />
* trading engine will no longer allow positions reversals for now (to be consistent with backtests)<br />
* bid and ask sizes are now displayed in the UI<br />
* other minor fixes and changes<br />
<br />
Internal release.<br />
<br />
== 1.2.0 ==<br />
* internal architecture changes related to compatibility with PTL portfolio system<br />
* RSI filter for Ratio model now fully supported<br />
* all entry modes are now supported for Residual model<br />
<br />
Released 2015-01-02.<br />
<br />
== 1.1.1 ==<br />
Bugfix release & internal architecture improvements:<br />
<br />
* redesigned concurrency and threading architecture (pair strategies now use thread confinement)<br />
* fixed few potential deadlock situations and GUI freezes<br />
* fixed concurrency issue which could potentially lead to allow N+1 slots opened in a rare occasion<br />
* fixed retrying fetching historical data after IB data pacing violation (regression introduced in 1.0.4, it worked in earlier versions)<br />
<br />
Released 2014-10-07.<br />
<br />
== 1.1.0 ==<br />
* basic FA (Financial Adviser) account support (you have to enter the account code manually when connecting)<br />
* increased log file max size and number of backups<br />
* unadjusted split protection (stops auto trading if there is a significant price discrepancy which may indicate that IB forgot to adjust historical data after split event)<br />
* prevents automated trading if historical data fetched are obsolete (older than 5 days)<br />
* connection to IB API is now handled in a separate thread to prevent UI freezes<br />
* updated IB client API files to 971.01<br />
* minor cosmetic changes and bugfixes<br />
* minor changes in log file verbosity<br />
<br />
Released 2014-08-13.<br />
<br />
== 1.0.6 ==<br />
* workaround for "ambiguous contract" issues for some particular IB symbols (MSFT, ...)<br />
* fix for stock symbols like XXX.Y (RDS.A for instance)<br />
* some other small changes/fixes<br />
* more verbose logging of slot allocation process<br />
<br />
Released 2014-03-19.<br />
<br />
== 1.0.5 ==<br />
* fixed a bug causing "wait for min profit" engine status indicated outside trading hours<br />
<br />
Released 2013-11-07.<br />
<br />
== 1.0.4 ==<br />
* z-score and profit potential now update also outside user's configured trading window (as long historical data were available)<br />
* manually opening/closing positions is now enabled in more engine states (for instance in "wait for min profit")<br />
* modified profit estimation formula for residual model, profit estimation now disabled for cases where A<0.1<br />
* fixed PDT protection in position exit logic<br />
* PTL Trader does not try to initialize trading engine and load historical data for portfolios bound to accounts not belonging to current IB account connected anymore<br />
* historical data are now downloaded and models are recalculated every day at 9:30 EST, so z-score and profit potential indicators work with updated model, instead of waiting for user's configured trading window<br />
<br />
Internal release.<br />
<br />
== 1.0.3 ==<br />
* workaround for strategy P/L tracking (ignore portfolio data in IB "black hour" 0:** EST)<br />
* modified default log levels<br />
<br />
First official stable release (2013-08-27).<br />
<br />
== 1.0.2 ==<br />
* workaround for equity tracking (ignore equity data in IB "black hour" 0:** EST)<br />
* more detailed logs for IB portfolio updates<br />
<br />
Internal release.<br />
<br />
== 1.0.1 ==<br />
* debug logs for equity tracking<br />
* reworked execution and commission report handling (fixes slippage tracking problems and missing entries in transaction history)<br />
* PDT protection fix<br />
* libraries updated<br />
<br />
Internal release.<br />
<br />
== 1.0.0 ==<br />
* changed valid range for instrument margins (15% - 100%)<br />
* cooldown period after closing a pair trade (it is not possible to open new pair trade earlier than in two minutes) - to prevent sending new orders until IB portfolio stats settle<br />
* more verbose logging of executions and commission reports (internal log)<br />
* reworked periodic premium membership checks<br />
* updated logo and icons<br />
<br />
Internal release.<br />
<br />
== 0.9.6b ==<br />
* just a small fix - NYSEAMEX exchange has been apparently renamed to NYSEMKT, so both exchanges are now supported<br />
<br />
* Initial version released to public (2013-05-17).<br />
<br />
== 0.9.6 ==<br />
* Status = BETA, not recommended for real trading yet (although we already use it that way ourselves)<br />
<br />
<br />
== Known issues we are working on ==<br />
...</div>Adminhttps://wiki.pairtradinglab.com/wiki/PTL_Trader_ChangelogPTL Trader Changelog2021-07-02T09:37:25Z<p>Admin: /* 1.6.0 */</p>
<hr />
<div>== 1.6.0 ==<br />
Since 1.6.0 this project has been released as open-source and hosted on GitHub. Read the further release history here: https://github.com/quantverse/ptltrader/releases<br />
<br />
Released 2021-07-02.<br />
<br />
== 1.5.0 ==<br />
* CFD trading is supported<br />
* fixed missing z-scores in history for Kalman-Grid and Kalman-Auto models<br />
* fixed race condition with portfolio updates shortly after positions are closed (leading to "position mismatch" error and marking the pair as "requiring intervention")<br />
<br />
Released 2017-11-09.<br />
<br />
== 1.4.0 ==<br />
* Kalman-Grid (v2) model fully supported<br />
* Kalman-Auto model fully supported<br />
* removed forgotten last price constraint (causing ''suspicious market data'' state in markets missing last price quotes)<br />
<br />
Released 2016-12-21.<br />
<br />
== 1.3.1 ==<br />
* fixed serious bug causing strategy thread to crash completely if an incomplete historical data are provided from IB API (bug in IB API side)- if a strategy is in ''wait for hist_data'' state for few hours, you can treat it like crashed because of this issue<br />
* profit potential is indicated even in ''no new positions'' and ''fully inactive'' states (for manual traders)<br />
* added ''Reset Last Opened'' button for maintenance operations and manual traders<br />
* removed last price constraints (causing ''suspicious market data'' state in markets missing last price quotes)<br />
<br />
Released 2016-11-23.<br />
<br />
== 1.3.0 ==<br />
* multi-platform - builds for win32, win64, linux64 and macOS now available<br />
* indicating both z-score bid and z-score ask separately<br />
* more effective margin allocation algorithm (only differs when margins differ per leg)<br />
* profit potential is now calculated from bid-ask quotes (instead of trades)<br />
* custom thresholds for downtick mode supported<br />
* allow-reversals constraint supported<br />
* copyright and license update<br />
* cosmetic changes<br />
* updated 3rd party components<br />
* migrated to Gradle build system<br />
<br />
Released 2016-11-08.<br />
<br />
== 1.2.4 ==<br />
Minor bugfix release.<br />
* fix for incorrect handling of some time zones (Moscow, etc...)<br />
* Z-score and RSI is now being indicated in more situations, including "No new positions"<br />
<br />
Released 2015-12-21.<br />
<br />
== 1.2.3 ==<br />
* fix for duplicate order ID issues occurring when many orders are being sent at the same time<br />
* Open Long, Open Short and Close buttons can be used in more situations<br />
* Close button can be used to manually resolve "one leg opened" situation<br />
* cosmetic UI changes<br />
* copyright and license update<br />
<br />
Released 2015-07-23.<br />
<br />
== 1.2.2 ==<br />
* added some price event sanity checks and constraints<br />
* other minor fixes / changes<br />
<br />
Released 2015-04-18.<br />
<br />
== 1.2.1 ==<br />
* application JAR is now wrapped in EXE - fix for shortcut/installer issues with Java 1.8<br />
* fixed authentication fail message<br />
* more precise z-score indication (now also the indicated z-score is calculated using bid/ask prices)<br />
* cooldown after external close detected<br />
* "last" prices are no longer used for entry/exit signals (only ask/bid prices are used now)<br />
* redesigned exchange activity detection (to detect holidays / days with no activity)<br />
* trading engine should be more friendly to low liquidity stocks (removed some unnecessary constraints in signal evaluation)<br />
* trading engine will no longer allow positions reversals for now (to be consistent with backtests)<br />
* bid and ask sizes are now displayed in the UI<br />
* other minor fixes and changes<br />
<br />
Internal release.<br />
<br />
== 1.2.0 ==<br />
* internal architecture changes related to compatibility with PTL portfolio system<br />
* RSI filter for Ratio model now fully supported<br />
* all entry modes are now supported for Residual model<br />
<br />
Released 2015-01-02.<br />
<br />
== 1.1.1 ==<br />
Bugfix release & internal architecture improvements:<br />
<br />
* redesigned concurrency and threading architecture (pair strategies now use thread confinement)<br />
* fixed few potential deadlock situations and GUI freezes<br />
* fixed concurrency issue which could potentially lead to allow N+1 slots opened in a rare occasion<br />
* fixed retrying fetching historical data after IB data pacing violation (regression introduced in 1.0.4, it worked in earlier versions)<br />
<br />
Released 2014-10-07.<br />
<br />
== 1.1.0 ==<br />
* basic FA (Financial Adviser) account support (you have to enter the account code manually when connecting)<br />
* increased log file max size and number of backups<br />
* unadjusted split protection (stops auto trading if there is a significant price discrepancy which may indicate that IB forgot to adjust historical data after split event)<br />
* prevents automated trading if historical data fetched are obsolete (older than 5 days)<br />
* connection to IB API is now handled in a separate thread to prevent UI freezes<br />
* updated IB client API files to 971.01<br />
* minor cosmetic changes and bugfixes<br />
* minor changes in log file verbosity<br />
<br />
Released 2014-08-13.<br />
<br />
== 1.0.6 ==<br />
* workaround for "ambiguous contract" issues for some particular IB symbols (MSFT, ...)<br />
* fix for stock symbols like XXX.Y (RDS.A for instance)<br />
* some other small changes/fixes<br />
* more verbose logging of slot allocation process<br />
<br />
Released 2014-03-19.<br />
<br />
== 1.0.5 ==<br />
* fixed a bug causing "wait for min profit" engine status indicated outside trading hours<br />
<br />
Released 2013-11-07.<br />
<br />
== 1.0.4 ==<br />
* z-score and profit potential now update also outside user's configured trading window (as long historical data were available)<br />
* manually opening/closing positions is now enabled in more engine states (for instance in "wait for min profit")<br />
* modified profit estimation formula for residual model, profit estimation now disabled for cases where A<0.1<br />
* fixed PDT protection in position exit logic<br />
* PTL Trader does not try to initialize trading engine and load historical data for portfolios bound to accounts not belonging to current IB account connected anymore<br />
* historical data are now downloaded and models are recalculated every day at 9:30 EST, so z-score and profit potential indicators work with updated model, instead of waiting for user's configured trading window<br />
<br />
Internal release.<br />
<br />
== 1.0.3 ==<br />
* workaround for strategy P/L tracking (ignore portfolio data in IB "black hour" 0:** EST)<br />
* modified default log levels<br />
<br />
First official stable release (2013-08-27).<br />
<br />
== 1.0.2 ==<br />
* workaround for equity tracking (ignore equity data in IB "black hour" 0:** EST)<br />
* more detailed logs for IB portfolio updates<br />
<br />
Internal release.<br />
<br />
== 1.0.1 ==<br />
* debug logs for equity tracking<br />
* reworked execution and commission report handling (fixes slippage tracking problems and missing entries in transaction history)<br />
* PDT protection fix<br />
* libraries updated<br />
<br />
Internal release.<br />
<br />
== 1.0.0 ==<br />
* changed valid range for instrument margins (15% - 100%)<br />
* cooldown period after closing a pair trade (it is not possible to open new pair trade earlier than in two minutes) - to prevent sending new orders until IB portfolio stats settle<br />
* more verbose logging of executions and commission reports (internal log)<br />
* reworked periodic premium membership checks<br />
* updated logo and icons<br />
<br />
Internal release.<br />
<br />
== 0.9.6b ==<br />
* just a small fix - NYSEAMEX exchange has been apparently renamed to NYSEMKT, so both exchanges are now supported<br />
<br />
* Initial version released to public (2013-05-17).<br />
<br />
== 0.9.6 ==<br />
* Status = BETA, not recommended for real trading yet (although we already use it that way ourselves)<br />
<br />
<br />
== Known issues we are working on ==<br />
...</div>Adminhttps://wiki.pairtradinglab.com/wiki/PTL_Trader_ChangelogPTL Trader Changelog2021-07-02T09:36:43Z<p>Admin: </p>
<hr />
<div>== 1.6.0 ==<br />
Since 1.6.0 this project has been released as open-source and hosted on GitHub. Read the further release history here: https://github.com/quantverse/ptltrader/releases<br />
<br />
== 1.5.0 ==<br />
* CFD trading is supported<br />
* fixed missing z-scores in history for Kalman-Grid and Kalman-Auto models<br />
* fixed race condition with portfolio updates shortly after positions are closed (leading to "position mismatch" error and marking the pair as "requiring intervention")<br />
<br />
Released 2017-11-09.<br />
<br />
== 1.4.0 ==<br />
* Kalman-Grid (v2) model fully supported<br />
* Kalman-Auto model fully supported<br />
* removed forgotten last price constraint (causing ''suspicious market data'' state in markets missing last price quotes)<br />
<br />
Released 2016-12-21.<br />
<br />
== 1.3.1 ==<br />
* fixed serious bug causing strategy thread to crash completely if an incomplete historical data are provided from IB API (bug in IB API side)- if a strategy is in ''wait for hist_data'' state for few hours, you can treat it like crashed because of this issue<br />
* profit potential is indicated even in ''no new positions'' and ''fully inactive'' states (for manual traders)<br />
* added ''Reset Last Opened'' button for maintenance operations and manual traders<br />
* removed last price constraints (causing ''suspicious market data'' state in markets missing last price quotes)<br />
<br />
Released 2016-11-23.<br />
<br />
== 1.3.0 ==<br />
* multi-platform - builds for win32, win64, linux64 and macOS now available<br />
* indicating both z-score bid and z-score ask separately<br />
* more effective margin allocation algorithm (only differs when margins differ per leg)<br />
* profit potential is now calculated from bid-ask quotes (instead of trades)<br />
* custom thresholds for downtick mode supported<br />
* allow-reversals constraint supported<br />
* copyright and license update<br />
* cosmetic changes<br />
* updated 3rd party components<br />
* migrated to Gradle build system<br />
<br />
Released 2016-11-08.<br />
<br />
== 1.2.4 ==<br />
Minor bugfix release.<br />
* fix for incorrect handling of some time zones (Moscow, etc...)<br />
* Z-score and RSI is now being indicated in more situations, including "No new positions"<br />
<br />
Released 2015-12-21.<br />
<br />
== 1.2.3 ==<br />
* fix for duplicate order ID issues occurring when many orders are being sent at the same time<br />
* Open Long, Open Short and Close buttons can be used in more situations<br />
* Close button can be used to manually resolve "one leg opened" situation<br />
* cosmetic UI changes<br />
* copyright and license update<br />
<br />
Released 2015-07-23.<br />
<br />
== 1.2.2 ==<br />
* added some price event sanity checks and constraints<br />
* other minor fixes / changes<br />
<br />
Released 2015-04-18.<br />
<br />
== 1.2.1 ==<br />
* application JAR is now wrapped in EXE - fix for shortcut/installer issues with Java 1.8<br />
* fixed authentication fail message<br />
* more precise z-score indication (now also the indicated z-score is calculated using bid/ask prices)<br />
* cooldown after external close detected<br />
* "last" prices are no longer used for entry/exit signals (only ask/bid prices are used now)<br />
* redesigned exchange activity detection (to detect holidays / days with no activity)<br />
* trading engine should be more friendly to low liquidity stocks (removed some unnecessary constraints in signal evaluation)<br />
* trading engine will no longer allow positions reversals for now (to be consistent with backtests)<br />
* bid and ask sizes are now displayed in the UI<br />
* other minor fixes and changes<br />
<br />
Internal release.<br />
<br />
== 1.2.0 ==<br />
* internal architecture changes related to compatibility with PTL portfolio system<br />
* RSI filter for Ratio model now fully supported<br />
* all entry modes are now supported for Residual model<br />
<br />
Released 2015-01-02.<br />
<br />
== 1.1.1 ==<br />
Bugfix release & internal architecture improvements:<br />
<br />
* redesigned concurrency and threading architecture (pair strategies now use thread confinement)<br />
* fixed few potential deadlock situations and GUI freezes<br />
* fixed concurrency issue which could potentially lead to allow N+1 slots opened in a rare occasion<br />
* fixed retrying fetching historical data after IB data pacing violation (regression introduced in 1.0.4, it worked in earlier versions)<br />
<br />
Released 2014-10-07.<br />
<br />
== 1.1.0 ==<br />
* basic FA (Financial Adviser) account support (you have to enter the account code manually when connecting)<br />
* increased log file max size and number of backups<br />
* unadjusted split protection (stops auto trading if there is a significant price discrepancy which may indicate that IB forgot to adjust historical data after split event)<br />
* prevents automated trading if historical data fetched are obsolete (older than 5 days)<br />
* connection to IB API is now handled in a separate thread to prevent UI freezes<br />
* updated IB client API files to 971.01<br />
* minor cosmetic changes and bugfixes<br />
* minor changes in log file verbosity<br />
<br />
Released 2014-08-13.<br />
<br />
== 1.0.6 ==<br />
* workaround for "ambiguous contract" issues for some particular IB symbols (MSFT, ...)<br />
* fix for stock symbols like XXX.Y (RDS.A for instance)<br />
* some other small changes/fixes<br />
* more verbose logging of slot allocation process<br />
<br />
Released 2014-03-19.<br />
<br />
== 1.0.5 ==<br />
* fixed a bug causing "wait for min profit" engine status indicated outside trading hours<br />
<br />
Released 2013-11-07.<br />
<br />
== 1.0.4 ==<br />
* z-score and profit potential now update also outside user's configured trading window (as long historical data were available)<br />
* manually opening/closing positions is now enabled in more engine states (for instance in "wait for min profit")<br />
* modified profit estimation formula for residual model, profit estimation now disabled for cases where A<0.1<br />
* fixed PDT protection in position exit logic<br />
* PTL Trader does not try to initialize trading engine and load historical data for portfolios bound to accounts not belonging to current IB account connected anymore<br />
* historical data are now downloaded and models are recalculated every day at 9:30 EST, so z-score and profit potential indicators work with updated model, instead of waiting for user's configured trading window<br />
<br />
Internal release.<br />
<br />
== 1.0.3 ==<br />
* workaround for strategy P/L tracking (ignore portfolio data in IB "black hour" 0:** EST)<br />
* modified default log levels<br />
<br />
First official stable release (2013-08-27).<br />
<br />
== 1.0.2 ==<br />
* workaround for equity tracking (ignore equity data in IB "black hour" 0:** EST)<br />
* more detailed logs for IB portfolio updates<br />
<br />
Internal release.<br />
<br />
== 1.0.1 ==<br />
* debug logs for equity tracking<br />
* reworked execution and commission report handling (fixes slippage tracking problems and missing entries in transaction history)<br />
* PDT protection fix<br />
* libraries updated<br />
<br />
Internal release.<br />
<br />
== 1.0.0 ==<br />
* changed valid range for instrument margins (15% - 100%)<br />
* cooldown period after closing a pair trade (it is not possible to open new pair trade earlier than in two minutes) - to prevent sending new orders until IB portfolio stats settle<br />
* more verbose logging of executions and commission reports (internal log)<br />
* reworked periodic premium membership checks<br />
* updated logo and icons<br />
<br />
Internal release.<br />
<br />
== 0.9.6b ==<br />
* just a small fix - NYSEAMEX exchange has been apparently renamed to NYSEMKT, so both exchanges are now supported<br />
<br />
* Initial version released to public (2013-05-17).<br />
<br />
== 0.9.6 ==<br />
* Status = BETA, not recommended for real trading yet (although we already use it that way ourselves)<br />
<br />
<br />
== Known issues we are working on ==<br />
...</div>Adminhttps://wiki.pairtradinglab.com/wiki/PTL_Trader_ManualPTL Trader Manual2021-07-02T09:28:24Z<p>Admin: /* Confidential Mode */</p>
<hr />
<div>== Introduction ==<br />
PTL Trader is a free, open source automated trading system solution for pair trading. See more information [https://www.pairtradinglab.com/ptltrader here]. You can use it to execute your pair trading strategy portfolios you have built using the [https://www.pairtradinglab.com Pair Trading Lab website] Portfolio Manager.<br />
<br />
== Requirements ==<br />
# 64bit operating system (Microsoft Windows, Linux or macOS) compatible with Java 8 (or newer) is required<br />
# 64-bit Java 8 (1.8) or newer is required. The installer checks this.<br />
# PTL Trader requires Interactive Brokers TWS or IB Gateway, providing an API access. Server IB API version 66 or newer is required.<br />
<br />
== Installation & Setup ==<br />
In order to use PTL Trader application, you must obtain PTL Trader API keys first. They are used to authenticate the application running at your side against our server, so the application is able to retrieve your portfolio and settings and send back your trading stats (unless confidential mode is used).<br />
There are two keys needed - the '''Access Key''' and the '''Secret Key'''<br />
<br />
Steps to do:<br />
# Go to the [https://www.pairtradinglab.com Pair Trading Lab website] and login to your account<br />
# Go to Menu -> PTL Trader -> Setup & Download (if you don't see this, you are not a Premium Member or your membership has expired) (see the screenshot)<br />
#:[[File:Ptl-download.jpg|right]]<br />
# If you don't have API access keys yes, you will see a button ''Generate PTL Trader keys for me now''. Press it, it will generate your keys and setup your PTL Trader access immediately<br />
# When you already have API keys generated, you can see your '''Access Key''' already. In order to see your '''Secret Key''' press the link saying "click here to see my Secret Key".<br />
# Now, you can download the PTL Trader MSI installer by clicking the button ''Download PTL Trader''. You download should start.<br />
# Check software requirements above<br />
# Run the installer to install the PTL Trader application. The installer checks the Java version, so it won't allow you to install if you don't have Java or if it is outdated<br />
# Run PTL Trader from your start menu<br />
# Go to the Settings tab and fill in your access keys. By pressing ''Connect'' button it should authenticate and connect to our website and download your portfolios you created in Portfolio Manager<br />
# Start IB Trading Workstation or IB gateway, enable socket-base API access in settings<br />
# In PTL Trader settings, check the IB ABI Client Id and Port if matches what you need. Client Id can be 1 if PTL Trader is the only application connecting to your API. Default port numbers are '''7496''' for TWS connection and '''4001''' for IB Gateway connection.<br />
# Press ''Connect''. It should connect to your IB API now.<br />
# You are ready now to setup your portfolios and account bindings.<br />
[[File:Ptltrader-settings.png|center]]<br />
'''Firewall Remark''': PTL Trader communicates with PTL server using two TCP ports - '''443 (https)''' and '''5671 (amqps)'''. If you use a firewall, '''please make sure that outbound connections are enabled for these ports, otherwise PTL Trader will not work!'''<br />
<br />
== Multiple Instances & Autostart ==<br />
[[File:Ptltrader-shortcut.png|right]]<br />
PTL Trader supports running in multiple instances on the same machine, but each instance must run under a different configuration profile and must connect to different IB account. The default configuration profile name is ''default'', which is used when no command line arguments are used. If you want to start the application using different profile, you have to manually specify the profile in command line arguments. Argument syntax is this:<br />
<profile name> [autostart]<br />
If ''autostart'' is specified, PTL Trader will automatically connect to both Pair Trading Lab site and IB account, as long all credentials are already configured in the Settings panel. You can use autostart feature e.g. for automatic system start after your VPS reboots.<br />
Please note that the ''autostart'' is always the second argument, so if you want to use autostart with default profile, command line arguments will look like this:<br />
default autostart<br />
Good practice is to create file shortcuts, where you can specify command line arguments directly like on the screenshot on the right.<br />
PTL Trader installation comes with two default application shortcuts:<br />
* '''PTL Trader''' - profile: ''default'', no autostart<br />
* '''PTL Trader (second instance)''' - profile: ''secondary'', no autostart<br />
For instance, you can use the first one for regular trading, the second one for paper trading. You can of course edit those default shortcuts yourself, e.g. to enable autostart feature.<br />
<br />
Again - you need to worry about profiles only if you use two or more PTL Trader instances on the same computer. If you run them on different machines, you can stick with ''default'' profile.<br />
<br />
'''Remark: if you run multiple instances on the same computer, each instance must connect to a different IB TWS/Gateway instance!'''<br />
== Confidential Mode ==<br />
By default, PTL Trader is sending the complete trade history to Pair Trading Lab. Although we guarantee we never disclose any of that data to any 3rd side and we try to maintain as highest level of security as possible, we fully understand this may still be a concern for same users.<br />
<br />
So if you really won't want to disclose your trading results to our side, you can use the '''Confidential Mode'''. While enabled, your sensitive data never leaves the PTL Trader application and your computer. However, there are certain implied limitations:<br />
<br />
# whenever you restart PTL Trader, you trading history is gone<br />
<br />
'''Confidential Mode''' can be enabled in the Setting tab.<br />
<br />
== Interface ==<br />
[[File:Ptltrader-big-1024.png|center]]<br />
== Portfolios ==<br />
The purpose of PTL Trader is to allow you to execute portfolios of pair trading strategies in real-time. Those portfolios are not defined in PTL Trader itself - they are stored on the Pair Trading Lab website under your account. You manage your strategy portfolios using the Portfolio Manager application which is available from the menu as long you log in and as long you are a Premium Member.<br />
<br />
Please read more about portfolios here: [[Pair Trading Lab Portfolios]] and about strategies & algorithms here: [[Pair Trading Lab Algorithms & Strategies]]<br />
=== Binding Portfolio To IB Account ===<br />
When connected to your account, PTL Trader has an access to you portfolios and loads them from your PTL Trader account. Then you can ''bind'' particular portfolio to your IB account and start the autotrading of all pair strategies. There is no way to define portfolios or strategies inside PTL Trader - you have to manage them at the website. There is some limited management functionality on PTL Trader though - you can delete strategies and edit some portfolio and strategy properties also from the PTL Trader side.<br />
<br />
In order to auto-trade the portfolio, you have to '''bind it to the IB account'''. You do this in the PTL Trader:<br />
# start PTL Trader and connect to both PTL and IB API<br />
# portfolio list will appear in the left section<br />
# click on the portfolio you want to bind<br />
# in the right side, go to the Settings tab<br />
# you can see the ''Bind to account'' section with select and ''Bind'' button<br />
# select the IB account you want to trade with and press the ''Bind'' button<br />
# after confirmation, the portfolio will bind to the account you selected and '''the automated trading will start right away'''<br />
[[File:Ptltrader-bind.png|center]]<br />
'''Beware: Trading may start immediately after you bind the portfolio to an account!'''<br />
<br />
Remark: '''Standard license allows you to trade two IB accounts using PTL Trader software and your account.''' So you can bind just two of your portfolios to an IB account.<br />
=== Unbinding Portfolio ===<br />
If you don't want to auto-trade some portfolio anymore on a particular IB account (e.g. you want to move the portfolio from paper account to live account), you may want to ''unbind'' it. There is a button for that in the portfolio management section.<br />
<br />
'''Important: If you unbind a portfolio from an IB account, nothing happens to positions already opened on the account! They will just become ignored by the PTL Trader. You are responsible to deal with those positions in TWS yourself!'''<br />
=== Updating Portfolios in PTL Trader ===<br />
'''Very important:''' every time you make changes in portfolio at the website (change parameters, add pairs to portfolio), the update is never automatically synchronized to PTL Trader. You '''must explicitly synchronize them yourself''' by going into File menu and selecting ''Update Portfolios From PTL''.<br />
<br />
On the other hand, if you make changes on the PTL Trader side, these updates are synced to the website automatically. If you have the Portfolio Manager opened in the browser, you have to press F5 to see the changes, because the web application UI is not refreshed in real-time.<br />
== Monitoring ==<br />
Pair Trading Lab website offers an unique feature - it can watch your PTL Trader instances automatically for you and send a warning e-mail to you if there is something suspicious going on. How it does work? PTL Trader sends every minute its status to our website. Another software running on our side analyzes this information and may send a warning message to you if decided.<br />
Situations detected are these:<br />
* PTL Trader instance did not report itself for more than 15 minutes - your instance does not run, your VPS is down or lost an Internet connection<br />
* PTL Trader instance lost connection to IB API (TWS crashed and not recovered in time limit?)<br />
* manual intervention is required for at least one pair strategy (see below for an explanation)<br />
In order to enable monitoring for any PTL Trader instance, please follow these steps:<br />
# Make sure you have at least once started your PTL Trader instance, for at least one minute (connected to PTL).<br />
# Go to PTL website and login to your account<br />
# in menu, go to PTL Trader > Monitoring.<br />
# You should see a table of your known PTL Trader instances, identified by your computer hostname and instance profile.<br />
# If you check the checkbox next to your instance and press ''Save Settings'' button, monitoring will start and you may get messages.<br />
# Below you can configure an e-mail address for sending you monitoring messages, by default it is the same e-mail you have used for your registration.<br />
[[File:Ptl-monitoring.png]]<br />
=== Manual Intervention Requests ===<br />
There are some situations which may happen on your IB account side, which cannot be automatically dealt with by PTL Trader, although it is doing the best. Some examples are these:<br />
* you change direction of existing pair leg positions manually in TWS<br />
* reverse split occurs on any opened instrument and the split ratio is not "clean"<br />
In these situations, PTL Trader gets confused and does not know, what to do with the pair anymore. So it just marks the pair strategy as "requires manual intervention" and ignores the pair since then. You should just solve the situation manually (e.g. close or alter pair positions in TWS) and then you can resume trading by pressing ''Resume Trading'' button in PTL Trader.</div>Adminhttps://wiki.pairtradinglab.com/wiki/PTL_Trader_ManualPTL Trader Manual2021-07-02T09:26:54Z<p>Admin: /* Requirements */</p>
<hr />
<div>== Introduction ==<br />
PTL Trader is a free, open source automated trading system solution for pair trading. See more information [https://www.pairtradinglab.com/ptltrader here]. You can use it to execute your pair trading strategy portfolios you have built using the [https://www.pairtradinglab.com Pair Trading Lab website] Portfolio Manager.<br />
<br />
== Requirements ==<br />
# 64bit operating system (Microsoft Windows, Linux or macOS) compatible with Java 8 (or newer) is required<br />
# 64-bit Java 8 (1.8) or newer is required. The installer checks this.<br />
# PTL Trader requires Interactive Brokers TWS or IB Gateway, providing an API access. Server IB API version 66 or newer is required.<br />
<br />
== Installation & Setup ==<br />
In order to use PTL Trader application, you must obtain PTL Trader API keys first. They are used to authenticate the application running at your side against our server, so the application is able to retrieve your portfolio and settings and send back your trading stats (unless confidential mode is used).<br />
There are two keys needed - the '''Access Key''' and the '''Secret Key'''<br />
<br />
Steps to do:<br />
# Go to the [https://www.pairtradinglab.com Pair Trading Lab website] and login to your account<br />
# Go to Menu -> PTL Trader -> Setup & Download (if you don't see this, you are not a Premium Member or your membership has expired) (see the screenshot)<br />
#:[[File:Ptl-download.jpg|right]]<br />
# If you don't have API access keys yes, you will see a button ''Generate PTL Trader keys for me now''. Press it, it will generate your keys and setup your PTL Trader access immediately<br />
# When you already have API keys generated, you can see your '''Access Key''' already. In order to see your '''Secret Key''' press the link saying "click here to see my Secret Key".<br />
# Now, you can download the PTL Trader MSI installer by clicking the button ''Download PTL Trader''. You download should start.<br />
# Check software requirements above<br />
# Run the installer to install the PTL Trader application. The installer checks the Java version, so it won't allow you to install if you don't have Java or if it is outdated<br />
# Run PTL Trader from your start menu<br />
# Go to the Settings tab and fill in your access keys. By pressing ''Connect'' button it should authenticate and connect to our website and download your portfolios you created in Portfolio Manager<br />
# Start IB Trading Workstation or IB gateway, enable socket-base API access in settings<br />
# In PTL Trader settings, check the IB ABI Client Id and Port if matches what you need. Client Id can be 1 if PTL Trader is the only application connecting to your API. Default port numbers are '''7496''' for TWS connection and '''4001''' for IB Gateway connection.<br />
# Press ''Connect''. It should connect to your IB API now.<br />
# You are ready now to setup your portfolios and account bindings.<br />
[[File:Ptltrader-settings.png|center]]<br />
'''Firewall Remark''': PTL Trader communicates with PTL server using two TCP ports - '''443 (https)''' and '''5671 (amqps)'''. If you use a firewall, '''please make sure that outbound connections are enabled for these ports, otherwise PTL Trader will not work!'''<br />
<br />
== Multiple Instances & Autostart ==<br />
[[File:Ptltrader-shortcut.png|right]]<br />
PTL Trader supports running in multiple instances on the same machine, but each instance must run under a different configuration profile and must connect to different IB account. The default configuration profile name is ''default'', which is used when no command line arguments are used. If you want to start the application using different profile, you have to manually specify the profile in command line arguments. Argument syntax is this:<br />
<profile name> [autostart]<br />
If ''autostart'' is specified, PTL Trader will automatically connect to both Pair Trading Lab site and IB account, as long all credentials are already configured in the Settings panel. You can use autostart feature e.g. for automatic system start after your VPS reboots.<br />
Please note that the ''autostart'' is always the second argument, so if you want to use autostart with default profile, command line arguments will look like this:<br />
default autostart<br />
Good practice is to create file shortcuts, where you can specify command line arguments directly like on the screenshot on the right.<br />
PTL Trader installation comes with two default application shortcuts:<br />
* '''PTL Trader''' - profile: ''default'', no autostart<br />
* '''PTL Trader (second instance)''' - profile: ''secondary'', no autostart<br />
For instance, you can use the first one for regular trading, the second one for paper trading. You can of course edit those default shortcuts yourself, e.g. to enable autostart feature.<br />
<br />
Again - you need to worry about profiles only if you use two or more PTL Trader instances on the same computer. If you run them on different machines, you can stick with ''default'' profile.<br />
<br />
'''Remark: if you run multiple instances on the same computer, each instance must connect to a different IB TWS/Gateway instance!'''<br />
== Confidential Mode ==<br />
By default, PTL Trader is sending the complete trade history and samples of account equity and position open profit/loss to the Pair Trading Lab. It is stored there inside your account to be used by Reporting feature which will be developed soon, so users will be able to create advanced reports of pair trading strategy performance. Although we guarantee we never disclose any of that data to any 3rd side and we try to maintain as highest level of security as possible, we fully understand this may still be a concern for same users.<br />
<br />
So if you really won't want to disclose your trading results and account equities to our side, you can use the '''Confidential Mode'''. While enabled, your sensitive data never leaves the PTL Trader application and your computer. However, there are certain implied limitations:<br />
# you will not be able to use the Reporting feature in the future (there are no data to builds reports on)<br />
# whenever you restart PTL Trader, you trading history is gone<br />
<br />
'''Confidential Mode''' can be enabled in the Setting tab.<br />
== Interface ==<br />
[[File:Ptltrader-big-1024.png|center]]<br />
== Portfolios ==<br />
The purpose of PTL Trader is to allow you to execute portfolios of pair trading strategies in real-time. Those portfolios are not defined in PTL Trader itself - they are stored on the Pair Trading Lab website under your account. You manage your strategy portfolios using the Portfolio Manager application which is available from the menu as long you log in and as long you are a Premium Member.<br />
<br />
Please read more about portfolios here: [[Pair Trading Lab Portfolios]] and about strategies & algorithms here: [[Pair Trading Lab Algorithms & Strategies]]<br />
=== Binding Portfolio To IB Account ===<br />
When connected to your account, PTL Trader has an access to you portfolios and loads them from your PTL Trader account. Then you can ''bind'' particular portfolio to your IB account and start the autotrading of all pair strategies. There is no way to define portfolios or strategies inside PTL Trader - you have to manage them at the website. There is some limited management functionality on PTL Trader though - you can delete strategies and edit some portfolio and strategy properties also from the PTL Trader side.<br />
<br />
In order to auto-trade the portfolio, you have to '''bind it to the IB account'''. You do this in the PTL Trader:<br />
# start PTL Trader and connect to both PTL and IB API<br />
# portfolio list will appear in the left section<br />
# click on the portfolio you want to bind<br />
# in the right side, go to the Settings tab<br />
# you can see the ''Bind to account'' section with select and ''Bind'' button<br />
# select the IB account you want to trade with and press the ''Bind'' button<br />
# after confirmation, the portfolio will bind to the account you selected and '''the automated trading will start right away'''<br />
[[File:Ptltrader-bind.png|center]]<br />
'''Beware: Trading may start immediately after you bind the portfolio to an account!'''<br />
<br />
Remark: '''Standard license allows you to trade two IB accounts using PTL Trader software and your account.''' So you can bind just two of your portfolios to an IB account.<br />
=== Unbinding Portfolio ===<br />
If you don't want to auto-trade some portfolio anymore on a particular IB account (e.g. you want to move the portfolio from paper account to live account), you may want to ''unbind'' it. There is a button for that in the portfolio management section.<br />
<br />
'''Important: If you unbind a portfolio from an IB account, nothing happens to positions already opened on the account! They will just become ignored by the PTL Trader. You are responsible to deal with those positions in TWS yourself!'''<br />
=== Updating Portfolios in PTL Trader ===<br />
'''Very important:''' every time you make changes in portfolio at the website (change parameters, add pairs to portfolio), the update is never automatically synchronized to PTL Trader. You '''must explicitly synchronize them yourself''' by going into File menu and selecting ''Update Portfolios From PTL''.<br />
<br />
On the other hand, if you make changes on the PTL Trader side, these updates are synced to the website automatically. If you have the Portfolio Manager opened in the browser, you have to press F5 to see the changes, because the web application UI is not refreshed in real-time.<br />
== Monitoring ==<br />
Pair Trading Lab website offers an unique feature - it can watch your PTL Trader instances automatically for you and send a warning e-mail to you if there is something suspicious going on. How it does work? PTL Trader sends every minute its status to our website. Another software running on our side analyzes this information and may send a warning message to you if decided.<br />
Situations detected are these:<br />
* PTL Trader instance did not report itself for more than 15 minutes - your instance does not run, your VPS is down or lost an Internet connection<br />
* PTL Trader instance lost connection to IB API (TWS crashed and not recovered in time limit?)<br />
* manual intervention is required for at least one pair strategy (see below for an explanation)<br />
In order to enable monitoring for any PTL Trader instance, please follow these steps:<br />
# Make sure you have at least once started your PTL Trader instance, for at least one minute (connected to PTL).<br />
# Go to PTL website and login to your account<br />
# in menu, go to PTL Trader > Monitoring.<br />
# You should see a table of your known PTL Trader instances, identified by your computer hostname and instance profile.<br />
# If you check the checkbox next to your instance and press ''Save Settings'' button, monitoring will start and you may get messages.<br />
# Below you can configure an e-mail address for sending you monitoring messages, by default it is the same e-mail you have used for your registration.<br />
[[File:Ptl-monitoring.png]]<br />
=== Manual Intervention Requests ===<br />
There are some situations which may happen on your IB account side, which cannot be automatically dealt with by PTL Trader, although it is doing the best. Some examples are these:<br />
* you change direction of existing pair leg positions manually in TWS<br />
* reverse split occurs on any opened instrument and the split ratio is not "clean"<br />
In these situations, PTL Trader gets confused and does not know, what to do with the pair anymore. So it just marks the pair strategy as "requires manual intervention" and ignores the pair since then. You should just solve the situation manually (e.g. close or alter pair positions in TWS) and then you can resume trading by pressing ''Resume Trading'' button in PTL Trader.</div>Adminhttps://wiki.pairtradinglab.com/wiki/PTL_Trader_ManualPTL Trader Manual2021-07-02T09:25:41Z<p>Admin: /* Introduction */</p>
<hr />
<div>== Introduction ==<br />
PTL Trader is a free, open source automated trading system solution for pair trading. See more information [https://www.pairtradinglab.com/ptltrader here]. You can use it to execute your pair trading strategy portfolios you have built using the [https://www.pairtradinglab.com Pair Trading Lab website] Portfolio Manager.<br />
<br />
== Requirements ==<br />
# Windows OS compatible with Java 7 (or newer) is required<br />
# 32-bit Java 7 (1.7) or newer is required. The installer checks this. 64-bit Java is not supported at this moment, but it will be at some point.<br />
# PTL Trader requires Interactive Brokers TWS or IB Gateway, providing an API access. Server IB API version 66 or newer is required.<br />
<br />
PTL Trader and Portfolio Manager are only available to Premium Members.<br />
<br />
== Installation & Setup ==<br />
In order to use PTL Trader application, you must obtain PTL Trader API keys first. They are used to authenticate the application running at your side against our server, so the application is able to retrieve your portfolio and settings and send back your trading stats (unless confidential mode is used).<br />
There are two keys needed - the '''Access Key''' and the '''Secret Key'''<br />
<br />
Steps to do:<br />
# Go to the [https://www.pairtradinglab.com Pair Trading Lab website] and login to your account<br />
# Go to Menu -> PTL Trader -> Setup & Download (if you don't see this, you are not a Premium Member or your membership has expired) (see the screenshot)<br />
#:[[File:Ptl-download.jpg|right]]<br />
# If you don't have API access keys yes, you will see a button ''Generate PTL Trader keys for me now''. Press it, it will generate your keys and setup your PTL Trader access immediately<br />
# When you already have API keys generated, you can see your '''Access Key''' already. In order to see your '''Secret Key''' press the link saying "click here to see my Secret Key".<br />
# Now, you can download the PTL Trader MSI installer by clicking the button ''Download PTL Trader''. You download should start.<br />
# Check software requirements above<br />
# Run the installer to install the PTL Trader application. The installer checks the Java version, so it won't allow you to install if you don't have Java or if it is outdated<br />
# Run PTL Trader from your start menu<br />
# Go to the Settings tab and fill in your access keys. By pressing ''Connect'' button it should authenticate and connect to our website and download your portfolios you created in Portfolio Manager<br />
# Start IB Trading Workstation or IB gateway, enable socket-base API access in settings<br />
# In PTL Trader settings, check the IB ABI Client Id and Port if matches what you need. Client Id can be 1 if PTL Trader is the only application connecting to your API. Default port numbers are '''7496''' for TWS connection and '''4001''' for IB Gateway connection.<br />
# Press ''Connect''. It should connect to your IB API now.<br />
# You are ready now to setup your portfolios and account bindings.<br />
[[File:Ptltrader-settings.png|center]]<br />
'''Firewall Remark''': PTL Trader communicates with PTL server using two TCP ports - '''443 (https)''' and '''5671 (amqps)'''. If you use a firewall, '''please make sure that outbound connections are enabled for these ports, otherwise PTL Trader will not work!'''<br />
<br />
== Multiple Instances & Autostart ==<br />
[[File:Ptltrader-shortcut.png|right]]<br />
PTL Trader supports running in multiple instances on the same machine, but each instance must run under a different configuration profile and must connect to different IB account. The default configuration profile name is ''default'', which is used when no command line arguments are used. If you want to start the application using different profile, you have to manually specify the profile in command line arguments. Argument syntax is this:<br />
<profile name> [autostart]<br />
If ''autostart'' is specified, PTL Trader will automatically connect to both Pair Trading Lab site and IB account, as long all credentials are already configured in the Settings panel. You can use autostart feature e.g. for automatic system start after your VPS reboots.<br />
Please note that the ''autostart'' is always the second argument, so if you want to use autostart with default profile, command line arguments will look like this:<br />
default autostart<br />
Good practice is to create file shortcuts, where you can specify command line arguments directly like on the screenshot on the right.<br />
PTL Trader installation comes with two default application shortcuts:<br />
* '''PTL Trader''' - profile: ''default'', no autostart<br />
* '''PTL Trader (second instance)''' - profile: ''secondary'', no autostart<br />
For instance, you can use the first one for regular trading, the second one for paper trading. You can of course edit those default shortcuts yourself, e.g. to enable autostart feature.<br />
<br />
Again - you need to worry about profiles only if you use two or more PTL Trader instances on the same computer. If you run them on different machines, you can stick with ''default'' profile.<br />
<br />
'''Remark: if you run multiple instances on the same computer, each instance must connect to a different IB TWS/Gateway instance!'''<br />
== Confidential Mode ==<br />
By default, PTL Trader is sending the complete trade history and samples of account equity and position open profit/loss to the Pair Trading Lab. It is stored there inside your account to be used by Reporting feature which will be developed soon, so users will be able to create advanced reports of pair trading strategy performance. Although we guarantee we never disclose any of that data to any 3rd side and we try to maintain as highest level of security as possible, we fully understand this may still be a concern for same users.<br />
<br />
So if you really won't want to disclose your trading results and account equities to our side, you can use the '''Confidential Mode'''. While enabled, your sensitive data never leaves the PTL Trader application and your computer. However, there are certain implied limitations:<br />
# you will not be able to use the Reporting feature in the future (there are no data to builds reports on)<br />
# whenever you restart PTL Trader, you trading history is gone<br />
<br />
'''Confidential Mode''' can be enabled in the Setting tab.<br />
== Interface ==<br />
[[File:Ptltrader-big-1024.png|center]]<br />
== Portfolios ==<br />
The purpose of PTL Trader is to allow you to execute portfolios of pair trading strategies in real-time. Those portfolios are not defined in PTL Trader itself - they are stored on the Pair Trading Lab website under your account. You manage your strategy portfolios using the Portfolio Manager application which is available from the menu as long you log in and as long you are a Premium Member.<br />
<br />
Please read more about portfolios here: [[Pair Trading Lab Portfolios]] and about strategies & algorithms here: [[Pair Trading Lab Algorithms & Strategies]]<br />
=== Binding Portfolio To IB Account ===<br />
When connected to your account, PTL Trader has an access to you portfolios and loads them from your PTL Trader account. Then you can ''bind'' particular portfolio to your IB account and start the autotrading of all pair strategies. There is no way to define portfolios or strategies inside PTL Trader - you have to manage them at the website. There is some limited management functionality on PTL Trader though - you can delete strategies and edit some portfolio and strategy properties also from the PTL Trader side.<br />
<br />
In order to auto-trade the portfolio, you have to '''bind it to the IB account'''. You do this in the PTL Trader:<br />
# start PTL Trader and connect to both PTL and IB API<br />
# portfolio list will appear in the left section<br />
# click on the portfolio you want to bind<br />
# in the right side, go to the Settings tab<br />
# you can see the ''Bind to account'' section with select and ''Bind'' button<br />
# select the IB account you want to trade with and press the ''Bind'' button<br />
# after confirmation, the portfolio will bind to the account you selected and '''the automated trading will start right away'''<br />
[[File:Ptltrader-bind.png|center]]<br />
'''Beware: Trading may start immediately after you bind the portfolio to an account!'''<br />
<br />
Remark: '''Standard license allows you to trade two IB accounts using PTL Trader software and your account.''' So you can bind just two of your portfolios to an IB account.<br />
=== Unbinding Portfolio ===<br />
If you don't want to auto-trade some portfolio anymore on a particular IB account (e.g. you want to move the portfolio from paper account to live account), you may want to ''unbind'' it. There is a button for that in the portfolio management section.<br />
<br />
'''Important: If you unbind a portfolio from an IB account, nothing happens to positions already opened on the account! They will just become ignored by the PTL Trader. You are responsible to deal with those positions in TWS yourself!'''<br />
=== Updating Portfolios in PTL Trader ===<br />
'''Very important:''' every time you make changes in portfolio at the website (change parameters, add pairs to portfolio), the update is never automatically synchronized to PTL Trader. You '''must explicitly synchronize them yourself''' by going into File menu and selecting ''Update Portfolios From PTL''.<br />
<br />
On the other hand, if you make changes on the PTL Trader side, these updates are synced to the website automatically. If you have the Portfolio Manager opened in the browser, you have to press F5 to see the changes, because the web application UI is not refreshed in real-time.<br />
== Monitoring ==<br />
Pair Trading Lab website offers an unique feature - it can watch your PTL Trader instances automatically for you and send a warning e-mail to you if there is something suspicious going on. How it does work? PTL Trader sends every minute its status to our website. Another software running on our side analyzes this information and may send a warning message to you if decided.<br />
Situations detected are these:<br />
* PTL Trader instance did not report itself for more than 15 minutes - your instance does not run, your VPS is down or lost an Internet connection<br />
* PTL Trader instance lost connection to IB API (TWS crashed and not recovered in time limit?)<br />
* manual intervention is required for at least one pair strategy (see below for an explanation)<br />
In order to enable monitoring for any PTL Trader instance, please follow these steps:<br />
# Make sure you have at least once started your PTL Trader instance, for at least one minute (connected to PTL).<br />
# Go to PTL website and login to your account<br />
# in menu, go to PTL Trader > Monitoring.<br />
# You should see a table of your known PTL Trader instances, identified by your computer hostname and instance profile.<br />
# If you check the checkbox next to your instance and press ''Save Settings'' button, monitoring will start and you may get messages.<br />
# Below you can configure an e-mail address for sending you monitoring messages, by default it is the same e-mail you have used for your registration.<br />
[[File:Ptl-monitoring.png]]<br />
=== Manual Intervention Requests ===<br />
There are some situations which may happen on your IB account side, which cannot be automatically dealt with by PTL Trader, although it is doing the best. Some examples are these:<br />
* you change direction of existing pair leg positions manually in TWS<br />
* reverse split occurs on any opened instrument and the split ratio is not "clean"<br />
In these situations, PTL Trader gets confused and does not know, what to do with the pair anymore. So it just marks the pair strategy as "requires manual intervention" and ignores the pair since then. You should just solve the situation manually (e.g. close or alter pair positions in TWS) and then you can resume trading by pressing ''Resume Trading'' button in PTL Trader.</div>Adminhttps://wiki.pairtradinglab.com/wiki/PTL_Trader_ChangelogPTL Trader Changelog2017-11-09T10:30:22Z<p>Admin: </p>
<hr />
<div>== 1.5.0 ==<br />
* CFD trading is supported<br />
* fixed missing z-scores in history for Kalman-Grid and Kalman-Auto models<br />
* fixed race condition with portfolio updates shortly after positions are closed (leading to "position mismatch" error and marking the pair as "requiring intervention")<br />
<br />
Released 2017-11-09.<br />
<br />
== 1.4.0 ==<br />
* Kalman-Grid (v2) model fully supported<br />
* Kalman-Auto model fully supported<br />
* removed forgotten last price constraint (causing ''suspicious market data'' state in markets missing last price quotes)<br />
<br />
Released 2016-12-21.<br />
<br />
== 1.3.1 ==<br />
* fixed serious bug causing strategy thread to crash completely if an incomplete historical data are provided from IB API (bug in IB API side)- if a strategy is in ''wait for hist_data'' state for few hours, you can treat it like crashed because of this issue<br />
* profit potential is indicated even in ''no new positions'' and ''fully inactive'' states (for manual traders)<br />
* added ''Reset Last Opened'' button for maintenance operations and manual traders<br />
* removed last price constraints (causing ''suspicious market data'' state in markets missing last price quotes)<br />
<br />
Released 2016-11-23.<br />
<br />
== 1.3.0 ==<br />
* multi-platform - builds for win32, win64, linux64 and macOS now available<br />
* indicating both z-score bid and z-score ask separately<br />
* more effective margin allocation algorithm (only differs when margins differ per leg)<br />
* profit potential is now calculated from bid-ask quotes (instead of trades)<br />
* custom thresholds for downtick mode supported<br />
* allow-reversals constraint supported<br />
* copyright and license update<br />
* cosmetic changes<br />
* updated 3rd party components<br />
* migrated to Gradle build system<br />
<br />
Released 2016-11-08.<br />
<br />
== 1.2.4 ==<br />
Minor bugfix release.<br />
* fix for incorrect handling of some time zones (Moscow, etc...)<br />
* Z-score and RSI is now being indicated in more situations, including "No new positions"<br />
<br />
Released 2015-12-21.<br />
<br />
== 1.2.3 ==<br />
* fix for duplicate order ID issues occurring when many orders are being sent at the same time<br />
* Open Long, Open Short and Close buttons can be used in more situations<br />
* Close button can be used to manually resolve "one leg opened" situation<br />
* cosmetic UI changes<br />
* copyright and license update<br />
<br />
Released 2015-07-23.<br />
<br />
== 1.2.2 ==<br />
* added some price event sanity checks and constraints<br />
* other minor fixes / changes<br />
<br />
Released 2015-04-18.<br />
<br />
== 1.2.1 ==<br />
* application JAR is now wrapped in EXE - fix for shortcut/installer issues with Java 1.8<br />
* fixed authentication fail message<br />
* more precise z-score indication (now also the indicated z-score is calculated using bid/ask prices)<br />
* cooldown after external close detected<br />
* "last" prices are no longer used for entry/exit signals (only ask/bid prices are used now)<br />
* redesigned exchange activity detection (to detect holidays / days with no activity)<br />
* trading engine should be more friendly to low liquidity stocks (removed some unnecessary constraints in signal evaluation)<br />
* trading engine will no longer allow positions reversals for now (to be consistent with backtests)<br />
* bid and ask sizes are now displayed in the UI<br />
* other minor fixes and changes<br />
<br />
Internal release.<br />
<br />
== 1.2.0 ==<br />
* internal architecture changes related to compatibility with PTL portfolio system<br />
* RSI filter for Ratio model now fully supported<br />
* all entry modes are now supported for Residual model<br />
<br />
Released 2015-01-02.<br />
<br />
== 1.1.1 ==<br />
Bugfix release & internal architecture improvements:<br />
<br />
* redesigned concurrency and threading architecture (pair strategies now use thread confinement)<br />
* fixed few potential deadlock situations and GUI freezes<br />
* fixed concurrency issue which could potentially lead to allow N+1 slots opened in a rare occasion<br />
* fixed retrying fetching historical data after IB data pacing violation (regression introduced in 1.0.4, it worked in earlier versions)<br />
<br />
Released 2014-10-07.<br />
<br />
== 1.1.0 ==<br />
* basic FA (Financial Adviser) account support (you have to enter the account code manually when connecting)<br />
* increased log file max size and number of backups<br />
* unadjusted split protection (stops auto trading if there is a significant price discrepancy which may indicate that IB forgot to adjust historical data after split event)<br />
* prevents automated trading if historical data fetched are obsolete (older than 5 days)<br />
* connection to IB API is now handled in a separate thread to prevent UI freezes<br />
* updated IB client API files to 971.01<br />
* minor cosmetic changes and bugfixes<br />
* minor changes in log file verbosity<br />
<br />
Released 2014-08-13.<br />
<br />
== 1.0.6 ==<br />
* workaround for "ambiguous contract" issues for some particular IB symbols (MSFT, ...)<br />
* fix for stock symbols like XXX.Y (RDS.A for instance)<br />
* some other small changes/fixes<br />
* more verbose logging of slot allocation process<br />
<br />
Released 2014-03-19.<br />
<br />
== 1.0.5 ==<br />
* fixed a bug causing "wait for min profit" engine status indicated outside trading hours<br />
<br />
Released 2013-11-07.<br />
<br />
== 1.0.4 ==<br />
* z-score and profit potential now update also outside user's configured trading window (as long historical data were available)<br />
* manually opening/closing positions is now enabled in more engine states (for instance in "wait for min profit")<br />
* modified profit estimation formula for residual model, profit estimation now disabled for cases where A<0.1<br />
* fixed PDT protection in position exit logic<br />
* PTL Trader does not try to initialize trading engine and load historical data for portfolios bound to accounts not belonging to current IB account connected anymore<br />
* historical data are now downloaded and models are recalculated every day at 9:30 EST, so z-score and profit potential indicators work with updated model, instead of waiting for user's configured trading window<br />
<br />
Internal release.<br />
<br />
== 1.0.3 ==<br />
* workaround for strategy P/L tracking (ignore portfolio data in IB "black hour" 0:** EST)<br />
* modified default log levels<br />
<br />
First official stable release (2013-08-27).<br />
<br />
== 1.0.2 ==<br />
* workaround for equity tracking (ignore equity data in IB "black hour" 0:** EST)<br />
* more detailed logs for IB portfolio updates<br />
<br />
Internal release.<br />
<br />
== 1.0.1 ==<br />
* debug logs for equity tracking<br />
* reworked execution and commission report handling (fixes slippage tracking problems and missing entries in transaction history)<br />
* PDT protection fix<br />
* libraries updated<br />
<br />
Internal release.<br />
<br />
== 1.0.0 ==<br />
* changed valid range for instrument margins (15% - 100%)<br />
* cooldown period after closing a pair trade (it is not possible to open new pair trade earlier than in two minutes) - to prevent sending new orders until IB portfolio stats settle<br />
* more verbose logging of executions and commission reports (internal log)<br />
* reworked periodic premium membership checks<br />
* updated logo and icons<br />
<br />
Internal release.<br />
<br />
== 0.9.6b ==<br />
* just a small fix - NYSEAMEX exchange has been apparently renamed to NYSEMKT, so both exchanges are now supported<br />
<br />
* Initial version released to public (2013-05-17).<br />
<br />
== 0.9.6 ==<br />
* Status = BETA, not recommended for real trading yet (although we already use it that way ourselves)<br />
<br />
<br />
== Known issues we are working on ==<br />
...</div>Adminhttps://wiki.pairtradinglab.com/wiki/File:AVONAqv_700b.jpgFile:AVONAqv 700b.jpg2017-09-02T11:01:55Z<p>Admin: test</p>
<hr />
<div>test</div>Adminhttps://wiki.pairtradinglab.com/wiki/Pair_Trading_ModelsPair Trading Models2016-12-21T15:37:10Z<p>Admin: /* Model Support */</p>
<hr />
<div>Pair Trading Lab offers pair trading algorithms based on various mathematical models. These are models currently supported in PTL:<br />
== Ratio Model ==<br />
[[File:Ratio-illustration.png|right|650px|thumb|Ratio Uptick Example]]<br />
This is one of the standard pair trading models described in literature. It is based in ratio of instrument prices, [http://en.wikipedia.org/wiki/Moving_average moving average] and [http://en.wikipedia.org/wiki/Standard_deviation standard deviation]. In other words, it is based on [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator.<br />
<br />
Since Nov 27th 2014, this model also supports additional [http://www.investopedia.com/terms/r/rsi.asp RSI filter] you can use in addition to Bollinger Bands method.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (RSI supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.5, 2.5>, 2.0 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values are allowed (to exit on other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical values is <0, 1>, 0 is used most often...only used for downtick entry mode as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* moving average period P<sub>m</sub> (typical range <10, 100>), default = 15<br />
* moving average type T (algorithm), default = exponential<br />
* standard deviation period P<sub>s</sub> (typical range <10, 100>), default = 15<br />
* entry mode (simple, uptick, downtick)<br />
* RSI period and threshold (optional RSI filtering)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* we need to calculate ratio time series ''R(t)'' = ''A(t)'' / ''B(t)''<br />
* let's apply moving average of type T with period P<sub>m</sub> on ''R(t)'' to get time series ''M(t)''<br />
* let's apply standard deviation with period P<sub>s</sub> on ''R(t)'' to get time series ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = (''R(t)'' - ''M(t)'') / ''S(t)'', this time series can give us z-score to signal trading decision directly (in reality we have two Z-scores: Z-score<sub>ask</sub> and Z-score<sub>bid</sub> as they are calculated using different prices, but for the sake of simplicity let's now pretend we don't pay bid-ask spread and we have just one Z-score)<br />
* another common approach (to visualize) is to create bands and put it above the moving average ''M(t)'':<br />
** upper entry band ''U<sub>n</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>n</sub><br />
** lower entry band ''L<sub>n</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>n</sub><br />
** upper exit band ''U<sub>x</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>x</sub><br />
** lower exit band ''L<sub>x</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>x</sub><br />
** upper downtick band ''U<sub>d</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** lower downtick band ''L<sub>d</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** these bands are actually the same bands as in [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator and we can use crossing of ''R(t)'' and bands as trade signals<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'')<br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'')<br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' < E<sub>n</sub> (same as ''R(t-1)'' < ''U<sub>n</sub>(t-1)'')<br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' > -E<sub>n</sub> (same as ''R(t-1)'' > ''L<sub>n</sub>(t-1)'')<br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside (but it must stay above the downtick band):<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' < ''U<sub>n</sub>(t)'' '''and''' ''R(t-1)'' >= ''U<sub>n</sub>(t-1)'' '''and''' ''R(t)'' > ''U<sub>d</sub>(t)''<br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' > ''L<sub>n</sub>(t)'' '''and''' ''R(t-1)'' <= ''L<sub>n</sub>(t-1)'' '''and''' ''R(t)'' < ''L<sub>d</sub>(t)''<br />
<br />
Why do we have the '''simple entry mode'''? In normal situations and backtests, it gives same results as the '''uptick mode'''. But the difference comes up while trading multiple pairs in portfolio. The simple mode allows you to jump in the position immediately after a new slot is freed, regardless of the previous Z-scores.<br />
<br />
'''Which entry mode is better?''' Hard to tell, sometimes the uptick, sometimes the downtick. You have to do your homework and decide, which idea suits your trading style better. In general, uptick/simple mode is more aggressive, as it does not wait for first signs of spread mean reversion.<br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub> (equivalent to ''R(t)'' <= ''U<sub>x</sub>(t)'')<br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub> (equivalent to ''R(t)'' >= ''L<sub>x</sub>(t)'')<br />
<br />
=== Moving Average Types ===<br />
[[File:Rsi-illustration.png|right|650px|thumb|Ratio RSI Trading Example]]<br />
You can choose from these moving average algorithms:<br />
* [http://www.investopedia.com/terms/s/sma.asp Simple Moving Average] (SMA)<br />
* [http://www.investopedia.com/terms/e/ema.asp Exponential Moving Average] (EMA)<br />
* [http://fxtrade.oanda.com/learn/forex-indicators/weighted-moving-average Weighted Moving Average] (WMA)<br />
* [http://www.investopedia.com/terms/d/double-exponential-moving-average.asp Double Exponential Moving Average] (DEMA)<br />
* [http://www.investopedia.com/terms/t/triple-exponential-moving-average.asp Triple Exponential Moving Average] (TEMA)<br />
* [http://daytrading.about.com/od/indicators/a/Triangular.htm Triangular Moving Average] (TMA)<br />
* [http://etfhq.com/blog/2011/11/07/adaptive-moving-average-ama-aka-kaufman-adaptive-moving-average-kama Kaufman Adaptive Moving Average] (KAMA)<br />
* [http://www.mesasoftware.com/Papers/MAMA.pdf MESA Adaptive Moving Average] (MAMA)<br />
* [http://www.tradeescort.com/AgenaIndicatorHelp/AgenaIndicator.html?T3TripleExponentialMovingAverage.html Triple Exponential T3 Moving Average]<br />
<br />
Which one is the best? It depends, you have to test for yourself. They mostly differ in the term of memory and how fast they react to changes. Industry standard and the default is EMA. We suggest to try all of them on some sample pair to see how they work.<br />
<br />
=== RSI Filtering ===<br />
You can combine the Z-score/Bollinger Band model with [http://www.investopedia.com/terms/r/rsi.asp RSI indicator] applied on the ratio ''R(t)''. RSI filter will be automatically enabled if you set RSI threshold to other value than zero. RSI filter is combined with the Z-score rules using '''AND''' operator ('''both''' entry rule and RSI entry rule must be true to open a position).<br />
<br />
You can also change the RSI period if you want (default period = 15).<br />
<br />
RSI Threshold is value between 0 and 50. Because RSI indicator value oscillates between 0 and 100 (where 50 = mean), your threshold value is just used to set real thresholds for RSI:<br />
* let's assume RSI Period P<sub>r</sub> was entered<br />
* effective RSI value threshold is then 50+P<sub>r</sub> (upper) and 50-P<sub>r</sub> (lower)<br />
* you can see both thresholds in the example image at the right side<br />
<br />
Example:<br />
* RSI Threshold entered is 15<br />
* then, short positions are only opened, if RSI >= 65 (50+15)<br />
* long positions are only opened, if RSI <=35 (50-15)<br />
<br />
Useful hint: if you want to control entry rules just by RSI, you can set Entry Mode to simple and Entry Threshold to some low value.<br />
<br />
== Residual Model ==<br />
[[File:Residual-illustration2.png|right|650px|thumb|Residual Downtick Example]]<br />
Residual mode is based on [http://en.wikipedia.org/wiki/Linear_regression linear regression]. In literature it has been also referred to as the '''cointegration approach'''. Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the OLS method (Ordinary Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
This particular implementation is very simple. The regression is constructed using floating window of a fixed period, the same period is used for calculating the standard deviation.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (uptick and downtick modes are supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <15, 300><br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* first we need to construct a linear regression between ''A(t)'', ''B(t)'' using OLS, where ''A(t)'' = ''&beta;'' * ''B(t)'' + ''&alpha;'' + ''R(t)''<br />
* because we use floating window of period P (we calculate new regression each day), we actually get new series ''&beta;(t)'', ''&alpha;(t)'', ''R(t)'', where ''&beta;(t)'', ''&alpha;(t)'' are series of regression coefficients and ''R(t)'' are residuals (prediction errors)<br />
* ''R(t)'' = ''A(t)'' - (''&beta;(t)'' * ''B(t)'' + ''&alpha;(t)'')<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly (in reality we have two Z-scores: Z-score<sub>ask</sub> and Z-score<sub>bid</sub> as they are calculated using different prices, but for the sake of simplicity let's now pretend we don't pay bid-ask spread and we have just one Z-score)<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside (while we must stay above the downtick band):<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>d</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>d</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman Model ==<br />
This model is based on [http://en.wikipedia.org/wiki/Kalman_filter Kalman Filter]. In this case, the filter is used here instead of linear regression to determine proper hedge ratio, deviation from the mean and the standard deviation of the spread. The advantage is that this filter is superior when dealing with noise compared to OLS or TLS methods, also it does not have any lookback period to optimize.<br />
The disadvantage is that is has another parameters (like &delta;) to find out.<br />
<br />
We strongly recommend to read more details about Kalman filter applications in pair trading in [http://www.amazon.com/Algorithmic-Trading-Winning-Strategies-Rationale/dp/1118460146 this book].<br />
<br />
While Kalman Filter also estimates the standard deviation on the fly, our implementation also supports a possibility of having an auxiliary standard deviation indicator applied directly on the forecast error time series. The advantage is that you get less sensitivity on the &delta; parameter, but on the other hand a new lookback parameter is introduced.<br />
<br />
'''Kalman Model has too sensitive parameters which prevents it to be traded in reality. This model is provided only for education purposes - to be able to see how parameters affects its performance. Use Kalman Grid model for any serious trading.'''<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not planned'''<br />
* in PTL Trader: '''not planned'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter transition covariance &delta;, typical value is 0.0001 - '''unfortunately this model is very sensitive to this parameter especially when using standard deviation estimate coming from the Kalman filter itself'''<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* auxiliary standard deviation period - if equal to zero, Kalman filter is used to estimate standard deviation, if non-zero, auxiliary standard deviation indicator with this period is used (an the estimate from Kalman filter is ignored)<br />
* unstable period = how many Kalman filter observations are ignored at the beginning (first observations are quite unstable)<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1, 2><br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-1, 0>...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >10 if used)<br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model.<br />
<br />
'''This model is provided for education purposes only.'''<br />
<br />
== Kalman-grid Model (v2) ==<br />
<br />
After one year of experience with the [[Pair Trading Models#Kalman-grid Model (v1)|Kalman-grid Model (v1)]] and researching [https://en.wikipedia.org/wiki/Machine_learning machine learning principles], we have spent weeks of additional work on developing new version of Kalman Grid model, redesigned from scratch. Kalman Grid v2 is now the most advanced model PTL offers.<br />
<br />
This model brings the superior performance of Kalman filter design, but it also deals with all the flaws of Kalman and old Kalman Grid v1:<br />
* all parameters of Kalman filter have been eliminated (&delta;, V<sub>e</sub>) - they are auto-estimated by the grid logic and updated with each price sample<br />
* increased average performance compared to v1<br />
* fixed issues of old version which prevented model to trade too often (biggest flaw of v1)<br />
<br />
This model rendered the version 1 obsolete and completely replaces it.<br />
<br />
How does it work? Instead of single strategy (and single Kalman filter), the whole grid of Kalman filters is evaluated by a proprietary system at the same time. Then, machine learning principles are applied to rank particular Kalman filter performances. Another algorithm is then used to figure filter outputs (slope, intercept, standard deviation) from the whole grid. The whole system actually behaves like a single Kalman filter, but with no need to provide any parameters.<br />
<br />
Main advantages of this model:<br />
* it squeezes all profit from the pair<br />
* beats any other models in performance (in average) and also in performance per day-in-position<br />
* no risk of over-fitting in parameter optimization (nothing to optimize, except thresholds)<br />
<br />
Issues of this model:<br />
* you still need to set entry/exit thresholds - basically this affect number of trades and slot usage...exit threshold is usually 0, try entry threshold from range <2,4><br />
* the simple A.I. inside is profit-based - so not-so-good pairs with waves in profit can "fool" the algorithm so it falls from the optimum<br />
* model performance is proportional to margin usage<br />
* evaluating this model needs a lot of memory and processing resources<br />
<br />
Specialties:<br />
* if you set entry threshold to 1, exit threshold to -1, model to "simple" and if you allow reversals, the model will be "always-in-position" squeezing the most profit from good pairs<br />
<br />
To give some insight on performance: we took 300 best performing pairs from PTL database in period Jan 2013 - Jan 2016. Then we backtested all 300 pairs using '''out-of-sample''' period (Jan 2016 - Sep 2016) and we compared their performance. Models used: Ratio(per 14), Residual(20), Kalman Grid v1, Kalman Grid v2 using exit threshold of -1 (normal) and 0 (aggressive). Margin 50% (Req-T). Results here:<br />
{|<br />
|'''Model'''<br />
|'''Median CAGR %'''<br />
|'''Mean CAGR %'''<br />
|-<br />
|Kalman-grid v2 (normal)<br />
|'''7.206409'''<br />
|9.538509<br />
|-<br />
|Kalman-grid v2 (aggressive)<br />
|7.088950<br />
|'''11.433199'''<br />
|-<br />
|Kalman-grid v1<br />
|4.877391<br />
|10.794117<br />
|-<br />
|Residual(20)<br />
|3.596212<br />
|6.631089<br />
|-<br />
|Ratio(14)<br />
|3.590883<br />
|8.954484 <br />
|}<br />
<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''since v1.4.0'''<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1, 4><br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-1, 00>...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band (not used often)<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >10 if used)<br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model.<br />
<br />
== Kalman-Auto Model ==<br />
This model is very similar to Kalman-grid model, it is like his younger brother. The main difference is that the simple A.I. of this model is not trying to work with profit (which is problem with certain kind of pairs). Instead, it justs tries to find optimum &delta; (transition covariance) of the Kalman filter together with the entry threshold to match the '''margin usage target'''. An example: you can set this model to try to optimize everything so the strategy spends 50% time in position (so if you traded one year, the model spent only half a year in positions). Or you can say the average slot usage (margin usage) is 50%. This is useful for trading whole portfolios of Kalman-Auto based strategies, so you can fine tune the diversification inside the portfolio correctly.<br />
<br />
Advantages of this model:<br />
* the only parameter to fine-tune of this model is (apart from the usage target) V<sub>e</sub> (Kalman filter observation covariance), which is luckily not sensitive<br />
* the model is much faster to evaluate than Kalman-Grid<br />
* the simple A.I. of this model is robust and not-so easy to fool<br />
* entry threshold is side-product of the self-optimization!<br />
<br />
This is the model you need if you don't want to spent hours of optimizing parameters. You just tell it to trade and stay in position 60% of the time and that's it. On the other hands, it does not match in profits with Kalman-Grid for very good pairs (but actually it has similar performance when you divide the profit to the margin usage).<br />
<br />
Please note there is no regulation feedback loop inside, the self optimization works just with ring-buffer with follows the current point and it is used for finding best parameters. So do not expect you set the target to 80% and at the end you get 80% portfolio usage. The good working range is <40, 60>.<br />
<br />
Example: [https://www.pairtradinglab.com/backtests/WCGoZyG8rDVDQnY1 Visa/Mastercard backtest] with Usage Target set to 60%. Notice the '''% Days In Trade''' resulted in 64.9%. <br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''since v1.4.0'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman-Auto Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* desired margin usage threshold in %, allowed range <20, 80>, the range <40,60> works the best in terms of how precise is the margin usage matched to the target<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model, but:<br />
* entry threshold is figured automatically by the model<br />
* exit threshold is always 0<br />
* entry mode is always "simple"<br />
<br />
= Deprecated Models =<br />
* Orthogonal Model<br />
* Kalman-grid Model (v1)</div>Adminhttps://wiki.pairtradinglab.com/wiki/Pair_Trading_ModelsPair Trading Models2016-12-21T15:36:28Z<p>Admin: /* Model Support */</p>
<hr />
<div>Pair Trading Lab offers pair trading algorithms based on various mathematical models. These are models currently supported in PTL:<br />
== Ratio Model ==<br />
[[File:Ratio-illustration.png|right|650px|thumb|Ratio Uptick Example]]<br />
This is one of the standard pair trading models described in literature. It is based in ratio of instrument prices, [http://en.wikipedia.org/wiki/Moving_average moving average] and [http://en.wikipedia.org/wiki/Standard_deviation standard deviation]. In other words, it is based on [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator.<br />
<br />
Since Nov 27th 2014, this model also supports additional [http://www.investopedia.com/terms/r/rsi.asp RSI filter] you can use in addition to Bollinger Bands method.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (RSI supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.5, 2.5>, 2.0 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values are allowed (to exit on other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical values is <0, 1>, 0 is used most often...only used for downtick entry mode as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* moving average period P<sub>m</sub> (typical range <10, 100>), default = 15<br />
* moving average type T (algorithm), default = exponential<br />
* standard deviation period P<sub>s</sub> (typical range <10, 100>), default = 15<br />
* entry mode (simple, uptick, downtick)<br />
* RSI period and threshold (optional RSI filtering)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* we need to calculate ratio time series ''R(t)'' = ''A(t)'' / ''B(t)''<br />
* let's apply moving average of type T with period P<sub>m</sub> on ''R(t)'' to get time series ''M(t)''<br />
* let's apply standard deviation with period P<sub>s</sub> on ''R(t)'' to get time series ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = (''R(t)'' - ''M(t)'') / ''S(t)'', this time series can give us z-score to signal trading decision directly (in reality we have two Z-scores: Z-score<sub>ask</sub> and Z-score<sub>bid</sub> as they are calculated using different prices, but for the sake of simplicity let's now pretend we don't pay bid-ask spread and we have just one Z-score)<br />
* another common approach (to visualize) is to create bands and put it above the moving average ''M(t)'':<br />
** upper entry band ''U<sub>n</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>n</sub><br />
** lower entry band ''L<sub>n</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>n</sub><br />
** upper exit band ''U<sub>x</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>x</sub><br />
** lower exit band ''L<sub>x</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>x</sub><br />
** upper downtick band ''U<sub>d</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** lower downtick band ''L<sub>d</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** these bands are actually the same bands as in [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator and we can use crossing of ''R(t)'' and bands as trade signals<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'')<br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'')<br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' < E<sub>n</sub> (same as ''R(t-1)'' < ''U<sub>n</sub>(t-1)'')<br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' > -E<sub>n</sub> (same as ''R(t-1)'' > ''L<sub>n</sub>(t-1)'')<br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside (but it must stay above the downtick band):<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' < ''U<sub>n</sub>(t)'' '''and''' ''R(t-1)'' >= ''U<sub>n</sub>(t-1)'' '''and''' ''R(t)'' > ''U<sub>d</sub>(t)''<br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' > ''L<sub>n</sub>(t)'' '''and''' ''R(t-1)'' <= ''L<sub>n</sub>(t-1)'' '''and''' ''R(t)'' < ''L<sub>d</sub>(t)''<br />
<br />
Why do we have the '''simple entry mode'''? In normal situations and backtests, it gives same results as the '''uptick mode'''. But the difference comes up while trading multiple pairs in portfolio. The simple mode allows you to jump in the position immediately after a new slot is freed, regardless of the previous Z-scores.<br />
<br />
'''Which entry mode is better?''' Hard to tell, sometimes the uptick, sometimes the downtick. You have to do your homework and decide, which idea suits your trading style better. In general, uptick/simple mode is more aggressive, as it does not wait for first signs of spread mean reversion.<br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub> (equivalent to ''R(t)'' <= ''U<sub>x</sub>(t)'')<br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub> (equivalent to ''R(t)'' >= ''L<sub>x</sub>(t)'')<br />
<br />
=== Moving Average Types ===<br />
[[File:Rsi-illustration.png|right|650px|thumb|Ratio RSI Trading Example]]<br />
You can choose from these moving average algorithms:<br />
* [http://www.investopedia.com/terms/s/sma.asp Simple Moving Average] (SMA)<br />
* [http://www.investopedia.com/terms/e/ema.asp Exponential Moving Average] (EMA)<br />
* [http://fxtrade.oanda.com/learn/forex-indicators/weighted-moving-average Weighted Moving Average] (WMA)<br />
* [http://www.investopedia.com/terms/d/double-exponential-moving-average.asp Double Exponential Moving Average] (DEMA)<br />
* [http://www.investopedia.com/terms/t/triple-exponential-moving-average.asp Triple Exponential Moving Average] (TEMA)<br />
* [http://daytrading.about.com/od/indicators/a/Triangular.htm Triangular Moving Average] (TMA)<br />
* [http://etfhq.com/blog/2011/11/07/adaptive-moving-average-ama-aka-kaufman-adaptive-moving-average-kama Kaufman Adaptive Moving Average] (KAMA)<br />
* [http://www.mesasoftware.com/Papers/MAMA.pdf MESA Adaptive Moving Average] (MAMA)<br />
* [http://www.tradeescort.com/AgenaIndicatorHelp/AgenaIndicator.html?T3TripleExponentialMovingAverage.html Triple Exponential T3 Moving Average]<br />
<br />
Which one is the best? It depends, you have to test for yourself. They mostly differ in the term of memory and how fast they react to changes. Industry standard and the default is EMA. We suggest to try all of them on some sample pair to see how they work.<br />
<br />
=== RSI Filtering ===<br />
You can combine the Z-score/Bollinger Band model with [http://www.investopedia.com/terms/r/rsi.asp RSI indicator] applied on the ratio ''R(t)''. RSI filter will be automatically enabled if you set RSI threshold to other value than zero. RSI filter is combined with the Z-score rules using '''AND''' operator ('''both''' entry rule and RSI entry rule must be true to open a position).<br />
<br />
You can also change the RSI period if you want (default period = 15).<br />
<br />
RSI Threshold is value between 0 and 50. Because RSI indicator value oscillates between 0 and 100 (where 50 = mean), your threshold value is just used to set real thresholds for RSI:<br />
* let's assume RSI Period P<sub>r</sub> was entered<br />
* effective RSI value threshold is then 50+P<sub>r</sub> (upper) and 50-P<sub>r</sub> (lower)<br />
* you can see both thresholds in the example image at the right side<br />
<br />
Example:<br />
* RSI Threshold entered is 15<br />
* then, short positions are only opened, if RSI >= 65 (50+15)<br />
* long positions are only opened, if RSI <=35 (50-15)<br />
<br />
Useful hint: if you want to control entry rules just by RSI, you can set Entry Mode to simple and Entry Threshold to some low value.<br />
<br />
== Residual Model ==<br />
[[File:Residual-illustration2.png|right|650px|thumb|Residual Downtick Example]]<br />
Residual mode is based on [http://en.wikipedia.org/wiki/Linear_regression linear regression]. In literature it has been also referred to as the '''cointegration approach'''. Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the OLS method (Ordinary Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
This particular implementation is very simple. The regression is constructed using floating window of a fixed period, the same period is used for calculating the standard deviation.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (uptick and downtick modes are supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <15, 300><br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* first we need to construct a linear regression between ''A(t)'', ''B(t)'' using OLS, where ''A(t)'' = ''&beta;'' * ''B(t)'' + ''&alpha;'' + ''R(t)''<br />
* because we use floating window of period P (we calculate new regression each day), we actually get new series ''&beta;(t)'', ''&alpha;(t)'', ''R(t)'', where ''&beta;(t)'', ''&alpha;(t)'' are series of regression coefficients and ''R(t)'' are residuals (prediction errors)<br />
* ''R(t)'' = ''A(t)'' - (''&beta;(t)'' * ''B(t)'' + ''&alpha;(t)'')<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly (in reality we have two Z-scores: Z-score<sub>ask</sub> and Z-score<sub>bid</sub> as they are calculated using different prices, but for the sake of simplicity let's now pretend we don't pay bid-ask spread and we have just one Z-score)<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside (while we must stay above the downtick band):<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>d</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>d</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman Model ==<br />
This model is based on [http://en.wikipedia.org/wiki/Kalman_filter Kalman Filter]. In this case, the filter is used here instead of linear regression to determine proper hedge ratio, deviation from the mean and the standard deviation of the spread. The advantage is that this filter is superior when dealing with noise compared to OLS or TLS methods, also it does not have any lookback period to optimize.<br />
The disadvantage is that is has another parameters (like &delta;) to find out.<br />
<br />
We strongly recommend to read more details about Kalman filter applications in pair trading in [http://www.amazon.com/Algorithmic-Trading-Winning-Strategies-Rationale/dp/1118460146 this book].<br />
<br />
While Kalman Filter also estimates the standard deviation on the fly, our implementation also supports a possibility of having an auxiliary standard deviation indicator applied directly on the forecast error time series. The advantage is that you get less sensitivity on the &delta; parameter, but on the other hand a new lookback parameter is introduced.<br />
<br />
'''Kalman Model has too sensitive parameters which prevents it to be traded in reality. This model is provided only for education purposes - to be able to see how parameters affects its performance. Use Kalman Grid model for any serious trading.'''<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not planned'''<br />
* in PTL Trader: '''not planned'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter transition covariance &delta;, typical value is 0.0001 - '''unfortunately this model is very sensitive to this parameter especially when using standard deviation estimate coming from the Kalman filter itself'''<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* auxiliary standard deviation period - if equal to zero, Kalman filter is used to estimate standard deviation, if non-zero, auxiliary standard deviation indicator with this period is used (an the estimate from Kalman filter is ignored)<br />
* unstable period = how many Kalman filter observations are ignored at the beginning (first observations are quite unstable)<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1, 2><br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-1, 0>...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >10 if used)<br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model.<br />
<br />
'''This model is provided for education purposes only.'''<br />
<br />
== Kalman-grid Model (v2) ==<br />
<br />
After one year of experience with the [[Pair Trading Models#Kalman-grid Model (v1)|Kalman-grid Model (v1)]] and researching [https://en.wikipedia.org/wiki/Machine_learning machine learning principles], we have spent weeks of additional work on developing new version of Kalman Grid model, redesigned from scratch. Kalman Grid v2 is now the most advanced model PTL offers.<br />
<br />
This model brings the superior performance of Kalman filter design, but it also deals with all the flaws of Kalman and old Kalman Grid v1:<br />
* all parameters of Kalman filter have been eliminated (&delta;, V<sub>e</sub>) - they are auto-estimated by the grid logic and updated with each price sample<br />
* increased average performance compared to v1<br />
* fixed issues of old version which prevented model to trade too often (biggest flaw of v1)<br />
<br />
This model rendered the version 1 obsolete and completely replaces it.<br />
<br />
How does it work? Instead of single strategy (and single Kalman filter), the whole grid of Kalman filters is evaluated by a proprietary system at the same time. Then, machine learning principles are applied to rank particular Kalman filter performances. Another algorithm is then used to figure filter outputs (slope, intercept, standard deviation) from the whole grid. The whole system actually behaves like a single Kalman filter, but with no need to provide any parameters.<br />
<br />
Main advantages of this model:<br />
* it squeezes all profit from the pair<br />
* beats any other models in performance (in average) and also in performance per day-in-position<br />
* no risk of over-fitting in parameter optimization (nothing to optimize, except thresholds)<br />
<br />
Issues of this model:<br />
* you still need to set entry/exit thresholds - basically this affect number of trades and slot usage...exit threshold is usually 0, try entry threshold from range <2,4><br />
* the simple A.I. inside is profit-based - so not-so-good pairs with waves in profit can "fool" the algorithm so it falls from the optimum<br />
* model performance is proportional to margin usage<br />
* evaluating this model needs a lot of memory and processing resources<br />
<br />
Specialties:<br />
* if you set entry threshold to 1, exit threshold to -1, model to "simple" and if you allow reversals, the model will be "always-in-position" squeezing the most profit from good pairs<br />
<br />
To give some insight on performance: we took 300 best performing pairs from PTL database in period Jan 2013 - Jan 2016. Then we backtested all 300 pairs using '''out-of-sample''' period (Jan 2016 - Sep 2016) and we compared their performance. Models used: Ratio(per 14), Residual(20), Kalman Grid v1, Kalman Grid v2 using exit threshold of -1 (normal) and 0 (aggressive). Margin 50% (Req-T). Results here:<br />
{|<br />
|'''Model'''<br />
|'''Median CAGR %'''<br />
|'''Mean CAGR %'''<br />
|-<br />
|Kalman-grid v2 (normal)<br />
|'''7.206409'''<br />
|9.538509<br />
|-<br />
|Kalman-grid v2 (aggressive)<br />
|7.088950<br />
|'''11.433199'''<br />
|-<br />
|Kalman-grid v1<br />
|4.877391<br />
|10.794117<br />
|-<br />
|Residual(20)<br />
|3.596212<br />
|6.631089<br />
|-<br />
|Ratio(14)<br />
|3.590883<br />
|8.954484 <br />
|}<br />
<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''since v1.4.0'''<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1, 4><br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-1, 00>...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band (not used often)<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >10 if used)<br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model.<br />
<br />
== Kalman-Auto Model ==<br />
This model is very similar to Kalman-grid model, it is like his younger brother. The main difference is that the simple A.I. of this model is not trying to work with profit (which is problem with certain kind of pairs). Instead, it justs tries to find optimum &delta; (transition covariance) of the Kalman filter together with the entry threshold to match the '''margin usage target'''. An example: you can set this model to try to optimize everything so the strategy spends 50% time in position (so if you traded one year, the model spent only half a year in positions). Or you can say the average slot usage (margin usage) is 50%. This is useful for trading whole portfolios of Kalman-Auto based strategies, so you can fine tune the diversification inside the portfolio correctly.<br />
<br />
Advantages of this model:<br />
* the only parameter to fine-tune of this model is (apart from the usage target) V<sub>e</sub> (Kalman filter observation covariance), which is luckily not sensitive<br />
* the model is much faster to evaluate than Kalman-Grid<br />
* the simple A.I. of this model is robust and not-so easy to fool<br />
* entry threshold is side-product of the self-optimization!<br />
<br />
This is the model you need if you don't want to spent hours of optimizing parameters. You just tell it to trade and stay in position 60% of the time and that's it. On the other hands, it does not match in profits with Kalman-Grid for very good pairs (but actually it has similar performance when you divide the profit to the margin usage).<br />
<br />
Please note there is no regulation feedback loop inside, the self optimization works just with ring-buffer with follows the current point and it is used for finding best parameters. So do not expect you set the target to 80% and at the end you get 80% portfolio usage. The good working range is <40, 60>.<br />
<br />
Example: [https://www.pairtradinglab.com/backtests/WCGoZyG8rDVDQnY1 Visa/Mastercard backtest] with Usage Target set to 60%. Notice the '''% Days In Trade''' resulted in 64.9%. <br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''planned Dec 2016'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman-Auto Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* desired margin usage threshold in %, allowed range <20, 80>, the range <40,60> works the best in terms of how precise is the margin usage matched to the target<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model, but:<br />
* entry threshold is figured automatically by the model<br />
* exit threshold is always 0<br />
* entry mode is always "simple"<br />
<br />
= Deprecated Models =<br />
* Orthogonal Model<br />
* Kalman-grid Model (v1)</div>Adminhttps://wiki.pairtradinglab.com/wiki/Pair_Trading_ModelsPair Trading Models2016-12-21T15:36:04Z<p>Admin: /* Kalman-grid Model (v2) */</p>
<hr />
<div>Pair Trading Lab offers pair trading algorithms based on various mathematical models. These are models currently supported in PTL:<br />
== Ratio Model ==<br />
[[File:Ratio-illustration.png|right|650px|thumb|Ratio Uptick Example]]<br />
This is one of the standard pair trading models described in literature. It is based in ratio of instrument prices, [http://en.wikipedia.org/wiki/Moving_average moving average] and [http://en.wikipedia.org/wiki/Standard_deviation standard deviation]. In other words, it is based on [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator.<br />
<br />
Since Nov 27th 2014, this model also supports additional [http://www.investopedia.com/terms/r/rsi.asp RSI filter] you can use in addition to Bollinger Bands method.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (RSI supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.5, 2.5>, 2.0 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values are allowed (to exit on other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical values is <0, 1>, 0 is used most often...only used for downtick entry mode as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* moving average period P<sub>m</sub> (typical range <10, 100>), default = 15<br />
* moving average type T (algorithm), default = exponential<br />
* standard deviation period P<sub>s</sub> (typical range <10, 100>), default = 15<br />
* entry mode (simple, uptick, downtick)<br />
* RSI period and threshold (optional RSI filtering)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* we need to calculate ratio time series ''R(t)'' = ''A(t)'' / ''B(t)''<br />
* let's apply moving average of type T with period P<sub>m</sub> on ''R(t)'' to get time series ''M(t)''<br />
* let's apply standard deviation with period P<sub>s</sub> on ''R(t)'' to get time series ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = (''R(t)'' - ''M(t)'') / ''S(t)'', this time series can give us z-score to signal trading decision directly (in reality we have two Z-scores: Z-score<sub>ask</sub> and Z-score<sub>bid</sub> as they are calculated using different prices, but for the sake of simplicity let's now pretend we don't pay bid-ask spread and we have just one Z-score)<br />
* another common approach (to visualize) is to create bands and put it above the moving average ''M(t)'':<br />
** upper entry band ''U<sub>n</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>n</sub><br />
** lower entry band ''L<sub>n</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>n</sub><br />
** upper exit band ''U<sub>x</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>x</sub><br />
** lower exit band ''L<sub>x</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>x</sub><br />
** upper downtick band ''U<sub>d</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** lower downtick band ''L<sub>d</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** these bands are actually the same bands as in [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator and we can use crossing of ''R(t)'' and bands as trade signals<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'')<br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'')<br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' < E<sub>n</sub> (same as ''R(t-1)'' < ''U<sub>n</sub>(t-1)'')<br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' > -E<sub>n</sub> (same as ''R(t-1)'' > ''L<sub>n</sub>(t-1)'')<br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside (but it must stay above the downtick band):<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' < ''U<sub>n</sub>(t)'' '''and''' ''R(t-1)'' >= ''U<sub>n</sub>(t-1)'' '''and''' ''R(t)'' > ''U<sub>d</sub>(t)''<br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' > ''L<sub>n</sub>(t)'' '''and''' ''R(t-1)'' <= ''L<sub>n</sub>(t-1)'' '''and''' ''R(t)'' < ''L<sub>d</sub>(t)''<br />
<br />
Why do we have the '''simple entry mode'''? In normal situations and backtests, it gives same results as the '''uptick mode'''. But the difference comes up while trading multiple pairs in portfolio. The simple mode allows you to jump in the position immediately after a new slot is freed, regardless of the previous Z-scores.<br />
<br />
'''Which entry mode is better?''' Hard to tell, sometimes the uptick, sometimes the downtick. You have to do your homework and decide, which idea suits your trading style better. In general, uptick/simple mode is more aggressive, as it does not wait for first signs of spread mean reversion.<br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub> (equivalent to ''R(t)'' <= ''U<sub>x</sub>(t)'')<br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub> (equivalent to ''R(t)'' >= ''L<sub>x</sub>(t)'')<br />
<br />
=== Moving Average Types ===<br />
[[File:Rsi-illustration.png|right|650px|thumb|Ratio RSI Trading Example]]<br />
You can choose from these moving average algorithms:<br />
* [http://www.investopedia.com/terms/s/sma.asp Simple Moving Average] (SMA)<br />
* [http://www.investopedia.com/terms/e/ema.asp Exponential Moving Average] (EMA)<br />
* [http://fxtrade.oanda.com/learn/forex-indicators/weighted-moving-average Weighted Moving Average] (WMA)<br />
* [http://www.investopedia.com/terms/d/double-exponential-moving-average.asp Double Exponential Moving Average] (DEMA)<br />
* [http://www.investopedia.com/terms/t/triple-exponential-moving-average.asp Triple Exponential Moving Average] (TEMA)<br />
* [http://daytrading.about.com/od/indicators/a/Triangular.htm Triangular Moving Average] (TMA)<br />
* [http://etfhq.com/blog/2011/11/07/adaptive-moving-average-ama-aka-kaufman-adaptive-moving-average-kama Kaufman Adaptive Moving Average] (KAMA)<br />
* [http://www.mesasoftware.com/Papers/MAMA.pdf MESA Adaptive Moving Average] (MAMA)<br />
* [http://www.tradeescort.com/AgenaIndicatorHelp/AgenaIndicator.html?T3TripleExponentialMovingAverage.html Triple Exponential T3 Moving Average]<br />
<br />
Which one is the best? It depends, you have to test for yourself. They mostly differ in the term of memory and how fast they react to changes. Industry standard and the default is EMA. We suggest to try all of them on some sample pair to see how they work.<br />
<br />
=== RSI Filtering ===<br />
You can combine the Z-score/Bollinger Band model with [http://www.investopedia.com/terms/r/rsi.asp RSI indicator] applied on the ratio ''R(t)''. RSI filter will be automatically enabled if you set RSI threshold to other value than zero. RSI filter is combined with the Z-score rules using '''AND''' operator ('''both''' entry rule and RSI entry rule must be true to open a position).<br />
<br />
You can also change the RSI period if you want (default period = 15).<br />
<br />
RSI Threshold is value between 0 and 50. Because RSI indicator value oscillates between 0 and 100 (where 50 = mean), your threshold value is just used to set real thresholds for RSI:<br />
* let's assume RSI Period P<sub>r</sub> was entered<br />
* effective RSI value threshold is then 50+P<sub>r</sub> (upper) and 50-P<sub>r</sub> (lower)<br />
* you can see both thresholds in the example image at the right side<br />
<br />
Example:<br />
* RSI Threshold entered is 15<br />
* then, short positions are only opened, if RSI >= 65 (50+15)<br />
* long positions are only opened, if RSI <=35 (50-15)<br />
<br />
Useful hint: if you want to control entry rules just by RSI, you can set Entry Mode to simple and Entry Threshold to some low value.<br />
<br />
== Residual Model ==<br />
[[File:Residual-illustration2.png|right|650px|thumb|Residual Downtick Example]]<br />
Residual mode is based on [http://en.wikipedia.org/wiki/Linear_regression linear regression]. In literature it has been also referred to as the '''cointegration approach'''. Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the OLS method (Ordinary Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
This particular implementation is very simple. The regression is constructed using floating window of a fixed period, the same period is used for calculating the standard deviation.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (uptick and downtick modes are supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <15, 300><br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* first we need to construct a linear regression between ''A(t)'', ''B(t)'' using OLS, where ''A(t)'' = ''&beta;'' * ''B(t)'' + ''&alpha;'' + ''R(t)''<br />
* because we use floating window of period P (we calculate new regression each day), we actually get new series ''&beta;(t)'', ''&alpha;(t)'', ''R(t)'', where ''&beta;(t)'', ''&alpha;(t)'' are series of regression coefficients and ''R(t)'' are residuals (prediction errors)<br />
* ''R(t)'' = ''A(t)'' - (''&beta;(t)'' * ''B(t)'' + ''&alpha;(t)'')<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly (in reality we have two Z-scores: Z-score<sub>ask</sub> and Z-score<sub>bid</sub> as they are calculated using different prices, but for the sake of simplicity let's now pretend we don't pay bid-ask spread and we have just one Z-score)<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside (while we must stay above the downtick band):<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>d</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>d</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman Model ==<br />
This model is based on [http://en.wikipedia.org/wiki/Kalman_filter Kalman Filter]. In this case, the filter is used here instead of linear regression to determine proper hedge ratio, deviation from the mean and the standard deviation of the spread. The advantage is that this filter is superior when dealing with noise compared to OLS or TLS methods, also it does not have any lookback period to optimize.<br />
The disadvantage is that is has another parameters (like &delta;) to find out.<br />
<br />
We strongly recommend to read more details about Kalman filter applications in pair trading in [http://www.amazon.com/Algorithmic-Trading-Winning-Strategies-Rationale/dp/1118460146 this book].<br />
<br />
While Kalman Filter also estimates the standard deviation on the fly, our implementation also supports a possibility of having an auxiliary standard deviation indicator applied directly on the forecast error time series. The advantage is that you get less sensitivity on the &delta; parameter, but on the other hand a new lookback parameter is introduced.<br />
<br />
'''Kalman Model has too sensitive parameters which prevents it to be traded in reality. This model is provided only for education purposes - to be able to see how parameters affects its performance. Use Kalman Grid model for any serious trading.'''<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not planned'''<br />
* in PTL Trader: '''not planned'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter transition covariance &delta;, typical value is 0.0001 - '''unfortunately this model is very sensitive to this parameter especially when using standard deviation estimate coming from the Kalman filter itself'''<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* auxiliary standard deviation period - if equal to zero, Kalman filter is used to estimate standard deviation, if non-zero, auxiliary standard deviation indicator with this period is used (an the estimate from Kalman filter is ignored)<br />
* unstable period = how many Kalman filter observations are ignored at the beginning (first observations are quite unstable)<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1, 2><br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-1, 0>...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >10 if used)<br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model.<br />
<br />
'''This model is provided for education purposes only.'''<br />
<br />
== Kalman-grid Model (v2) ==<br />
<br />
After one year of experience with the [[Pair Trading Models#Kalman-grid Model (v1)|Kalman-grid Model (v1)]] and researching [https://en.wikipedia.org/wiki/Machine_learning machine learning principles], we have spent weeks of additional work on developing new version of Kalman Grid model, redesigned from scratch. Kalman Grid v2 is now the most advanced model PTL offers.<br />
<br />
This model brings the superior performance of Kalman filter design, but it also deals with all the flaws of Kalman and old Kalman Grid v1:<br />
* all parameters of Kalman filter have been eliminated (&delta;, V<sub>e</sub>) - they are auto-estimated by the grid logic and updated with each price sample<br />
* increased average performance compared to v1<br />
* fixed issues of old version which prevented model to trade too often (biggest flaw of v1)<br />
<br />
This model rendered the version 1 obsolete and completely replaces it.<br />
<br />
How does it work? Instead of single strategy (and single Kalman filter), the whole grid of Kalman filters is evaluated by a proprietary system at the same time. Then, machine learning principles are applied to rank particular Kalman filter performances. Another algorithm is then used to figure filter outputs (slope, intercept, standard deviation) from the whole grid. The whole system actually behaves like a single Kalman filter, but with no need to provide any parameters.<br />
<br />
Main advantages of this model:<br />
* it squeezes all profit from the pair<br />
* beats any other models in performance (in average) and also in performance per day-in-position<br />
* no risk of over-fitting in parameter optimization (nothing to optimize, except thresholds)<br />
<br />
Issues of this model:<br />
* you still need to set entry/exit thresholds - basically this affect number of trades and slot usage...exit threshold is usually 0, try entry threshold from range <2,4><br />
* the simple A.I. inside is profit-based - so not-so-good pairs with waves in profit can "fool" the algorithm so it falls from the optimum<br />
* model performance is proportional to margin usage<br />
* evaluating this model needs a lot of memory and processing resources<br />
<br />
Specialties:<br />
* if you set entry threshold to 1, exit threshold to -1, model to "simple" and if you allow reversals, the model will be "always-in-position" squeezing the most profit from good pairs<br />
<br />
To give some insight on performance: we took 300 best performing pairs from PTL database in period Jan 2013 - Jan 2016. Then we backtested all 300 pairs using '''out-of-sample''' period (Jan 2016 - Sep 2016) and we compared their performance. Models used: Ratio(per 14), Residual(20), Kalman Grid v1, Kalman Grid v2 using exit threshold of -1 (normal) and 0 (aggressive). Margin 50% (Req-T). Results here:<br />
{|<br />
|'''Model'''<br />
|'''Median CAGR %'''<br />
|'''Mean CAGR %'''<br />
|-<br />
|Kalman-grid v2 (normal)<br />
|'''7.206409'''<br />
|9.538509<br />
|-<br />
|Kalman-grid v2 (aggressive)<br />
|7.088950<br />
|'''11.433199'''<br />
|-<br />
|Kalman-grid v1<br />
|4.877391<br />
|10.794117<br />
|-<br />
|Residual(20)<br />
|3.596212<br />
|6.631089<br />
|-<br />
|Ratio(14)<br />
|3.590883<br />
|8.954484 <br />
|}<br />
<br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''since v1.4.0'''<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1, 4><br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-1, 00>...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band (not used often)<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >10 if used)<br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model.<br />
<br />
== Kalman-Auto Model ==<br />
This model is very similar to Kalman-grid model, it is like his younger brother. The main difference is that the simple A.I. of this model is not trying to work with profit (which is problem with certain kind of pairs). Instead, it justs tries to find optimum &delta; (transition covariance) of the Kalman filter together with the entry threshold to match the '''margin usage target'''. An example: you can set this model to try to optimize everything so the strategy spends 50% time in position (so if you traded one year, the model spent only half a year in positions). Or you can say the average slot usage (margin usage) is 50%. This is useful for trading whole portfolios of Kalman-Auto based strategies, so you can fine tune the diversification inside the portfolio correctly.<br />
<br />
Advantages of this model:<br />
* the only parameter to fine-tune of this model is (apart from the usage target) V<sub>e</sub> (Kalman filter observation covariance), which is luckily not sensitive<br />
* the model is much faster to evaluate than Kalman-Grid<br />
* the simple A.I. of this model is robust and not-so easy to fool<br />
* entry threshold is side-product of the self-optimization!<br />
<br />
This is the model you need if you don't want to spent hours of optimizing parameters. You just tell it to trade and stay in position 60% of the time and that's it. On the other hands, it does not match in profits with Kalman-Grid for very good pairs (but actually it has similar performance when you divide the profit to the margin usage).<br />
<br />
Please note there is no regulation feedback loop inside, the self optimization works just with ring-buffer with follows the current point and it is used for finding best parameters. So do not expect you set the target to 80% and at the end you get 80% portfolio usage. The good working range is <40, 60>.<br />
<br />
Example: [https://www.pairtradinglab.com/backtests/WCGoZyG8rDVDQnY1 Visa/Mastercard backtest] with Usage Target set to 60%. Notice the '''% Days In Trade''' resulted in 64.9%. <br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''planned Dec 2016'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman-Auto Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* desired margin usage threshold in %, allowed range <20, 80>, the range <40,60> works the best in terms of how precise is the margin usage matched to the target<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model, but:<br />
* entry threshold is figured automatically by the model<br />
* exit threshold is always 0<br />
* entry mode is always "simple"<br />
<br />
= Deprecated Models =<br />
* Orthogonal Model<br />
* Kalman-grid Model (v1)</div>Adminhttps://wiki.pairtradinglab.com/wiki/PTL_Trader_ChangelogPTL Trader Changelog2016-12-21T15:35:14Z<p>Admin: </p>
<hr />
<div>== 1.4.0 ==<br />
* Kalman-Grid (v2) model fully supported<br />
* Kalman-Auto model fully supported<br />
* removed forgotten last price constraint (causing ''suspicious market data'' state in markets missing last price quotes)<br />
<br />
Released 2016-12-21.<br />
<br />
== 1.3.1 ==<br />
* fixed serious bug causing strategy thread to crash completely if an incomplete historical data are provided from IB API (bug in IB API side)- if a strategy is in ''wait for hist_data'' state for few hours, you can treat it like crashed because of this issue<br />
* profit potential is indicated even in ''no new positions'' and ''fully inactive'' states (for manual traders)<br />
* added ''Reset Last Opened'' button for maintenance operations and manual traders<br />
* removed last price constraints (causing ''suspicious market data'' state in markets missing last price quotes)<br />
<br />
Released 2016-11-23.<br />
<br />
== 1.3.0 ==<br />
* multi-platform - builds for win32, win64, linux64 and macOS now available<br />
* indicating both z-score bid and z-score ask separately<br />
* more effective margin allocation algorithm (only differs when margins differ per leg)<br />
* profit potential is now calculated from bid-ask quotes (instead of trades)<br />
* custom thresholds for downtick mode supported<br />
* allow-reversals constraint supported<br />
* copyright and license update<br />
* cosmetic changes<br />
* updated 3rd party components<br />
* migrated to Gradle build system<br />
<br />
Released 2016-11-08.<br />
<br />
== 1.2.4 ==<br />
Minor bugfix release.<br />
* fix for incorrect handling of some time zones (Moscow, etc...)<br />
* Z-score and RSI is now being indicated in more situations, including "No new positions"<br />
<br />
Released 2015-12-21.<br />
<br />
== 1.2.3 ==<br />
* fix for duplicate order ID issues occurring when many orders are being sent at the same time<br />
* Open Long, Open Short and Close buttons can be used in more situations<br />
* Close button can be used to manually resolve "one leg opened" situation<br />
* cosmetic UI changes<br />
* copyright and license update<br />
<br />
Released 2015-07-23.<br />
<br />
== 1.2.2 ==<br />
* added some price event sanity checks and constraints<br />
* other minor fixes / changes<br />
<br />
Released 2015-04-18.<br />
<br />
== 1.2.1 ==<br />
* application JAR is now wrapped in EXE - fix for shortcut/installer issues with Java 1.8<br />
* fixed authentication fail message<br />
* more precise z-score indication (now also the indicated z-score is calculated using bid/ask prices)<br />
* cooldown after external close detected<br />
* "last" prices are no longer used for entry/exit signals (only ask/bid prices are used now)<br />
* redesigned exchange activity detection (to detect holidays / days with no activity)<br />
* trading engine should be more friendly to low liquidity stocks (removed some unnecessary constraints in signal evaluation)<br />
* trading engine will no longer allow positions reversals for now (to be consistent with backtests)<br />
* bid and ask sizes are now displayed in the UI<br />
* other minor fixes and changes<br />
<br />
Internal release.<br />
<br />
== 1.2.0 ==<br />
* internal architecture changes related to compatibility with PTL portfolio system<br />
* RSI filter for Ratio model now fully supported<br />
* all entry modes are now supported for Residual model<br />
<br />
Released 2015-01-02.<br />
<br />
== 1.1.1 ==<br />
Bugfix release & internal architecture improvements:<br />
<br />
* redesigned concurrency and threading architecture (pair strategies now use thread confinement)<br />
* fixed few potential deadlock situations and GUI freezes<br />
* fixed concurrency issue which could potentially lead to allow N+1 slots opened in a rare occasion<br />
* fixed retrying fetching historical data after IB data pacing violation (regression introduced in 1.0.4, it worked in earlier versions)<br />
<br />
Released 2014-10-07.<br />
<br />
== 1.1.0 ==<br />
* basic FA (Financial Adviser) account support (you have to enter the account code manually when connecting)<br />
* increased log file max size and number of backups<br />
* unadjusted split protection (stops auto trading if there is a significant price discrepancy which may indicate that IB forgot to adjust historical data after split event)<br />
* prevents automated trading if historical data fetched are obsolete (older than 5 days)<br />
* connection to IB API is now handled in a separate thread to prevent UI freezes<br />
* updated IB client API files to 971.01<br />
* minor cosmetic changes and bugfixes<br />
* minor changes in log file verbosity<br />
<br />
Released 2014-08-13.<br />
<br />
== 1.0.6 ==<br />
* workaround for "ambiguous contract" issues for some particular IB symbols (MSFT, ...)<br />
* fix for stock symbols like XXX.Y (RDS.A for instance)<br />
* some other small changes/fixes<br />
* more verbose logging of slot allocation process<br />
<br />
Released 2014-03-19.<br />
<br />
== 1.0.5 ==<br />
* fixed a bug causing "wait for min profit" engine status indicated outside trading hours<br />
<br />
Released 2013-11-07.<br />
<br />
== 1.0.4 ==<br />
* z-score and profit potential now update also outside user's configured trading window (as long historical data were available)<br />
* manually opening/closing positions is now enabled in more engine states (for instance in "wait for min profit")<br />
* modified profit estimation formula for residual model, profit estimation now disabled for cases where A<0.1<br />
* fixed PDT protection in position exit logic<br />
* PTL Trader does not try to initialize trading engine and load historical data for portfolios bound to accounts not belonging to current IB account connected anymore<br />
* historical data are now downloaded and models are recalculated every day at 9:30 EST, so z-score and profit potential indicators work with updated model, instead of waiting for user's configured trading window<br />
<br />
Internal release.<br />
<br />
== 1.0.3 ==<br />
* workaround for strategy P/L tracking (ignore portfolio data in IB "black hour" 0:** EST)<br />
* modified default log levels<br />
<br />
First official stable release (2013-08-27).<br />
<br />
== 1.0.2 ==<br />
* workaround for equity tracking (ignore equity data in IB "black hour" 0:** EST)<br />
* more detailed logs for IB portfolio updates<br />
<br />
Internal release.<br />
<br />
== 1.0.1 ==<br />
* debug logs for equity tracking<br />
* reworked execution and commission report handling (fixes slippage tracking problems and missing entries in transaction history)<br />
* PDT protection fix<br />
* libraries updated<br />
<br />
Internal release.<br />
<br />
== 1.0.0 ==<br />
* changed valid range for instrument margins (15% - 100%)<br />
* cooldown period after closing a pair trade (it is not possible to open new pair trade earlier than in two minutes) - to prevent sending new orders until IB portfolio stats settle<br />
* more verbose logging of executions and commission reports (internal log)<br />
* reworked periodic premium membership checks<br />
* updated logo and icons<br />
<br />
Internal release.<br />
<br />
== 0.9.6b ==<br />
* just a small fix - NYSEAMEX exchange has been apparently renamed to NYSEMKT, so both exchanges are now supported<br />
<br />
* Initial version released to public (2013-05-17).<br />
<br />
== 0.9.6 ==<br />
* Status = BETA, not recommended for real trading yet (although we already use it that way ourselves)<br />
<br />
<br />
== Known issues we are working on ==<br />
...</div>Adminhttps://wiki.pairtradinglab.com/wiki/PTL_Trader_ChangelogPTL Trader Changelog2016-11-23T16:02:32Z<p>Admin: </p>
<hr />
<div>== 1.3.1 ==<br />
* fixed serious bug causing strategy thread to crash completely if an incomplete historical data are provided from IB API (bug in IB API side)- if a strategy is in ''wait for hist_data'' state for few hours, you can treat it like crashed because of this issue<br />
* profit potential is indicated even in ''no new positions'' and ''fully inactive'' states (for manual traders)<br />
* added ''Reset Last Opened'' button for maintenance operations and manual traders<br />
* removed last price constraints (causing ''suspicious market data'' state in markets missing last price quotes)<br />
<br />
Released 2016-11-23.<br />
<br />
== 1.3.0 ==<br />
* multi-platform - builds for win32, win64, linux64 and macOS now available<br />
* indicating both z-score bid and z-score ask separately<br />
* more effective margin allocation algorithm (only differs when margins differ per leg)<br />
* profit potential is now calculated from bid-ask quotes (instead of trades)<br />
* custom thresholds for downtick mode supported<br />
* allow-reversals constraint supported<br />
* copyright and license update<br />
* cosmetic changes<br />
* updated 3rd party components<br />
* migrated to Gradle build system<br />
<br />
Released 2016-11-08.<br />
<br />
== 1.2.4 ==<br />
Minor bugfix release.<br />
* fix for incorrect handling of some time zones (Moscow, etc...)<br />
* Z-score and RSI is now being indicated in more situations, including "No new positions"<br />
<br />
Released 2015-12-21.<br />
<br />
== 1.2.3 ==<br />
* fix for duplicate order ID issues occurring when many orders are being sent at the same time<br />
* Open Long, Open Short and Close buttons can be used in more situations<br />
* Close button can be used to manually resolve "one leg opened" situation<br />
* cosmetic UI changes<br />
* copyright and license update<br />
<br />
Released 2015-07-23.<br />
<br />
== 1.2.2 ==<br />
* added some price event sanity checks and constraints<br />
* other minor fixes / changes<br />
<br />
Released 2015-04-18.<br />
<br />
== 1.2.1 ==<br />
* application JAR is now wrapped in EXE - fix for shortcut/installer issues with Java 1.8<br />
* fixed authentication fail message<br />
* more precise z-score indication (now also the indicated z-score is calculated using bid/ask prices)<br />
* cooldown after external close detected<br />
* "last" prices are no longer used for entry/exit signals (only ask/bid prices are used now)<br />
* redesigned exchange activity detection (to detect holidays / days with no activity)<br />
* trading engine should be more friendly to low liquidity stocks (removed some unnecessary constraints in signal evaluation)<br />
* trading engine will no longer allow positions reversals for now (to be consistent with backtests)<br />
* bid and ask sizes are now displayed in the UI<br />
* other minor fixes and changes<br />
<br />
Internal release.<br />
<br />
== 1.2.0 ==<br />
* internal architecture changes related to compatibility with PTL portfolio system<br />
* RSI filter for Ratio model now fully supported<br />
* all entry modes are now supported for Residual model<br />
<br />
Released 2015-01-02.<br />
<br />
== 1.1.1 ==<br />
Bugfix release & internal architecture improvements:<br />
<br />
* redesigned concurrency and threading architecture (pair strategies now use thread confinement)<br />
* fixed few potential deadlock situations and GUI freezes<br />
* fixed concurrency issue which could potentially lead to allow N+1 slots opened in a rare occasion<br />
* fixed retrying fetching historical data after IB data pacing violation (regression introduced in 1.0.4, it worked in earlier versions)<br />
<br />
Released 2014-10-07.<br />
<br />
== 1.1.0 ==<br />
* basic FA (Financial Adviser) account support (you have to enter the account code manually when connecting)<br />
* increased log file max size and number of backups<br />
* unadjusted split protection (stops auto trading if there is a significant price discrepancy which may indicate that IB forgot to adjust historical data after split event)<br />
* prevents automated trading if historical data fetched are obsolete (older than 5 days)<br />
* connection to IB API is now handled in a separate thread to prevent UI freezes<br />
* updated IB client API files to 971.01<br />
* minor cosmetic changes and bugfixes<br />
* minor changes in log file verbosity<br />
<br />
Released 2014-08-13.<br />
<br />
== 1.0.6 ==<br />
* workaround for "ambiguous contract" issues for some particular IB symbols (MSFT, ...)<br />
* fix for stock symbols like XXX.Y (RDS.A for instance)<br />
* some other small changes/fixes<br />
* more verbose logging of slot allocation process<br />
<br />
Released 2014-03-19.<br />
<br />
== 1.0.5 ==<br />
* fixed a bug causing "wait for min profit" engine status indicated outside trading hours<br />
<br />
Released 2013-11-07.<br />
<br />
== 1.0.4 ==<br />
* z-score and profit potential now update also outside user's configured trading window (as long historical data were available)<br />
* manually opening/closing positions is now enabled in more engine states (for instance in "wait for min profit")<br />
* modified profit estimation formula for residual model, profit estimation now disabled for cases where A<0.1<br />
* fixed PDT protection in position exit logic<br />
* PTL Trader does not try to initialize trading engine and load historical data for portfolios bound to accounts not belonging to current IB account connected anymore<br />
* historical data are now downloaded and models are recalculated every day at 9:30 EST, so z-score and profit potential indicators work with updated model, instead of waiting for user's configured trading window<br />
<br />
Internal release.<br />
<br />
== 1.0.3 ==<br />
* workaround for strategy P/L tracking (ignore portfolio data in IB "black hour" 0:** EST)<br />
* modified default log levels<br />
<br />
First official stable release (2013-08-27).<br />
<br />
== 1.0.2 ==<br />
* workaround for equity tracking (ignore equity data in IB "black hour" 0:** EST)<br />
* more detailed logs for IB portfolio updates<br />
<br />
Internal release.<br />
<br />
== 1.0.1 ==<br />
* debug logs for equity tracking<br />
* reworked execution and commission report handling (fixes slippage tracking problems and missing entries in transaction history)<br />
* PDT protection fix<br />
* libraries updated<br />
<br />
Internal release.<br />
<br />
== 1.0.0 ==<br />
* changed valid range for instrument margins (15% - 100%)<br />
* cooldown period after closing a pair trade (it is not possible to open new pair trade earlier than in two minutes) - to prevent sending new orders until IB portfolio stats settle<br />
* more verbose logging of executions and commission reports (internal log)<br />
* reworked periodic premium membership checks<br />
* updated logo and icons<br />
<br />
Internal release.<br />
<br />
== 0.9.6b ==<br />
* just a small fix - NYSEAMEX exchange has been apparently renamed to NYSEMKT, so both exchanges are now supported<br />
<br />
* Initial version released to public (2013-05-17).<br />
<br />
== 0.9.6 ==<br />
* Status = BETA, not recommended for real trading yet (although we already use it that way ourselves)<br />
<br />
<br />
== Known issues we are working on ==<br />
...</div>Adminhttps://wiki.pairtradinglab.com/wiki/Pair_Trading_ModelsPair Trading Models2016-11-08T10:30:27Z<p>Admin: /* Kalman-Auto Model */</p>
<hr />
<div>Pair Trading Lab offers pair trading algorithms based on various mathematical models. These are models currently supported in PTL:<br />
== Ratio Model ==<br />
[[File:Ratio-illustration.png|right|650px|thumb|Ratio Uptick Example]]<br />
This is one of the standard pair trading models described in literature. It is based in ratio of instrument prices, [http://en.wikipedia.org/wiki/Moving_average moving average] and [http://en.wikipedia.org/wiki/Standard_deviation standard deviation]. In other words, it is based on [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator.<br />
<br />
Since Nov 27th 2014, this model also supports additional [http://www.investopedia.com/terms/r/rsi.asp RSI filter] you can use in addition to Bollinger Bands method.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (RSI supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.5, 2.5>, 2.0 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values are allowed (to exit on other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical values is <0, 1>, 0 is used most often...only used for downtick entry mode as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* moving average period P<sub>m</sub> (typical range <10, 100>), default = 15<br />
* moving average type T (algorithm), default = exponential<br />
* standard deviation period P<sub>s</sub> (typical range <10, 100>), default = 15<br />
* entry mode (simple, uptick, downtick)<br />
* RSI period and threshold (optional RSI filtering)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* we need to calculate ratio time series ''R(t)'' = ''A(t)'' / ''B(t)''<br />
* let's apply moving average of type T with period P<sub>m</sub> on ''R(t)'' to get time series ''M(t)''<br />
* let's apply standard deviation with period P<sub>s</sub> on ''R(t)'' to get time series ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = (''R(t)'' - ''M(t)'') / ''S(t)'', this time series can give us z-score to signal trading decision directly (in reality we have two Z-scores: Z-score<sub>ask</sub> and Z-score<sub>bid</sub> as they are calculated using different prices, but for the sake of simplicity let's now pretend we don't pay bid-ask spread and we have just one Z-score)<br />
* another common approach (to visualize) is to create bands and put it above the moving average ''M(t)'':<br />
** upper entry band ''U<sub>n</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>n</sub><br />
** lower entry band ''L<sub>n</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>n</sub><br />
** upper exit band ''U<sub>x</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>x</sub><br />
** lower exit band ''L<sub>x</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>x</sub><br />
** upper downtick band ''U<sub>d</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** lower downtick band ''L<sub>d</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** these bands are actually the same bands as in [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator and we can use crossing of ''R(t)'' and bands as trade signals<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'')<br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'')<br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' < E<sub>n</sub> (same as ''R(t-1)'' < ''U<sub>n</sub>(t-1)'')<br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' > -E<sub>n</sub> (same as ''R(t-1)'' > ''L<sub>n</sub>(t-1)'')<br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside (but it must stay above the downtick band):<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' < ''U<sub>n</sub>(t)'' '''and''' ''R(t-1)'' >= ''U<sub>n</sub>(t-1)'' '''and''' ''R(t)'' > ''U<sub>d</sub>(t)''<br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' > ''L<sub>n</sub>(t)'' '''and''' ''R(t-1)'' <= ''L<sub>n</sub>(t-1)'' '''and''' ''R(t)'' < ''L<sub>d</sub>(t)''<br />
<br />
Why do we have the '''simple entry mode'''? In normal situations and backtests, it gives same results as the '''uptick mode'''. But the difference comes up while trading multiple pairs in portfolio. The simple mode allows you to jump in the position immediately after a new slot is freed, regardless of the previous Z-scores.<br />
<br />
'''Which entry mode is better?''' Hard to tell, sometimes the uptick, sometimes the downtick. You have to do your homework and decide, which idea suits your trading style better. In general, uptick/simple mode is more aggressive, as it does not wait for first signs of spread mean reversion.<br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub> (equivalent to ''R(t)'' <= ''U<sub>x</sub>(t)'')<br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub> (equivalent to ''R(t)'' >= ''L<sub>x</sub>(t)'')<br />
<br />
=== Moving Average Types ===<br />
[[File:Rsi-illustration.png|right|650px|thumb|Ratio RSI Trading Example]]<br />
You can choose from these moving average algorithms:<br />
* [http://www.investopedia.com/terms/s/sma.asp Simple Moving Average] (SMA)<br />
* [http://www.investopedia.com/terms/e/ema.asp Exponential Moving Average] (EMA)<br />
* [http://fxtrade.oanda.com/learn/forex-indicators/weighted-moving-average Weighted Moving Average] (WMA)<br />
* [http://www.investopedia.com/terms/d/double-exponential-moving-average.asp Double Exponential Moving Average] (DEMA)<br />
* [http://www.investopedia.com/terms/t/triple-exponential-moving-average.asp Triple Exponential Moving Average] (TEMA)<br />
* [http://daytrading.about.com/od/indicators/a/Triangular.htm Triangular Moving Average] (TMA)<br />
* [http://etfhq.com/blog/2011/11/07/adaptive-moving-average-ama-aka-kaufman-adaptive-moving-average-kama Kaufman Adaptive Moving Average] (KAMA)<br />
* [http://www.mesasoftware.com/Papers/MAMA.pdf MESA Adaptive Moving Average] (MAMA)<br />
* [http://www.tradeescort.com/AgenaIndicatorHelp/AgenaIndicator.html?T3TripleExponentialMovingAverage.html Triple Exponential T3 Moving Average]<br />
<br />
Which one is the best? It depends, you have to test for yourself. They mostly differ in the term of memory and how fast they react to changes. Industry standard and the default is EMA. We suggest to try all of them on some sample pair to see how they work.<br />
<br />
=== RSI Filtering ===<br />
You can combine the Z-score/Bollinger Band model with [http://www.investopedia.com/terms/r/rsi.asp RSI indicator] applied on the ratio ''R(t)''. RSI filter will be automatically enabled if you set RSI threshold to other value than zero. RSI filter is combined with the Z-score rules using '''AND''' operator ('''both''' entry rule and RSI entry rule must be true to open a position).<br />
<br />
You can also change the RSI period if you want (default period = 15).<br />
<br />
RSI Threshold is value between 0 and 50. Because RSI indicator value oscillates between 0 and 100 (where 50 = mean), your threshold value is just used to set real thresholds for RSI:<br />
* let's assume RSI Period P<sub>r</sub> was entered<br />
* effective RSI value threshold is then 50+P<sub>r</sub> (upper) and 50-P<sub>r</sub> (lower)<br />
* you can see both thresholds in the example image at the right side<br />
<br />
Example:<br />
* RSI Threshold entered is 15<br />
* then, short positions are only opened, if RSI >= 65 (50+15)<br />
* long positions are only opened, if RSI <=35 (50-15)<br />
<br />
Useful hint: if you want to control entry rules just by RSI, you can set Entry Mode to simple and Entry Threshold to some low value.<br />
<br />
== Residual Model ==<br />
[[File:Residual-illustration2.png|right|650px|thumb|Residual Downtick Example]]<br />
Residual mode is based on [http://en.wikipedia.org/wiki/Linear_regression linear regression]. In literature it has been also referred to as the '''cointegration approach'''. Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the OLS method (Ordinary Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
This particular implementation is very simple. The regression is constructed using floating window of a fixed period, the same period is used for calculating the standard deviation.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (uptick and downtick modes are supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <15, 300><br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* first we need to construct a linear regression between ''A(t)'', ''B(t)'' using OLS, where ''A(t)'' = ''&beta;'' * ''B(t)'' + ''&alpha;'' + ''R(t)''<br />
* because we use floating window of period P (we calculate new regression each day), we actually get new series ''&beta;(t)'', ''&alpha;(t)'', ''R(t)'', where ''&beta;(t)'', ''&alpha;(t)'' are series of regression coefficients and ''R(t)'' are residuals (prediction errors)<br />
* ''R(t)'' = ''A(t)'' - (''&beta;(t)'' * ''B(t)'' + ''&alpha;(t)'')<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly (in reality we have two Z-scores: Z-score<sub>ask</sub> and Z-score<sub>bid</sub> as they are calculated using different prices, but for the sake of simplicity let's now pretend we don't pay bid-ask spread and we have just one Z-score)<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside (while we must stay above the downtick band):<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>d</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>d</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman Model ==<br />
This model is based on [http://en.wikipedia.org/wiki/Kalman_filter Kalman Filter]. In this case, the filter is used here instead of linear regression to determine proper hedge ratio, deviation from the mean and the standard deviation of the spread. The advantage is that this filter is superior when dealing with noise compared to OLS or TLS methods, also it does not have any lookback period to optimize.<br />
The disadvantage is that is has another parameters (like &delta;) to find out.<br />
<br />
We strongly recommend to read more details about Kalman filter applications in pair trading in [http://www.amazon.com/Algorithmic-Trading-Winning-Strategies-Rationale/dp/1118460146 this book].<br />
<br />
While Kalman Filter also estimates the standard deviation on the fly, our implementation also supports a possibility of having an auxiliary standard deviation indicator applied directly on the forecast error time series. The advantage is that you get less sensitivity on the &delta; parameter, but on the other hand a new lookback parameter is introduced.<br />
<br />
'''Kalman Model has too sensitive parameters which prevents it to be traded in reality. This model is provided only for education purposes - to be able to see how parameters affects its performance. Use Kalman Grid model for any serious trading.'''<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not planned'''<br />
* in PTL Trader: '''not planned'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter transition covariance &delta;, typical value is 0.0001 - '''unfortunately this model is very sensitive to this parameter especially when using standard deviation estimate coming from the Kalman filter itself'''<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* auxiliary standard deviation period - if equal to zero, Kalman filter is used to estimate standard deviation, if non-zero, auxiliary standard deviation indicator with this period is used (an the estimate from Kalman filter is ignored)<br />
* unstable period = how many Kalman filter observations are ignored at the beginning (first observations are quite unstable)<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1, 2><br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-1, 0>...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >10 if used)<br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model.<br />
<br />
'''This model is provided for education purposes only.'''<br />
<br />
== Kalman-grid Model (v2) ==<br />
<br />
After one year of experience with the [[Pair Trading Models#Kalman-grid Model (v1)|Kalman-grid Model (v1)]] and researching [https://en.wikipedia.org/wiki/Machine_learning machine learning principles], we have spent weeks of additional work on developing new version of Kalman Grid model, redesigned from scratch. Kalman Grid v2 is now the most advanced model PTL offers.<br />
<br />
This model brings the superior performance of Kalman filter design, but it also deals with all the flaws of Kalman and old Kalman Grid v1:<br />
* all parameters of Kalman filter have been eliminated (&delta;, V<sub>e</sub>) - they are auto-estimated by the grid logic and updated with each price sample<br />
* increased average performance compared to v1<br />
* fixed issues of old version which prevented model to trade too often (biggest flaw of v1)<br />
<br />
This model rendered the version 1 obsolete and completely replaces it.<br />
<br />
How does it work? Instead of single strategy (and single Kalman filter), the whole grid of Kalman filters is evaluated by a proprietary system at the same time. Then, machine learning principles are applied to rank particular Kalman filter performances. Another algorithm is then used to figure filter outputs (slope, intercept, standard deviation) from the whole grid. The whole system actually behaves like a single Kalman filter, but with no need to provide any parameters.<br />
<br />
Main advantages of this model:<br />
* it squeezes all profit from the pair<br />
* beats any other models in performance (in average) and also in performance per day-in-position<br />
* no risk of over-fitting in parameter optimization (nothing to optimize, except thresholds)<br />
<br />
Issues of this model:<br />
* you still need to set entry/exit thresholds - basically this affect number of trades and slot usage...exit threshold is usually 0, try entry threshold from range <2,4><br />
* the simple A.I. inside is profit-based - so not-so-good pairs with waves in profit can "fool" the algorithm so it falls from the optimum<br />
* model performance is proportional to margin usage<br />
* evaluating this model needs a lot of memory and processing resources<br />
<br />
Specialties:<br />
* if you set entry threshold to 1, exit threshold to -1, model to "simple" and if you allow reversals, the model will be "always-in-position" squeezing the most profit from good pairs<br />
<br />
To give some insight on performance: we took 300 best performing pairs from PTL database in period Jan 2013 - Jan 2016. Then we backtested all 300 pairs using '''out-of-sample''' period (Jan 2016 - Sep 2016) and we compared their performance. Models used: Ratio(per 14), Residual(20), Kalman Grid v1, Kalman Grid v2 using exit threshold of -1 (normal) and 0 (aggressive). Margin 50% (Req-T). Results here:<br />
{|<br />
|'''Model'''<br />
|'''Median CAGR %'''<br />
|'''Mean CAGR %'''<br />
|-<br />
|Kalman-grid v2 (normal)<br />
|'''7.206409'''<br />
|9.538509<br />
|-<br />
|Kalman-grid v2 (aggressive)<br />
|7.088950<br />
|'''11.433199'''<br />
|-<br />
|Kalman-grid v1<br />
|4.877391<br />
|10.794117<br />
|-<br />
|Residual(20)<br />
|3.596212<br />
|6.631089<br />
|-<br />
|Ratio(14)<br />
|3.590883<br />
|8.954484 <br />
|}<br />
<br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1, 4><br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-1, 00>...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band (not used often)<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >10 if used)<br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model.<br />
<br />
== Kalman-Auto Model ==<br />
This model is very similar to Kalman-grid model, it is like his younger brother. The main difference is that the simple A.I. of this model is not trying to work with profit (which is problem with certain kind of pairs). Instead, it justs tries to find optimum &delta; (transition covariance) of the Kalman filter together with the entry threshold to match the '''margin usage target'''. An example: you can set this model to try to optimize everything so the strategy spends 50% time in position (so if you traded one year, the model spent only half a year in positions). Or you can say the average slot usage (margin usage) is 50%. This is useful for trading whole portfolios of Kalman-Auto based strategies, so you can fine tune the diversification inside the portfolio correctly.<br />
<br />
Advantages of this model:<br />
* the only parameter to fine-tune of this model is (apart from the usage target) V<sub>e</sub> (Kalman filter observation covariance), which is luckily not sensitive<br />
* the model is much faster to evaluate than Kalman-Grid<br />
* the simple A.I. of this model is robust and not-so easy to fool<br />
* entry threshold is side-product of the self-optimization!<br />
<br />
This is the model you need if you don't want to spent hours of optimizing parameters. You just tell it to trade and stay in position 60% of the time and that's it. On the other hands, it does not match in profits with Kalman-Grid for very good pairs (but actually it has similar performance when you divide the profit to the margin usage).<br />
<br />
Please note there is no regulation feedback loop inside, the self optimization works just with ring-buffer with follows the current point and it is used for finding best parameters. So do not expect you set the target to 80% and at the end you get 80% portfolio usage. The good working range is <40, 60>.<br />
<br />
Example: [https://www.pairtradinglab.com/backtests/WCGoZyG8rDVDQnY1 Visa/Mastercard backtest] with Usage Target set to 60%. Notice the '''% Days In Trade''' resulted in 64.9%. <br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''planned Dec 2016'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman-Auto Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* desired margin usage threshold in %, allowed range <20, 80>, the range <40,60> works the best in terms of how precise is the margin usage matched to the target<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model, but:<br />
* entry threshold is figured automatically by the model<br />
* exit threshold is always 0<br />
* entry mode is always "simple"<br />
<br />
= Deprecated Models =<br />
* Orthogonal Model<br />
* Kalman-grid Model (v1)</div>Adminhttps://wiki.pairtradinglab.com/wiki/Pair_Trading_ModelsPair Trading Models2016-11-08T10:25:50Z<p>Admin: /* Kalman-Auto Model */</p>
<hr />
<div>Pair Trading Lab offers pair trading algorithms based on various mathematical models. These are models currently supported in PTL:<br />
== Ratio Model ==<br />
[[File:Ratio-illustration.png|right|650px|thumb|Ratio Uptick Example]]<br />
This is one of the standard pair trading models described in literature. It is based in ratio of instrument prices, [http://en.wikipedia.org/wiki/Moving_average moving average] and [http://en.wikipedia.org/wiki/Standard_deviation standard deviation]. In other words, it is based on [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator.<br />
<br />
Since Nov 27th 2014, this model also supports additional [http://www.investopedia.com/terms/r/rsi.asp RSI filter] you can use in addition to Bollinger Bands method.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (RSI supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.5, 2.5>, 2.0 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values are allowed (to exit on other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical values is <0, 1>, 0 is used most often...only used for downtick entry mode as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* moving average period P<sub>m</sub> (typical range <10, 100>), default = 15<br />
* moving average type T (algorithm), default = exponential<br />
* standard deviation period P<sub>s</sub> (typical range <10, 100>), default = 15<br />
* entry mode (simple, uptick, downtick)<br />
* RSI period and threshold (optional RSI filtering)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* we need to calculate ratio time series ''R(t)'' = ''A(t)'' / ''B(t)''<br />
* let's apply moving average of type T with period P<sub>m</sub> on ''R(t)'' to get time series ''M(t)''<br />
* let's apply standard deviation with period P<sub>s</sub> on ''R(t)'' to get time series ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = (''R(t)'' - ''M(t)'') / ''S(t)'', this time series can give us z-score to signal trading decision directly (in reality we have two Z-scores: Z-score<sub>ask</sub> and Z-score<sub>bid</sub> as they are calculated using different prices, but for the sake of simplicity let's now pretend we don't pay bid-ask spread and we have just one Z-score)<br />
* another common approach (to visualize) is to create bands and put it above the moving average ''M(t)'':<br />
** upper entry band ''U<sub>n</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>n</sub><br />
** lower entry band ''L<sub>n</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>n</sub><br />
** upper exit band ''U<sub>x</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>x</sub><br />
** lower exit band ''L<sub>x</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>x</sub><br />
** upper downtick band ''U<sub>d</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** lower downtick band ''L<sub>d</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** these bands are actually the same bands as in [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator and we can use crossing of ''R(t)'' and bands as trade signals<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'')<br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'')<br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' < E<sub>n</sub> (same as ''R(t-1)'' < ''U<sub>n</sub>(t-1)'')<br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' > -E<sub>n</sub> (same as ''R(t-1)'' > ''L<sub>n</sub>(t-1)'')<br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside (but it must stay above the downtick band):<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' < ''U<sub>n</sub>(t)'' '''and''' ''R(t-1)'' >= ''U<sub>n</sub>(t-1)'' '''and''' ''R(t)'' > ''U<sub>d</sub>(t)''<br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' > ''L<sub>n</sub>(t)'' '''and''' ''R(t-1)'' <= ''L<sub>n</sub>(t-1)'' '''and''' ''R(t)'' < ''L<sub>d</sub>(t)''<br />
<br />
Why do we have the '''simple entry mode'''? In normal situations and backtests, it gives same results as the '''uptick mode'''. But the difference comes up while trading multiple pairs in portfolio. The simple mode allows you to jump in the position immediately after a new slot is freed, regardless of the previous Z-scores.<br />
<br />
'''Which entry mode is better?''' Hard to tell, sometimes the uptick, sometimes the downtick. You have to do your homework and decide, which idea suits your trading style better. In general, uptick/simple mode is more aggressive, as it does not wait for first signs of spread mean reversion.<br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub> (equivalent to ''R(t)'' <= ''U<sub>x</sub>(t)'')<br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub> (equivalent to ''R(t)'' >= ''L<sub>x</sub>(t)'')<br />
<br />
=== Moving Average Types ===<br />
[[File:Rsi-illustration.png|right|650px|thumb|Ratio RSI Trading Example]]<br />
You can choose from these moving average algorithms:<br />
* [http://www.investopedia.com/terms/s/sma.asp Simple Moving Average] (SMA)<br />
* [http://www.investopedia.com/terms/e/ema.asp Exponential Moving Average] (EMA)<br />
* [http://fxtrade.oanda.com/learn/forex-indicators/weighted-moving-average Weighted Moving Average] (WMA)<br />
* [http://www.investopedia.com/terms/d/double-exponential-moving-average.asp Double Exponential Moving Average] (DEMA)<br />
* [http://www.investopedia.com/terms/t/triple-exponential-moving-average.asp Triple Exponential Moving Average] (TEMA)<br />
* [http://daytrading.about.com/od/indicators/a/Triangular.htm Triangular Moving Average] (TMA)<br />
* [http://etfhq.com/blog/2011/11/07/adaptive-moving-average-ama-aka-kaufman-adaptive-moving-average-kama Kaufman Adaptive Moving Average] (KAMA)<br />
* [http://www.mesasoftware.com/Papers/MAMA.pdf MESA Adaptive Moving Average] (MAMA)<br />
* [http://www.tradeescort.com/AgenaIndicatorHelp/AgenaIndicator.html?T3TripleExponentialMovingAverage.html Triple Exponential T3 Moving Average]<br />
<br />
Which one is the best? It depends, you have to test for yourself. They mostly differ in the term of memory and how fast they react to changes. Industry standard and the default is EMA. We suggest to try all of them on some sample pair to see how they work.<br />
<br />
=== RSI Filtering ===<br />
You can combine the Z-score/Bollinger Band model with [http://www.investopedia.com/terms/r/rsi.asp RSI indicator] applied on the ratio ''R(t)''. RSI filter will be automatically enabled if you set RSI threshold to other value than zero. RSI filter is combined with the Z-score rules using '''AND''' operator ('''both''' entry rule and RSI entry rule must be true to open a position).<br />
<br />
You can also change the RSI period if you want (default period = 15).<br />
<br />
RSI Threshold is value between 0 and 50. Because RSI indicator value oscillates between 0 and 100 (where 50 = mean), your threshold value is just used to set real thresholds for RSI:<br />
* let's assume RSI Period P<sub>r</sub> was entered<br />
* effective RSI value threshold is then 50+P<sub>r</sub> (upper) and 50-P<sub>r</sub> (lower)<br />
* you can see both thresholds in the example image at the right side<br />
<br />
Example:<br />
* RSI Threshold entered is 15<br />
* then, short positions are only opened, if RSI >= 65 (50+15)<br />
* long positions are only opened, if RSI <=35 (50-15)<br />
<br />
Useful hint: if you want to control entry rules just by RSI, you can set Entry Mode to simple and Entry Threshold to some low value.<br />
<br />
== Residual Model ==<br />
[[File:Residual-illustration2.png|right|650px|thumb|Residual Downtick Example]]<br />
Residual mode is based on [http://en.wikipedia.org/wiki/Linear_regression linear regression]. In literature it has been also referred to as the '''cointegration approach'''. Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the OLS method (Ordinary Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
This particular implementation is very simple. The regression is constructed using floating window of a fixed period, the same period is used for calculating the standard deviation.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (uptick and downtick modes are supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <15, 300><br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* first we need to construct a linear regression between ''A(t)'', ''B(t)'' using OLS, where ''A(t)'' = ''&beta;'' * ''B(t)'' + ''&alpha;'' + ''R(t)''<br />
* because we use floating window of period P (we calculate new regression each day), we actually get new series ''&beta;(t)'', ''&alpha;(t)'', ''R(t)'', where ''&beta;(t)'', ''&alpha;(t)'' are series of regression coefficients and ''R(t)'' are residuals (prediction errors)<br />
* ''R(t)'' = ''A(t)'' - (''&beta;(t)'' * ''B(t)'' + ''&alpha;(t)'')<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly (in reality we have two Z-scores: Z-score<sub>ask</sub> and Z-score<sub>bid</sub> as they are calculated using different prices, but for the sake of simplicity let's now pretend we don't pay bid-ask spread and we have just one Z-score)<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside (while we must stay above the downtick band):<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>d</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>d</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman Model ==<br />
This model is based on [http://en.wikipedia.org/wiki/Kalman_filter Kalman Filter]. In this case, the filter is used here instead of linear regression to determine proper hedge ratio, deviation from the mean and the standard deviation of the spread. The advantage is that this filter is superior when dealing with noise compared to OLS or TLS methods, also it does not have any lookback period to optimize.<br />
The disadvantage is that is has another parameters (like &delta;) to find out.<br />
<br />
We strongly recommend to read more details about Kalman filter applications in pair trading in [http://www.amazon.com/Algorithmic-Trading-Winning-Strategies-Rationale/dp/1118460146 this book].<br />
<br />
While Kalman Filter also estimates the standard deviation on the fly, our implementation also supports a possibility of having an auxiliary standard deviation indicator applied directly on the forecast error time series. The advantage is that you get less sensitivity on the &delta; parameter, but on the other hand a new lookback parameter is introduced.<br />
<br />
'''Kalman Model has too sensitive parameters which prevents it to be traded in reality. This model is provided only for education purposes - to be able to see how parameters affects its performance. Use Kalman Grid model for any serious trading.'''<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not planned'''<br />
* in PTL Trader: '''not planned'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter transition covariance &delta;, typical value is 0.0001 - '''unfortunately this model is very sensitive to this parameter especially when using standard deviation estimate coming from the Kalman filter itself'''<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* auxiliary standard deviation period - if equal to zero, Kalman filter is used to estimate standard deviation, if non-zero, auxiliary standard deviation indicator with this period is used (an the estimate from Kalman filter is ignored)<br />
* unstable period = how many Kalman filter observations are ignored at the beginning (first observations are quite unstable)<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1, 2><br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-1, 0>...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >10 if used)<br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model.<br />
<br />
'''This model is provided for education purposes only.'''<br />
<br />
== Kalman-grid Model (v2) ==<br />
<br />
After one year of experience with the [[Pair Trading Models#Kalman-grid Model (v1)|Kalman-grid Model (v1)]] and researching [https://en.wikipedia.org/wiki/Machine_learning machine learning principles], we have spent weeks of additional work on developing new version of Kalman Grid model, redesigned from scratch. Kalman Grid v2 is now the most advanced model PTL offers.<br />
<br />
This model brings the superior performance of Kalman filter design, but it also deals with all the flaws of Kalman and old Kalman Grid v1:<br />
* all parameters of Kalman filter have been eliminated (&delta;, V<sub>e</sub>) - they are auto-estimated by the grid logic and updated with each price sample<br />
* increased average performance compared to v1<br />
* fixed issues of old version which prevented model to trade too often (biggest flaw of v1)<br />
<br />
This model rendered the version 1 obsolete and completely replaces it.<br />
<br />
How does it work? Instead of single strategy (and single Kalman filter), the whole grid of Kalman filters is evaluated by a proprietary system at the same time. Then, machine learning principles are applied to rank particular Kalman filter performances. Another algorithm is then used to figure filter outputs (slope, intercept, standard deviation) from the whole grid. The whole system actually behaves like a single Kalman filter, but with no need to provide any parameters.<br />
<br />
Main advantages of this model:<br />
* it squeezes all profit from the pair<br />
* beats any other models in performance (in average) and also in performance per day-in-position<br />
* no risk of over-fitting in parameter optimization (nothing to optimize, except thresholds)<br />
<br />
Issues of this model:<br />
* you still need to set entry/exit thresholds - basically this affect number of trades and slot usage...exit threshold is usually 0, try entry threshold from range <2,4><br />
* the simple A.I. inside is profit-based - so not-so-good pairs with waves in profit can "fool" the algorithm so it falls from the optimum<br />
* model performance is proportional to margin usage<br />
* evaluating this model needs a lot of memory and processing resources<br />
<br />
Specialties:<br />
* if you set entry threshold to 1, exit threshold to -1, model to "simple" and if you allow reversals, the model will be "always-in-position" squeezing the most profit from good pairs<br />
<br />
To give some insight on performance: we took 300 best performing pairs from PTL database in period Jan 2013 - Jan 2016. Then we backtested all 300 pairs using '''out-of-sample''' period (Jan 2016 - Sep 2016) and we compared their performance. Models used: Ratio(per 14), Residual(20), Kalman Grid v1, Kalman Grid v2 using exit threshold of -1 (normal) and 0 (aggressive). Margin 50% (Req-T). Results here:<br />
{|<br />
|'''Model'''<br />
|'''Median CAGR %'''<br />
|'''Mean CAGR %'''<br />
|-<br />
|Kalman-grid v2 (normal)<br />
|'''7.206409'''<br />
|9.538509<br />
|-<br />
|Kalman-grid v2 (aggressive)<br />
|7.088950<br />
|'''11.433199'''<br />
|-<br />
|Kalman-grid v1<br />
|4.877391<br />
|10.794117<br />
|-<br />
|Residual(20)<br />
|3.596212<br />
|6.631089<br />
|-<br />
|Ratio(14)<br />
|3.590883<br />
|8.954484 <br />
|}<br />
<br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1, 4><br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-1, 00>...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band (not used often)<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >10 if used)<br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model.<br />
<br />
== Kalman-Auto Model ==<br />
This model is very similar to Kalman-grid model, it is like his younger brother. The main difference is that the simple A.I. of this model is not trying to work with profit (which is problem with certain kind of pairs). Instead, it justs tries to find optimum &delta; (transition covariance) of the Kalman filter together with the entry threshold to match the '''margin usage target'''. An example: you can set this model to try to optimize everything so the strategy spends 50% time in position (so if you traded one year, the model spent only half a year in positions). Or you can say the average slot usage (margin usage) is 50%. This is useful for trading whole portfolios of Kalman-Auto based strategies, so you can fine tune the diversification inside the portfolio correctly.<br />
<br />
Advantages of this model:<br />
* the only parameter to fine-tune of this model is (apart from the usage target) V<sub>e</sub> (Kalman filter observation covariance), which is luckily not sensitive<br />
* the model is much faster to evaluate than Kalman-Grid<br />
* the simple A.I. of this model is robust and not-so easy to fool<br />
* entry threshold is side-product of the self-optimization!<br />
<br />
This is the model you need if you don't want to spent hours of optimizing parameters. You just tell it to trade and stay in position 60% of the time and that's it. On the other hands, it does not match in profits with Kalman-Grid for very good pairs (but actually it has similar performance when you divide the profit to the margin usage).<br />
<br />
Please note there is no regulation feedback loop inside, the self optimization works just with ring-buffer with follows the current point and it is used for finding best parameters. So do not expect you set the target to 80% and at the end you get 80% portfolio usage. The good working range is <40, 60>.<br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''planned Dec 2016'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman-Auto Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* desired margin usage threshold in %, allowed range <20, 80>, the range <40,60> works the best in terms of how precise is the margin usage matched to the target<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model, but:<br />
* entry threshold is figured automatically by the model<br />
* exit threshold is always 0<br />
* entry mode is always "simple"<br />
<br />
= Deprecated Models =<br />
* Orthogonal Model<br />
* Kalman-grid Model (v1)</div>Adminhttps://wiki.pairtradinglab.com/wiki/Pair_Trading_ModelsPair Trading Models2016-11-08T10:02:13Z<p>Admin: /* Kalman-grid Model (v2) */</p>
<hr />
<div>Pair Trading Lab offers pair trading algorithms based on various mathematical models. These are models currently supported in PTL:<br />
== Ratio Model ==<br />
[[File:Ratio-illustration.png|right|650px|thumb|Ratio Uptick Example]]<br />
This is one of the standard pair trading models described in literature. It is based in ratio of instrument prices, [http://en.wikipedia.org/wiki/Moving_average moving average] and [http://en.wikipedia.org/wiki/Standard_deviation standard deviation]. In other words, it is based on [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator.<br />
<br />
Since Nov 27th 2014, this model also supports additional [http://www.investopedia.com/terms/r/rsi.asp RSI filter] you can use in addition to Bollinger Bands method.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (RSI supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.5, 2.5>, 2.0 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values are allowed (to exit on other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical values is <0, 1>, 0 is used most often...only used for downtick entry mode as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* moving average period P<sub>m</sub> (typical range <10, 100>), default = 15<br />
* moving average type T (algorithm), default = exponential<br />
* standard deviation period P<sub>s</sub> (typical range <10, 100>), default = 15<br />
* entry mode (simple, uptick, downtick)<br />
* RSI period and threshold (optional RSI filtering)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* we need to calculate ratio time series ''R(t)'' = ''A(t)'' / ''B(t)''<br />
* let's apply moving average of type T with period P<sub>m</sub> on ''R(t)'' to get time series ''M(t)''<br />
* let's apply standard deviation with period P<sub>s</sub> on ''R(t)'' to get time series ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = (''R(t)'' - ''M(t)'') / ''S(t)'', this time series can give us z-score to signal trading decision directly (in reality we have two Z-scores: Z-score<sub>ask</sub> and Z-score<sub>bid</sub> as they are calculated using different prices, but for the sake of simplicity let's now pretend we don't pay bid-ask spread and we have just one Z-score)<br />
* another common approach (to visualize) is to create bands and put it above the moving average ''M(t)'':<br />
** upper entry band ''U<sub>n</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>n</sub><br />
** lower entry band ''L<sub>n</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>n</sub><br />
** upper exit band ''U<sub>x</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>x</sub><br />
** lower exit band ''L<sub>x</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>x</sub><br />
** upper downtick band ''U<sub>d</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** lower downtick band ''L<sub>d</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** these bands are actually the same bands as in [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator and we can use crossing of ''R(t)'' and bands as trade signals<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'')<br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'')<br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' < E<sub>n</sub> (same as ''R(t-1)'' < ''U<sub>n</sub>(t-1)'')<br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' > -E<sub>n</sub> (same as ''R(t-1)'' > ''L<sub>n</sub>(t-1)'')<br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside (but it must stay above the downtick band):<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' < ''U<sub>n</sub>(t)'' '''and''' ''R(t-1)'' >= ''U<sub>n</sub>(t-1)'' '''and''' ''R(t)'' > ''U<sub>d</sub>(t)''<br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' > ''L<sub>n</sub>(t)'' '''and''' ''R(t-1)'' <= ''L<sub>n</sub>(t-1)'' '''and''' ''R(t)'' < ''L<sub>d</sub>(t)''<br />
<br />
Why do we have the '''simple entry mode'''? In normal situations and backtests, it gives same results as the '''uptick mode'''. But the difference comes up while trading multiple pairs in portfolio. The simple mode allows you to jump in the position immediately after a new slot is freed, regardless of the previous Z-scores.<br />
<br />
'''Which entry mode is better?''' Hard to tell, sometimes the uptick, sometimes the downtick. You have to do your homework and decide, which idea suits your trading style better. In general, uptick/simple mode is more aggressive, as it does not wait for first signs of spread mean reversion.<br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub> (equivalent to ''R(t)'' <= ''U<sub>x</sub>(t)'')<br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub> (equivalent to ''R(t)'' >= ''L<sub>x</sub>(t)'')<br />
<br />
=== Moving Average Types ===<br />
[[File:Rsi-illustration.png|right|650px|thumb|Ratio RSI Trading Example]]<br />
You can choose from these moving average algorithms:<br />
* [http://www.investopedia.com/terms/s/sma.asp Simple Moving Average] (SMA)<br />
* [http://www.investopedia.com/terms/e/ema.asp Exponential Moving Average] (EMA)<br />
* [http://fxtrade.oanda.com/learn/forex-indicators/weighted-moving-average Weighted Moving Average] (WMA)<br />
* [http://www.investopedia.com/terms/d/double-exponential-moving-average.asp Double Exponential Moving Average] (DEMA)<br />
* [http://www.investopedia.com/terms/t/triple-exponential-moving-average.asp Triple Exponential Moving Average] (TEMA)<br />
* [http://daytrading.about.com/od/indicators/a/Triangular.htm Triangular Moving Average] (TMA)<br />
* [http://etfhq.com/blog/2011/11/07/adaptive-moving-average-ama-aka-kaufman-adaptive-moving-average-kama Kaufman Adaptive Moving Average] (KAMA)<br />
* [http://www.mesasoftware.com/Papers/MAMA.pdf MESA Adaptive Moving Average] (MAMA)<br />
* [http://www.tradeescort.com/AgenaIndicatorHelp/AgenaIndicator.html?T3TripleExponentialMovingAverage.html Triple Exponential T3 Moving Average]<br />
<br />
Which one is the best? It depends, you have to test for yourself. They mostly differ in the term of memory and how fast they react to changes. Industry standard and the default is EMA. We suggest to try all of them on some sample pair to see how they work.<br />
<br />
=== RSI Filtering ===<br />
You can combine the Z-score/Bollinger Band model with [http://www.investopedia.com/terms/r/rsi.asp RSI indicator] applied on the ratio ''R(t)''. RSI filter will be automatically enabled if you set RSI threshold to other value than zero. RSI filter is combined with the Z-score rules using '''AND''' operator ('''both''' entry rule and RSI entry rule must be true to open a position).<br />
<br />
You can also change the RSI period if you want (default period = 15).<br />
<br />
RSI Threshold is value between 0 and 50. Because RSI indicator value oscillates between 0 and 100 (where 50 = mean), your threshold value is just used to set real thresholds for RSI:<br />
* let's assume RSI Period P<sub>r</sub> was entered<br />
* effective RSI value threshold is then 50+P<sub>r</sub> (upper) and 50-P<sub>r</sub> (lower)<br />
* you can see both thresholds in the example image at the right side<br />
<br />
Example:<br />
* RSI Threshold entered is 15<br />
* then, short positions are only opened, if RSI >= 65 (50+15)<br />
* long positions are only opened, if RSI <=35 (50-15)<br />
<br />
Useful hint: if you want to control entry rules just by RSI, you can set Entry Mode to simple and Entry Threshold to some low value.<br />
<br />
== Residual Model ==<br />
[[File:Residual-illustration2.png|right|650px|thumb|Residual Downtick Example]]<br />
Residual mode is based on [http://en.wikipedia.org/wiki/Linear_regression linear regression]. In literature it has been also referred to as the '''cointegration approach'''. Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the OLS method (Ordinary Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
This particular implementation is very simple. The regression is constructed using floating window of a fixed period, the same period is used for calculating the standard deviation.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (uptick and downtick modes are supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <15, 300><br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* first we need to construct a linear regression between ''A(t)'', ''B(t)'' using OLS, where ''A(t)'' = ''&beta;'' * ''B(t)'' + ''&alpha;'' + ''R(t)''<br />
* because we use floating window of period P (we calculate new regression each day), we actually get new series ''&beta;(t)'', ''&alpha;(t)'', ''R(t)'', where ''&beta;(t)'', ''&alpha;(t)'' are series of regression coefficients and ''R(t)'' are residuals (prediction errors)<br />
* ''R(t)'' = ''A(t)'' - (''&beta;(t)'' * ''B(t)'' + ''&alpha;(t)'')<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly (in reality we have two Z-scores: Z-score<sub>ask</sub> and Z-score<sub>bid</sub> as they are calculated using different prices, but for the sake of simplicity let's now pretend we don't pay bid-ask spread and we have just one Z-score)<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside (while we must stay above the downtick band):<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>d</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>d</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman Model ==<br />
This model is based on [http://en.wikipedia.org/wiki/Kalman_filter Kalman Filter]. In this case, the filter is used here instead of linear regression to determine proper hedge ratio, deviation from the mean and the standard deviation of the spread. The advantage is that this filter is superior when dealing with noise compared to OLS or TLS methods, also it does not have any lookback period to optimize.<br />
The disadvantage is that is has another parameters (like &delta;) to find out.<br />
<br />
We strongly recommend to read more details about Kalman filter applications in pair trading in [http://www.amazon.com/Algorithmic-Trading-Winning-Strategies-Rationale/dp/1118460146 this book].<br />
<br />
While Kalman Filter also estimates the standard deviation on the fly, our implementation also supports a possibility of having an auxiliary standard deviation indicator applied directly on the forecast error time series. The advantage is that you get less sensitivity on the &delta; parameter, but on the other hand a new lookback parameter is introduced.<br />
<br />
'''Kalman Model has too sensitive parameters which prevents it to be traded in reality. This model is provided only for education purposes - to be able to see how parameters affects its performance. Use Kalman Grid model for any serious trading.'''<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not planned'''<br />
* in PTL Trader: '''not planned'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter transition covariance &delta;, typical value is 0.0001 - '''unfortunately this model is very sensitive to this parameter especially when using standard deviation estimate coming from the Kalman filter itself'''<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* auxiliary standard deviation period - if equal to zero, Kalman filter is used to estimate standard deviation, if non-zero, auxiliary standard deviation indicator with this period is used (an the estimate from Kalman filter is ignored)<br />
* unstable period = how many Kalman filter observations are ignored at the beginning (first observations are quite unstable)<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1, 2><br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-1, 0>...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >10 if used)<br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model.<br />
<br />
'''This model is provided for education purposes only.'''<br />
<br />
== Kalman-grid Model (v2) ==<br />
<br />
After one year of experience with the [[Pair Trading Models#Kalman-grid Model (v1)|Kalman-grid Model (v1)]] and researching [https://en.wikipedia.org/wiki/Machine_learning machine learning principles], we have spent weeks of additional work on developing new version of Kalman Grid model, redesigned from scratch. Kalman Grid v2 is now the most advanced model PTL offers.<br />
<br />
This model brings the superior performance of Kalman filter design, but it also deals with all the flaws of Kalman and old Kalman Grid v1:<br />
* all parameters of Kalman filter have been eliminated (&delta;, V<sub>e</sub>) - they are auto-estimated by the grid logic and updated with each price sample<br />
* increased average performance compared to v1<br />
* fixed issues of old version which prevented model to trade too often (biggest flaw of v1)<br />
<br />
This model rendered the version 1 obsolete and completely replaces it.<br />
<br />
How does it work? Instead of single strategy (and single Kalman filter), the whole grid of Kalman filters is evaluated by a proprietary system at the same time. Then, machine learning principles are applied to rank particular Kalman filter performances. Another algorithm is then used to figure filter outputs (slope, intercept, standard deviation) from the whole grid. The whole system actually behaves like a single Kalman filter, but with no need to provide any parameters.<br />
<br />
Main advantages of this model:<br />
* it squeezes all profit from the pair<br />
* beats any other models in performance (in average) and also in performance per day-in-position<br />
* no risk of over-fitting in parameter optimization (nothing to optimize, except thresholds)<br />
<br />
Issues of this model:<br />
* you still need to set entry/exit thresholds - basically this affect number of trades and slot usage...exit threshold is usually 0, try entry threshold from range <2,4><br />
* the simple A.I. inside is profit-based - so not-so-good pairs with waves in profit can "fool" the algorithm so it falls from the optimum<br />
* model performance is proportional to margin usage<br />
* evaluating this model needs a lot of memory and processing resources<br />
<br />
Specialties:<br />
* if you set entry threshold to 1, exit threshold to -1, model to "simple" and if you allow reversals, the model will be "always-in-position" squeezing the most profit from good pairs<br />
<br />
To give some insight on performance: we took 300 best performing pairs from PTL database in period Jan 2013 - Jan 2016. Then we backtested all 300 pairs using '''out-of-sample''' period (Jan 2016 - Sep 2016) and we compared their performance. Models used: Ratio(per 14), Residual(20), Kalman Grid v1, Kalman Grid v2 using exit threshold of -1 (normal) and 0 (aggressive). Margin 50% (Req-T). Results here:<br />
{|<br />
|'''Model'''<br />
|'''Median CAGR %'''<br />
|'''Mean CAGR %'''<br />
|-<br />
|Kalman-grid v2 (normal)<br />
|'''7.206409'''<br />
|9.538509<br />
|-<br />
|Kalman-grid v2 (aggressive)<br />
|7.088950<br />
|'''11.433199'''<br />
|-<br />
|Kalman-grid v1<br />
|4.877391<br />
|10.794117<br />
|-<br />
|Residual(20)<br />
|3.596212<br />
|6.631089<br />
|-<br />
|Ratio(14)<br />
|3.590883<br />
|8.954484 <br />
|}<br />
<br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1, 4><br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-1, 00>...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band (not used often)<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >10 if used)<br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model.<br />
<br />
== Kalman-Auto Model ==<br />
'''This section is under construction'''<br />
<br />
This model is similar to Kalman-grid model, but it is more simple. This model does not optimize profit, but instead it optimizes margin usage to match predefined target. This model is very robust (is not fooled by glitches in performance) and it automatically optimizes entry rules (thresholds).<br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''planned Dec 2016'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman-Auto Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* Usage Threshold in %, allowed range <20, 80><br />
<br />
= Deprecated Models =<br />
* Orthogonal Model<br />
* Kalman-grid Model (v1)</div>Adminhttps://wiki.pairtradinglab.com/wiki/Pair_Trading_ModelsPair Trading Models2016-11-08T10:01:01Z<p>Admin: /* Kalman Model */</p>
<hr />
<div>Pair Trading Lab offers pair trading algorithms based on various mathematical models. These are models currently supported in PTL:<br />
== Ratio Model ==<br />
[[File:Ratio-illustration.png|right|650px|thumb|Ratio Uptick Example]]<br />
This is one of the standard pair trading models described in literature. It is based in ratio of instrument prices, [http://en.wikipedia.org/wiki/Moving_average moving average] and [http://en.wikipedia.org/wiki/Standard_deviation standard deviation]. In other words, it is based on [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator.<br />
<br />
Since Nov 27th 2014, this model also supports additional [http://www.investopedia.com/terms/r/rsi.asp RSI filter] you can use in addition to Bollinger Bands method.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (RSI supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.5, 2.5>, 2.0 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values are allowed (to exit on other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical values is <0, 1>, 0 is used most often...only used for downtick entry mode as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* moving average period P<sub>m</sub> (typical range <10, 100>), default = 15<br />
* moving average type T (algorithm), default = exponential<br />
* standard deviation period P<sub>s</sub> (typical range <10, 100>), default = 15<br />
* entry mode (simple, uptick, downtick)<br />
* RSI period and threshold (optional RSI filtering)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* we need to calculate ratio time series ''R(t)'' = ''A(t)'' / ''B(t)''<br />
* let's apply moving average of type T with period P<sub>m</sub> on ''R(t)'' to get time series ''M(t)''<br />
* let's apply standard deviation with period P<sub>s</sub> on ''R(t)'' to get time series ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = (''R(t)'' - ''M(t)'') / ''S(t)'', this time series can give us z-score to signal trading decision directly (in reality we have two Z-scores: Z-score<sub>ask</sub> and Z-score<sub>bid</sub> as they are calculated using different prices, but for the sake of simplicity let's now pretend we don't pay bid-ask spread and we have just one Z-score)<br />
* another common approach (to visualize) is to create bands and put it above the moving average ''M(t)'':<br />
** upper entry band ''U<sub>n</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>n</sub><br />
** lower entry band ''L<sub>n</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>n</sub><br />
** upper exit band ''U<sub>x</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>x</sub><br />
** lower exit band ''L<sub>x</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>x</sub><br />
** upper downtick band ''U<sub>d</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** lower downtick band ''L<sub>d</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** these bands are actually the same bands as in [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator and we can use crossing of ''R(t)'' and bands as trade signals<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'')<br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'')<br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' < E<sub>n</sub> (same as ''R(t-1)'' < ''U<sub>n</sub>(t-1)'')<br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' > -E<sub>n</sub> (same as ''R(t-1)'' > ''L<sub>n</sub>(t-1)'')<br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside (but it must stay above the downtick band):<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' < ''U<sub>n</sub>(t)'' '''and''' ''R(t-1)'' >= ''U<sub>n</sub>(t-1)'' '''and''' ''R(t)'' > ''U<sub>d</sub>(t)''<br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' > ''L<sub>n</sub>(t)'' '''and''' ''R(t-1)'' <= ''L<sub>n</sub>(t-1)'' '''and''' ''R(t)'' < ''L<sub>d</sub>(t)''<br />
<br />
Why do we have the '''simple entry mode'''? In normal situations and backtests, it gives same results as the '''uptick mode'''. But the difference comes up while trading multiple pairs in portfolio. The simple mode allows you to jump in the position immediately after a new slot is freed, regardless of the previous Z-scores.<br />
<br />
'''Which entry mode is better?''' Hard to tell, sometimes the uptick, sometimes the downtick. You have to do your homework and decide, which idea suits your trading style better. In general, uptick/simple mode is more aggressive, as it does not wait for first signs of spread mean reversion.<br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub> (equivalent to ''R(t)'' <= ''U<sub>x</sub>(t)'')<br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub> (equivalent to ''R(t)'' >= ''L<sub>x</sub>(t)'')<br />
<br />
=== Moving Average Types ===<br />
[[File:Rsi-illustration.png|right|650px|thumb|Ratio RSI Trading Example]]<br />
You can choose from these moving average algorithms:<br />
* [http://www.investopedia.com/terms/s/sma.asp Simple Moving Average] (SMA)<br />
* [http://www.investopedia.com/terms/e/ema.asp Exponential Moving Average] (EMA)<br />
* [http://fxtrade.oanda.com/learn/forex-indicators/weighted-moving-average Weighted Moving Average] (WMA)<br />
* [http://www.investopedia.com/terms/d/double-exponential-moving-average.asp Double Exponential Moving Average] (DEMA)<br />
* [http://www.investopedia.com/terms/t/triple-exponential-moving-average.asp Triple Exponential Moving Average] (TEMA)<br />
* [http://daytrading.about.com/od/indicators/a/Triangular.htm Triangular Moving Average] (TMA)<br />
* [http://etfhq.com/blog/2011/11/07/adaptive-moving-average-ama-aka-kaufman-adaptive-moving-average-kama Kaufman Adaptive Moving Average] (KAMA)<br />
* [http://www.mesasoftware.com/Papers/MAMA.pdf MESA Adaptive Moving Average] (MAMA)<br />
* [http://www.tradeescort.com/AgenaIndicatorHelp/AgenaIndicator.html?T3TripleExponentialMovingAverage.html Triple Exponential T3 Moving Average]<br />
<br />
Which one is the best? It depends, you have to test for yourself. They mostly differ in the term of memory and how fast they react to changes. Industry standard and the default is EMA. We suggest to try all of them on some sample pair to see how they work.<br />
<br />
=== RSI Filtering ===<br />
You can combine the Z-score/Bollinger Band model with [http://www.investopedia.com/terms/r/rsi.asp RSI indicator] applied on the ratio ''R(t)''. RSI filter will be automatically enabled if you set RSI threshold to other value than zero. RSI filter is combined with the Z-score rules using '''AND''' operator ('''both''' entry rule and RSI entry rule must be true to open a position).<br />
<br />
You can also change the RSI period if you want (default period = 15).<br />
<br />
RSI Threshold is value between 0 and 50. Because RSI indicator value oscillates between 0 and 100 (where 50 = mean), your threshold value is just used to set real thresholds for RSI:<br />
* let's assume RSI Period P<sub>r</sub> was entered<br />
* effective RSI value threshold is then 50+P<sub>r</sub> (upper) and 50-P<sub>r</sub> (lower)<br />
* you can see both thresholds in the example image at the right side<br />
<br />
Example:<br />
* RSI Threshold entered is 15<br />
* then, short positions are only opened, if RSI >= 65 (50+15)<br />
* long positions are only opened, if RSI <=35 (50-15)<br />
<br />
Useful hint: if you want to control entry rules just by RSI, you can set Entry Mode to simple and Entry Threshold to some low value.<br />
<br />
== Residual Model ==<br />
[[File:Residual-illustration2.png|right|650px|thumb|Residual Downtick Example]]<br />
Residual mode is based on [http://en.wikipedia.org/wiki/Linear_regression linear regression]. In literature it has been also referred to as the '''cointegration approach'''. Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the OLS method (Ordinary Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
This particular implementation is very simple. The regression is constructed using floating window of a fixed period, the same period is used for calculating the standard deviation.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (uptick and downtick modes are supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <15, 300><br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* first we need to construct a linear regression between ''A(t)'', ''B(t)'' using OLS, where ''A(t)'' = ''&beta;'' * ''B(t)'' + ''&alpha;'' + ''R(t)''<br />
* because we use floating window of period P (we calculate new regression each day), we actually get new series ''&beta;(t)'', ''&alpha;(t)'', ''R(t)'', where ''&beta;(t)'', ''&alpha;(t)'' are series of regression coefficients and ''R(t)'' are residuals (prediction errors)<br />
* ''R(t)'' = ''A(t)'' - (''&beta;(t)'' * ''B(t)'' + ''&alpha;(t)'')<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly (in reality we have two Z-scores: Z-score<sub>ask</sub> and Z-score<sub>bid</sub> as they are calculated using different prices, but for the sake of simplicity let's now pretend we don't pay bid-ask spread and we have just one Z-score)<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside (while we must stay above the downtick band):<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>d</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>d</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman Model ==<br />
This model is based on [http://en.wikipedia.org/wiki/Kalman_filter Kalman Filter]. In this case, the filter is used here instead of linear regression to determine proper hedge ratio, deviation from the mean and the standard deviation of the spread. The advantage is that this filter is superior when dealing with noise compared to OLS or TLS methods, also it does not have any lookback period to optimize.<br />
The disadvantage is that is has another parameters (like &delta;) to find out.<br />
<br />
We strongly recommend to read more details about Kalman filter applications in pair trading in [http://www.amazon.com/Algorithmic-Trading-Winning-Strategies-Rationale/dp/1118460146 this book].<br />
<br />
While Kalman Filter also estimates the standard deviation on the fly, our implementation also supports a possibility of having an auxiliary standard deviation indicator applied directly on the forecast error time series. The advantage is that you get less sensitivity on the &delta; parameter, but on the other hand a new lookback parameter is introduced.<br />
<br />
'''Kalman Model has too sensitive parameters which prevents it to be traded in reality. This model is provided only for education purposes - to be able to see how parameters affects its performance. Use Kalman Grid model for any serious trading.'''<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not planned'''<br />
* in PTL Trader: '''not planned'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter transition covariance &delta;, typical value is 0.0001 - '''unfortunately this model is very sensitive to this parameter especially when using standard deviation estimate coming from the Kalman filter itself'''<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* auxiliary standard deviation period - if equal to zero, Kalman filter is used to estimate standard deviation, if non-zero, auxiliary standard deviation indicator with this period is used (an the estimate from Kalman filter is ignored)<br />
* unstable period = how many Kalman filter observations are ignored at the beginning (first observations are quite unstable)<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1, 2><br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-1, 0>...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >10 if used)<br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model.<br />
<br />
'''This model is provided for education purposes only.'''<br />
<br />
== Kalman-grid Model (v2) ==<br />
<br />
After one year of experience with the [[Pair Trading Models#Kalman-grid Model (v1)|Kalman-grid Model (v1)]] and researching [https://en.wikipedia.org/wiki/Machine_learning machine learning principles], we have spent weeks of additional work on developing new version of Kalman Grid model, redesigned from scratch. Kalman Grid v2 is now the most advanced model PTL offers.<br />
<br />
This model brings the superior performance of Kalman filter design, but it also deals with all the flaws of Kalman and old Kalman Grid v1:<br />
* all parameters of Kalman filter have been eliminated (&delta;, V<sub>e</sub>) - they are auto-estimated by the grid logic and updated with each price sample<br />
* increased average performance compared to v1<br />
* fixed issues of old version which prevented model to trade too often (biggest flaw of v1)<br />
<br />
This model rendered the version 1 obsolete and completely replaces it.<br />
<br />
How does it work? Instead of single strategy (and single Kalman filter), the whole grid of Kalman filters is evaluated by a proprietary system at the same time. Then, machine learning principles are applied to rank particular Kalman filter performances. Another algorithm is then used to figure filter outputs (slope, intercept, standard deviation) from the whole grid. The whole system actually behaves like a single Kalman filter, but with no need to provide any parameters.<br />
<br />
Main advantages of this model:<br />
* it squeezes all profit from the pair<br />
* beats any other models in performance (in average) and also in performance per day-in-position<br />
* no risk of over-fitting in parameter optimization (nothing to optimize, except thresholds)<br />
<br />
Issues of this model:<br />
* you still need to set entry/exit thresholds - basically this affect number of trades and slot usage...exit threshold is usually 0, try entry threshold from range <2,4><br />
* the simple A.I. inside is profit-based - so not-so-good pairs with waves in profit can "fool" the algorithm so it falls from the optimum<br />
* model performance is proportional to margin usage<br />
<br />
Specialties:<br />
* if you set entry threshold to 1, exit threshold to -1, model to "simple" and if you allow reversals, the model will be "always-in-position" squeezing the most profit from good pairs<br />
<br />
To give some insight on performance: we took 300 best performing pairs from PTL database in period Jan 2013 - Jan 2016. Then we backtested all 300 pairs using '''out-of-sample''' period (Jan 2016 - Sep 2016) and we compared their performance. Models used: Ratio(per 14), Residual(20), Kalman Grid v1, Kalman Grid v2 using exit threshold of -1 (normal) and 0 (aggressive). Margin 50% (Req-T). Results here:<br />
{|<br />
|'''Model'''<br />
|'''Median CAGR %'''<br />
|'''Mean CAGR %'''<br />
|-<br />
|Kalman-grid v2 (normal)<br />
|'''7.206409'''<br />
|9.538509<br />
|-<br />
|Kalman-grid v2 (aggressive)<br />
|7.088950<br />
|'''11.433199'''<br />
|-<br />
|Kalman-grid v1<br />
|4.877391<br />
|10.794117<br />
|-<br />
|Residual(20)<br />
|3.596212<br />
|6.631089<br />
|-<br />
|Ratio(14)<br />
|3.590883<br />
|8.954484 <br />
|}<br />
<br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1, 4><br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-1, 00>...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band (not used often)<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >10 if used)<br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model.<br />
<br />
== Kalman-Auto Model ==<br />
'''This section is under construction'''<br />
<br />
This model is similar to Kalman-grid model, but it is more simple. This model does not optimize profit, but instead it optimizes margin usage to match predefined target. This model is very robust (is not fooled by glitches in performance) and it automatically optimizes entry rules (thresholds).<br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''planned Dec 2016'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman-Auto Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* Usage Threshold in %, allowed range <20, 80><br />
<br />
= Deprecated Models =<br />
* Orthogonal Model<br />
* Kalman-grid Model (v1)</div>Adminhttps://wiki.pairtradinglab.com/wiki/Pair_Trading_ModelsPair Trading Models2016-11-08T10:00:19Z<p>Admin: /* Kalman-grid Model (v2) */</p>
<hr />
<div>Pair Trading Lab offers pair trading algorithms based on various mathematical models. These are models currently supported in PTL:<br />
== Ratio Model ==<br />
[[File:Ratio-illustration.png|right|650px|thumb|Ratio Uptick Example]]<br />
This is one of the standard pair trading models described in literature. It is based in ratio of instrument prices, [http://en.wikipedia.org/wiki/Moving_average moving average] and [http://en.wikipedia.org/wiki/Standard_deviation standard deviation]. In other words, it is based on [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator.<br />
<br />
Since Nov 27th 2014, this model also supports additional [http://www.investopedia.com/terms/r/rsi.asp RSI filter] you can use in addition to Bollinger Bands method.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (RSI supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.5, 2.5>, 2.0 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values are allowed (to exit on other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical values is <0, 1>, 0 is used most often...only used for downtick entry mode as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* moving average period P<sub>m</sub> (typical range <10, 100>), default = 15<br />
* moving average type T (algorithm), default = exponential<br />
* standard deviation period P<sub>s</sub> (typical range <10, 100>), default = 15<br />
* entry mode (simple, uptick, downtick)<br />
* RSI period and threshold (optional RSI filtering)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* we need to calculate ratio time series ''R(t)'' = ''A(t)'' / ''B(t)''<br />
* let's apply moving average of type T with period P<sub>m</sub> on ''R(t)'' to get time series ''M(t)''<br />
* let's apply standard deviation with period P<sub>s</sub> on ''R(t)'' to get time series ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = (''R(t)'' - ''M(t)'') / ''S(t)'', this time series can give us z-score to signal trading decision directly (in reality we have two Z-scores: Z-score<sub>ask</sub> and Z-score<sub>bid</sub> as they are calculated using different prices, but for the sake of simplicity let's now pretend we don't pay bid-ask spread and we have just one Z-score)<br />
* another common approach (to visualize) is to create bands and put it above the moving average ''M(t)'':<br />
** upper entry band ''U<sub>n</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>n</sub><br />
** lower entry band ''L<sub>n</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>n</sub><br />
** upper exit band ''U<sub>x</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>x</sub><br />
** lower exit band ''L<sub>x</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>x</sub><br />
** upper downtick band ''U<sub>d</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** lower downtick band ''L<sub>d</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** these bands are actually the same bands as in [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator and we can use crossing of ''R(t)'' and bands as trade signals<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'')<br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'')<br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' < E<sub>n</sub> (same as ''R(t-1)'' < ''U<sub>n</sub>(t-1)'')<br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' > -E<sub>n</sub> (same as ''R(t-1)'' > ''L<sub>n</sub>(t-1)'')<br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside (but it must stay above the downtick band):<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' < ''U<sub>n</sub>(t)'' '''and''' ''R(t-1)'' >= ''U<sub>n</sub>(t-1)'' '''and''' ''R(t)'' > ''U<sub>d</sub>(t)''<br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' > ''L<sub>n</sub>(t)'' '''and''' ''R(t-1)'' <= ''L<sub>n</sub>(t-1)'' '''and''' ''R(t)'' < ''L<sub>d</sub>(t)''<br />
<br />
Why do we have the '''simple entry mode'''? In normal situations and backtests, it gives same results as the '''uptick mode'''. But the difference comes up while trading multiple pairs in portfolio. The simple mode allows you to jump in the position immediately after a new slot is freed, regardless of the previous Z-scores.<br />
<br />
'''Which entry mode is better?''' Hard to tell, sometimes the uptick, sometimes the downtick. You have to do your homework and decide, which idea suits your trading style better. In general, uptick/simple mode is more aggressive, as it does not wait for first signs of spread mean reversion.<br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub> (equivalent to ''R(t)'' <= ''U<sub>x</sub>(t)'')<br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub> (equivalent to ''R(t)'' >= ''L<sub>x</sub>(t)'')<br />
<br />
=== Moving Average Types ===<br />
[[File:Rsi-illustration.png|right|650px|thumb|Ratio RSI Trading Example]]<br />
You can choose from these moving average algorithms:<br />
* [http://www.investopedia.com/terms/s/sma.asp Simple Moving Average] (SMA)<br />
* [http://www.investopedia.com/terms/e/ema.asp Exponential Moving Average] (EMA)<br />
* [http://fxtrade.oanda.com/learn/forex-indicators/weighted-moving-average Weighted Moving Average] (WMA)<br />
* [http://www.investopedia.com/terms/d/double-exponential-moving-average.asp Double Exponential Moving Average] (DEMA)<br />
* [http://www.investopedia.com/terms/t/triple-exponential-moving-average.asp Triple Exponential Moving Average] (TEMA)<br />
* [http://daytrading.about.com/od/indicators/a/Triangular.htm Triangular Moving Average] (TMA)<br />
* [http://etfhq.com/blog/2011/11/07/adaptive-moving-average-ama-aka-kaufman-adaptive-moving-average-kama Kaufman Adaptive Moving Average] (KAMA)<br />
* [http://www.mesasoftware.com/Papers/MAMA.pdf MESA Adaptive Moving Average] (MAMA)<br />
* [http://www.tradeescort.com/AgenaIndicatorHelp/AgenaIndicator.html?T3TripleExponentialMovingAverage.html Triple Exponential T3 Moving Average]<br />
<br />
Which one is the best? It depends, you have to test for yourself. They mostly differ in the term of memory and how fast they react to changes. Industry standard and the default is EMA. We suggest to try all of them on some sample pair to see how they work.<br />
<br />
=== RSI Filtering ===<br />
You can combine the Z-score/Bollinger Band model with [http://www.investopedia.com/terms/r/rsi.asp RSI indicator] applied on the ratio ''R(t)''. RSI filter will be automatically enabled if you set RSI threshold to other value than zero. RSI filter is combined with the Z-score rules using '''AND''' operator ('''both''' entry rule and RSI entry rule must be true to open a position).<br />
<br />
You can also change the RSI period if you want (default period = 15).<br />
<br />
RSI Threshold is value between 0 and 50. Because RSI indicator value oscillates between 0 and 100 (where 50 = mean), your threshold value is just used to set real thresholds for RSI:<br />
* let's assume RSI Period P<sub>r</sub> was entered<br />
* effective RSI value threshold is then 50+P<sub>r</sub> (upper) and 50-P<sub>r</sub> (lower)<br />
* you can see both thresholds in the example image at the right side<br />
<br />
Example:<br />
* RSI Threshold entered is 15<br />
* then, short positions are only opened, if RSI >= 65 (50+15)<br />
* long positions are only opened, if RSI <=35 (50-15)<br />
<br />
Useful hint: if you want to control entry rules just by RSI, you can set Entry Mode to simple and Entry Threshold to some low value.<br />
<br />
== Residual Model ==<br />
[[File:Residual-illustration2.png|right|650px|thumb|Residual Downtick Example]]<br />
Residual mode is based on [http://en.wikipedia.org/wiki/Linear_regression linear regression]. In literature it has been also referred to as the '''cointegration approach'''. Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the OLS method (Ordinary Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
This particular implementation is very simple. The regression is constructed using floating window of a fixed period, the same period is used for calculating the standard deviation.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (uptick and downtick modes are supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <15, 300><br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* first we need to construct a linear regression between ''A(t)'', ''B(t)'' using OLS, where ''A(t)'' = ''&beta;'' * ''B(t)'' + ''&alpha;'' + ''R(t)''<br />
* because we use floating window of period P (we calculate new regression each day), we actually get new series ''&beta;(t)'', ''&alpha;(t)'', ''R(t)'', where ''&beta;(t)'', ''&alpha;(t)'' are series of regression coefficients and ''R(t)'' are residuals (prediction errors)<br />
* ''R(t)'' = ''A(t)'' - (''&beta;(t)'' * ''B(t)'' + ''&alpha;(t)'')<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly (in reality we have two Z-scores: Z-score<sub>ask</sub> and Z-score<sub>bid</sub> as they are calculated using different prices, but for the sake of simplicity let's now pretend we don't pay bid-ask spread and we have just one Z-score)<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside (while we must stay above the downtick band):<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>d</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>d</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman Model ==<br />
This model is based on [http://en.wikipedia.org/wiki/Kalman_filter Kalman Filter]. In this case, the filter is used here instead of linear regression to determine proper hedge ratio, deviation from the mean and the standard deviation of the spread. The advantage is that this filter is superior when dealing with noise compared to OLS or TLS methods, also it does not have any lookback period to optimize.<br />
The disadvantage is that is has another parameters (like &delta;) to find out.<br />
<br />
We strongly recommend to read more details about Kalman filter applications in pair trading in [http://www.amazon.com/Algorithmic-Trading-Winning-Strategies-Rationale/dp/1118460146 this book].<br />
<br />
While Kalman Filter also estimates the standard deviation on the fly, our implementation also supports a possibility of having an auxiliary standard deviation indicator applied directly on the forecast error time series. The advantage is that you get less sensitivity on the &delta; parameter, but on the other hand a new lookback parameter is introduced.<br />
<br />
'''Kalman Model has too sensitive parameters which prevents it to be traded in reality. This model is provided only for education purposes - to be able to see how parameters affects its performance. Use Kalman Grid model for any serious trading.'''<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not planned'''<br />
* in PTL Trader: '''not planned'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter transition covariance &delta;, typical value is 0.0001 - '''unfortunately this model is very sensitive to this parameter especially when using standard deviation estimate coming from the Kalman filter itself'''<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* auxiliary standard deviation period - if equal to zero, Kalman filter is used to estimate standard deviation, if non-zero, auxiliary standard deviation indicator with this period is used (an the estimate from Kalman filter is ignored)<br />
* unstable period = how many Kalman filter observations are ignored at the beginning (first observations are quite unstable)<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >10 if used)<br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model.<br />
<br />
'''This model is provided for education purposes only.'''<br />
<br />
== Kalman-grid Model (v2) ==<br />
<br />
After one year of experience with the [[Pair Trading Models#Kalman-grid Model (v1)|Kalman-grid Model (v1)]] and researching [https://en.wikipedia.org/wiki/Machine_learning machine learning principles], we have spent weeks of additional work on developing new version of Kalman Grid model, redesigned from scratch. Kalman Grid v2 is now the most advanced model PTL offers.<br />
<br />
This model brings the superior performance of Kalman filter design, but it also deals with all the flaws of Kalman and old Kalman Grid v1:<br />
* all parameters of Kalman filter have been eliminated (&delta;, V<sub>e</sub>) - they are auto-estimated by the grid logic and updated with each price sample<br />
* increased average performance compared to v1<br />
* fixed issues of old version which prevented model to trade too often (biggest flaw of v1)<br />
<br />
This model rendered the version 1 obsolete and completely replaces it.<br />
<br />
How does it work? Instead of single strategy (and single Kalman filter), the whole grid of Kalman filters is evaluated by a proprietary system at the same time. Then, machine learning principles are applied to rank particular Kalman filter performances. Another algorithm is then used to figure filter outputs (slope, intercept, standard deviation) from the whole grid. The whole system actually behaves like a single Kalman filter, but with no need to provide any parameters.<br />
<br />
Main advantages of this model:<br />
* it squeezes all profit from the pair<br />
* beats any other models in performance (in average) and also in performance per day-in-position<br />
* no risk of over-fitting in parameter optimization (nothing to optimize, except thresholds)<br />
<br />
Issues of this model:<br />
* you still need to set entry/exit thresholds - basically this affect number of trades and slot usage...exit threshold is usually 0, try entry threshold from range <2,4><br />
* the simple A.I. inside is profit-based - so not-so-good pairs with waves in profit can "fool" the algorithm so it falls from the optimum<br />
* model performance is proportional to margin usage<br />
<br />
Specialties:<br />
* if you set entry threshold to 1, exit threshold to -1, model to "simple" and if you allow reversals, the model will be "always-in-position" squeezing the most profit from good pairs<br />
<br />
To give some insight on performance: we took 300 best performing pairs from PTL database in period Jan 2013 - Jan 2016. Then we backtested all 300 pairs using '''out-of-sample''' period (Jan 2016 - Sep 2016) and we compared their performance. Models used: Ratio(per 14), Residual(20), Kalman Grid v1, Kalman Grid v2 using exit threshold of -1 (normal) and 0 (aggressive). Margin 50% (Req-T). Results here:<br />
{|<br />
|'''Model'''<br />
|'''Median CAGR %'''<br />
|'''Mean CAGR %'''<br />
|-<br />
|Kalman-grid v2 (normal)<br />
|'''7.206409'''<br />
|9.538509<br />
|-<br />
|Kalman-grid v2 (aggressive)<br />
|7.088950<br />
|'''11.433199'''<br />
|-<br />
|Kalman-grid v1<br />
|4.877391<br />
|10.794117<br />
|-<br />
|Residual(20)<br />
|3.596212<br />
|6.631089<br />
|-<br />
|Ratio(14)<br />
|3.590883<br />
|8.954484 <br />
|}<br />
<br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1, 4><br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-1, 00>...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band (not used often)<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >10 if used)<br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model.<br />
<br />
== Kalman-Auto Model ==<br />
'''This section is under construction'''<br />
<br />
This model is similar to Kalman-grid model, but it is more simple. This model does not optimize profit, but instead it optimizes margin usage to match predefined target. This model is very robust (is not fooled by glitches in performance) and it automatically optimizes entry rules (thresholds).<br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''planned Dec 2016'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman-Auto Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* Usage Threshold in %, allowed range <20, 80><br />
<br />
= Deprecated Models =<br />
* Orthogonal Model<br />
* Kalman-grid Model (v1)</div>Adminhttps://wiki.pairtradinglab.com/wiki/Pair_Trading_ModelsPair Trading Models2016-11-08T09:47:16Z<p>Admin: /* Kalman Model */</p>
<hr />
<div>Pair Trading Lab offers pair trading algorithms based on various mathematical models. These are models currently supported in PTL:<br />
== Ratio Model ==<br />
[[File:Ratio-illustration.png|right|650px|thumb|Ratio Uptick Example]]<br />
This is one of the standard pair trading models described in literature. It is based in ratio of instrument prices, [http://en.wikipedia.org/wiki/Moving_average moving average] and [http://en.wikipedia.org/wiki/Standard_deviation standard deviation]. In other words, it is based on [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator.<br />
<br />
Since Nov 27th 2014, this model also supports additional [http://www.investopedia.com/terms/r/rsi.asp RSI filter] you can use in addition to Bollinger Bands method.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (RSI supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.5, 2.5>, 2.0 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values are allowed (to exit on other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical values is <0, 1>, 0 is used most often...only used for downtick entry mode as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* moving average period P<sub>m</sub> (typical range <10, 100>), default = 15<br />
* moving average type T (algorithm), default = exponential<br />
* standard deviation period P<sub>s</sub> (typical range <10, 100>), default = 15<br />
* entry mode (simple, uptick, downtick)<br />
* RSI period and threshold (optional RSI filtering)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* we need to calculate ratio time series ''R(t)'' = ''A(t)'' / ''B(t)''<br />
* let's apply moving average of type T with period P<sub>m</sub> on ''R(t)'' to get time series ''M(t)''<br />
* let's apply standard deviation with period P<sub>s</sub> on ''R(t)'' to get time series ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = (''R(t)'' - ''M(t)'') / ''S(t)'', this time series can give us z-score to signal trading decision directly (in reality we have two Z-scores: Z-score<sub>ask</sub> and Z-score<sub>bid</sub> as they are calculated using different prices, but for the sake of simplicity let's now pretend we don't pay bid-ask spread and we have just one Z-score)<br />
* another common approach (to visualize) is to create bands and put it above the moving average ''M(t)'':<br />
** upper entry band ''U<sub>n</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>n</sub><br />
** lower entry band ''L<sub>n</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>n</sub><br />
** upper exit band ''U<sub>x</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>x</sub><br />
** lower exit band ''L<sub>x</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>x</sub><br />
** upper downtick band ''U<sub>d</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** lower downtick band ''L<sub>d</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** these bands are actually the same bands as in [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator and we can use crossing of ''R(t)'' and bands as trade signals<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'')<br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'')<br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' < E<sub>n</sub> (same as ''R(t-1)'' < ''U<sub>n</sub>(t-1)'')<br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' > -E<sub>n</sub> (same as ''R(t-1)'' > ''L<sub>n</sub>(t-1)'')<br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside (but it must stay above the downtick band):<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' < ''U<sub>n</sub>(t)'' '''and''' ''R(t-1)'' >= ''U<sub>n</sub>(t-1)'' '''and''' ''R(t)'' > ''U<sub>d</sub>(t)''<br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' > ''L<sub>n</sub>(t)'' '''and''' ''R(t-1)'' <= ''L<sub>n</sub>(t-1)'' '''and''' ''R(t)'' < ''L<sub>d</sub>(t)''<br />
<br />
Why do we have the '''simple entry mode'''? In normal situations and backtests, it gives same results as the '''uptick mode'''. But the difference comes up while trading multiple pairs in portfolio. The simple mode allows you to jump in the position immediately after a new slot is freed, regardless of the previous Z-scores.<br />
<br />
'''Which entry mode is better?''' Hard to tell, sometimes the uptick, sometimes the downtick. You have to do your homework and decide, which idea suits your trading style better. In general, uptick/simple mode is more aggressive, as it does not wait for first signs of spread mean reversion.<br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub> (equivalent to ''R(t)'' <= ''U<sub>x</sub>(t)'')<br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub> (equivalent to ''R(t)'' >= ''L<sub>x</sub>(t)'')<br />
<br />
=== Moving Average Types ===<br />
[[File:Rsi-illustration.png|right|650px|thumb|Ratio RSI Trading Example]]<br />
You can choose from these moving average algorithms:<br />
* [http://www.investopedia.com/terms/s/sma.asp Simple Moving Average] (SMA)<br />
* [http://www.investopedia.com/terms/e/ema.asp Exponential Moving Average] (EMA)<br />
* [http://fxtrade.oanda.com/learn/forex-indicators/weighted-moving-average Weighted Moving Average] (WMA)<br />
* [http://www.investopedia.com/terms/d/double-exponential-moving-average.asp Double Exponential Moving Average] (DEMA)<br />
* [http://www.investopedia.com/terms/t/triple-exponential-moving-average.asp Triple Exponential Moving Average] (TEMA)<br />
* [http://daytrading.about.com/od/indicators/a/Triangular.htm Triangular Moving Average] (TMA)<br />
* [http://etfhq.com/blog/2011/11/07/adaptive-moving-average-ama-aka-kaufman-adaptive-moving-average-kama Kaufman Adaptive Moving Average] (KAMA)<br />
* [http://www.mesasoftware.com/Papers/MAMA.pdf MESA Adaptive Moving Average] (MAMA)<br />
* [http://www.tradeescort.com/AgenaIndicatorHelp/AgenaIndicator.html?T3TripleExponentialMovingAverage.html Triple Exponential T3 Moving Average]<br />
<br />
Which one is the best? It depends, you have to test for yourself. They mostly differ in the term of memory and how fast they react to changes. Industry standard and the default is EMA. We suggest to try all of them on some sample pair to see how they work.<br />
<br />
=== RSI Filtering ===<br />
You can combine the Z-score/Bollinger Band model with [http://www.investopedia.com/terms/r/rsi.asp RSI indicator] applied on the ratio ''R(t)''. RSI filter will be automatically enabled if you set RSI threshold to other value than zero. RSI filter is combined with the Z-score rules using '''AND''' operator ('''both''' entry rule and RSI entry rule must be true to open a position).<br />
<br />
You can also change the RSI period if you want (default period = 15).<br />
<br />
RSI Threshold is value between 0 and 50. Because RSI indicator value oscillates between 0 and 100 (where 50 = mean), your threshold value is just used to set real thresholds for RSI:<br />
* let's assume RSI Period P<sub>r</sub> was entered<br />
* effective RSI value threshold is then 50+P<sub>r</sub> (upper) and 50-P<sub>r</sub> (lower)<br />
* you can see both thresholds in the example image at the right side<br />
<br />
Example:<br />
* RSI Threshold entered is 15<br />
* then, short positions are only opened, if RSI >= 65 (50+15)<br />
* long positions are only opened, if RSI <=35 (50-15)<br />
<br />
Useful hint: if you want to control entry rules just by RSI, you can set Entry Mode to simple and Entry Threshold to some low value.<br />
<br />
== Residual Model ==<br />
[[File:Residual-illustration2.png|right|650px|thumb|Residual Downtick Example]]<br />
Residual mode is based on [http://en.wikipedia.org/wiki/Linear_regression linear regression]. In literature it has been also referred to as the '''cointegration approach'''. Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the OLS method (Ordinary Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
This particular implementation is very simple. The regression is constructed using floating window of a fixed period, the same period is used for calculating the standard deviation.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (uptick and downtick modes are supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <15, 300><br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* first we need to construct a linear regression between ''A(t)'', ''B(t)'' using OLS, where ''A(t)'' = ''&beta;'' * ''B(t)'' + ''&alpha;'' + ''R(t)''<br />
* because we use floating window of period P (we calculate new regression each day), we actually get new series ''&beta;(t)'', ''&alpha;(t)'', ''R(t)'', where ''&beta;(t)'', ''&alpha;(t)'' are series of regression coefficients and ''R(t)'' are residuals (prediction errors)<br />
* ''R(t)'' = ''A(t)'' - (''&beta;(t)'' * ''B(t)'' + ''&alpha;(t)'')<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly (in reality we have two Z-scores: Z-score<sub>ask</sub> and Z-score<sub>bid</sub> as they are calculated using different prices, but for the sake of simplicity let's now pretend we don't pay bid-ask spread and we have just one Z-score)<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside (while we must stay above the downtick band):<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>d</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>d</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman Model ==<br />
This model is based on [http://en.wikipedia.org/wiki/Kalman_filter Kalman Filter]. In this case, the filter is used here instead of linear regression to determine proper hedge ratio, deviation from the mean and the standard deviation of the spread. The advantage is that this filter is superior when dealing with noise compared to OLS or TLS methods, also it does not have any lookback period to optimize.<br />
The disadvantage is that is has another parameters (like &delta;) to find out.<br />
<br />
We strongly recommend to read more details about Kalman filter applications in pair trading in [http://www.amazon.com/Algorithmic-Trading-Winning-Strategies-Rationale/dp/1118460146 this book].<br />
<br />
While Kalman Filter also estimates the standard deviation on the fly, our implementation also supports a possibility of having an auxiliary standard deviation indicator applied directly on the forecast error time series. The advantage is that you get less sensitivity on the &delta; parameter, but on the other hand a new lookback parameter is introduced.<br />
<br />
'''Kalman Model has too sensitive parameters which prevents it to be traded in reality. This model is provided only for education purposes - to be able to see how parameters affects its performance. Use Kalman Grid model for any serious trading.'''<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not planned'''<br />
* in PTL Trader: '''not planned'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter transition covariance &delta;, typical value is 0.0001 - '''unfortunately this model is very sensitive to this parameter especially when using standard deviation estimate coming from the Kalman filter itself'''<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* auxiliary standard deviation period - if equal to zero, Kalman filter is used to estimate standard deviation, if non-zero, auxiliary standard deviation indicator with this period is used (an the estimate from Kalman filter is ignored)<br />
* unstable period = how many Kalman filter observations are ignored at the beginning (first observations are quite unstable)<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >10 if used)<br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Entering & Exiting Positions ===<br />
Same as in Residual Model.<br />
<br />
'''This model is provided for education purposes only.'''<br />
<br />
== Kalman-grid Model (v2) ==<br />
'''This has been has been updated 2016-11-08. Please wait for the documentation update.'''<br />
<br />
After one year of experience with the [[Pair Trading Models#Kalman-grid Model (v1)|Kalman-grid Model (v1)]] and researching [https://en.wikipedia.org/wiki/Machine_learning machine learning principles], we have spent weeks of additional work on developing new version of Kalman Grid model, redesigned from scratch. Kalman Grid v2 is now the most advanced model PTL offers and has no competition whatsoever. After proven as stable it will become the default model for the whole PTL ecosystem.<br />
<br />
This model brings the superior performance of Kalman filter design, but it also deals with all the flaws of Kalman and old Kalman Grid v1:<br />
* all parameters of Kalman filter have been eliminated (&delta;, V<sub>e</sub>) - they are auto-estimated by the grid logic and updated with each price sample<br />
* increased average performance compared to v1<br />
* fixed issues of old version which prevented model to trade too often (biggest flaw of v1)<br />
<br />
This model rendered the version 1 obsolete and completely replaces it.<br />
<br />
How does it work? Instead of single strategy (and single Kalman filter), the whole grid of Kalman filters is evaluated by a proprietary system at the same time. Then, machine learning principles are applied to rank particular Kalman filter performances. Another algorithm is then used to figure filter outputs (slope, intercept, standard deviation) from the whole grid. The whole system actually behaves like a single Kalman filter, but with no need to provide any parameters.<br />
<br />
As a side effect, the entry threshold is also auto-optimized by the grid system itself. '''This means that the entry threshold is always indicated as 1.0.'''<br />
<br />
Main advantages of this model:<br />
* it is robust (because there are no parameters to tune)<br />
* beats any other models in performance (in average)<br />
* no risk of over-fitting in parameter optimization (nothing to optimize)<br />
<br />
To give some insight on performance: we took 300 best performing pairs from PTL database in period Jan 2013 - Jan 2016. Then we backtested all 300 pairs using '''out-of-sample''' period (Jan 2016 - Sep 2016) and we compared their performance. Models used: Ratio(per 14), Residual(20), Kalman Grid v1, Kalman Grid v2 using exit threshold of -1 (normal) and 0 (aggressive). Margin 50% (Req-T). Results here:<br />
{|<br />
|'''Model'''<br />
|'''Median CAGR %'''<br />
|'''Mean CAGR %'''<br />
|-<br />
|Kalman-grid v2 (normal)<br />
|'''7.206409'''<br />
|9.538509<br />
|-<br />
|Kalman-grid v2 (aggressive)<br />
|7.088950<br />
|'''11.433199'''<br />
|-<br />
|Kalman-grid v1<br />
|4.877391<br />
|10.794117<br />
|-<br />
|Residual(20)<br />
|3.596212<br />
|6.631089<br />
|-<br />
|Ratio(14)<br />
|3.590883<br />
|8.954484 <br />
|}<br />
<br />
'''You can clearly see how this model beats other models in out-of-sample performance (mixture of good and bad pairs).'''<br />
<br />
Profit distribution over all models (300 pairs, out-of-sample):<br />
[[File:Rplot1.png|center]]<br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
== Kalman-Auto Model ==<br />
'''This section is under construction'''<br />
<br />
This model is similar to Kalman-grid model, but it is more simple. This model does not optimize profit, but instead it optimizes margin usage to match predefined target. This model is very robust (is not fooled by glitches in performance) and it automatically optimizes entry rules (thresholds).<br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''planned Dec 2016'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman-Auto Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* Usage Threshold in %, allowed range <20, 80><br />
<br />
= Deprecated Models =<br />
* Orthogonal Model<br />
* Kalman-grid Model (v1)</div>Adminhttps://wiki.pairtradinglab.com/wiki/Pair_Trading_ModelsPair Trading Models2016-11-08T09:44:11Z<p>Admin: /* Residual Model */</p>
<hr />
<div>Pair Trading Lab offers pair trading algorithms based on various mathematical models. These are models currently supported in PTL:<br />
== Ratio Model ==<br />
[[File:Ratio-illustration.png|right|650px|thumb|Ratio Uptick Example]]<br />
This is one of the standard pair trading models described in literature. It is based in ratio of instrument prices, [http://en.wikipedia.org/wiki/Moving_average moving average] and [http://en.wikipedia.org/wiki/Standard_deviation standard deviation]. In other words, it is based on [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator.<br />
<br />
Since Nov 27th 2014, this model also supports additional [http://www.investopedia.com/terms/r/rsi.asp RSI filter] you can use in addition to Bollinger Bands method.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (RSI supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.5, 2.5>, 2.0 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values are allowed (to exit on other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical values is <0, 1>, 0 is used most often...only used for downtick entry mode as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* moving average period P<sub>m</sub> (typical range <10, 100>), default = 15<br />
* moving average type T (algorithm), default = exponential<br />
* standard deviation period P<sub>s</sub> (typical range <10, 100>), default = 15<br />
* entry mode (simple, uptick, downtick)<br />
* RSI period and threshold (optional RSI filtering)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* we need to calculate ratio time series ''R(t)'' = ''A(t)'' / ''B(t)''<br />
* let's apply moving average of type T with period P<sub>m</sub> on ''R(t)'' to get time series ''M(t)''<br />
* let's apply standard deviation with period P<sub>s</sub> on ''R(t)'' to get time series ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = (''R(t)'' - ''M(t)'') / ''S(t)'', this time series can give us z-score to signal trading decision directly (in reality we have two Z-scores: Z-score<sub>ask</sub> and Z-score<sub>bid</sub> as they are calculated using different prices, but for the sake of simplicity let's now pretend we don't pay bid-ask spread and we have just one Z-score)<br />
* another common approach (to visualize) is to create bands and put it above the moving average ''M(t)'':<br />
** upper entry band ''U<sub>n</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>n</sub><br />
** lower entry band ''L<sub>n</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>n</sub><br />
** upper exit band ''U<sub>x</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>x</sub><br />
** lower exit band ''L<sub>x</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>x</sub><br />
** upper downtick band ''U<sub>d</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** lower downtick band ''L<sub>d</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** these bands are actually the same bands as in [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator and we can use crossing of ''R(t)'' and bands as trade signals<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'')<br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'')<br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' < E<sub>n</sub> (same as ''R(t-1)'' < ''U<sub>n</sub>(t-1)'')<br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' > -E<sub>n</sub> (same as ''R(t-1)'' > ''L<sub>n</sub>(t-1)'')<br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside (but it must stay above the downtick band):<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' < ''U<sub>n</sub>(t)'' '''and''' ''R(t-1)'' >= ''U<sub>n</sub>(t-1)'' '''and''' ''R(t)'' > ''U<sub>d</sub>(t)''<br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' > ''L<sub>n</sub>(t)'' '''and''' ''R(t-1)'' <= ''L<sub>n</sub>(t-1)'' '''and''' ''R(t)'' < ''L<sub>d</sub>(t)''<br />
<br />
Why do we have the '''simple entry mode'''? In normal situations and backtests, it gives same results as the '''uptick mode'''. But the difference comes up while trading multiple pairs in portfolio. The simple mode allows you to jump in the position immediately after a new slot is freed, regardless of the previous Z-scores.<br />
<br />
'''Which entry mode is better?''' Hard to tell, sometimes the uptick, sometimes the downtick. You have to do your homework and decide, which idea suits your trading style better. In general, uptick/simple mode is more aggressive, as it does not wait for first signs of spread mean reversion.<br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub> (equivalent to ''R(t)'' <= ''U<sub>x</sub>(t)'')<br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub> (equivalent to ''R(t)'' >= ''L<sub>x</sub>(t)'')<br />
<br />
=== Moving Average Types ===<br />
[[File:Rsi-illustration.png|right|650px|thumb|Ratio RSI Trading Example]]<br />
You can choose from these moving average algorithms:<br />
* [http://www.investopedia.com/terms/s/sma.asp Simple Moving Average] (SMA)<br />
* [http://www.investopedia.com/terms/e/ema.asp Exponential Moving Average] (EMA)<br />
* [http://fxtrade.oanda.com/learn/forex-indicators/weighted-moving-average Weighted Moving Average] (WMA)<br />
* [http://www.investopedia.com/terms/d/double-exponential-moving-average.asp Double Exponential Moving Average] (DEMA)<br />
* [http://www.investopedia.com/terms/t/triple-exponential-moving-average.asp Triple Exponential Moving Average] (TEMA)<br />
* [http://daytrading.about.com/od/indicators/a/Triangular.htm Triangular Moving Average] (TMA)<br />
* [http://etfhq.com/blog/2011/11/07/adaptive-moving-average-ama-aka-kaufman-adaptive-moving-average-kama Kaufman Adaptive Moving Average] (KAMA)<br />
* [http://www.mesasoftware.com/Papers/MAMA.pdf MESA Adaptive Moving Average] (MAMA)<br />
* [http://www.tradeescort.com/AgenaIndicatorHelp/AgenaIndicator.html?T3TripleExponentialMovingAverage.html Triple Exponential T3 Moving Average]<br />
<br />
Which one is the best? It depends, you have to test for yourself. They mostly differ in the term of memory and how fast they react to changes. Industry standard and the default is EMA. We suggest to try all of them on some sample pair to see how they work.<br />
<br />
=== RSI Filtering ===<br />
You can combine the Z-score/Bollinger Band model with [http://www.investopedia.com/terms/r/rsi.asp RSI indicator] applied on the ratio ''R(t)''. RSI filter will be automatically enabled if you set RSI threshold to other value than zero. RSI filter is combined with the Z-score rules using '''AND''' operator ('''both''' entry rule and RSI entry rule must be true to open a position).<br />
<br />
You can also change the RSI period if you want (default period = 15).<br />
<br />
RSI Threshold is value between 0 and 50. Because RSI indicator value oscillates between 0 and 100 (where 50 = mean), your threshold value is just used to set real thresholds for RSI:<br />
* let's assume RSI Period P<sub>r</sub> was entered<br />
* effective RSI value threshold is then 50+P<sub>r</sub> (upper) and 50-P<sub>r</sub> (lower)<br />
* you can see both thresholds in the example image at the right side<br />
<br />
Example:<br />
* RSI Threshold entered is 15<br />
* then, short positions are only opened, if RSI >= 65 (50+15)<br />
* long positions are only opened, if RSI <=35 (50-15)<br />
<br />
Useful hint: if you want to control entry rules just by RSI, you can set Entry Mode to simple and Entry Threshold to some low value.<br />
<br />
== Residual Model ==<br />
[[File:Residual-illustration2.png|right|650px|thumb|Residual Downtick Example]]<br />
Residual mode is based on [http://en.wikipedia.org/wiki/Linear_regression linear regression]. In literature it has been also referred to as the '''cointegration approach'''. Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the OLS method (Ordinary Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
This particular implementation is very simple. The regression is constructed using floating window of a fixed period, the same period is used for calculating the standard deviation.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (uptick and downtick modes are supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values allowed (allows to exit at other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical value is <0, 1>...used for downtick mode only as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <15, 300><br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* first we need to construct a linear regression between ''A(t)'', ''B(t)'' using OLS, where ''A(t)'' = ''&beta;'' * ''B(t)'' + ''&alpha;'' + ''R(t)''<br />
* because we use floating window of period P (we calculate new regression each day), we actually get new series ''&beta;(t)'', ''&alpha;(t)'', ''R(t)'', where ''&beta;(t)'', ''&alpha;(t)'' are series of regression coefficients and ''R(t)'' are residuals (prediction errors)<br />
* ''R(t)'' = ''A(t)'' - (''&beta;(t)'' * ''B(t)'' + ''&alpha;(t)'')<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly (in reality we have two Z-scores: Z-score<sub>ask</sub> and Z-score<sub>bid</sub> as they are calculated using different prices, but for the sake of simplicity let's now pretend we don't pay bid-ask spread and we have just one Z-score)<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside (while we must stay above the downtick band):<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>d</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>d</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman Model ==<br />
This model is based on [http://en.wikipedia.org/wiki/Kalman_filter Kalman Filter]. In this case, the filter is used here instead of linear regression to determine proper hedge ratio, deviation from the mean and the standard deviation of the spread. The advantage is that this filter is superior when dealing with noise compared to OLS or TLS methods, also it does not have any lookback period to optimize.<br />
The disadvantage is that is has another parameters (like &delta;) to find out.<br />
<br />
We strongly recommend to read more details about Kalman filter applications in pair trading in [http://www.amazon.com/Algorithmic-Trading-Winning-Strategies-Rationale/dp/1118460146 this book].<br />
<br />
While Kalman Filter also estimates the standard deviation on the fly, our implementation also supports a possibility of having an auxiliary standard deviation indicator applied directly on the forecast error time series. The advantage is that you get less sensitivity on the &delta; parameter, but on the other hand a new lookback parameter is introduced.<br />
<br />
'''Kalman Model has too sensitive parameters which prevents it to be traded in reality. This model is provided only for education purposes - to be able to see how parameters affects its performance. Use Kalman Grid model for any serious trading.'''<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not planned'''<br />
* in PTL Trader: '''not planned'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter transition covariance &delta;, typical value is 0.0001 - '''unfortunately this model is very sensitive to this parameter especially when using standard deviation estimate coming from the Kalman filter itself'''<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* Auxiliary standard deviation period - if equal to zero, Kalman filter is used to estimate standard deviation, if non-zero, auxiliary standard deviation indicator with this period is used (an the estimate from Kalman filter is ignored)<br />
* Unstable period = how many Kalman filter observations are ignored at the beginning (first observations are quite unstable)<br />
<br />
'''This model is provided for education purposes only.'''<br />
<br />
== Kalman-grid Model (v2) ==<br />
'''This has been has been updated 2016-11-08. Please wait for the documentation update.'''<br />
<br />
After one year of experience with the [[Pair Trading Models#Kalman-grid Model (v1)|Kalman-grid Model (v1)]] and researching [https://en.wikipedia.org/wiki/Machine_learning machine learning principles], we have spent weeks of additional work on developing new version of Kalman Grid model, redesigned from scratch. Kalman Grid v2 is now the most advanced model PTL offers and has no competition whatsoever. After proven as stable it will become the default model for the whole PTL ecosystem.<br />
<br />
This model brings the superior performance of Kalman filter design, but it also deals with all the flaws of Kalman and old Kalman Grid v1:<br />
* all parameters of Kalman filter have been eliminated (&delta;, V<sub>e</sub>) - they are auto-estimated by the grid logic and updated with each price sample<br />
* increased average performance compared to v1<br />
* fixed issues of old version which prevented model to trade too often (biggest flaw of v1)<br />
<br />
This model rendered the version 1 obsolete and completely replaces it.<br />
<br />
How does it work? Instead of single strategy (and single Kalman filter), the whole grid of Kalman filters is evaluated by a proprietary system at the same time. Then, machine learning principles are applied to rank particular Kalman filter performances. Another algorithm is then used to figure filter outputs (slope, intercept, standard deviation) from the whole grid. The whole system actually behaves like a single Kalman filter, but with no need to provide any parameters.<br />
<br />
As a side effect, the entry threshold is also auto-optimized by the grid system itself. '''This means that the entry threshold is always indicated as 1.0.'''<br />
<br />
Main advantages of this model:<br />
* it is robust (because there are no parameters to tune)<br />
* beats any other models in performance (in average)<br />
* no risk of over-fitting in parameter optimization (nothing to optimize)<br />
<br />
To give some insight on performance: we took 300 best performing pairs from PTL database in period Jan 2013 - Jan 2016. Then we backtested all 300 pairs using '''out-of-sample''' period (Jan 2016 - Sep 2016) and we compared their performance. Models used: Ratio(per 14), Residual(20), Kalman Grid v1, Kalman Grid v2 using exit threshold of -1 (normal) and 0 (aggressive). Margin 50% (Req-T). Results here:<br />
{|<br />
|'''Model'''<br />
|'''Median CAGR %'''<br />
|'''Mean CAGR %'''<br />
|-<br />
|Kalman-grid v2 (normal)<br />
|'''7.206409'''<br />
|9.538509<br />
|-<br />
|Kalman-grid v2 (aggressive)<br />
|7.088950<br />
|'''11.433199'''<br />
|-<br />
|Kalman-grid v1<br />
|4.877391<br />
|10.794117<br />
|-<br />
|Residual(20)<br />
|3.596212<br />
|6.631089<br />
|-<br />
|Ratio(14)<br />
|3.590883<br />
|8.954484 <br />
|}<br />
<br />
'''You can clearly see how this model beats other models in out-of-sample performance (mixture of good and bad pairs).'''<br />
<br />
Profit distribution over all models (300 pairs, out-of-sample):<br />
[[File:Rplot1.png|center]]<br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
== Kalman-Auto Model ==<br />
'''This section is under construction'''<br />
<br />
This model is similar to Kalman-grid model, but it is more simple. This model does not optimize profit, but instead it optimizes margin usage to match predefined target. This model is very robust (is not fooled by glitches in performance) and it automatically optimizes entry rules (thresholds).<br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''planned Dec 2016'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman-Auto Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* Usage Threshold in %, allowed range <20, 80><br />
<br />
= Deprecated Models =<br />
* Orthogonal Model<br />
* Kalman-grid Model (v1)</div>Adminhttps://wiki.pairtradinglab.com/wiki/Pair_Trading_ModelsPair Trading Models2016-11-08T09:38:22Z<p>Admin: /* Ratio Model */</p>
<hr />
<div>Pair Trading Lab offers pair trading algorithms based on various mathematical models. These are models currently supported in PTL:<br />
== Ratio Model ==<br />
[[File:Ratio-illustration.png|right|650px|thumb|Ratio Uptick Example]]<br />
This is one of the standard pair trading models described in literature. It is based in ratio of instrument prices, [http://en.wikipedia.org/wiki/Moving_average moving average] and [http://en.wikipedia.org/wiki/Standard_deviation standard deviation]. In other words, it is based on [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator.<br />
<br />
Since Nov 27th 2014, this model also supports additional [http://www.investopedia.com/terms/r/rsi.asp RSI filter] you can use in addition to Bollinger Bands method.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (RSI supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.5, 2.5>, 2.0 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...negative values are allowed (to exit on other side)<br />
* downtick threshold E<sub>d</sub> for Z-score, typical values is <0, 1>, 0 is used most often...only used for downtick entry mode as a third band<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* moving average period P<sub>m</sub> (typical range <10, 100>), default = 15<br />
* moving average type T (algorithm), default = exponential<br />
* standard deviation period P<sub>s</sub> (typical range <10, 100>), default = 15<br />
* entry mode (simple, uptick, downtick)<br />
* RSI period and threshold (optional RSI filtering)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* we need to calculate ratio time series ''R(t)'' = ''A(t)'' / ''B(t)''<br />
* let's apply moving average of type T with period P<sub>m</sub> on ''R(t)'' to get time series ''M(t)''<br />
* let's apply standard deviation with period P<sub>s</sub> on ''R(t)'' to get time series ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = (''R(t)'' - ''M(t)'') / ''S(t)'', this time series can give us z-score to signal trading decision directly (in reality we have two Z-scores: Z-score<sub>ask</sub> and Z-score<sub>bid</sub> as they are calculated using different prices, but for the sake of simplicity let's now pretend we don't pay bid-ask spread and we have just one Z-score)<br />
* another common approach (to visualize) is to create bands and put it above the moving average ''M(t)'':<br />
** upper entry band ''U<sub>n</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>n</sub><br />
** lower entry band ''L<sub>n</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>n</sub><br />
** upper exit band ''U<sub>x</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>x</sub><br />
** lower exit band ''L<sub>x</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>x</sub><br />
** upper downtick band ''U<sub>d</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** lower downtick band ''L<sub>d</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>d</sub> (applies to downtick entry mode only)<br />
** these bands are actually the same bands as in [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator and we can use crossing of ''R(t)'' and bands as trade signals<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'')<br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'')<br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' < E<sub>n</sub> (same as ''R(t-1)'' < ''U<sub>n</sub>(t-1)'')<br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' > -E<sub>n</sub> (same as ''R(t-1)'' > ''L<sub>n</sub>(t-1)'')<br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside (but it must stay above the downtick band):<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' < ''U<sub>n</sub>(t)'' '''and''' ''R(t-1)'' >= ''U<sub>n</sub>(t-1)'' '''and''' ''R(t)'' > ''U<sub>d</sub>(t)''<br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>d</sub><br />
** when using bands, it is the same as having ''R(t)'' > ''L<sub>n</sub>(t)'' '''and''' ''R(t-1)'' <= ''L<sub>n</sub>(t-1)'' '''and''' ''R(t)'' < ''L<sub>d</sub>(t)''<br />
<br />
Why do we have the '''simple entry mode'''? In normal situations and backtests, it gives same results as the '''uptick mode'''. But the difference comes up while trading multiple pairs in portfolio. The simple mode allows you to jump in the position immediately after a new slot is freed, regardless of the previous Z-scores.<br />
<br />
'''Which entry mode is better?''' Hard to tell, sometimes the uptick, sometimes the downtick. You have to do your homework and decide, which idea suits your trading style better. In general, uptick/simple mode is more aggressive, as it does not wait for first signs of spread mean reversion.<br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub> (equivalent to ''R(t)'' <= ''U<sub>x</sub>(t)'')<br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub> (equivalent to ''R(t)'' >= ''L<sub>x</sub>(t)'')<br />
<br />
=== Moving Average Types ===<br />
[[File:Rsi-illustration.png|right|650px|thumb|Ratio RSI Trading Example]]<br />
You can choose from these moving average algorithms:<br />
* [http://www.investopedia.com/terms/s/sma.asp Simple Moving Average] (SMA)<br />
* [http://www.investopedia.com/terms/e/ema.asp Exponential Moving Average] (EMA)<br />
* [http://fxtrade.oanda.com/learn/forex-indicators/weighted-moving-average Weighted Moving Average] (WMA)<br />
* [http://www.investopedia.com/terms/d/double-exponential-moving-average.asp Double Exponential Moving Average] (DEMA)<br />
* [http://www.investopedia.com/terms/t/triple-exponential-moving-average.asp Triple Exponential Moving Average] (TEMA)<br />
* [http://daytrading.about.com/od/indicators/a/Triangular.htm Triangular Moving Average] (TMA)<br />
* [http://etfhq.com/blog/2011/11/07/adaptive-moving-average-ama-aka-kaufman-adaptive-moving-average-kama Kaufman Adaptive Moving Average] (KAMA)<br />
* [http://www.mesasoftware.com/Papers/MAMA.pdf MESA Adaptive Moving Average] (MAMA)<br />
* [http://www.tradeescort.com/AgenaIndicatorHelp/AgenaIndicator.html?T3TripleExponentialMovingAverage.html Triple Exponential T3 Moving Average]<br />
<br />
Which one is the best? It depends, you have to test for yourself. They mostly differ in the term of memory and how fast they react to changes. Industry standard and the default is EMA. We suggest to try all of them on some sample pair to see how they work.<br />
<br />
=== RSI Filtering ===<br />
You can combine the Z-score/Bollinger Band model with [http://www.investopedia.com/terms/r/rsi.asp RSI indicator] applied on the ratio ''R(t)''. RSI filter will be automatically enabled if you set RSI threshold to other value than zero. RSI filter is combined with the Z-score rules using '''AND''' operator ('''both''' entry rule and RSI entry rule must be true to open a position).<br />
<br />
You can also change the RSI period if you want (default period = 15).<br />
<br />
RSI Threshold is value between 0 and 50. Because RSI indicator value oscillates between 0 and 100 (where 50 = mean), your threshold value is just used to set real thresholds for RSI:<br />
* let's assume RSI Period P<sub>r</sub> was entered<br />
* effective RSI value threshold is then 50+P<sub>r</sub> (upper) and 50-P<sub>r</sub> (lower)<br />
* you can see both thresholds in the example image at the right side<br />
<br />
Example:<br />
* RSI Threshold entered is 15<br />
* then, short positions are only opened, if RSI >= 65 (50+15)<br />
* long positions are only opened, if RSI <=35 (50-15)<br />
<br />
Useful hint: if you want to control entry rules just by RSI, you can set Entry Mode to simple and Entry Threshold to some low value.<br />
<br />
== Residual Model ==<br />
[[File:Residual-illustration2.png|right|650px|thumb|Residual Downtick Example]]<br />
Residual mode is based on [http://en.wikipedia.org/wiki/Linear_regression linear regression]. In literature it has been also referred to as the '''cointegration approach'''. Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the OLS method (Ordinary Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
This particular implementation is very simple. The regression is constructed using floating window of a fixed period, the same period is used for calculating the standard deviation.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (uptick and downtick modes are supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <15, 300><br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* first we need to construct a linear regression between ''A(t)'', ''B(t)'' using OLS, where ''A(t)'' = ''&beta;'' * ''B(t)'' + ''&alpha;'' + ''R(t)''<br />
* because we use floating window of period P (we calculate new regression each day), we actually get new series ''&beta;(t)'', ''&alpha;(t)'', ''R(t)'', where ''&beta;(t)'', ''&alpha;(t)'' are series of regression coefficients and ''R(t)'' are residuals (prediction errors)<br />
* ''R(t)'' = ''A(t)'' - (''&beta;(t)'' * ''B(t)'' + ''&alpha;(t)'')<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>x</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>x</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman Model ==<br />
This model is based on [http://en.wikipedia.org/wiki/Kalman_filter Kalman Filter]. In this case, the filter is used here instead of linear regression to determine proper hedge ratio, deviation from the mean and the standard deviation of the spread. The advantage is that this filter is superior when dealing with noise compared to OLS or TLS methods, also it does not have any lookback period to optimize.<br />
The disadvantage is that is has another parameters (like &delta;) to find out.<br />
<br />
We strongly recommend to read more details about Kalman filter applications in pair trading in [http://www.amazon.com/Algorithmic-Trading-Winning-Strategies-Rationale/dp/1118460146 this book].<br />
<br />
While Kalman Filter also estimates the standard deviation on the fly, our implementation also supports a possibility of having an auxiliary standard deviation indicator applied directly on the forecast error time series. The advantage is that you get less sensitivity on the &delta; parameter, but on the other hand a new lookback parameter is introduced.<br />
<br />
'''Kalman Model has too sensitive parameters which prevents it to be traded in reality. This model is provided only for education purposes - to be able to see how parameters affects its performance. Use Kalman Grid model for any serious trading.'''<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not planned'''<br />
* in PTL Trader: '''not planned'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter transition covariance &delta;, typical value is 0.0001 - '''unfortunately this model is very sensitive to this parameter especially when using standard deviation estimate coming from the Kalman filter itself'''<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* Auxiliary standard deviation period - if equal to zero, Kalman filter is used to estimate standard deviation, if non-zero, auxiliary standard deviation indicator with this period is used (an the estimate from Kalman filter is ignored)<br />
* Unstable period = how many Kalman filter observations are ignored at the beginning (first observations are quite unstable)<br />
<br />
'''This model is provided for education purposes only.'''<br />
<br />
== Kalman-grid Model (v2) ==<br />
'''This has been has been updated 2016-11-08. Please wait for the documentation update.'''<br />
<br />
After one year of experience with the [[Pair Trading Models#Kalman-grid Model (v1)|Kalman-grid Model (v1)]] and researching [https://en.wikipedia.org/wiki/Machine_learning machine learning principles], we have spent weeks of additional work on developing new version of Kalman Grid model, redesigned from scratch. Kalman Grid v2 is now the most advanced model PTL offers and has no competition whatsoever. After proven as stable it will become the default model for the whole PTL ecosystem.<br />
<br />
This model brings the superior performance of Kalman filter design, but it also deals with all the flaws of Kalman and old Kalman Grid v1:<br />
* all parameters of Kalman filter have been eliminated (&delta;, V<sub>e</sub>) - they are auto-estimated by the grid logic and updated with each price sample<br />
* increased average performance compared to v1<br />
* fixed issues of old version which prevented model to trade too often (biggest flaw of v1)<br />
<br />
This model rendered the version 1 obsolete and completely replaces it.<br />
<br />
How does it work? Instead of single strategy (and single Kalman filter), the whole grid of Kalman filters is evaluated by a proprietary system at the same time. Then, machine learning principles are applied to rank particular Kalman filter performances. Another algorithm is then used to figure filter outputs (slope, intercept, standard deviation) from the whole grid. The whole system actually behaves like a single Kalman filter, but with no need to provide any parameters.<br />
<br />
As a side effect, the entry threshold is also auto-optimized by the grid system itself. '''This means that the entry threshold is always indicated as 1.0.'''<br />
<br />
Main advantages of this model:<br />
* it is robust (because there are no parameters to tune)<br />
* beats any other models in performance (in average)<br />
* no risk of over-fitting in parameter optimization (nothing to optimize)<br />
<br />
To give some insight on performance: we took 300 best performing pairs from PTL database in period Jan 2013 - Jan 2016. Then we backtested all 300 pairs using '''out-of-sample''' period (Jan 2016 - Sep 2016) and we compared their performance. Models used: Ratio(per 14), Residual(20), Kalman Grid v1, Kalman Grid v2 using exit threshold of -1 (normal) and 0 (aggressive). Margin 50% (Req-T). Results here:<br />
{|<br />
|'''Model'''<br />
|'''Median CAGR %'''<br />
|'''Mean CAGR %'''<br />
|-<br />
|Kalman-grid v2 (normal)<br />
|'''7.206409'''<br />
|9.538509<br />
|-<br />
|Kalman-grid v2 (aggressive)<br />
|7.088950<br />
|'''11.433199'''<br />
|-<br />
|Kalman-grid v1<br />
|4.877391<br />
|10.794117<br />
|-<br />
|Residual(20)<br />
|3.596212<br />
|6.631089<br />
|-<br />
|Ratio(14)<br />
|3.590883<br />
|8.954484 <br />
|}<br />
<br />
'''You can clearly see how this model beats other models in out-of-sample performance (mixture of good and bad pairs).'''<br />
<br />
Profit distribution over all models (300 pairs, out-of-sample):<br />
[[File:Rplot1.png|center]]<br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
== Kalman-Auto Model ==<br />
'''This section is under construction'''<br />
<br />
This model is similar to Kalman-grid model, but it is more simple. This model does not optimize profit, but instead it optimizes margin usage to match predefined target. This model is very robust (is not fooled by glitches in performance) and it automatically optimizes entry rules (thresholds).<br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''planned Dec 2016'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman-Auto Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* Usage Threshold in %, allowed range <20, 80><br />
<br />
= Deprecated Models =<br />
* Orthogonal Model<br />
* Kalman-grid Model (v1)</div>Adminhttps://wiki.pairtradinglab.com/wiki/Pair_Trading_ModelsPair Trading Models2016-11-07T22:41:11Z<p>Admin: </p>
<hr />
<div>Pair Trading Lab offers pair trading algorithms based on various mathematical models. These are models currently supported in PTL:<br />
== Ratio Model ==<br />
[[File:Ratio-illustration.png|right|650px|thumb|Ratio Uptick Example]]<br />
This is one of the standard pair trading models described in literature. It is based in ratio of instrument prices, [http://en.wikipedia.org/wiki/Moving_average moving average] and [http://en.wikipedia.org/wiki/Standard_deviation standard deviation]. In other words, it is based on [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator.<br />
<br />
Since Nov 27th 2014, this model also supports additional [http://www.investopedia.com/terms/r/rsi.asp RSI filter] you can use in addition to Bollinger Bands method.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (RSI supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.5, 2.5>, 2.0 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* moving average period P<sub>m</sub> (typical range <10, 100>), default = 15<br />
* moving average type T (algorithm), default = exponential<br />
* standard deviation period P<sub>s</sub> (typical range <10, 100>), default = 15<br />
* entry mode (simple, uptick, downtick)<br />
* RSI period and threshold (optional RSI filtering)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* we need to calculate ratio time series ''R(t)'' = ''A(t)'' / ''B(t)''<br />
* let's apply moving average of type T with period P<sub>m</sub> on ''R(t)'' to get time series ''M(t)''<br />
* let's apply standard deviation with period P<sub>s</sub> on ''R(t)'' to get time series ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = (''R(t)'' - ''M(t)'') / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
* another common approach (to visualize) is to create bands and put it above the moving average ''M(t)'':<br />
** upper entry band ''U<sub>n</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>n</sub><br />
** lower entry band ''L<sub>n</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>n</sub><br />
** upper exit band ''U<sub>x</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>x</sub><br />
** lower exit band ''L<sub>x</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>x</sub><br />
** these bands are actually the same bands as in [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator and we can use crossing of ''R(t)'' and bands as trade signals<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'')<br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'')<br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' < E<sub>n</sub> (same as ''R(t-1)'' < ''U<sub>n</sub>(t-1)'')<br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' > -E<sub>n</sub> (same as ''R(t-1)'' > ''L<sub>n</sub>(t-1)'')<br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>x</sub><br />
** when using bands, it is the same as having ''R(t)'' < ''U<sub>n</sub>(t)'' '''and''' ''R(t-1)'' >= ''U<sub>n</sub>(t-1)'' '''and''' ''R(t)'' > ''U<sub>x</sub>(t)''<br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>x</sub><br />
** when using bands, it is the same as having ''R(t)'' > ''L<sub>n</sub>(t)'' '''and''' ''R(t-1)'' <= ''L<sub>n</sub>(t-1)'' '''and''' ''R(t)'' < ''L<sub>x</sub>(t)''<br />
<br />
Why do we have the '''simple entry mode'''? In normal situations and backtests, it gives same results as the '''uptick mode'''. But the difference comes up while trading multiple pairs in portfolio. The simple mode allows you to jump in the position immediately after a new slot is freed, regardless of the previous Z-scores.<br />
<br />
'''Which entry mode is better?''' Hard to tell, sometimes the uptick, sometimes the downtick. You have to do your homework and decide, which idea suits your trading style better. In general, uptick/simple mode is more aggressive, as it does not wait for first signs of spread mean reversion.<br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub> (equivalent to ''R(t)'' <= ''U<sub>x</sub>(t)'')<br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub> (equivalent to ''R(t)'' >= ''L<sub>x</sub>(t)'')<br />
<br />
=== Moving Average Types ===<br />
[[File:Rsi-illustration.png|right|650px|thumb|Ratio RSI Trading Example]]<br />
You can choose from these moving average algorithms:<br />
* [http://www.investopedia.com/terms/s/sma.asp Simple Moving Average] (SMA)<br />
* [http://www.investopedia.com/terms/e/ema.asp Exponential Moving Average] (EMA)<br />
* [http://fxtrade.oanda.com/learn/forex-indicators/weighted-moving-average Weighted Moving Average] (WMA)<br />
* [http://www.investopedia.com/terms/d/double-exponential-moving-average.asp Double Exponential Moving Average] (DEMA)<br />
* [http://www.investopedia.com/terms/t/triple-exponential-moving-average.asp Triple Exponential Moving Average] (TEMA)<br />
* [http://daytrading.about.com/od/indicators/a/Triangular.htm Triangular Moving Average] (TMA)<br />
* [http://etfhq.com/blog/2011/11/07/adaptive-moving-average-ama-aka-kaufman-adaptive-moving-average-kama Kaufman Adaptive Moving Average] (KAMA)<br />
* [http://www.mesasoftware.com/Papers/MAMA.pdf MESA Adaptive Moving Average] (MAMA)<br />
* [http://www.tradeescort.com/AgenaIndicatorHelp/AgenaIndicator.html?T3TripleExponentialMovingAverage.html Triple Exponential T3 Moving Average]<br />
<br />
Which one is the best? It depends, you have to test for yourself. They mostly differ in the term of memory and how fast they react to changes. Industry standard and the default is EMA. We suggest to try all of them on some sample pair to see how they work.<br />
<br />
=== RSI Filtering ===<br />
You can combine the Z-score/Bollinger Band model with [http://www.investopedia.com/terms/r/rsi.asp RSI indicator] applied on the ratio ''R(t)''. RSI filter will be automatically enabled if you set RSI threshold to other value than zero. RSI filter is combined with the Z-score rules using '''AND''' operator ('''both''' entry rule and RSI entry rule must be true to open a position).<br />
<br />
You can also change the RSI period if you want (default period = 15).<br />
<br />
RSI Threshold is value between 0 and 50. Because RSI indicator value oscillates between 0 and 100 (where 50 = mean), your threshold value is just used to set real thresholds for RSI:<br />
* let's assume RSI Period P<sub>r</sub> was entered<br />
* effective RSI value threshold is then 50+P<sub>r</sub> (upper) and 50-P<sub>r</sub> (lower)<br />
* you can see both thresholds in the example image at the right side<br />
<br />
Example:<br />
* RSI Threshold entered is 15<br />
* then, short positions are only opened, if RSI >= 65 (50+15)<br />
* long positions are only opened, if RSI <=35 (50-15)<br />
<br />
Useful hint: if you want to control entry rules just by RSI, you can set Entry Mode to simple and Entry Threshold to some low value.<br />
<br />
== Residual Model ==<br />
[[File:Residual-illustration2.png|right|650px|thumb|Residual Downtick Example]]<br />
Residual mode is based on [http://en.wikipedia.org/wiki/Linear_regression linear regression]. In literature it has been also referred to as the '''cointegration approach'''. Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the OLS method (Ordinary Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
This particular implementation is very simple. The regression is constructed using floating window of a fixed period, the same period is used for calculating the standard deviation.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (uptick and downtick modes are supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <15, 300><br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* first we need to construct a linear regression between ''A(t)'', ''B(t)'' using OLS, where ''A(t)'' = ''&beta;'' * ''B(t)'' + ''&alpha;'' + ''R(t)''<br />
* because we use floating window of period P (we calculate new regression each day), we actually get new series ''&beta;(t)'', ''&alpha;(t)'', ''R(t)'', where ''&beta;(t)'', ''&alpha;(t)'' are series of regression coefficients and ''R(t)'' are residuals (prediction errors)<br />
* ''R(t)'' = ''A(t)'' - (''&beta;(t)'' * ''B(t)'' + ''&alpha;(t)'')<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>x</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>x</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman Model ==<br />
This model is based on [http://en.wikipedia.org/wiki/Kalman_filter Kalman Filter]. In this case, the filter is used here instead of linear regression to determine proper hedge ratio, deviation from the mean and the standard deviation of the spread. The advantage is that this filter is superior when dealing with noise compared to OLS or TLS methods, also it does not have any lookback period to optimize.<br />
The disadvantage is that is has another parameters (like &delta;) to find out.<br />
<br />
We strongly recommend to read more details about Kalman filter applications in pair trading in [http://www.amazon.com/Algorithmic-Trading-Winning-Strategies-Rationale/dp/1118460146 this book].<br />
<br />
While Kalman Filter also estimates the standard deviation on the fly, our implementation also supports a possibility of having an auxiliary standard deviation indicator applied directly on the forecast error time series. The advantage is that you get less sensitivity on the &delta; parameter, but on the other hand a new lookback parameter is introduced.<br />
<br />
'''Kalman Model has too sensitive parameters which prevents it to be traded in reality. This model is provided only for education purposes - to be able to see how parameters affects its performance. Use Kalman Grid model for any serious trading.'''<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not planned'''<br />
* in PTL Trader: '''not planned'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter transition covariance &delta;, typical value is 0.0001 - '''unfortunately this model is very sensitive to this parameter especially when using standard deviation estimate coming from the Kalman filter itself'''<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* Auxiliary standard deviation period - if equal to zero, Kalman filter is used to estimate standard deviation, if non-zero, auxiliary standard deviation indicator with this period is used (an the estimate from Kalman filter is ignored)<br />
* Unstable period = how many Kalman filter observations are ignored at the beginning (first observations are quite unstable)<br />
<br />
'''This model is provided for education purposes only.'''<br />
<br />
== Kalman-grid Model (v2) ==<br />
'''This has been has been updated 2016-11-08. Please wait for the documentation update.'''<br />
<br />
After one year of experience with the [[Pair Trading Models#Kalman-grid Model (v1)|Kalman-grid Model (v1)]] and researching [https://en.wikipedia.org/wiki/Machine_learning machine learning principles], we have spent weeks of additional work on developing new version of Kalman Grid model, redesigned from scratch. Kalman Grid v2 is now the most advanced model PTL offers and has no competition whatsoever. After proven as stable it will become the default model for the whole PTL ecosystem.<br />
<br />
This model brings the superior performance of Kalman filter design, but it also deals with all the flaws of Kalman and old Kalman Grid v1:<br />
* all parameters of Kalman filter have been eliminated (&delta;, V<sub>e</sub>) - they are auto-estimated by the grid logic and updated with each price sample<br />
* increased average performance compared to v1<br />
* fixed issues of old version which prevented model to trade too often (biggest flaw of v1)<br />
<br />
This model rendered the version 1 obsolete and completely replaces it.<br />
<br />
How does it work? Instead of single strategy (and single Kalman filter), the whole grid of Kalman filters is evaluated by a proprietary system at the same time. Then, machine learning principles are applied to rank particular Kalman filter performances. Another algorithm is then used to figure filter outputs (slope, intercept, standard deviation) from the whole grid. The whole system actually behaves like a single Kalman filter, but with no need to provide any parameters.<br />
<br />
As a side effect, the entry threshold is also auto-optimized by the grid system itself. '''This means that the entry threshold is always indicated as 1.0.'''<br />
<br />
Main advantages of this model:<br />
* it is robust (because there are no parameters to tune)<br />
* beats any other models in performance (in average)<br />
* no risk of over-fitting in parameter optimization (nothing to optimize)<br />
<br />
To give some insight on performance: we took 300 best performing pairs from PTL database in period Jan 2013 - Jan 2016. Then we backtested all 300 pairs using '''out-of-sample''' period (Jan 2016 - Sep 2016) and we compared their performance. Models used: Ratio(per 14), Residual(20), Kalman Grid v1, Kalman Grid v2 using exit threshold of -1 (normal) and 0 (aggressive). Margin 50% (Req-T). Results here:<br />
{|<br />
|'''Model'''<br />
|'''Median CAGR %'''<br />
|'''Mean CAGR %'''<br />
|-<br />
|Kalman-grid v2 (normal)<br />
|'''7.206409'''<br />
|9.538509<br />
|-<br />
|Kalman-grid v2 (aggressive)<br />
|7.088950<br />
|'''11.433199'''<br />
|-<br />
|Kalman-grid v1<br />
|4.877391<br />
|10.794117<br />
|-<br />
|Residual(20)<br />
|3.596212<br />
|6.631089<br />
|-<br />
|Ratio(14)<br />
|3.590883<br />
|8.954484 <br />
|}<br />
<br />
'''You can clearly see how this model beats other models in out-of-sample performance (mixture of good and bad pairs).'''<br />
<br />
Profit distribution over all models (300 pairs, out-of-sample):<br />
[[File:Rplot1.png|center]]<br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
== Kalman-Auto Model ==<br />
'''This section is under construction'''<br />
<br />
This model is similar to Kalman-grid model, but it is more simple. This model does not optimize profit, but instead it optimizes margin usage to match predefined target. This model is very robust (is not fooled by glitches in performance) and it automatically optimizes entry rules (thresholds).<br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''planned Dec 2016'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman-Auto Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* Usage Threshold in %, allowed range <20, 80><br />
<br />
= Deprecated Models =<br />
* Orthogonal Model<br />
* Kalman-grid Model (v1)</div>Adminhttps://wiki.pairtradinglab.com/wiki/Pair_Trading_ModelsPair Trading Models2016-11-07T22:33:21Z<p>Admin: /* Kalman-grid Model (v2) */</p>
<hr />
<div>Pair Trading Lab offers pair trading algorithms based on various mathematical models. These are models currently supported in PTL:<br />
== Ratio Model ==<br />
[[File:Ratio-illustration.png|right|650px|thumb|Ratio Uptick Example]]<br />
This is one of the standard pair trading models described in literature. It is based in ratio of instrument prices, [http://en.wikipedia.org/wiki/Moving_average moving average] and [http://en.wikipedia.org/wiki/Standard_deviation standard deviation]. In other words, it is based on [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator.<br />
<br />
Since Nov 27th 2014, this model also supports additional [http://www.investopedia.com/terms/r/rsi.asp RSI filter] you can use in addition to Bollinger Bands method.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (RSI supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.5, 2.5>, 2.0 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* moving average period P<sub>m</sub> (typical range <10, 100>), default = 15<br />
* moving average type T (algorithm), default = exponential<br />
* standard deviation period P<sub>s</sub> (typical range <10, 100>), default = 15<br />
* entry mode (simple, uptick, downtick)<br />
* RSI period and threshold (optional RSI filtering)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* we need to calculate ratio time series ''R(t)'' = ''A(t)'' / ''B(t)''<br />
* let's apply moving average of type T with period P<sub>m</sub> on ''R(t)'' to get time series ''M(t)''<br />
* let's apply standard deviation with period P<sub>s</sub> on ''R(t)'' to get time series ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = (''R(t)'' - ''M(t)'') / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
* another common approach (to visualize) is to create bands and put it above the moving average ''M(t)'':<br />
** upper entry band ''U<sub>n</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>n</sub><br />
** lower entry band ''L<sub>n</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>n</sub><br />
** upper exit band ''U<sub>x</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>x</sub><br />
** lower exit band ''L<sub>x</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>x</sub><br />
** these bands are actually the same bands as in [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator and we can use crossing of ''R(t)'' and bands as trade signals<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'')<br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'')<br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' < E<sub>n</sub> (same as ''R(t-1)'' < ''U<sub>n</sub>(t-1)'')<br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' > -E<sub>n</sub> (same as ''R(t-1)'' > ''L<sub>n</sub>(t-1)'')<br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>x</sub><br />
** when using bands, it is the same as having ''R(t)'' < ''U<sub>n</sub>(t)'' '''and''' ''R(t-1)'' >= ''U<sub>n</sub>(t-1)'' '''and''' ''R(t)'' > ''U<sub>x</sub>(t)''<br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>x</sub><br />
** when using bands, it is the same as having ''R(t)'' > ''L<sub>n</sub>(t)'' '''and''' ''R(t-1)'' <= ''L<sub>n</sub>(t-1)'' '''and''' ''R(t)'' < ''L<sub>x</sub>(t)''<br />
<br />
Why do we have the '''simple entry mode'''? In normal situations and backtests, it gives same results as the '''uptick mode'''. But the difference comes up while trading multiple pairs in portfolio. The simple mode allows you to jump in the position immediately after a new slot is freed, regardless of the previous Z-scores.<br />
<br />
'''Which entry mode is better?''' Hard to tell, sometimes the uptick, sometimes the downtick. You have to do your homework and decide, which idea suits your trading style better. In general, uptick/simple mode is more aggressive, as it does not wait for first signs of spread mean reversion.<br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub> (equivalent to ''R(t)'' <= ''U<sub>x</sub>(t)'')<br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub> (equivalent to ''R(t)'' >= ''L<sub>x</sub>(t)'')<br />
<br />
=== Moving Average Types ===<br />
[[File:Rsi-illustration.png|right|650px|thumb|Ratio RSI Trading Example]]<br />
You can choose from these moving average algorithms:<br />
* [http://www.investopedia.com/terms/s/sma.asp Simple Moving Average] (SMA)<br />
* [http://www.investopedia.com/terms/e/ema.asp Exponential Moving Average] (EMA)<br />
* [http://fxtrade.oanda.com/learn/forex-indicators/weighted-moving-average Weighted Moving Average] (WMA)<br />
* [http://www.investopedia.com/terms/d/double-exponential-moving-average.asp Double Exponential Moving Average] (DEMA)<br />
* [http://www.investopedia.com/terms/t/triple-exponential-moving-average.asp Triple Exponential Moving Average] (TEMA)<br />
* [http://daytrading.about.com/od/indicators/a/Triangular.htm Triangular Moving Average] (TMA)<br />
* [http://etfhq.com/blog/2011/11/07/adaptive-moving-average-ama-aka-kaufman-adaptive-moving-average-kama Kaufman Adaptive Moving Average] (KAMA)<br />
* [http://www.mesasoftware.com/Papers/MAMA.pdf MESA Adaptive Moving Average] (MAMA)<br />
* [http://www.tradeescort.com/AgenaIndicatorHelp/AgenaIndicator.html?T3TripleExponentialMovingAverage.html Triple Exponential T3 Moving Average]<br />
<br />
Which one is the best? It depends, you have to test for yourself. They mostly differ in the term of memory and how fast they react to changes. Industry standard and the default is EMA. We suggest to try all of them on some sample pair to see how they work.<br />
<br />
=== RSI Filtering ===<br />
You can combine the Z-score/Bollinger Band model with [http://www.investopedia.com/terms/r/rsi.asp RSI indicator] applied on the ratio ''R(t)''. RSI filter will be automatically enabled if you set RSI threshold to other value than zero. RSI filter is combined with the Z-score rules using '''AND''' operator ('''both''' entry rule and RSI entry rule must be true to open a position).<br />
<br />
You can also change the RSI period if you want (default period = 15).<br />
<br />
RSI Threshold is value between 0 and 50. Because RSI indicator value oscillates between 0 and 100 (where 50 = mean), your threshold value is just used to set real thresholds for RSI:<br />
* let's assume RSI Period P<sub>r</sub> was entered<br />
* effective RSI value threshold is then 50+P<sub>r</sub> (upper) and 50-P<sub>r</sub> (lower)<br />
* you can see both thresholds in the example image at the right side<br />
<br />
Example:<br />
* RSI Threshold entered is 15<br />
* then, short positions are only opened, if RSI >= 65 (50+15)<br />
* long positions are only opened, if RSI <=35 (50-15)<br />
<br />
Useful hint: if you want to control entry rules just by RSI, you can set Entry Mode to simple and Entry Threshold to some low value.<br />
<br />
== Residual Model ==<br />
[[File:Residual-illustration2.png|right|650px|thumb|Residual Downtick Example]]<br />
Residual mode is based on [http://en.wikipedia.org/wiki/Linear_regression linear regression]. In literature it has been also referred to as the '''cointegration approach'''. Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the OLS method (Ordinary Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
This particular implementation is very simple. The regression is constructed using floating window of a fixed period, the same period is used for calculating the standard deviation.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (uptick and downtick modes are supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <15, 300><br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* first we need to construct a linear regression between ''A(t)'', ''B(t)'' using OLS, where ''A(t)'' = ''&beta;'' * ''B(t)'' + ''&alpha;'' + ''R(t)''<br />
* because we use floating window of period P (we calculate new regression each day), we actually get new series ''&beta;(t)'', ''&alpha;(t)'', ''R(t)'', where ''&beta;(t)'', ''&alpha;(t)'' are series of regression coefficients and ''R(t)'' are residuals (prediction errors)<br />
* ''R(t)'' = ''A(t)'' - (''&beta;(t)'' * ''B(t)'' + ''&alpha;(t)'')<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>x</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>x</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman Model ==<br />
This model is based on [http://en.wikipedia.org/wiki/Kalman_filter Kalman Filter]. In this case, the filter is used here instead of linear regression to determine proper hedge ratio, deviation from the mean and the standard deviation of the spread. The advantage is that this filter is superior when dealing with noise compared to OLS or TLS methods, also it does not have any lookback period to optimize.<br />
The disadvantage is that is has another parameters (like &delta;) to find out.<br />
<br />
We strongly recommend to read more details about Kalman filter applications in pair trading in [http://www.amazon.com/Algorithmic-Trading-Winning-Strategies-Rationale/dp/1118460146 this book].<br />
<br />
While Kalman Filter also estimates the standard deviation on the fly, our implementation also supports a possibility of having an auxiliary standard deviation indicator applied directly on the forecast error time series. The advantage is that you get less sensitivity on the &delta; parameter, but on the other hand a new lookback parameter is introduced.<br />
<br />
'''Kalman Model has too sensitive parameters which prevents it to be traded in reality. This model is provided only for education purposes - to be able to see how parameters affects its performance. Use Kalman Grid model for any serious trading.'''<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not planned'''<br />
* in PTL Trader: '''not planned'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter transition covariance &delta;, typical value is 0.0001 - '''unfortunately this model is very sensitive to this parameter especially when using standard deviation estimate coming from the Kalman filter itself'''<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* Auxiliary standard deviation period - if equal to zero, Kalman filter is used to estimate standard deviation, if non-zero, auxiliary standard deviation indicator with this period is used (an the estimate from Kalman filter is ignored)<br />
* Unstable period = how many Kalman filter observations are ignored at the beginning (first observations are quite unstable)<br />
<br />
'''This model is provided for education purposes only.'''<br />
<br />
== Kalman-grid Model (v2) ==<br />
'''This has been has been updated 2016-11-08. Please wait for the documentation update.'''<br />
<br />
After one year of experience with the [[Pair Trading Models#Kalman-grid Model (v1)|Kalman-grid Model (v1)]] and researching [https://en.wikipedia.org/wiki/Machine_learning machine learning principles], we have spent weeks of additional work on developing new version of Kalman Grid model, redesigned from scratch. Kalman Grid v2 is now the most advanced model PTL offers and has no competition whatsoever. After proven as stable it will become the default model for the whole PTL ecosystem.<br />
<br />
This model brings the superior performance of Kalman filter design, but it also deals with all the flaws of Kalman and old Kalman Grid v1:<br />
* all parameters of Kalman filter have been eliminated (&delta;, V<sub>e</sub>) - they are auto-estimated by the grid logic and updated with each price sample<br />
* increased average performance compared to v1<br />
* fixed issues of old version which prevented model to trade too often (biggest flaw of v1)<br />
<br />
This model rendered the version 1 obsolete and completely replaces it.<br />
<br />
How does it work? Instead of single strategy (and single Kalman filter), the whole grid of Kalman filters is evaluated by a proprietary system at the same time. Then, machine learning principles are applied to rank particular Kalman filter performances. Another algorithm is then used to figure filter outputs (slope, intercept, standard deviation) from the whole grid. The whole system actually behaves like a single Kalman filter, but with no need to provide any parameters.<br />
<br />
As a side effect, the entry threshold is also auto-optimized by the grid system itself. '''This means that the entry threshold is always indicated as 1.0.'''<br />
<br />
Main advantages of this model:<br />
* it is robust (because there are no parameters to tune)<br />
* beats any other models in performance (in average)<br />
* no risk of over-fitting in parameter optimization (nothing to optimize)<br />
<br />
To give some insight on performance: we took 300 best performing pairs from PTL database in period Jan 2013 - Jan 2016. Then we backtested all 300 pairs using '''out-of-sample''' period (Jan 2016 - Sep 2016) and we compared their performance. Models used: Ratio(per 14), Residual(20), Kalman Grid v1, Kalman Grid v2 using exit threshold of -1 (normal) and 0 (aggressive). Margin 50% (Req-T). Results here:<br />
{|<br />
|'''Model'''<br />
|'''Median CAGR %'''<br />
|'''Mean CAGR %'''<br />
|-<br />
|Kalman-grid v2 (normal)<br />
|'''7.206409'''<br />
|9.538509<br />
|-<br />
|Kalman-grid v2 (aggressive)<br />
|7.088950<br />
|'''11.433199'''<br />
|-<br />
|Kalman-grid v1<br />
|4.877391<br />
|10.794117<br />
|-<br />
|Residual(20)<br />
|3.596212<br />
|6.631089<br />
|-<br />
|Ratio(14)<br />
|3.590883<br />
|8.954484 <br />
|}<br />
<br />
'''You can clearly see how this model beats other models in out-of-sample performance (mixture of good and bad pairs).'''<br />
<br />
Profit distribution over all models (300 pairs, out-of-sample):<br />
[[File:Rplot1.png|center]]<br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''planned Dec 2016'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* exit threshold E<sub>x</sub> for Z-score, allowed value is <-1, 0> - this is the only parameter left to fine-tune. It determines how aggressively the model will trade: 0 means that trades are closed each time the forecast error crosses zero point (most aggressive mode, many trades), -1 means that trades are closed when the opposite band is crossed (less aggressive, less trades). -1 is recommended as it is more robust, 0 can boost performance at some well cointegrated pairs.<br />
<br />
= Deprecated Models =<br />
* Orthogonal Model<br />
* Kalman-grid Model (v1)</div>Adminhttps://wiki.pairtradinglab.com/wiki/Pair_Trading_ModelsPair Trading Models2016-11-07T22:32:18Z<p>Admin: /* Deprecated Models */</p>
<hr />
<div>Pair Trading Lab offers pair trading algorithms based on various mathematical models. These are models currently supported in PTL:<br />
== Ratio Model ==<br />
[[File:Ratio-illustration.png|right|650px|thumb|Ratio Uptick Example]]<br />
This is one of the standard pair trading models described in literature. It is based in ratio of instrument prices, [http://en.wikipedia.org/wiki/Moving_average moving average] and [http://en.wikipedia.org/wiki/Standard_deviation standard deviation]. In other words, it is based on [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator.<br />
<br />
Since Nov 27th 2014, this model also supports additional [http://www.investopedia.com/terms/r/rsi.asp RSI filter] you can use in addition to Bollinger Bands method.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (RSI supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.5, 2.5>, 2.0 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* moving average period P<sub>m</sub> (typical range <10, 100>), default = 15<br />
* moving average type T (algorithm), default = exponential<br />
* standard deviation period P<sub>s</sub> (typical range <10, 100>), default = 15<br />
* entry mode (simple, uptick, downtick)<br />
* RSI period and threshold (optional RSI filtering)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* we need to calculate ratio time series ''R(t)'' = ''A(t)'' / ''B(t)''<br />
* let's apply moving average of type T with period P<sub>m</sub> on ''R(t)'' to get time series ''M(t)''<br />
* let's apply standard deviation with period P<sub>s</sub> on ''R(t)'' to get time series ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = (''R(t)'' - ''M(t)'') / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
* another common approach (to visualize) is to create bands and put it above the moving average ''M(t)'':<br />
** upper entry band ''U<sub>n</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>n</sub><br />
** lower entry band ''L<sub>n</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>n</sub><br />
** upper exit band ''U<sub>x</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>x</sub><br />
** lower exit band ''L<sub>x</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>x</sub><br />
** these bands are actually the same bands as in [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator and we can use crossing of ''R(t)'' and bands as trade signals<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'')<br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'')<br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' < E<sub>n</sub> (same as ''R(t-1)'' < ''U<sub>n</sub>(t-1)'')<br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' > -E<sub>n</sub> (same as ''R(t-1)'' > ''L<sub>n</sub>(t-1)'')<br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>x</sub><br />
** when using bands, it is the same as having ''R(t)'' < ''U<sub>n</sub>(t)'' '''and''' ''R(t-1)'' >= ''U<sub>n</sub>(t-1)'' '''and''' ''R(t)'' > ''U<sub>x</sub>(t)''<br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>x</sub><br />
** when using bands, it is the same as having ''R(t)'' > ''L<sub>n</sub>(t)'' '''and''' ''R(t-1)'' <= ''L<sub>n</sub>(t-1)'' '''and''' ''R(t)'' < ''L<sub>x</sub>(t)''<br />
<br />
Why do we have the '''simple entry mode'''? In normal situations and backtests, it gives same results as the '''uptick mode'''. But the difference comes up while trading multiple pairs in portfolio. The simple mode allows you to jump in the position immediately after a new slot is freed, regardless of the previous Z-scores.<br />
<br />
'''Which entry mode is better?''' Hard to tell, sometimes the uptick, sometimes the downtick. You have to do your homework and decide, which idea suits your trading style better. In general, uptick/simple mode is more aggressive, as it does not wait for first signs of spread mean reversion.<br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub> (equivalent to ''R(t)'' <= ''U<sub>x</sub>(t)'')<br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub> (equivalent to ''R(t)'' >= ''L<sub>x</sub>(t)'')<br />
<br />
=== Moving Average Types ===<br />
[[File:Rsi-illustration.png|right|650px|thumb|Ratio RSI Trading Example]]<br />
You can choose from these moving average algorithms:<br />
* [http://www.investopedia.com/terms/s/sma.asp Simple Moving Average] (SMA)<br />
* [http://www.investopedia.com/terms/e/ema.asp Exponential Moving Average] (EMA)<br />
* [http://fxtrade.oanda.com/learn/forex-indicators/weighted-moving-average Weighted Moving Average] (WMA)<br />
* [http://www.investopedia.com/terms/d/double-exponential-moving-average.asp Double Exponential Moving Average] (DEMA)<br />
* [http://www.investopedia.com/terms/t/triple-exponential-moving-average.asp Triple Exponential Moving Average] (TEMA)<br />
* [http://daytrading.about.com/od/indicators/a/Triangular.htm Triangular Moving Average] (TMA)<br />
* [http://etfhq.com/blog/2011/11/07/adaptive-moving-average-ama-aka-kaufman-adaptive-moving-average-kama Kaufman Adaptive Moving Average] (KAMA)<br />
* [http://www.mesasoftware.com/Papers/MAMA.pdf MESA Adaptive Moving Average] (MAMA)<br />
* [http://www.tradeescort.com/AgenaIndicatorHelp/AgenaIndicator.html?T3TripleExponentialMovingAverage.html Triple Exponential T3 Moving Average]<br />
<br />
Which one is the best? It depends, you have to test for yourself. They mostly differ in the term of memory and how fast they react to changes. Industry standard and the default is EMA. We suggest to try all of them on some sample pair to see how they work.<br />
<br />
=== RSI Filtering ===<br />
You can combine the Z-score/Bollinger Band model with [http://www.investopedia.com/terms/r/rsi.asp RSI indicator] applied on the ratio ''R(t)''. RSI filter will be automatically enabled if you set RSI threshold to other value than zero. RSI filter is combined with the Z-score rules using '''AND''' operator ('''both''' entry rule and RSI entry rule must be true to open a position).<br />
<br />
You can also change the RSI period if you want (default period = 15).<br />
<br />
RSI Threshold is value between 0 and 50. Because RSI indicator value oscillates between 0 and 100 (where 50 = mean), your threshold value is just used to set real thresholds for RSI:<br />
* let's assume RSI Period P<sub>r</sub> was entered<br />
* effective RSI value threshold is then 50+P<sub>r</sub> (upper) and 50-P<sub>r</sub> (lower)<br />
* you can see both thresholds in the example image at the right side<br />
<br />
Example:<br />
* RSI Threshold entered is 15<br />
* then, short positions are only opened, if RSI >= 65 (50+15)<br />
* long positions are only opened, if RSI <=35 (50-15)<br />
<br />
Useful hint: if you want to control entry rules just by RSI, you can set Entry Mode to simple and Entry Threshold to some low value.<br />
<br />
== Residual Model ==<br />
[[File:Residual-illustration2.png|right|650px|thumb|Residual Downtick Example]]<br />
Residual mode is based on [http://en.wikipedia.org/wiki/Linear_regression linear regression]. In literature it has been also referred to as the '''cointegration approach'''. Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the OLS method (Ordinary Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
This particular implementation is very simple. The regression is constructed using floating window of a fixed period, the same period is used for calculating the standard deviation.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (uptick and downtick modes are supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <15, 300><br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* first we need to construct a linear regression between ''A(t)'', ''B(t)'' using OLS, where ''A(t)'' = ''&beta;'' * ''B(t)'' + ''&alpha;'' + ''R(t)''<br />
* because we use floating window of period P (we calculate new regression each day), we actually get new series ''&beta;(t)'', ''&alpha;(t)'', ''R(t)'', where ''&beta;(t)'', ''&alpha;(t)'' are series of regression coefficients and ''R(t)'' are residuals (prediction errors)<br />
* ''R(t)'' = ''A(t)'' - (''&beta;(t)'' * ''B(t)'' + ''&alpha;(t)'')<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>x</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>x</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman Model ==<br />
This model is based on [http://en.wikipedia.org/wiki/Kalman_filter Kalman Filter]. In this case, the filter is used here instead of linear regression to determine proper hedge ratio, deviation from the mean and the standard deviation of the spread. The advantage is that this filter is superior when dealing with noise compared to OLS or TLS methods, also it does not have any lookback period to optimize.<br />
The disadvantage is that is has another parameters (like &delta;) to find out.<br />
<br />
We strongly recommend to read more details about Kalman filter applications in pair trading in [http://www.amazon.com/Algorithmic-Trading-Winning-Strategies-Rationale/dp/1118460146 this book].<br />
<br />
While Kalman Filter also estimates the standard deviation on the fly, our implementation also supports a possibility of having an auxiliary standard deviation indicator applied directly on the forecast error time series. The advantage is that you get less sensitivity on the &delta; parameter, but on the other hand a new lookback parameter is introduced.<br />
<br />
'''Kalman Model has too sensitive parameters which prevents it to be traded in reality. This model is provided only for education purposes - to be able to see how parameters affects its performance. Use Kalman Grid model for any serious trading.'''<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not planned'''<br />
* in PTL Trader: '''not planned'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter transition covariance &delta;, typical value is 0.0001 - '''unfortunately this model is very sensitive to this parameter especially when using standard deviation estimate coming from the Kalman filter itself'''<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* Auxiliary standard deviation period - if equal to zero, Kalman filter is used to estimate standard deviation, if non-zero, auxiliary standard deviation indicator with this period is used (an the estimate from Kalman filter is ignored)<br />
* Unstable period = how many Kalman filter observations are ignored at the beginning (first observations are quite unstable)<br />
<br />
'''This model is provided for education purposes only.'''<br />
<br />
== Kalman-grid Model (v2) ==<br />
After one year of experience with the [[Pair Trading Models#Kalman-grid Model (v1)|Kalman-grid Model (v1)]] and researching [https://en.wikipedia.org/wiki/Machine_learning machine learning principles], we have spent weeks of additional work on developing new version of Kalman Grid model, redesigned from scratch. Kalman Grid v2 is now the most advanced model PTL offers and has no competition whatsoever. After proven as stable it will become the default model for the whole PTL ecosystem.<br />
<br />
This model brings the superior performance of Kalman filter design, but it also deals with all the flaws of Kalman and old Kalman Grid v1:<br />
* all parameters of Kalman filter have been eliminated (&delta;, V<sub>e</sub>) - they are auto-estimated by the grid logic and updated with each price sample<br />
* increased average performance compared to v1<br />
* fixed issues of old version which prevented model to trade too often (biggest flaw of v1)<br />
<br />
This model rendered the version 1 obsolete and completely replaces it.<br />
<br />
How does it work? Instead of single strategy (and single Kalman filter), the whole grid of Kalman filters is evaluated by a proprietary system at the same time. Then, machine learning principles are applied to rank particular Kalman filter performances. Another algorithm is then used to figure filter outputs (slope, intercept, standard deviation) from the whole grid. The whole system actually behaves like a single Kalman filter, but with no need to provide any parameters.<br />
<br />
As a side effect, the entry threshold is also auto-optimized by the grid system itself. '''This means that the entry threshold is always indicated as 1.0.'''<br />
<br />
Main advantages of this model:<br />
* it is robust (because there are no parameters to tune)<br />
* beats any other models in performance (in average)<br />
* no risk of over-fitting in parameter optimization (nothing to optimize)<br />
<br />
To give some insight on performance: we took 300 best performing pairs from PTL database in period Jan 2013 - Jan 2016. Then we backtested all 300 pairs using '''out-of-sample''' period (Jan 2016 - Sep 2016) and we compared their performance. Models used: Ratio(per 14), Residual(20), Kalman Grid v1, Kalman Grid v2 using exit threshold of -1 (normal) and 0 (aggressive). Margin 50% (Req-T). Results here:<br />
{|<br />
|'''Model'''<br />
|'''Median CAGR %'''<br />
|'''Mean CAGR %'''<br />
|-<br />
|Kalman-grid v2 (normal)<br />
|'''7.206409'''<br />
|9.538509<br />
|-<br />
|Kalman-grid v2 (aggressive)<br />
|7.088950<br />
|'''11.433199'''<br />
|-<br />
|Kalman-grid v1<br />
|4.877391<br />
|10.794117<br />
|-<br />
|Residual(20)<br />
|3.596212<br />
|6.631089<br />
|-<br />
|Ratio(14)<br />
|3.590883<br />
|8.954484 <br />
|}<br />
<br />
'''You can clearly see how this model beats other models in out-of-sample performance (mixture of good and bad pairs).'''<br />
<br />
Profit distribution over all models (300 pairs, out-of-sample):<br />
[[File:Rplot1.png|center]]<br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''planned Dec 2016'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* exit threshold E<sub>x</sub> for Z-score, allowed value is <-1, 0> - this is the only parameter left to fine-tune. It determines how aggressively the model will trade: 0 means that trades are closed each time the forecast error crosses zero point (most aggressive mode, many trades), -1 means that trades are closed when the opposite band is crossed (less aggressive, less trades). -1 is recommended as it is more robust, 0 can boost performance at some well cointegrated pairs.<br />
<br />
= Deprecated Models =<br />
* Orthogonal Model<br />
* Kalman-grid Model (v1)</div>Adminhttps://wiki.pairtradinglab.com/wiki/PTL_Trader_ChangelogPTL Trader Changelog2016-11-07T21:25:56Z<p>Admin: </p>
<hr />
<div>== 1.3.0 ==<br />
* multi-platform - builds for win32, win64, linux64 and macOS now available<br />
* indicating both z-score bid and z-score ask separately<br />
* more effective margin allocation algorithm (only differs when margins differ per leg)<br />
* profit potential is now calculated from bid-ask quotes (instead of trades)<br />
* custom thresholds for downtick mode supported<br />
* allow-reversals constraint supported<br />
* copyright and license update<br />
* cosmetic changes<br />
* updated 3rd party components<br />
* migrated to Gradle build system<br />
<br />
Released 2016-11-08.<br />
<br />
== 1.2.4 ==<br />
Minor bugfix release.<br />
* fix for incorrect handling of some time zones (Moscow, etc...)<br />
* Z-score and RSI is now being indicated in more situations, including "No new positions"<br />
<br />
Released 2015-12-21.<br />
<br />
== 1.2.3 ==<br />
* fix for duplicate order ID issues occurring when many orders are being sent at the same time<br />
* Open Long, Open Short and Close buttons can be used in more situations<br />
* Close button can be used to manually resolve "one leg opened" situation<br />
* cosmetic UI changes<br />
* copyright and license update<br />
<br />
Released 2015-07-23.<br />
<br />
== 1.2.2 ==<br />
* added some price event sanity checks and constraints<br />
* other minor fixes / changes<br />
<br />
Released 2015-04-18.<br />
<br />
== 1.2.1 ==<br />
* application JAR is now wrapped in EXE - fix for shortcut/installer issues with Java 1.8<br />
* fixed authentication fail message<br />
* more precise z-score indication (now also the indicated z-score is calculated using bid/ask prices)<br />
* cooldown after external close detected<br />
* "last" prices are no longer used for entry/exit signals (only ask/bid prices are used now)<br />
* redesigned exchange activity detection (to detect holidays / days with no activity)<br />
* trading engine should be more friendly to low liquidity stocks (removed some unnecessary constraints in signal evaluation)<br />
* trading engine will no longer allow positions reversals for now (to be consistent with backtests)<br />
* bid and ask sizes are now displayed in the UI<br />
* other minor fixes and changes<br />
<br />
Internal release.<br />
<br />
== 1.2.0 ==<br />
* internal architecture changes related to compatibility with PTL portfolio system<br />
* RSI filter for Ratio model now fully supported<br />
* all entry modes are now supported for Residual model<br />
<br />
Released 2015-01-02.<br />
<br />
== 1.1.1 ==<br />
Bugfix release & internal architecture improvements:<br />
<br />
* redesigned concurrency and threading architecture (pair strategies now use thread confinement)<br />
* fixed few potential deadlock situations and GUI freezes<br />
* fixed concurrency issue which could potentially lead to allow N+1 slots opened in a rare occasion<br />
* fixed retrying fetching historical data after IB data pacing violation (regression introduced in 1.0.4, it worked in earlier versions)<br />
<br />
Released 2014-10-07.<br />
<br />
== 1.1.0 ==<br />
* basic FA (Financial Adviser) account support (you have to enter the account code manually when connecting)<br />
* increased log file max size and number of backups<br />
* unadjusted split protection (stops auto trading if there is a significant price discrepancy which may indicate that IB forgot to adjust historical data after split event)<br />
* prevents automated trading if historical data fetched are obsolete (older than 5 days)<br />
* connection to IB API is now handled in a separate thread to prevent UI freezes<br />
* updated IB client API files to 971.01<br />
* minor cosmetic changes and bugfixes<br />
* minor changes in log file verbosity<br />
<br />
Released 2014-08-13.<br />
<br />
== 1.0.6 ==<br />
* workaround for "ambiguous contract" issues for some particular IB symbols (MSFT, ...)<br />
* fix for stock symbols like XXX.Y (RDS.A for instance)<br />
* some other small changes/fixes<br />
* more verbose logging of slot allocation process<br />
<br />
Released 2014-03-19.<br />
<br />
== 1.0.5 ==<br />
* fixed a bug causing "wait for min profit" engine status indicated outside trading hours<br />
<br />
Released 2013-11-07.<br />
<br />
== 1.0.4 ==<br />
* z-score and profit potential now update also outside user's configured trading window (as long historical data were available)<br />
* manually opening/closing positions is now enabled in more engine states (for instance in "wait for min profit")<br />
* modified profit estimation formula for residual model, profit estimation now disabled for cases where A<0.1<br />
* fixed PDT protection in position exit logic<br />
* PTL Trader does not try to initialize trading engine and load historical data for portfolios bound to accounts not belonging to current IB account connected anymore<br />
* historical data are now downloaded and models are recalculated every day at 9:30 EST, so z-score and profit potential indicators work with updated model, instead of waiting for user's configured trading window<br />
<br />
Internal release.<br />
<br />
== 1.0.3 ==<br />
* workaround for strategy P/L tracking (ignore portfolio data in IB "black hour" 0:** EST)<br />
* modified default log levels<br />
<br />
First official stable release (2013-08-27).<br />
<br />
== 1.0.2 ==<br />
* workaround for equity tracking (ignore equity data in IB "black hour" 0:** EST)<br />
* more detailed logs for IB portfolio updates<br />
<br />
Internal release.<br />
<br />
== 1.0.1 ==<br />
* debug logs for equity tracking<br />
* reworked execution and commission report handling (fixes slippage tracking problems and missing entries in transaction history)<br />
* PDT protection fix<br />
* libraries updated<br />
<br />
Internal release.<br />
<br />
== 1.0.0 ==<br />
* changed valid range for instrument margins (15% - 100%)<br />
* cooldown period after closing a pair trade (it is not possible to open new pair trade earlier than in two minutes) - to prevent sending new orders until IB portfolio stats settle<br />
* more verbose logging of executions and commission reports (internal log)<br />
* reworked periodic premium membership checks<br />
* updated logo and icons<br />
<br />
Internal release.<br />
<br />
== 0.9.6b ==<br />
* just a small fix - NYSEAMEX exchange has been apparently renamed to NYSEMKT, so both exchanges are now supported<br />
<br />
* Initial version released to public (2013-05-17).<br />
<br />
== 0.9.6 ==<br />
* Status = BETA, not recommended for real trading yet (although we already use it that way ourselves)<br />
<br />
<br />
== Known issues we are working on ==<br />
...</div>Adminhttps://wiki.pairtradinglab.com/wiki/Pair_Trading_ModelsPair Trading Models2016-09-15T13:04:14Z<p>Admin: /* Kalman-grid Model (v2) */</p>
<hr />
<div>Pair Trading Lab offers pair trading algorithms based on various mathematical models. These are models currently supported in PTL:<br />
== Ratio Model ==<br />
[[File:Ratio-illustration.png|right|650px|thumb|Ratio Uptick Example]]<br />
This is one of the standard pair trading models described in literature. It is based in ratio of instrument prices, [http://en.wikipedia.org/wiki/Moving_average moving average] and [http://en.wikipedia.org/wiki/Standard_deviation standard deviation]. In other words, it is based on [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator.<br />
<br />
Since Nov 27th 2014, this model also supports additional [http://www.investopedia.com/terms/r/rsi.asp RSI filter] you can use in addition to Bollinger Bands method.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (RSI supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.5, 2.5>, 2.0 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* moving average period P<sub>m</sub> (typical range <10, 100>), default = 15<br />
* moving average type T (algorithm), default = exponential<br />
* standard deviation period P<sub>s</sub> (typical range <10, 100>), default = 15<br />
* entry mode (simple, uptick, downtick)<br />
* RSI period and threshold (optional RSI filtering)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* we need to calculate ratio time series ''R(t)'' = ''A(t)'' / ''B(t)''<br />
* let's apply moving average of type T with period P<sub>m</sub> on ''R(t)'' to get time series ''M(t)''<br />
* let's apply standard deviation with period P<sub>s</sub> on ''R(t)'' to get time series ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = (''R(t)'' - ''M(t)'') / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
* another common approach (to visualize) is to create bands and put it above the moving average ''M(t)'':<br />
** upper entry band ''U<sub>n</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>n</sub><br />
** lower entry band ''L<sub>n</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>n</sub><br />
** upper exit band ''U<sub>x</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>x</sub><br />
** lower exit band ''L<sub>x</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>x</sub><br />
** these bands are actually the same bands as in [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator and we can use crossing of ''R(t)'' and bands as trade signals<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'')<br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'')<br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' < E<sub>n</sub> (same as ''R(t-1)'' < ''U<sub>n</sub>(t-1)'')<br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' > -E<sub>n</sub> (same as ''R(t-1)'' > ''L<sub>n</sub>(t-1)'')<br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>x</sub><br />
** when using bands, it is the same as having ''R(t)'' < ''U<sub>n</sub>(t)'' '''and''' ''R(t-1)'' >= ''U<sub>n</sub>(t-1)'' '''and''' ''R(t)'' > ''U<sub>x</sub>(t)''<br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>x</sub><br />
** when using bands, it is the same as having ''R(t)'' > ''L<sub>n</sub>(t)'' '''and''' ''R(t-1)'' <= ''L<sub>n</sub>(t-1)'' '''and''' ''R(t)'' < ''L<sub>x</sub>(t)''<br />
<br />
Why do we have the '''simple entry mode'''? In normal situations and backtests, it gives same results as the '''uptick mode'''. But the difference comes up while trading multiple pairs in portfolio. The simple mode allows you to jump in the position immediately after a new slot is freed, regardless of the previous Z-scores.<br />
<br />
'''Which entry mode is better?''' Hard to tell, sometimes the uptick, sometimes the downtick. You have to do your homework and decide, which idea suits your trading style better. In general, uptick/simple mode is more aggressive, as it does not wait for first signs of spread mean reversion.<br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub> (equivalent to ''R(t)'' <= ''U<sub>x</sub>(t)'')<br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub> (equivalent to ''R(t)'' >= ''L<sub>x</sub>(t)'')<br />
<br />
=== Moving Average Types ===<br />
[[File:Rsi-illustration.png|right|650px|thumb|Ratio RSI Trading Example]]<br />
You can choose from these moving average algorithms:<br />
* [http://www.investopedia.com/terms/s/sma.asp Simple Moving Average] (SMA)<br />
* [http://www.investopedia.com/terms/e/ema.asp Exponential Moving Average] (EMA)<br />
* [http://fxtrade.oanda.com/learn/forex-indicators/weighted-moving-average Weighted Moving Average] (WMA)<br />
* [http://www.investopedia.com/terms/d/double-exponential-moving-average.asp Double Exponential Moving Average] (DEMA)<br />
* [http://www.investopedia.com/terms/t/triple-exponential-moving-average.asp Triple Exponential Moving Average] (TEMA)<br />
* [http://daytrading.about.com/od/indicators/a/Triangular.htm Triangular Moving Average] (TMA)<br />
* [http://etfhq.com/blog/2011/11/07/adaptive-moving-average-ama-aka-kaufman-adaptive-moving-average-kama Kaufman Adaptive Moving Average] (KAMA)<br />
* [http://www.mesasoftware.com/Papers/MAMA.pdf MESA Adaptive Moving Average] (MAMA)<br />
* [http://www.tradeescort.com/AgenaIndicatorHelp/AgenaIndicator.html?T3TripleExponentialMovingAverage.html Triple Exponential T3 Moving Average]<br />
<br />
Which one is the best? It depends, you have to test for yourself. They mostly differ in the term of memory and how fast they react to changes. Industry standard and the default is EMA. We suggest to try all of them on some sample pair to see how they work.<br />
<br />
=== RSI Filtering ===<br />
You can combine the Z-score/Bollinger Band model with [http://www.investopedia.com/terms/r/rsi.asp RSI indicator] applied on the ratio ''R(t)''. RSI filter will be automatically enabled if you set RSI threshold to other value than zero. RSI filter is combined with the Z-score rules using '''AND''' operator ('''both''' entry rule and RSI entry rule must be true to open a position).<br />
<br />
You can also change the RSI period if you want (default period = 15).<br />
<br />
RSI Threshold is value between 0 and 50. Because RSI indicator value oscillates between 0 and 100 (where 50 = mean), your threshold value is just used to set real thresholds for RSI:<br />
* let's assume RSI Period P<sub>r</sub> was entered<br />
* effective RSI value threshold is then 50+P<sub>r</sub> (upper) and 50-P<sub>r</sub> (lower)<br />
* you can see both thresholds in the example image at the right side<br />
<br />
Example:<br />
* RSI Threshold entered is 15<br />
* then, short positions are only opened, if RSI >= 65 (50+15)<br />
* long positions are only opened, if RSI <=35 (50-15)<br />
<br />
Useful hint: if you want to control entry rules just by RSI, you can set Entry Mode to simple and Entry Threshold to some low value.<br />
<br />
== Residual Model ==<br />
[[File:Residual-illustration2.png|right|650px|thumb|Residual Downtick Example]]<br />
Residual mode is based on [http://en.wikipedia.org/wiki/Linear_regression linear regression]. In literature it has been also referred to as the '''cointegration approach'''. Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the OLS method (Ordinary Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
This particular implementation is very simple. The regression is constructed using floating window of a fixed period, the same period is used for calculating the standard deviation.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (uptick and downtick modes are supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <15, 300><br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* first we need to construct a linear regression between ''A(t)'', ''B(t)'' using OLS, where ''A(t)'' = ''&beta;'' * ''B(t)'' + ''&alpha;'' + ''R(t)''<br />
* because we use floating window of period P (we calculate new regression each day), we actually get new series ''&beta;(t)'', ''&alpha;(t)'', ''R(t)'', where ''&beta;(t)'', ''&alpha;(t)'' are series of regression coefficients and ''R(t)'' are residuals (prediction errors)<br />
* ''R(t)'' = ''A(t)'' - (''&beta;(t)'' * ''B(t)'' + ''&alpha;(t)'')<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>x</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>x</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman Model ==<br />
This model is based on [http://en.wikipedia.org/wiki/Kalman_filter Kalman Filter]. In this case, the filter is used here instead of linear regression to determine proper hedge ratio, deviation from the mean and the standard deviation of the spread. The advantage is that this filter is superior when dealing with noise compared to OLS or TLS methods, also it does not have any lookback period to optimize.<br />
The disadvantage is that is has another parameters (like &delta;) to find out.<br />
<br />
We strongly recommend to read more details about Kalman filter applications in pair trading in [http://www.amazon.com/Algorithmic-Trading-Winning-Strategies-Rationale/dp/1118460146 this book].<br />
<br />
While Kalman Filter also estimates the standard deviation on the fly, our implementation also supports a possibility of having an auxiliary standard deviation indicator applied directly on the forecast error time series. The advantage is that you get less sensitivity on the &delta; parameter, but on the other hand a new lookback parameter is introduced.<br />
<br />
'''Kalman Model has too sensitive parameters which prevents it to be traded in reality. This model is provided only for education purposes - to be able to see how parameters affects its performance. Use Kalman Grid model for any serious trading.'''<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not planned'''<br />
* in PTL Trader: '''not planned'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter transition covariance &delta;, typical value is 0.0001 - '''unfortunately this model is very sensitive to this parameter especially when using standard deviation estimate coming from the Kalman filter itself'''<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* Auxiliary standard deviation period - if equal to zero, Kalman filter is used to estimate standard deviation, if non-zero, auxiliary standard deviation indicator with this period is used (an the estimate from Kalman filter is ignored)<br />
* Unstable period = how many Kalman filter observations are ignored at the beginning (first observations are quite unstable)<br />
<br />
'''This model is provided for education purposes only.'''<br />
<br />
== Kalman-grid Model (v2) ==<br />
After one year of experience with the [[Pair Trading Models#Kalman-grid Model (v1)|Kalman-grid Model (v1)]] and researching [https://en.wikipedia.org/wiki/Machine_learning machine learning principles], we have spent weeks of additional work on developing new version of Kalman Grid model, redesigned from scratch. Kalman Grid v2 is now the most advanced model PTL offers and has no competition whatsoever. After proven as stable it will become the default model for the whole PTL ecosystem.<br />
<br />
This model brings the superior performance of Kalman filter design, but it also deals with all the flaws of Kalman and old Kalman Grid v1:<br />
* all parameters of Kalman filter have been eliminated (&delta;, V<sub>e</sub>) - they are auto-estimated by the grid logic and updated with each price sample<br />
* increased average performance compared to v1<br />
* fixed issues of old version which prevented model to trade too often (biggest flaw of v1)<br />
<br />
This model rendered the version 1 obsolete and completely replaces it.<br />
<br />
How does it work? Instead of single strategy (and single Kalman filter), the whole grid of Kalman filters is evaluated by a proprietary system at the same time. Then, machine learning principles are applied to rank particular Kalman filter performances. Another algorithm is then used to figure filter outputs (slope, intercept, standard deviation) from the whole grid. The whole system actually behaves like a single Kalman filter, but with no need to provide any parameters.<br />
<br />
As a side effect, the entry threshold is also auto-optimized by the grid system itself. '''This means that the entry threshold is always indicated as 1.0.'''<br />
<br />
Main advantages of this model:<br />
* it is robust (because there are no parameters to tune)<br />
* beats any other models in performance (in average)<br />
* no risk of over-fitting in parameter optimization (nothing to optimize)<br />
<br />
To give some insight on performance: we took 300 best performing pairs from PTL database in period Jan 2013 - Jan 2016. Then we backtested all 300 pairs using '''out-of-sample''' period (Jan 2016 - Sep 2016) and we compared their performance. Models used: Ratio(per 14), Residual(20), Kalman Grid v1, Kalman Grid v2 using exit threshold of -1 (normal) and 0 (aggressive). Margin 50% (Req-T). Results here:<br />
{|<br />
|'''Model'''<br />
|'''Median CAGR %'''<br />
|'''Mean CAGR %'''<br />
|-<br />
|Kalman-grid v2 (normal)<br />
|'''7.206409'''<br />
|9.538509<br />
|-<br />
|Kalman-grid v2 (aggressive)<br />
|7.088950<br />
|'''11.433199'''<br />
|-<br />
|Kalman-grid v1<br />
|4.877391<br />
|10.794117<br />
|-<br />
|Residual(20)<br />
|3.596212<br />
|6.631089<br />
|-<br />
|Ratio(14)<br />
|3.590883<br />
|8.954484 <br />
|}<br />
<br />
'''You can clearly see how this model beats other models in out-of-sample performance (mixture of good and bad pairs).'''<br />
<br />
Profit distribution over all models (300 pairs, out-of-sample):<br />
[[File:Rplot1.png|center]]<br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''planned Dec 2016'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* exit threshold E<sub>x</sub> for Z-score, allowed value is <-1, 0> - this is the only parameter left to fine-tune. It determines how aggressively the model will trade: 0 means that trades are closed each time the forecast error crosses zero point (most aggressive mode, many trades), -1 means that trades are closed when the opposite band is crossed (less aggressive, less trades). -1 is recommended as it is more robust, 0 can boost performance at some well cointegrated pairs.<br />
<br />
= Deprecated Models =<br />
These models are deprecated and no longer supported:<br />
== Orthogonal Model ==<br />
This model is similar to Residual model, but it uses [http://en.wikipedia.org/wiki/Total_least_squares orthogonal regression/total least squares] instead of OLS. In addition, it has some extra features.<br />
Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the TLS method (Total Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
The regression is constructed using floating window of a fixed period. Standard deviation indicator is then applied to regression residuals (it can have a different period).<br />
<br />
=== Model Support ===<br />
* no longer supported<br />
<br />
=== Model Neutrality ===<br />
We support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes. In beta neutral mode, we determine the hedge ratio from TLS regression too, but using significantly longer floating period (240 or more).<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* entry threshold E<sub>nd</sub> for Z-score, typical value range is slightly less than E<sub>n</sub> - used as another band for downtick mode only<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <50, 300><br />
* standard deviation period, typical range <25, 100><br />
* entry mode (simple, uptick, downtick)<br />
<br />
(section still under construction)<br />
<br />
'''Important: TLS is numerically far less stable than OLS, especially for floating-window case with short periods. For this model, you should use periods of 40 or more. But even in case that TLS provides unstable results (you can see it as spikes in &beta;), residuals are still stable.'''<br />
<br />
=== Description ===<br />
* see the math behind the regression itself [http://en.wikipedia.org/wiki/Deming_regression here] (Deming regression is a 2D case of TLS)<br />
* from the regression, we get coefficients ''&beta;'', ''&alpha;'' and orthogonal residuals ''R(t)''<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>nd</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>nd</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman-grid Model (v1) ==<br />
This model follows up on the Kalman model and it tries to fix its biggest flaw - sensitivity to &delta; parameter. How? By executing '''whole grid''' of Kalman filters with different &delta; settings at the same time.<br />
<br />
Then it evaluates separate backtests of underlying Kalman strategies and from their measures it creates weights for calculating &alpha;, &beta; and standard deviation. This process goes on incrementally (at each price observation!).<br />
<br />
'''This model has been deprecated in favor of Kalman Grid V2'''<br />
<br />
=== Model Support ===<br />
* no longer supported<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman Grid filter observation covariance V<sub>e</sub>, typical value is 0.001</div>Adminhttps://wiki.pairtradinglab.com/wiki/Pair_Trading_ModelsPair Trading Models2016-09-15T13:00:18Z<p>Admin: /* Kalman-grid Model (v2) */</p>
<hr />
<div>Pair Trading Lab offers pair trading algorithms based on various mathematical models. These are models currently supported in PTL:<br />
== Ratio Model ==<br />
[[File:Ratio-illustration.png|right|650px|thumb|Ratio Uptick Example]]<br />
This is one of the standard pair trading models described in literature. It is based in ratio of instrument prices, [http://en.wikipedia.org/wiki/Moving_average moving average] and [http://en.wikipedia.org/wiki/Standard_deviation standard deviation]. In other words, it is based on [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator.<br />
<br />
Since Nov 27th 2014, this model also supports additional [http://www.investopedia.com/terms/r/rsi.asp RSI filter] you can use in addition to Bollinger Bands method.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (RSI supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.5, 2.5>, 2.0 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* moving average period P<sub>m</sub> (typical range <10, 100>), default = 15<br />
* moving average type T (algorithm), default = exponential<br />
* standard deviation period P<sub>s</sub> (typical range <10, 100>), default = 15<br />
* entry mode (simple, uptick, downtick)<br />
* RSI period and threshold (optional RSI filtering)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* we need to calculate ratio time series ''R(t)'' = ''A(t)'' / ''B(t)''<br />
* let's apply moving average of type T with period P<sub>m</sub> on ''R(t)'' to get time series ''M(t)''<br />
* let's apply standard deviation with period P<sub>s</sub> on ''R(t)'' to get time series ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = (''R(t)'' - ''M(t)'') / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
* another common approach (to visualize) is to create bands and put it above the moving average ''M(t)'':<br />
** upper entry band ''U<sub>n</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>n</sub><br />
** lower entry band ''L<sub>n</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>n</sub><br />
** upper exit band ''U<sub>x</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>x</sub><br />
** lower exit band ''L<sub>x</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>x</sub><br />
** these bands are actually the same bands as in [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator and we can use crossing of ''R(t)'' and bands as trade signals<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'')<br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'')<br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' < E<sub>n</sub> (same as ''R(t-1)'' < ''U<sub>n</sub>(t-1)'')<br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' > -E<sub>n</sub> (same as ''R(t-1)'' > ''L<sub>n</sub>(t-1)'')<br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>x</sub><br />
** when using bands, it is the same as having ''R(t)'' < ''U<sub>n</sub>(t)'' '''and''' ''R(t-1)'' >= ''U<sub>n</sub>(t-1)'' '''and''' ''R(t)'' > ''U<sub>x</sub>(t)''<br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>x</sub><br />
** when using bands, it is the same as having ''R(t)'' > ''L<sub>n</sub>(t)'' '''and''' ''R(t-1)'' <= ''L<sub>n</sub>(t-1)'' '''and''' ''R(t)'' < ''L<sub>x</sub>(t)''<br />
<br />
Why do we have the '''simple entry mode'''? In normal situations and backtests, it gives same results as the '''uptick mode'''. But the difference comes up while trading multiple pairs in portfolio. The simple mode allows you to jump in the position immediately after a new slot is freed, regardless of the previous Z-scores.<br />
<br />
'''Which entry mode is better?''' Hard to tell, sometimes the uptick, sometimes the downtick. You have to do your homework and decide, which idea suits your trading style better. In general, uptick/simple mode is more aggressive, as it does not wait for first signs of spread mean reversion.<br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub> (equivalent to ''R(t)'' <= ''U<sub>x</sub>(t)'')<br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub> (equivalent to ''R(t)'' >= ''L<sub>x</sub>(t)'')<br />
<br />
=== Moving Average Types ===<br />
[[File:Rsi-illustration.png|right|650px|thumb|Ratio RSI Trading Example]]<br />
You can choose from these moving average algorithms:<br />
* [http://www.investopedia.com/terms/s/sma.asp Simple Moving Average] (SMA)<br />
* [http://www.investopedia.com/terms/e/ema.asp Exponential Moving Average] (EMA)<br />
* [http://fxtrade.oanda.com/learn/forex-indicators/weighted-moving-average Weighted Moving Average] (WMA)<br />
* [http://www.investopedia.com/terms/d/double-exponential-moving-average.asp Double Exponential Moving Average] (DEMA)<br />
* [http://www.investopedia.com/terms/t/triple-exponential-moving-average.asp Triple Exponential Moving Average] (TEMA)<br />
* [http://daytrading.about.com/od/indicators/a/Triangular.htm Triangular Moving Average] (TMA)<br />
* [http://etfhq.com/blog/2011/11/07/adaptive-moving-average-ama-aka-kaufman-adaptive-moving-average-kama Kaufman Adaptive Moving Average] (KAMA)<br />
* [http://www.mesasoftware.com/Papers/MAMA.pdf MESA Adaptive Moving Average] (MAMA)<br />
* [http://www.tradeescort.com/AgenaIndicatorHelp/AgenaIndicator.html?T3TripleExponentialMovingAverage.html Triple Exponential T3 Moving Average]<br />
<br />
Which one is the best? It depends, you have to test for yourself. They mostly differ in the term of memory and how fast they react to changes. Industry standard and the default is EMA. We suggest to try all of them on some sample pair to see how they work.<br />
<br />
=== RSI Filtering ===<br />
You can combine the Z-score/Bollinger Band model with [http://www.investopedia.com/terms/r/rsi.asp RSI indicator] applied on the ratio ''R(t)''. RSI filter will be automatically enabled if you set RSI threshold to other value than zero. RSI filter is combined with the Z-score rules using '''AND''' operator ('''both''' entry rule and RSI entry rule must be true to open a position).<br />
<br />
You can also change the RSI period if you want (default period = 15).<br />
<br />
RSI Threshold is value between 0 and 50. Because RSI indicator value oscillates between 0 and 100 (where 50 = mean), your threshold value is just used to set real thresholds for RSI:<br />
* let's assume RSI Period P<sub>r</sub> was entered<br />
* effective RSI value threshold is then 50+P<sub>r</sub> (upper) and 50-P<sub>r</sub> (lower)<br />
* you can see both thresholds in the example image at the right side<br />
<br />
Example:<br />
* RSI Threshold entered is 15<br />
* then, short positions are only opened, if RSI >= 65 (50+15)<br />
* long positions are only opened, if RSI <=35 (50-15)<br />
<br />
Useful hint: if you want to control entry rules just by RSI, you can set Entry Mode to simple and Entry Threshold to some low value.<br />
<br />
== Residual Model ==<br />
[[File:Residual-illustration2.png|right|650px|thumb|Residual Downtick Example]]<br />
Residual mode is based on [http://en.wikipedia.org/wiki/Linear_regression linear regression]. In literature it has been also referred to as the '''cointegration approach'''. Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the OLS method (Ordinary Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
This particular implementation is very simple. The regression is constructed using floating window of a fixed period, the same period is used for calculating the standard deviation.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (uptick and downtick modes are supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <15, 300><br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* first we need to construct a linear regression between ''A(t)'', ''B(t)'' using OLS, where ''A(t)'' = ''&beta;'' * ''B(t)'' + ''&alpha;'' + ''R(t)''<br />
* because we use floating window of period P (we calculate new regression each day), we actually get new series ''&beta;(t)'', ''&alpha;(t)'', ''R(t)'', where ''&beta;(t)'', ''&alpha;(t)'' are series of regression coefficients and ''R(t)'' are residuals (prediction errors)<br />
* ''R(t)'' = ''A(t)'' - (''&beta;(t)'' * ''B(t)'' + ''&alpha;(t)'')<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>x</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>x</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman Model ==<br />
This model is based on [http://en.wikipedia.org/wiki/Kalman_filter Kalman Filter]. In this case, the filter is used here instead of linear regression to determine proper hedge ratio, deviation from the mean and the standard deviation of the spread. The advantage is that this filter is superior when dealing with noise compared to OLS or TLS methods, also it does not have any lookback period to optimize.<br />
The disadvantage is that is has another parameters (like &delta;) to find out.<br />
<br />
We strongly recommend to read more details about Kalman filter applications in pair trading in [http://www.amazon.com/Algorithmic-Trading-Winning-Strategies-Rationale/dp/1118460146 this book].<br />
<br />
While Kalman Filter also estimates the standard deviation on the fly, our implementation also supports a possibility of having an auxiliary standard deviation indicator applied directly on the forecast error time series. The advantage is that you get less sensitivity on the &delta; parameter, but on the other hand a new lookback parameter is introduced.<br />
<br />
'''Kalman Model has too sensitive parameters which prevents it to be traded in reality. This model is provided only for education purposes - to be able to see how parameters affects its performance. Use Kalman Grid model for any serious trading.'''<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not planned'''<br />
* in PTL Trader: '''not planned'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter transition covariance &delta;, typical value is 0.0001 - '''unfortunately this model is very sensitive to this parameter especially when using standard deviation estimate coming from the Kalman filter itself'''<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* Auxiliary standard deviation period - if equal to zero, Kalman filter is used to estimate standard deviation, if non-zero, auxiliary standard deviation indicator with this period is used (an the estimate from Kalman filter is ignored)<br />
* Unstable period = how many Kalman filter observations are ignored at the beginning (first observations are quite unstable)<br />
<br />
'''This model is provided for education purposes only.'''<br />
<br />
== Kalman-grid Model (v2) ==<br />
After one year of experience with the [[Pair Trading Models#Kalman-grid Model (v1)|Kalman-grid Model (v1)]] and researching [https://en.wikipedia.org/wiki/Machine_learning machine learning principles], we have spent weeks of additional work on developing new version of Kalman Grid model, redesigned from scratch. Kalman Grid v2 is now the most advanced model PTL offers and has no competition whatsoever. After proven as stable it will become the default model for the whole PTL ecosystem.<br />
<br />
This model brings the superior performance of Kalman filter design, but it also deals with all the flaws of Kalman and old Kalman Grid v1:<br />
* all parameters of Kalman filter have been eliminated (&delta;, V<sub>e</sub>) - they are auto-estimated by the grid logic and updated with each price sample<br />
* increased average performance compared to v1<br />
* fixed issues of old version which prevented model to trade too often (biggest flaw of v1)<br />
<br />
This model rendered the version 1 obsolete and completely replaces it.<br />
<br />
How does it work? Instead of single strategy (and single Kalman filter), the whole grid of Kalman filters is evaluated by a proprietary system at the same time. Then, machine learning principles are applied to rank particular Kalman filter performances. Another algorithm is then used to figure filter outputs (slope, intercept, standard deviation) from the whole grid. The whole system actually behaves like a single Kalman filter, but with no need to provide any parameters.<br />
<br />
As a side effect, the entry threshold is also auto-optimized by the grid system itself. '''This means that the entry threshold is always indicated as 1.0.'''<br />
<br />
Main advantages of this model:<br />
* it is robust (because there are no parameters to tune)<br />
* beats any other models in performance (in average)<br />
* no risk of over-fitting in parameter optimization (nothing to optimize)<br />
<br />
To give some insight on performance: we took 300 best performing pairs from PTL database in period Jan 2013 - Jan 2016. Then we backtested all 300 pairs using '''out-of-sample''' period (Jan 2016 - Sep 2016) and we compared their performance. Models used: Ratio(per 14), Residual(20), Kalman Grid v1, Kalman Grid v2 using exit threshold of -1 (normal) and 0 (aggressive). Margin 50% (Req-T). Results here:<br />
{|<br />
|'''Model'''<br />
|'''Median CAGR %'''<br />
|-<br />
|Kalman-grid v2 (normal)<br />
|'''7.206409'''<br />
|-<br />
|Kalman-grid v2 (aggressive)<br />
|7.088950<br />
|-<br />
|Kalman-grid v1<br />
|4.877391<br />
|-<br />
|Residual(20)<br />
|3.596212<br />
|-<br />
|Ratio(14)<br />
|3.590883 <br />
|}<br />
<br />
'''You can clearly see how this model beats other models in out-of-sample performance (mixture of good and bad pairs).'''<br />
<br />
Profit distribution over all models (300 pairs, out-of-sample):<br />
[[File:Rplot1.png|center]]<br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''planned Dec 2016'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* exit threshold E<sub>x</sub> for Z-score, allowed value is <-1, 0> - this is the only parameter left to fine-tune. It determines how aggressively the model will trade: 0 means that trades are closed each time the forecast error crosses zero point (most aggressive mode, many trades), -1 means that trades are closed when the opposite band is crossed (less aggressive, less trades). -1 is recommended as it is more robust, 0 can boost performance at some well cointegrated pairs.<br />
<br />
= Deprecated Models =<br />
These models are deprecated and no longer supported:<br />
== Orthogonal Model ==<br />
This model is similar to Residual model, but it uses [http://en.wikipedia.org/wiki/Total_least_squares orthogonal regression/total least squares] instead of OLS. In addition, it has some extra features.<br />
Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the TLS method (Total Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
The regression is constructed using floating window of a fixed period. Standard deviation indicator is then applied to regression residuals (it can have a different period).<br />
<br />
=== Model Support ===<br />
* no longer supported<br />
<br />
=== Model Neutrality ===<br />
We support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes. In beta neutral mode, we determine the hedge ratio from TLS regression too, but using significantly longer floating period (240 or more).<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* entry threshold E<sub>nd</sub> for Z-score, typical value range is slightly less than E<sub>n</sub> - used as another band for downtick mode only<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <50, 300><br />
* standard deviation period, typical range <25, 100><br />
* entry mode (simple, uptick, downtick)<br />
<br />
(section still under construction)<br />
<br />
'''Important: TLS is numerically far less stable than OLS, especially for floating-window case with short periods. For this model, you should use periods of 40 or more. But even in case that TLS provides unstable results (you can see it as spikes in &beta;), residuals are still stable.'''<br />
<br />
=== Description ===<br />
* see the math behind the regression itself [http://en.wikipedia.org/wiki/Deming_regression here] (Deming regression is a 2D case of TLS)<br />
* from the regression, we get coefficients ''&beta;'', ''&alpha;'' and orthogonal residuals ''R(t)''<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>nd</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>nd</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman-grid Model (v1) ==<br />
This model follows up on the Kalman model and it tries to fix its biggest flaw - sensitivity to &delta; parameter. How? By executing '''whole grid''' of Kalman filters with different &delta; settings at the same time.<br />
<br />
Then it evaluates separate backtests of underlying Kalman strategies and from their measures it creates weights for calculating &alpha;, &beta; and standard deviation. This process goes on incrementally (at each price observation!).<br />
<br />
'''This model has been deprecated in favor of Kalman Grid V2'''<br />
<br />
=== Model Support ===<br />
* no longer supported<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman Grid filter observation covariance V<sub>e</sub>, typical value is 0.001</div>Adminhttps://wiki.pairtradinglab.com/wiki/File:Rplot1.pngFile:Rplot1.png2016-09-15T12:57:05Z<p>Admin: </p>
<hr />
<div></div>Adminhttps://wiki.pairtradinglab.com/wiki/Pair_Trading_ModelsPair Trading Models2016-09-15T12:53:14Z<p>Admin: /* Kalman-grid Model (v2) */</p>
<hr />
<div>Pair Trading Lab offers pair trading algorithms based on various mathematical models. These are models currently supported in PTL:<br />
== Ratio Model ==<br />
[[File:Ratio-illustration.png|right|650px|thumb|Ratio Uptick Example]]<br />
This is one of the standard pair trading models described in literature. It is based in ratio of instrument prices, [http://en.wikipedia.org/wiki/Moving_average moving average] and [http://en.wikipedia.org/wiki/Standard_deviation standard deviation]. In other words, it is based on [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator.<br />
<br />
Since Nov 27th 2014, this model also supports additional [http://www.investopedia.com/terms/r/rsi.asp RSI filter] you can use in addition to Bollinger Bands method.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (RSI supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.5, 2.5>, 2.0 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* moving average period P<sub>m</sub> (typical range <10, 100>), default = 15<br />
* moving average type T (algorithm), default = exponential<br />
* standard deviation period P<sub>s</sub> (typical range <10, 100>), default = 15<br />
* entry mode (simple, uptick, downtick)<br />
* RSI period and threshold (optional RSI filtering)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* we need to calculate ratio time series ''R(t)'' = ''A(t)'' / ''B(t)''<br />
* let's apply moving average of type T with period P<sub>m</sub> on ''R(t)'' to get time series ''M(t)''<br />
* let's apply standard deviation with period P<sub>s</sub> on ''R(t)'' to get time series ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = (''R(t)'' - ''M(t)'') / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
* another common approach (to visualize) is to create bands and put it above the moving average ''M(t)'':<br />
** upper entry band ''U<sub>n</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>n</sub><br />
** lower entry band ''L<sub>n</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>n</sub><br />
** upper exit band ''U<sub>x</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>x</sub><br />
** lower exit band ''L<sub>x</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>x</sub><br />
** these bands are actually the same bands as in [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator and we can use crossing of ''R(t)'' and bands as trade signals<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'')<br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'')<br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' < E<sub>n</sub> (same as ''R(t-1)'' < ''U<sub>n</sub>(t-1)'')<br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' > -E<sub>n</sub> (same as ''R(t-1)'' > ''L<sub>n</sub>(t-1)'')<br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>x</sub><br />
** when using bands, it is the same as having ''R(t)'' < ''U<sub>n</sub>(t)'' '''and''' ''R(t-1)'' >= ''U<sub>n</sub>(t-1)'' '''and''' ''R(t)'' > ''U<sub>x</sub>(t)''<br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>x</sub><br />
** when using bands, it is the same as having ''R(t)'' > ''L<sub>n</sub>(t)'' '''and''' ''R(t-1)'' <= ''L<sub>n</sub>(t-1)'' '''and''' ''R(t)'' < ''L<sub>x</sub>(t)''<br />
<br />
Why do we have the '''simple entry mode'''? In normal situations and backtests, it gives same results as the '''uptick mode'''. But the difference comes up while trading multiple pairs in portfolio. The simple mode allows you to jump in the position immediately after a new slot is freed, regardless of the previous Z-scores.<br />
<br />
'''Which entry mode is better?''' Hard to tell, sometimes the uptick, sometimes the downtick. You have to do your homework and decide, which idea suits your trading style better. In general, uptick/simple mode is more aggressive, as it does not wait for first signs of spread mean reversion.<br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub> (equivalent to ''R(t)'' <= ''U<sub>x</sub>(t)'')<br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub> (equivalent to ''R(t)'' >= ''L<sub>x</sub>(t)'')<br />
<br />
=== Moving Average Types ===<br />
[[File:Rsi-illustration.png|right|650px|thumb|Ratio RSI Trading Example]]<br />
You can choose from these moving average algorithms:<br />
* [http://www.investopedia.com/terms/s/sma.asp Simple Moving Average] (SMA)<br />
* [http://www.investopedia.com/terms/e/ema.asp Exponential Moving Average] (EMA)<br />
* [http://fxtrade.oanda.com/learn/forex-indicators/weighted-moving-average Weighted Moving Average] (WMA)<br />
* [http://www.investopedia.com/terms/d/double-exponential-moving-average.asp Double Exponential Moving Average] (DEMA)<br />
* [http://www.investopedia.com/terms/t/triple-exponential-moving-average.asp Triple Exponential Moving Average] (TEMA)<br />
* [http://daytrading.about.com/od/indicators/a/Triangular.htm Triangular Moving Average] (TMA)<br />
* [http://etfhq.com/blog/2011/11/07/adaptive-moving-average-ama-aka-kaufman-adaptive-moving-average-kama Kaufman Adaptive Moving Average] (KAMA)<br />
* [http://www.mesasoftware.com/Papers/MAMA.pdf MESA Adaptive Moving Average] (MAMA)<br />
* [http://www.tradeescort.com/AgenaIndicatorHelp/AgenaIndicator.html?T3TripleExponentialMovingAverage.html Triple Exponential T3 Moving Average]<br />
<br />
Which one is the best? It depends, you have to test for yourself. They mostly differ in the term of memory and how fast they react to changes. Industry standard and the default is EMA. We suggest to try all of them on some sample pair to see how they work.<br />
<br />
=== RSI Filtering ===<br />
You can combine the Z-score/Bollinger Band model with [http://www.investopedia.com/terms/r/rsi.asp RSI indicator] applied on the ratio ''R(t)''. RSI filter will be automatically enabled if you set RSI threshold to other value than zero. RSI filter is combined with the Z-score rules using '''AND''' operator ('''both''' entry rule and RSI entry rule must be true to open a position).<br />
<br />
You can also change the RSI period if you want (default period = 15).<br />
<br />
RSI Threshold is value between 0 and 50. Because RSI indicator value oscillates between 0 and 100 (where 50 = mean), your threshold value is just used to set real thresholds for RSI:<br />
* let's assume RSI Period P<sub>r</sub> was entered<br />
* effective RSI value threshold is then 50+P<sub>r</sub> (upper) and 50-P<sub>r</sub> (lower)<br />
* you can see both thresholds in the example image at the right side<br />
<br />
Example:<br />
* RSI Threshold entered is 15<br />
* then, short positions are only opened, if RSI >= 65 (50+15)<br />
* long positions are only opened, if RSI <=35 (50-15)<br />
<br />
Useful hint: if you want to control entry rules just by RSI, you can set Entry Mode to simple and Entry Threshold to some low value.<br />
<br />
== Residual Model ==<br />
[[File:Residual-illustration2.png|right|650px|thumb|Residual Downtick Example]]<br />
Residual mode is based on [http://en.wikipedia.org/wiki/Linear_regression linear regression]. In literature it has been also referred to as the '''cointegration approach'''. Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the OLS method (Ordinary Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
This particular implementation is very simple. The regression is constructed using floating window of a fixed period, the same period is used for calculating the standard deviation.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (uptick and downtick modes are supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <15, 300><br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* first we need to construct a linear regression between ''A(t)'', ''B(t)'' using OLS, where ''A(t)'' = ''&beta;'' * ''B(t)'' + ''&alpha;'' + ''R(t)''<br />
* because we use floating window of period P (we calculate new regression each day), we actually get new series ''&beta;(t)'', ''&alpha;(t)'', ''R(t)'', where ''&beta;(t)'', ''&alpha;(t)'' are series of regression coefficients and ''R(t)'' are residuals (prediction errors)<br />
* ''R(t)'' = ''A(t)'' - (''&beta;(t)'' * ''B(t)'' + ''&alpha;(t)'')<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>x</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>x</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman Model ==<br />
This model is based on [http://en.wikipedia.org/wiki/Kalman_filter Kalman Filter]. In this case, the filter is used here instead of linear regression to determine proper hedge ratio, deviation from the mean and the standard deviation of the spread. The advantage is that this filter is superior when dealing with noise compared to OLS or TLS methods, also it does not have any lookback period to optimize.<br />
The disadvantage is that is has another parameters (like &delta;) to find out.<br />
<br />
We strongly recommend to read more details about Kalman filter applications in pair trading in [http://www.amazon.com/Algorithmic-Trading-Winning-Strategies-Rationale/dp/1118460146 this book].<br />
<br />
While Kalman Filter also estimates the standard deviation on the fly, our implementation also supports a possibility of having an auxiliary standard deviation indicator applied directly on the forecast error time series. The advantage is that you get less sensitivity on the &delta; parameter, but on the other hand a new lookback parameter is introduced.<br />
<br />
'''Kalman Model has too sensitive parameters which prevents it to be traded in reality. This model is provided only for education purposes - to be able to see how parameters affects its performance. Use Kalman Grid model for any serious trading.'''<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not planned'''<br />
* in PTL Trader: '''not planned'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter transition covariance &delta;, typical value is 0.0001 - '''unfortunately this model is very sensitive to this parameter especially when using standard deviation estimate coming from the Kalman filter itself'''<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* Auxiliary standard deviation period - if equal to zero, Kalman filter is used to estimate standard deviation, if non-zero, auxiliary standard deviation indicator with this period is used (an the estimate from Kalman filter is ignored)<br />
* Unstable period = how many Kalman filter observations are ignored at the beginning (first observations are quite unstable)<br />
<br />
'''This model is provided for education purposes only.'''<br />
<br />
== Kalman-grid Model (v2) ==<br />
After one year of experience with the [[Pair Trading Models#Kalman-grid Model (v1)|Kalman-grid Model (v1)]] and researching [https://en.wikipedia.org/wiki/Machine_learning machine learning principles], we have spent weeks of additional work on developing new version of Kalman Grid model, redesigned from scratch. Kalman Grid v2 is now the most advanced model PTL offers and has no competition whatsoever. After proven as stable it will become the default model for the whole PTL ecosystem.<br />
<br />
This model brings the superior performance of Kalman filter design, but it also deals with all the flaws of Kalman and old Kalman Grid v1:<br />
* all parameters of Kalman filter have been eliminated (&delta;, V<sub>e</sub>) - they are auto-estimated by the grid logic and updated with each price sample<br />
* increased average performance compared to v1<br />
* fixed issues of old version which prevented model to trade too often (biggest flaw of v1)<br />
<br />
This model rendered the version 1 obsolete and completely replaces it.<br />
<br />
How does it work? Instead of single strategy (and single Kalman filter), the whole grid of Kalman filters is evaluated by a proprietary system at the same time. Then, machine learning principles are applied to rank particular Kalman filter performances. Another algorithm is then used to figure filter outputs (slope, intercept, standard deviation) from the whole grid. The whole system actually behaves like a single Kalman filter, but with no need to provide any parameters.<br />
<br />
As a side effect, the entry threshold is also auto-optimized by the grid system itself. '''This means that the entry threshold is always indicated as 1.0.'''<br />
<br />
Main advantages of this model:<br />
* it is robust (because there are no parameters to tune)<br />
* beats any other models in performance (in average)<br />
* no risk of over-fitting in parameter optimization (nothing to optimize)<br />
<br />
To give some insight on performance: we took 300 best performing pairs from PTL database in period Jan 2013 - Jan 2016. Then we backtested all 300 pairs using '''out-of-sample''' period (Jan 2016 - Sep 2016) and we compared their performance. Models used: Ratio(per 14), Residual(20), Kalman Grid v1, Kalman Grid v2 using exit threshold of -1 (normal) and 0 (aggressive). Margin 50% (Req-T). Results here:<br />
{|<br />
|'''Model'''<br />
|'''Median CAGR %'''<br />
|-<br />
|Kalman-grid v2 (normal)<br />
|'''7.206409'''<br />
|-<br />
|Kalman-grid v2 (aggressive)<br />
|7.088950<br />
|-<br />
|Kalman-grid v1<br />
|4.877391<br />
|-<br />
|Residual(20)<br />
|3.596212<br />
|-<br />
|Ratio(14)<br />
|3.590883 <br />
|}<br />
<br />
'''You can clearly see how this model beats other models in out-of-sample performance (mixture of good and bad pairs).'''<br />
<br />
=== Model Support ===<br />
This model is currently in '''beta''' stage and requires more testing before it will be enabled in PTL Trader.<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''planned Dec 2016'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* exit threshold E<sub>x</sub> for Z-score, allowed value is <-1, 0> - this is the only parameter left to fine-tune. It determines how aggressively the model will trade: 0 means that trades are closed each time the forecast error crosses zero point (most aggressive mode, many trades), -1 means that trades are closed when the opposite band is crossed (less aggressive, less trades). -1 is recommended as it is more robust, 0 can boost performance at some well cointegrated pairs.<br />
<br />
= Deprecated Models =<br />
These models are deprecated and no longer supported:<br />
== Orthogonal Model ==<br />
This model is similar to Residual model, but it uses [http://en.wikipedia.org/wiki/Total_least_squares orthogonal regression/total least squares] instead of OLS. In addition, it has some extra features.<br />
Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the TLS method (Total Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
The regression is constructed using floating window of a fixed period. Standard deviation indicator is then applied to regression residuals (it can have a different period).<br />
<br />
=== Model Support ===<br />
* no longer supported<br />
<br />
=== Model Neutrality ===<br />
We support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes. In beta neutral mode, we determine the hedge ratio from TLS regression too, but using significantly longer floating period (240 or more).<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* entry threshold E<sub>nd</sub> for Z-score, typical value range is slightly less than E<sub>n</sub> - used as another band for downtick mode only<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <50, 300><br />
* standard deviation period, typical range <25, 100><br />
* entry mode (simple, uptick, downtick)<br />
<br />
(section still under construction)<br />
<br />
'''Important: TLS is numerically far less stable than OLS, especially for floating-window case with short periods. For this model, you should use periods of 40 or more. But even in case that TLS provides unstable results (you can see it as spikes in &beta;), residuals are still stable.'''<br />
<br />
=== Description ===<br />
* see the math behind the regression itself [http://en.wikipedia.org/wiki/Deming_regression here] (Deming regression is a 2D case of TLS)<br />
* from the regression, we get coefficients ''&beta;'', ''&alpha;'' and orthogonal residuals ''R(t)''<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>nd</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>nd</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman-grid Model (v1) ==<br />
This model follows up on the Kalman model and it tries to fix its biggest flaw - sensitivity to &delta; parameter. How? By executing '''whole grid''' of Kalman filters with different &delta; settings at the same time.<br />
<br />
Then it evaluates separate backtests of underlying Kalman strategies and from their measures it creates weights for calculating &alpha;, &beta; and standard deviation. This process goes on incrementally (at each price observation!).<br />
<br />
'''This model has been deprecated in favor of Kalman Grid V2'''<br />
<br />
=== Model Support ===<br />
* no longer supported<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman Grid filter observation covariance V<sub>e</sub>, typical value is 0.001</div>Adminhttps://wiki.pairtradinglab.com/wiki/Pair_Trading_ModelsPair Trading Models2016-09-15T12:32:17Z<p>Admin: </p>
<hr />
<div>Pair Trading Lab offers pair trading algorithms based on various mathematical models. These are models currently supported in PTL:<br />
== Ratio Model ==<br />
[[File:Ratio-illustration.png|right|650px|thumb|Ratio Uptick Example]]<br />
This is one of the standard pair trading models described in literature. It is based in ratio of instrument prices, [http://en.wikipedia.org/wiki/Moving_average moving average] and [http://en.wikipedia.org/wiki/Standard_deviation standard deviation]. In other words, it is based on [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator.<br />
<br />
Since Nov 27th 2014, this model also supports additional [http://www.investopedia.com/terms/r/rsi.asp RSI filter] you can use in addition to Bollinger Bands method.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (RSI supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.5, 2.5>, 2.0 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* moving average period P<sub>m</sub> (typical range <10, 100>), default = 15<br />
* moving average type T (algorithm), default = exponential<br />
* standard deviation period P<sub>s</sub> (typical range <10, 100>), default = 15<br />
* entry mode (simple, uptick, downtick)<br />
* RSI period and threshold (optional RSI filtering)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* we need to calculate ratio time series ''R(t)'' = ''A(t)'' / ''B(t)''<br />
* let's apply moving average of type T with period P<sub>m</sub> on ''R(t)'' to get time series ''M(t)''<br />
* let's apply standard deviation with period P<sub>s</sub> on ''R(t)'' to get time series ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = (''R(t)'' - ''M(t)'') / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
* another common approach (to visualize) is to create bands and put it above the moving average ''M(t)'':<br />
** upper entry band ''U<sub>n</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>n</sub><br />
** lower entry band ''L<sub>n</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>n</sub><br />
** upper exit band ''U<sub>x</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>x</sub><br />
** lower exit band ''L<sub>x</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>x</sub><br />
** these bands are actually the same bands as in [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator and we can use crossing of ''R(t)'' and bands as trade signals<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'')<br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'')<br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' < E<sub>n</sub> (same as ''R(t-1)'' < ''U<sub>n</sub>(t-1)'')<br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' > -E<sub>n</sub> (same as ''R(t-1)'' > ''L<sub>n</sub>(t-1)'')<br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>x</sub><br />
** when using bands, it is the same as having ''R(t)'' < ''U<sub>n</sub>(t)'' '''and''' ''R(t-1)'' >= ''U<sub>n</sub>(t-1)'' '''and''' ''R(t)'' > ''U<sub>x</sub>(t)''<br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>x</sub><br />
** when using bands, it is the same as having ''R(t)'' > ''L<sub>n</sub>(t)'' '''and''' ''R(t-1)'' <= ''L<sub>n</sub>(t-1)'' '''and''' ''R(t)'' < ''L<sub>x</sub>(t)''<br />
<br />
Why do we have the '''simple entry mode'''? In normal situations and backtests, it gives same results as the '''uptick mode'''. But the difference comes up while trading multiple pairs in portfolio. The simple mode allows you to jump in the position immediately after a new slot is freed, regardless of the previous Z-scores.<br />
<br />
'''Which entry mode is better?''' Hard to tell, sometimes the uptick, sometimes the downtick. You have to do your homework and decide, which idea suits your trading style better. In general, uptick/simple mode is more aggressive, as it does not wait for first signs of spread mean reversion.<br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub> (equivalent to ''R(t)'' <= ''U<sub>x</sub>(t)'')<br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub> (equivalent to ''R(t)'' >= ''L<sub>x</sub>(t)'')<br />
<br />
=== Moving Average Types ===<br />
[[File:Rsi-illustration.png|right|650px|thumb|Ratio RSI Trading Example]]<br />
You can choose from these moving average algorithms:<br />
* [http://www.investopedia.com/terms/s/sma.asp Simple Moving Average] (SMA)<br />
* [http://www.investopedia.com/terms/e/ema.asp Exponential Moving Average] (EMA)<br />
* [http://fxtrade.oanda.com/learn/forex-indicators/weighted-moving-average Weighted Moving Average] (WMA)<br />
* [http://www.investopedia.com/terms/d/double-exponential-moving-average.asp Double Exponential Moving Average] (DEMA)<br />
* [http://www.investopedia.com/terms/t/triple-exponential-moving-average.asp Triple Exponential Moving Average] (TEMA)<br />
* [http://daytrading.about.com/od/indicators/a/Triangular.htm Triangular Moving Average] (TMA)<br />
* [http://etfhq.com/blog/2011/11/07/adaptive-moving-average-ama-aka-kaufman-adaptive-moving-average-kama Kaufman Adaptive Moving Average] (KAMA)<br />
* [http://www.mesasoftware.com/Papers/MAMA.pdf MESA Adaptive Moving Average] (MAMA)<br />
* [http://www.tradeescort.com/AgenaIndicatorHelp/AgenaIndicator.html?T3TripleExponentialMovingAverage.html Triple Exponential T3 Moving Average]<br />
<br />
Which one is the best? It depends, you have to test for yourself. They mostly differ in the term of memory and how fast they react to changes. Industry standard and the default is EMA. We suggest to try all of them on some sample pair to see how they work.<br />
<br />
=== RSI Filtering ===<br />
You can combine the Z-score/Bollinger Band model with [http://www.investopedia.com/terms/r/rsi.asp RSI indicator] applied on the ratio ''R(t)''. RSI filter will be automatically enabled if you set RSI threshold to other value than zero. RSI filter is combined with the Z-score rules using '''AND''' operator ('''both''' entry rule and RSI entry rule must be true to open a position).<br />
<br />
You can also change the RSI period if you want (default period = 15).<br />
<br />
RSI Threshold is value between 0 and 50. Because RSI indicator value oscillates between 0 and 100 (where 50 = mean), your threshold value is just used to set real thresholds for RSI:<br />
* let's assume RSI Period P<sub>r</sub> was entered<br />
* effective RSI value threshold is then 50+P<sub>r</sub> (upper) and 50-P<sub>r</sub> (lower)<br />
* you can see both thresholds in the example image at the right side<br />
<br />
Example:<br />
* RSI Threshold entered is 15<br />
* then, short positions are only opened, if RSI >= 65 (50+15)<br />
* long positions are only opened, if RSI <=35 (50-15)<br />
<br />
Useful hint: if you want to control entry rules just by RSI, you can set Entry Mode to simple and Entry Threshold to some low value.<br />
<br />
== Residual Model ==<br />
[[File:Residual-illustration2.png|right|650px|thumb|Residual Downtick Example]]<br />
Residual mode is based on [http://en.wikipedia.org/wiki/Linear_regression linear regression]. In literature it has been also referred to as the '''cointegration approach'''. Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the OLS method (Ordinary Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
This particular implementation is very simple. The regression is constructed using floating window of a fixed period, the same period is used for calculating the standard deviation.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (uptick and downtick modes are supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <15, 300><br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* first we need to construct a linear regression between ''A(t)'', ''B(t)'' using OLS, where ''A(t)'' = ''&beta;'' * ''B(t)'' + ''&alpha;'' + ''R(t)''<br />
* because we use floating window of period P (we calculate new regression each day), we actually get new series ''&beta;(t)'', ''&alpha;(t)'', ''R(t)'', where ''&beta;(t)'', ''&alpha;(t)'' are series of regression coefficients and ''R(t)'' are residuals (prediction errors)<br />
* ''R(t)'' = ''A(t)'' - (''&beta;(t)'' * ''B(t)'' + ''&alpha;(t)'')<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>x</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>x</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman Model ==<br />
This model is based on [http://en.wikipedia.org/wiki/Kalman_filter Kalman Filter]. In this case, the filter is used here instead of linear regression to determine proper hedge ratio, deviation from the mean and the standard deviation of the spread. The advantage is that this filter is superior when dealing with noise compared to OLS or TLS methods, also it does not have any lookback period to optimize.<br />
The disadvantage is that is has another parameters (like &delta;) to find out.<br />
<br />
We strongly recommend to read more details about Kalman filter applications in pair trading in [http://www.amazon.com/Algorithmic-Trading-Winning-Strategies-Rationale/dp/1118460146 this book].<br />
<br />
While Kalman Filter also estimates the standard deviation on the fly, our implementation also supports a possibility of having an auxiliary standard deviation indicator applied directly on the forecast error time series. The advantage is that you get less sensitivity on the &delta; parameter, but on the other hand a new lookback parameter is introduced.<br />
<br />
'''Kalman Model has too sensitive parameters which prevents it to be traded in reality. This model is provided only for education purposes - to be able to see how parameters affects its performance. Use Kalman Grid model for any serious trading.'''<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not planned'''<br />
* in PTL Trader: '''not planned'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter transition covariance &delta;, typical value is 0.0001 - '''unfortunately this model is very sensitive to this parameter especially when using standard deviation estimate coming from the Kalman filter itself'''<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* Auxiliary standard deviation period - if equal to zero, Kalman filter is used to estimate standard deviation, if non-zero, auxiliary standard deviation indicator with this period is used (an the estimate from Kalman filter is ignored)<br />
* Unstable period = how many Kalman filter observations are ignored at the beginning (first observations are quite unstable)<br />
<br />
'''This model is provided for education purposes only.'''<br />
<br />
== Kalman-grid Model (v2) ==<br />
After one year of experience of testing the [[Pair Trading Models#Kalman-grid Model (v1)|Kalman-grid Model (v1)]] and researching [https://en.wikipedia.org/wiki/Machine_learning machine learning principles], we have spent few weeks of work on developing new version of Kalman Grid model, redesigned from scratch.<br />
<br />
This model brings the superior performance of Kalman filter design, but it also dealt with all the flaws:<br />
* all parameters of Kalman filter eliminated (&delta, V<sub>e</sub>) - they are auto-estimated by the grid logic on every step<br />
* increased average performance compared to old version<br />
* fixed issues of old version which prevented model to trade too often<br />
<br />
How does it work? Instead of single strategy (and single Kalman filter), the whole grid of Kalman filters is evaluated by a proprietary system at the same time. Then, machine learning principles are applied to rank particular Kalman filter performance. Another algorithm is then used to figure filter outputs (slope, intercept, standard deviation) from the whole grid. The whole grid actually behaves like a single Kalman filter, but with no need to provide any parameters.<br />
<br />
As a side effect, the entry threshold is also auto-optimized by the grid system itself.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes'''<br />
* in PTL portfolio backtester: '''planned Dec 2016'''<br />
* in PTL Trader: '''planned Dec 2016'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* exit threshold E<sub>x</sub> for Z-score, allowed value is <-1, 0> - this is the only parameter left to fine-tune. It determines how aggressively the model will trade: 0 means that trades are closed each time the forecast error crosses zero point (most aggressive mode, many trades), -1 means that trades are closed when the opposite band is crossed (less aggressive, less trades). -1 is recommended as it is more robust, 0 can boost performance at some well cointegrated pairs.<br />
<br />
= Deprecated Models =<br />
These models are deprecated and no longer supported:<br />
== Orthogonal Model ==<br />
This model is similar to Residual model, but it uses [http://en.wikipedia.org/wiki/Total_least_squares orthogonal regression/total least squares] instead of OLS. In addition, it has some extra features.<br />
Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the TLS method (Total Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
The regression is constructed using floating window of a fixed period. Standard deviation indicator is then applied to regression residuals (it can have a different period).<br />
<br />
=== Model Support ===<br />
* no longer supported<br />
<br />
=== Model Neutrality ===<br />
We support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes. In beta neutral mode, we determine the hedge ratio from TLS regression too, but using significantly longer floating period (240 or more).<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* entry threshold E<sub>nd</sub> for Z-score, typical value range is slightly less than E<sub>n</sub> - used as another band for downtick mode only<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <50, 300><br />
* standard deviation period, typical range <25, 100><br />
* entry mode (simple, uptick, downtick)<br />
<br />
(section still under construction)<br />
<br />
'''Important: TLS is numerically far less stable than OLS, especially for floating-window case with short periods. For this model, you should use periods of 40 or more. But even in case that TLS provides unstable results (you can see it as spikes in &beta;), residuals are still stable.'''<br />
<br />
=== Description ===<br />
* see the math behind the regression itself [http://en.wikipedia.org/wiki/Deming_regression here] (Deming regression is a 2D case of TLS)<br />
* from the regression, we get coefficients ''&beta;'', ''&alpha;'' and orthogonal residuals ''R(t)''<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>nd</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>nd</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman-grid Model (v1) ==<br />
This model follows up on the Kalman model and it tries to fix its biggest flaw - sensitivity to &delta; parameter. How? By executing '''whole grid''' of Kalman filters with different &delta; settings at the same time.<br />
<br />
Then it evaluates separate backtests of underlying Kalman strategies and from their measures it creates weights for calculating &alpha;, &beta; and standard deviation. This process goes on incrementally (at each price observation!).<br />
<br />
'''This model has been deprecated in favor of Kalman Grid V2'''<br />
<br />
=== Model Support ===<br />
* no longer supported<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman Grid filter observation covariance V<sub>e</sub>, typical value is 0.001</div>Adminhttps://wiki.pairtradinglab.com/wiki/Pair_Trading_ModelsPair Trading Models2016-09-15T12:09:19Z<p>Admin: </p>
<hr />
<div>Pair Trading Lab offers pair trading algorithms based on various mathematical models. These are models currently supported in PTL:<br />
== Ratio Model ==<br />
[[File:Ratio-illustration.png|right|650px|thumb|Ratio Uptick Example]]<br />
This is one of the standard pair trading models described in literature. It is based in ratio of instrument prices, [http://en.wikipedia.org/wiki/Moving_average moving average] and [http://en.wikipedia.org/wiki/Standard_deviation standard deviation]. In other words, it is based on [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator.<br />
<br />
Since Nov 27th 2014, this model also supports additional [http://www.investopedia.com/terms/r/rsi.asp RSI filter] you can use in addition to Bollinger Bands method.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (RSI supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.5, 2.5>, 2.0 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* moving average period P<sub>m</sub> (typical range <10, 100>), default = 15<br />
* moving average type T (algorithm), default = exponential<br />
* standard deviation period P<sub>s</sub> (typical range <10, 100>), default = 15<br />
* entry mode (simple, uptick, downtick)<br />
* RSI period and threshold (optional RSI filtering)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* we need to calculate ratio time series ''R(t)'' = ''A(t)'' / ''B(t)''<br />
* let's apply moving average of type T with period P<sub>m</sub> on ''R(t)'' to get time series ''M(t)''<br />
* let's apply standard deviation with period P<sub>s</sub> on ''R(t)'' to get time series ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = (''R(t)'' - ''M(t)'') / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
* another common approach (to visualize) is to create bands and put it above the moving average ''M(t)'':<br />
** upper entry band ''U<sub>n</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>n</sub><br />
** lower entry band ''L<sub>n</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>n</sub><br />
** upper exit band ''U<sub>x</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>x</sub><br />
** lower exit band ''L<sub>x</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>x</sub><br />
** these bands are actually the same bands as in [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator and we can use crossing of ''R(t)'' and bands as trade signals<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'')<br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'')<br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' < E<sub>n</sub> (same as ''R(t-1)'' < ''U<sub>n</sub>(t-1)'')<br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' > -E<sub>n</sub> (same as ''R(t-1)'' > ''L<sub>n</sub>(t-1)'')<br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>x</sub><br />
** when using bands, it is the same as having ''R(t)'' < ''U<sub>n</sub>(t)'' '''and''' ''R(t-1)'' >= ''U<sub>n</sub>(t-1)'' '''and''' ''R(t)'' > ''U<sub>x</sub>(t)''<br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>x</sub><br />
** when using bands, it is the same as having ''R(t)'' > ''L<sub>n</sub>(t)'' '''and''' ''R(t-1)'' <= ''L<sub>n</sub>(t-1)'' '''and''' ''R(t)'' < ''L<sub>x</sub>(t)''<br />
<br />
Why do we have the '''simple entry mode'''? In normal situations and backtests, it gives same results as the '''uptick mode'''. But the difference comes up while trading multiple pairs in portfolio. The simple mode allows you to jump in the position immediately after a new slot is freed, regardless of the previous Z-scores.<br />
<br />
'''Which entry mode is better?''' Hard to tell, sometimes the uptick, sometimes the downtick. You have to do your homework and decide, which idea suits your trading style better. In general, uptick/simple mode is more aggressive, as it does not wait for first signs of spread mean reversion.<br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub> (equivalent to ''R(t)'' <= ''U<sub>x</sub>(t)'')<br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub> (equivalent to ''R(t)'' >= ''L<sub>x</sub>(t)'')<br />
<br />
=== Moving Average Types ===<br />
[[File:Rsi-illustration.png|right|650px|thumb|Ratio RSI Trading Example]]<br />
You can choose from these moving average algorithms:<br />
* [http://www.investopedia.com/terms/s/sma.asp Simple Moving Average] (SMA)<br />
* [http://www.investopedia.com/terms/e/ema.asp Exponential Moving Average] (EMA)<br />
* [http://fxtrade.oanda.com/learn/forex-indicators/weighted-moving-average Weighted Moving Average] (WMA)<br />
* [http://www.investopedia.com/terms/d/double-exponential-moving-average.asp Double Exponential Moving Average] (DEMA)<br />
* [http://www.investopedia.com/terms/t/triple-exponential-moving-average.asp Triple Exponential Moving Average] (TEMA)<br />
* [http://daytrading.about.com/od/indicators/a/Triangular.htm Triangular Moving Average] (TMA)<br />
* [http://etfhq.com/blog/2011/11/07/adaptive-moving-average-ama-aka-kaufman-adaptive-moving-average-kama Kaufman Adaptive Moving Average] (KAMA)<br />
* [http://www.mesasoftware.com/Papers/MAMA.pdf MESA Adaptive Moving Average] (MAMA)<br />
* [http://www.tradeescort.com/AgenaIndicatorHelp/AgenaIndicator.html?T3TripleExponentialMovingAverage.html Triple Exponential T3 Moving Average]<br />
<br />
Which one is the best? It depends, you have to test for yourself. They mostly differ in the term of memory and how fast they react to changes. Industry standard and the default is EMA. We suggest to try all of them on some sample pair to see how they work.<br />
<br />
=== RSI Filtering ===<br />
You can combine the Z-score/Bollinger Band model with [http://www.investopedia.com/terms/r/rsi.asp RSI indicator] applied on the ratio ''R(t)''. RSI filter will be automatically enabled if you set RSI threshold to other value than zero. RSI filter is combined with the Z-score rules using '''AND''' operator ('''both''' entry rule and RSI entry rule must be true to open a position).<br />
<br />
You can also change the RSI period if you want (default period = 15).<br />
<br />
RSI Threshold is value between 0 and 50. Because RSI indicator value oscillates between 0 and 100 (where 50 = mean), your threshold value is just used to set real thresholds for RSI:<br />
* let's assume RSI Period P<sub>r</sub> was entered<br />
* effective RSI value threshold is then 50+P<sub>r</sub> (upper) and 50-P<sub>r</sub> (lower)<br />
* you can see both thresholds in the example image at the right side<br />
<br />
Example:<br />
* RSI Threshold entered is 15<br />
* then, short positions are only opened, if RSI >= 65 (50+15)<br />
* long positions are only opened, if RSI <=35 (50-15)<br />
<br />
Useful hint: if you want to control entry rules just by RSI, you can set Entry Mode to simple and Entry Threshold to some low value.<br />
<br />
== Residual Model ==<br />
[[File:Residual-illustration2.png|right|650px|thumb|Residual Downtick Example]]<br />
Residual mode is based on [http://en.wikipedia.org/wiki/Linear_regression linear regression]. In literature it has been also referred to as the '''cointegration approach'''. Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the OLS method (Ordinary Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
This particular implementation is very simple. The regression is constructed using floating window of a fixed period, the same period is used for calculating the standard deviation.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (uptick and downtick modes are supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <15, 300><br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* first we need to construct a linear regression between ''A(t)'', ''B(t)'' using OLS, where ''A(t)'' = ''&beta;'' * ''B(t)'' + ''&alpha;'' + ''R(t)''<br />
* because we use floating window of period P (we calculate new regression each day), we actually get new series ''&beta;(t)'', ''&alpha;(t)'', ''R(t)'', where ''&beta;(t)'', ''&alpha;(t)'' are series of regression coefficients and ''R(t)'' are residuals (prediction errors)<br />
* ''R(t)'' = ''A(t)'' - (''&beta;(t)'' * ''B(t)'' + ''&alpha;(t)'')<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>x</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>x</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
<br />
<br />
== Kalman Model ==<br />
This model is based on [http://en.wikipedia.org/wiki/Kalman_filter Kalman Filter]. In this case, the filter is used here instead of linear regression to determine proper hedge ratio, deviation from the mean and the standard deviation of the spread. The advantage is that this filter is superior when dealing with noise compared to OLS or TLS methods, also it does not have any lookback period to optimize.<br />
The disadvantage is that is has another parameters (like &delta;) to find out.<br />
<br />
We strongly recommend to read more details about Kalman filter applications in pair trading in [http://www.amazon.com/Algorithmic-Trading-Winning-Strategies-Rationale/dp/1118460146 this book].<br />
<br />
While Kalman Filter also estimates the standard deviation on the fly, our implementation also supports a possibility of having an auxiliary standard deviation indicator applied directly on the forecast error time series. The advantage is that you get less sensitivity on the &delta; parameter, but on the other hand a new lookback parameter is introduced.<br />
<br />
'''Kalman Model has too sensitive parameters which prevents it to be traded in reality. This model is provided only for education purposes - to be able to see how parameters affects its performance. Use Kalman Grid model for any serious trading.'''<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not planned'''<br />
* in PTL Trader: '''not planned'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter transition covariance &delta;, typical value is 0.0001 - '''unfortunately this model is very sensitive to this parameter especially when using standard deviation estimate coming from the Kalman filter itself'''<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* Auxiliary standard deviation period - if equal to zero, Kalman filter is used to estimate standard deviation, if non-zero, auxiliary standard deviation indicator with this period is used (an the estimate from Kalman filter is ignored)<br />
* Unstable period = how many Kalman filter observations are ignored at the beginning (first observations are quite unstable)<br />
<br />
'''This model is provided for education purposes only.'''<br />
<br />
= Deprecated Models =<br />
These models are deprecated and no longer supported:<br />
== Orthogonal Model ==<br />
This model is similar to Residual model, but it uses [http://en.wikipedia.org/wiki/Total_least_squares orthogonal regression/total least squares] instead of OLS. In addition, it has some extra features.<br />
Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the TLS method (Total Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
The regression is constructed using floating window of a fixed period. Standard deviation indicator is then applied to regression residuals (it can have a different period).<br />
<br />
=== Model Support ===<br />
* no longer supported<br />
<br />
=== Model Neutrality ===<br />
We support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes. In beta neutral mode, we determine the hedge ratio from TLS regression too, but using significantly longer floating period (240 or more).<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* entry threshold E<sub>nd</sub> for Z-score, typical value range is slightly less than E<sub>n</sub> - used as another band for downtick mode only<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <50, 300><br />
* standard deviation period, typical range <25, 100><br />
* entry mode (simple, uptick, downtick)<br />
<br />
(section still under construction)<br />
<br />
'''Important: TLS is numerically far less stable than OLS, especially for floating-window case with short periods. For this model, you should use periods of 40 or more. But even in case that TLS provides unstable results (you can see it as spikes in &beta;), residuals are still stable.'''<br />
<br />
=== Description ===<br />
* see the math behind the regression itself [http://en.wikipedia.org/wiki/Deming_regression here] (Deming regression is a 2D case of TLS)<br />
* from the regression, we get coefficients ''&beta;'', ''&alpha;'' and orthogonal residuals ''R(t)''<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>nd</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>nd</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
== Kalman-grid Model (v1) ==<br />
This model follows up on the Kalman model and it tries to fix its biggest flaw - sensitivity to &delta; parameter. How? By executing '''whole grid''' of Kalman filters with different &delta; settings at the same time.<br />
<br />
Then it evaluates separate backtests of underlying Kalman strategies and from their measures it creates weights for calculating &alpha;, &beta; and standard deviation. This process goes on incrementally (at each price observation!).<br />
<br />
'''This model has been deprecated in favor of Kalman Grid V2'''<br />
<br />
=== Model Support ===<br />
* no longer supported<br />
<br />
=== Model Neutrality ===<br />
For Kalman Grid Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman Grid filter observation covariance V<sub>e</sub>, typical value is 0.001</div>Adminhttps://wiki.pairtradinglab.com/wiki/Pair_Trading_ModelsPair Trading Models2016-09-15T12:07:00Z<p>Admin: /* Kalman Model */</p>
<hr />
<div>Pair Trading Lab offers pair trading algorithms based on various mathematical models. These are models currently supported in PTL:<br />
== Ratio Model ==<br />
[[File:Ratio-illustration.png|right|650px|thumb|Ratio Uptick Example]]<br />
This is one of the standard pair trading models described in literature. It is based in ratio of instrument prices, [http://en.wikipedia.org/wiki/Moving_average moving average] and [http://en.wikipedia.org/wiki/Standard_deviation standard deviation]. In other words, it is based on [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator.<br />
<br />
Since Nov 27th 2014, this model also supports additional [http://www.investopedia.com/terms/r/rsi.asp RSI filter] you can use in addition to Bollinger Bands method.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (RSI supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.5, 2.5>, 2.0 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* moving average period P<sub>m</sub> (typical range <10, 100>), default = 15<br />
* moving average type T (algorithm), default = exponential<br />
* standard deviation period P<sub>s</sub> (typical range <10, 100>), default = 15<br />
* entry mode (simple, uptick, downtick)<br />
* RSI period and threshold (optional RSI filtering)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* we need to calculate ratio time series ''R(t)'' = ''A(t)'' / ''B(t)''<br />
* let's apply moving average of type T with period P<sub>m</sub> on ''R(t)'' to get time series ''M(t)''<br />
* let's apply standard deviation with period P<sub>s</sub> on ''R(t)'' to get time series ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = (''R(t)'' - ''M(t)'') / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
* another common approach (to visualize) is to create bands and put it above the moving average ''M(t)'':<br />
** upper entry band ''U<sub>n</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>n</sub><br />
** lower entry band ''L<sub>n</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>n</sub><br />
** upper exit band ''U<sub>x</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>x</sub><br />
** lower exit band ''L<sub>x</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>x</sub><br />
** these bands are actually the same bands as in [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator and we can use crossing of ''R(t)'' and bands as trade signals<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'')<br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'')<br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' < E<sub>n</sub> (same as ''R(t-1)'' < ''U<sub>n</sub>(t-1)'')<br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' > -E<sub>n</sub> (same as ''R(t-1)'' > ''L<sub>n</sub>(t-1)'')<br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>x</sub><br />
** when using bands, it is the same as having ''R(t)'' < ''U<sub>n</sub>(t)'' '''and''' ''R(t-1)'' >= ''U<sub>n</sub>(t-1)'' '''and''' ''R(t)'' > ''U<sub>x</sub>(t)''<br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>x</sub><br />
** when using bands, it is the same as having ''R(t)'' > ''L<sub>n</sub>(t)'' '''and''' ''R(t-1)'' <= ''L<sub>n</sub>(t-1)'' '''and''' ''R(t)'' < ''L<sub>x</sub>(t)''<br />
<br />
Why do we have the '''simple entry mode'''? In normal situations and backtests, it gives same results as the '''uptick mode'''. But the difference comes up while trading multiple pairs in portfolio. The simple mode allows you to jump in the position immediately after a new slot is freed, regardless of the previous Z-scores.<br />
<br />
'''Which entry mode is better?''' Hard to tell, sometimes the uptick, sometimes the downtick. You have to do your homework and decide, which idea suits your trading style better. In general, uptick/simple mode is more aggressive, as it does not wait for first signs of spread mean reversion.<br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub> (equivalent to ''R(t)'' <= ''U<sub>x</sub>(t)'')<br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub> (equivalent to ''R(t)'' >= ''L<sub>x</sub>(t)'')<br />
<br />
=== Moving Average Types ===<br />
[[File:Rsi-illustration.png|right|650px|thumb|Ratio RSI Trading Example]]<br />
You can choose from these moving average algorithms:<br />
* [http://www.investopedia.com/terms/s/sma.asp Simple Moving Average] (SMA)<br />
* [http://www.investopedia.com/terms/e/ema.asp Exponential Moving Average] (EMA)<br />
* [http://fxtrade.oanda.com/learn/forex-indicators/weighted-moving-average Weighted Moving Average] (WMA)<br />
* [http://www.investopedia.com/terms/d/double-exponential-moving-average.asp Double Exponential Moving Average] (DEMA)<br />
* [http://www.investopedia.com/terms/t/triple-exponential-moving-average.asp Triple Exponential Moving Average] (TEMA)<br />
* [http://daytrading.about.com/od/indicators/a/Triangular.htm Triangular Moving Average] (TMA)<br />
* [http://etfhq.com/blog/2011/11/07/adaptive-moving-average-ama-aka-kaufman-adaptive-moving-average-kama Kaufman Adaptive Moving Average] (KAMA)<br />
* [http://www.mesasoftware.com/Papers/MAMA.pdf MESA Adaptive Moving Average] (MAMA)<br />
* [http://www.tradeescort.com/AgenaIndicatorHelp/AgenaIndicator.html?T3TripleExponentialMovingAverage.html Triple Exponential T3 Moving Average]<br />
<br />
Which one is the best? It depends, you have to test for yourself. They mostly differ in the term of memory and how fast they react to changes. Industry standard and the default is EMA. We suggest to try all of them on some sample pair to see how they work.<br />
<br />
=== RSI Filtering ===<br />
You can combine the Z-score/Bollinger Band model with [http://www.investopedia.com/terms/r/rsi.asp RSI indicator] applied on the ratio ''R(t)''. RSI filter will be automatically enabled if you set RSI threshold to other value than zero. RSI filter is combined with the Z-score rules using '''AND''' operator ('''both''' entry rule and RSI entry rule must be true to open a position).<br />
<br />
You can also change the RSI period if you want (default period = 15).<br />
<br />
RSI Threshold is value between 0 and 50. Because RSI indicator value oscillates between 0 and 100 (where 50 = mean), your threshold value is just used to set real thresholds for RSI:<br />
* let's assume RSI Period P<sub>r</sub> was entered<br />
* effective RSI value threshold is then 50+P<sub>r</sub> (upper) and 50-P<sub>r</sub> (lower)<br />
* you can see both thresholds in the example image at the right side<br />
<br />
Example:<br />
* RSI Threshold entered is 15<br />
* then, short positions are only opened, if RSI >= 65 (50+15)<br />
* long positions are only opened, if RSI <=35 (50-15)<br />
<br />
Useful hint: if you want to control entry rules just by RSI, you can set Entry Mode to simple and Entry Threshold to some low value.<br />
<br />
== Residual Model ==<br />
[[File:Residual-illustration2.png|right|650px|thumb|Residual Downtick Example]]<br />
Residual mode is based on [http://en.wikipedia.org/wiki/Linear_regression linear regression]. In literature it has been also referred to as the '''cointegration approach'''. Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the OLS method (Ordinary Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
This particular implementation is very simple. The regression is constructed using floating window of a fixed period, the same period is used for calculating the standard deviation.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (uptick and downtick modes are supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <15, 300><br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* first we need to construct a linear regression between ''A(t)'', ''B(t)'' using OLS, where ''A(t)'' = ''&beta;'' * ''B(t)'' + ''&alpha;'' + ''R(t)''<br />
* because we use floating window of period P (we calculate new regression each day), we actually get new series ''&beta;(t)'', ''&alpha;(t)'', ''R(t)'', where ''&beta;(t)'', ''&alpha;(t)'' are series of regression coefficients and ''R(t)'' are residuals (prediction errors)<br />
* ''R(t)'' = ''A(t)'' - (''&beta;(t)'' * ''B(t)'' + ''&alpha;(t)'')<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>x</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>x</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
<br />
<br />
== Kalman Model ==<br />
This model is based on [http://en.wikipedia.org/wiki/Kalman_filter Kalman Filter]. In this case, the filter is used here instead of linear regression to determine proper hedge ratio, deviation from the mean and the standard deviation of the spread. The advantage is that this filter is superior when dealing with noise compared to OLS or TLS methods, also it does not have any lookback period to optimize.<br />
The disadvantage is that is has another parameters (like &delta;) to find out.<br />
<br />
We strongly recommend to read more details about Kalman filter applications in pair trading in [http://www.amazon.com/Algorithmic-Trading-Winning-Strategies-Rationale/dp/1118460146 this book].<br />
<br />
While Kalman Filter also estimates the standard deviation on the fly, our implementation also supports a possibility of having an auxiliary standard deviation indicator applied directly on the forecast error time series. The advantage is that you get less sensitivity on the &delta; parameter, but on the other hand a new lookback parameter is introduced.<br />
<br />
'''Kalman Model has too sensitive parameters which prevents it to be traded in reality. This model is provided only for education purposes - to be able to see how parameters affects its performance. Use Kalman Grid model for any serious trading.'''<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not planned'''<br />
* in PTL Trader: '''not planned'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter transition covariance &delta;, typical value is 0.0001 - '''unfortunately this model is very sensitive to this parameter especially when using standard deviation estimate coming from the Kalman filter itself'''<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* Auxiliary standard deviation period - if equal to zero, Kalman filter is used to estimate standard deviation, if non-zero, auxiliary standard deviation indicator with this period is used (an the estimate from Kalman filter is ignored)<br />
* Unstable period = how many Kalman filter observations are ignored at the beginning (first observations are quite unstable)<br />
<br />
'''This model is provided for education purposes only.'''<br />
<br />
== Kalman-grid Model ==<br />
This model follows up on the Kalman model and it tries to fix its biggest flaw - sensitivity to &delta; parameter. How? By executing '''whole grid''' of Kalman filters with different &delta; settings at the same time.<br />
<br />
Then it evaluates separate backtests of underlying Kalman strategies and from their measures it creates weights for calculating &alpha;, &beta; and standard deviation. This process goes on incrementally (at each price observation!).<br />
<br />
(under construction)<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not yet'''<br />
* in PTL Trader: '''not yet'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
<br />
<br />
'''This model is experimental.'''<br />
<br />
= Deprecated Models =<br />
These models are deprecated and no longer supported:<br />
== Orthogonal Model ==<br />
This model is similar to Residual model, but it uses [http://en.wikipedia.org/wiki/Total_least_squares orthogonal regression/total least squares] instead of OLS. In addition, it has some extra features.<br />
Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the TLS method (Total Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
The regression is constructed using floating window of a fixed period. Standard deviation indicator is then applied to regression residuals (it can have a different period).<br />
<br />
=== Model Support ===<br />
* no longer supported<br />
<br />
=== Model Neutrality ===<br />
We support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes. In beta neutral mode, we determine the hedge ratio from TLS regression too, but using significantly longer floating period (240 or more).<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* entry threshold E<sub>nd</sub> for Z-score, typical value range is slightly less than E<sub>n</sub> - used as another band for downtick mode only<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <50, 300><br />
* standard deviation period, typical range <25, 100><br />
* entry mode (simple, uptick, downtick)<br />
<br />
(section still under construction)<br />
<br />
'''Important: TLS is numerically far less stable than OLS, especially for floating-window case with short periods. For this model, you should use periods of 40 or more. But even in case that TLS provides unstable results (you can see it as spikes in &beta;), residuals are still stable.'''<br />
<br />
=== Description ===<br />
* see the math behind the regression itself [http://en.wikipedia.org/wiki/Deming_regression here] (Deming regression is a 2D case of TLS)<br />
* from the regression, we get coefficients ''&beta;'', ''&alpha;'' and orthogonal residuals ''R(t)''<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>nd</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>nd</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub></div>Adminhttps://wiki.pairtradinglab.com/wiki/Pair_Trading_ModelsPair Trading Models2016-09-15T12:02:18Z<p>Admin: /* Orthogonal Model */</p>
<hr />
<div>Pair Trading Lab offers pair trading algorithms based on various mathematical models. These are models currently supported in PTL:<br />
== Ratio Model ==<br />
[[File:Ratio-illustration.png|right|650px|thumb|Ratio Uptick Example]]<br />
This is one of the standard pair trading models described in literature. It is based in ratio of instrument prices, [http://en.wikipedia.org/wiki/Moving_average moving average] and [http://en.wikipedia.org/wiki/Standard_deviation standard deviation]. In other words, it is based on [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator.<br />
<br />
Since Nov 27th 2014, this model also supports additional [http://www.investopedia.com/terms/r/rsi.asp RSI filter] you can use in addition to Bollinger Bands method.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (RSI supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.5, 2.5>, 2.0 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* moving average period P<sub>m</sub> (typical range <10, 100>), default = 15<br />
* moving average type T (algorithm), default = exponential<br />
* standard deviation period P<sub>s</sub> (typical range <10, 100>), default = 15<br />
* entry mode (simple, uptick, downtick)<br />
* RSI period and threshold (optional RSI filtering)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* we need to calculate ratio time series ''R(t)'' = ''A(t)'' / ''B(t)''<br />
* let's apply moving average of type T with period P<sub>m</sub> on ''R(t)'' to get time series ''M(t)''<br />
* let's apply standard deviation with period P<sub>s</sub> on ''R(t)'' to get time series ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = (''R(t)'' - ''M(t)'') / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
* another common approach (to visualize) is to create bands and put it above the moving average ''M(t)'':<br />
** upper entry band ''U<sub>n</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>n</sub><br />
** lower entry band ''L<sub>n</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>n</sub><br />
** upper exit band ''U<sub>x</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>x</sub><br />
** lower exit band ''L<sub>x</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>x</sub><br />
** these bands are actually the same bands as in [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator and we can use crossing of ''R(t)'' and bands as trade signals<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'')<br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'')<br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' < E<sub>n</sub> (same as ''R(t-1)'' < ''U<sub>n</sub>(t-1)'')<br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' > -E<sub>n</sub> (same as ''R(t-1)'' > ''L<sub>n</sub>(t-1)'')<br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>x</sub><br />
** when using bands, it is the same as having ''R(t)'' < ''U<sub>n</sub>(t)'' '''and''' ''R(t-1)'' >= ''U<sub>n</sub>(t-1)'' '''and''' ''R(t)'' > ''U<sub>x</sub>(t)''<br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>x</sub><br />
** when using bands, it is the same as having ''R(t)'' > ''L<sub>n</sub>(t)'' '''and''' ''R(t-1)'' <= ''L<sub>n</sub>(t-1)'' '''and''' ''R(t)'' < ''L<sub>x</sub>(t)''<br />
<br />
Why do we have the '''simple entry mode'''? In normal situations and backtests, it gives same results as the '''uptick mode'''. But the difference comes up while trading multiple pairs in portfolio. The simple mode allows you to jump in the position immediately after a new slot is freed, regardless of the previous Z-scores.<br />
<br />
'''Which entry mode is better?''' Hard to tell, sometimes the uptick, sometimes the downtick. You have to do your homework and decide, which idea suits your trading style better. In general, uptick/simple mode is more aggressive, as it does not wait for first signs of spread mean reversion.<br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub> (equivalent to ''R(t)'' <= ''U<sub>x</sub>(t)'')<br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub> (equivalent to ''R(t)'' >= ''L<sub>x</sub>(t)'')<br />
<br />
=== Moving Average Types ===<br />
[[File:Rsi-illustration.png|right|650px|thumb|Ratio RSI Trading Example]]<br />
You can choose from these moving average algorithms:<br />
* [http://www.investopedia.com/terms/s/sma.asp Simple Moving Average] (SMA)<br />
* [http://www.investopedia.com/terms/e/ema.asp Exponential Moving Average] (EMA)<br />
* [http://fxtrade.oanda.com/learn/forex-indicators/weighted-moving-average Weighted Moving Average] (WMA)<br />
* [http://www.investopedia.com/terms/d/double-exponential-moving-average.asp Double Exponential Moving Average] (DEMA)<br />
* [http://www.investopedia.com/terms/t/triple-exponential-moving-average.asp Triple Exponential Moving Average] (TEMA)<br />
* [http://daytrading.about.com/od/indicators/a/Triangular.htm Triangular Moving Average] (TMA)<br />
* [http://etfhq.com/blog/2011/11/07/adaptive-moving-average-ama-aka-kaufman-adaptive-moving-average-kama Kaufman Adaptive Moving Average] (KAMA)<br />
* [http://www.mesasoftware.com/Papers/MAMA.pdf MESA Adaptive Moving Average] (MAMA)<br />
* [http://www.tradeescort.com/AgenaIndicatorHelp/AgenaIndicator.html?T3TripleExponentialMovingAverage.html Triple Exponential T3 Moving Average]<br />
<br />
Which one is the best? It depends, you have to test for yourself. They mostly differ in the term of memory and how fast they react to changes. Industry standard and the default is EMA. We suggest to try all of them on some sample pair to see how they work.<br />
<br />
=== RSI Filtering ===<br />
You can combine the Z-score/Bollinger Band model with [http://www.investopedia.com/terms/r/rsi.asp RSI indicator] applied on the ratio ''R(t)''. RSI filter will be automatically enabled if you set RSI threshold to other value than zero. RSI filter is combined with the Z-score rules using '''AND''' operator ('''both''' entry rule and RSI entry rule must be true to open a position).<br />
<br />
You can also change the RSI period if you want (default period = 15).<br />
<br />
RSI Threshold is value between 0 and 50. Because RSI indicator value oscillates between 0 and 100 (where 50 = mean), your threshold value is just used to set real thresholds for RSI:<br />
* let's assume RSI Period P<sub>r</sub> was entered<br />
* effective RSI value threshold is then 50+P<sub>r</sub> (upper) and 50-P<sub>r</sub> (lower)<br />
* you can see both thresholds in the example image at the right side<br />
<br />
Example:<br />
* RSI Threshold entered is 15<br />
* then, short positions are only opened, if RSI >= 65 (50+15)<br />
* long positions are only opened, if RSI <=35 (50-15)<br />
<br />
Useful hint: if you want to control entry rules just by RSI, you can set Entry Mode to simple and Entry Threshold to some low value.<br />
<br />
== Residual Model ==<br />
[[File:Residual-illustration2.png|right|650px|thumb|Residual Downtick Example]]<br />
Residual mode is based on [http://en.wikipedia.org/wiki/Linear_regression linear regression]. In literature it has been also referred to as the '''cointegration approach'''. Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the OLS method (Ordinary Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
This particular implementation is very simple. The regression is constructed using floating window of a fixed period, the same period is used for calculating the standard deviation.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (uptick and downtick modes are supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <15, 300><br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* first we need to construct a linear regression between ''A(t)'', ''B(t)'' using OLS, where ''A(t)'' = ''&beta;'' * ''B(t)'' + ''&alpha;'' + ''R(t)''<br />
* because we use floating window of period P (we calculate new regression each day), we actually get new series ''&beta;(t)'', ''&alpha;(t)'', ''R(t)'', where ''&beta;(t)'', ''&alpha;(t)'' are series of regression coefficients and ''R(t)'' are residuals (prediction errors)<br />
* ''R(t)'' = ''A(t)'' - (''&beta;(t)'' * ''B(t)'' + ''&alpha;(t)'')<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>x</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>x</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
<br />
<br />
== Kalman Model ==<br />
This model is based on [http://en.wikipedia.org/wiki/Kalman_filter Kalman Filter]. In this case, the filter is used here instead of linear regression to determine proper hedge ratio, deviation from the mean and the standard deviation of the spread. The advantage is that this filter is superior when dealing with noise compared to OLS or TLS methods, also it does not have any lookback period to optimize.<br />
The disadvantage is that is has another parameters (like &delta;) to find out.<br />
<br />
We strongly recommend to read more details about Kalman filter applications in pair trading in [http://www.amazon.com/Algorithmic-Trading-Winning-Strategies-Rationale/dp/1118460146 this book].<br />
<br />
While Kalman Filter also estimates the standard deviation on the fly, our implementation also supports a possibility of having an auxiliary standard deviation indicator applied directly on the forecast error time series. The advantage is that you get less sensitivity on the &delta; parameter, but on the other hand a new lookback parameter is introduced.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not yet'''<br />
* in PTL Trader: '''not yet'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter transition covariance &delta;, typical value is 0.0001 - '''unfortunately this model is very sensitive to this parameter especially when using standard deviation estimate coming from the Kalman filter itself'''<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* Auxiliary standard deviation period - if equal to zero, Kalman filter is used to estimate standard deviation, if non-zero, auxiliary standard deviation indicator with this period is used (an the estimate from Kalman filter is ignored)<br />
* Unstable period = how many Kalman filter observations are ignored at the beginning (first observations are quite unstable)<br />
<br />
(section still under construction)<br />
<br />
'''This model is experimental.'''<br />
<br />
== Kalman-grid Model ==<br />
This model follows up on the Kalman model and it tries to fix its biggest flaw - sensitivity to &delta; parameter. How? By executing '''whole grid''' of Kalman filters with different &delta; settings at the same time.<br />
<br />
Then it evaluates separate backtests of underlying Kalman strategies and from their measures it creates weights for calculating &alpha;, &beta; and standard deviation. This process goes on incrementally (at each price observation!).<br />
<br />
(under construction)<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not yet'''<br />
* in PTL Trader: '''not yet'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
<br />
<br />
'''This model is experimental.'''<br />
<br />
= Deprecated Models =<br />
These models are deprecated and no longer supported:<br />
== Orthogonal Model ==<br />
This model is similar to Residual model, but it uses [http://en.wikipedia.org/wiki/Total_least_squares orthogonal regression/total least squares] instead of OLS. In addition, it has some extra features.<br />
Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the TLS method (Total Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
The regression is constructed using floating window of a fixed period. Standard deviation indicator is then applied to regression residuals (it can have a different period).<br />
<br />
=== Model Support ===<br />
* no longer supported<br />
<br />
=== Model Neutrality ===<br />
We support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes. In beta neutral mode, we determine the hedge ratio from TLS regression too, but using significantly longer floating period (240 or more).<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* entry threshold E<sub>nd</sub> for Z-score, typical value range is slightly less than E<sub>n</sub> - used as another band for downtick mode only<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <50, 300><br />
* standard deviation period, typical range <25, 100><br />
* entry mode (simple, uptick, downtick)<br />
<br />
(section still under construction)<br />
<br />
'''Important: TLS is numerically far less stable than OLS, especially for floating-window case with short periods. For this model, you should use periods of 40 or more. But even in case that TLS provides unstable results (you can see it as spikes in &beta;), residuals are still stable.'''<br />
<br />
=== Description ===<br />
* see the math behind the regression itself [http://en.wikipedia.org/wiki/Deming_regression here] (Deming regression is a 2D case of TLS)<br />
* from the regression, we get coefficients ''&beta;'', ''&alpha;'' and orthogonal residuals ''R(t)''<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>nd</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>nd</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub></div>Adminhttps://wiki.pairtradinglab.com/wiki/Pair_Trading_ModelsPair Trading Models2016-09-15T12:01:43Z<p>Admin: </p>
<hr />
<div>Pair Trading Lab offers pair trading algorithms based on various mathematical models. These are models currently supported in PTL:<br />
== Ratio Model ==<br />
[[File:Ratio-illustration.png|right|650px|thumb|Ratio Uptick Example]]<br />
This is one of the standard pair trading models described in literature. It is based in ratio of instrument prices, [http://en.wikipedia.org/wiki/Moving_average moving average] and [http://en.wikipedia.org/wiki/Standard_deviation standard deviation]. In other words, it is based on [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator.<br />
<br />
Since Nov 27th 2014, this model also supports additional [http://www.investopedia.com/terms/r/rsi.asp RSI filter] you can use in addition to Bollinger Bands method.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (RSI supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.5, 2.5>, 2.0 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* moving average period P<sub>m</sub> (typical range <10, 100>), default = 15<br />
* moving average type T (algorithm), default = exponential<br />
* standard deviation period P<sub>s</sub> (typical range <10, 100>), default = 15<br />
* entry mode (simple, uptick, downtick)<br />
* RSI period and threshold (optional RSI filtering)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* we need to calculate ratio time series ''R(t)'' = ''A(t)'' / ''B(t)''<br />
* let's apply moving average of type T with period P<sub>m</sub> on ''R(t)'' to get time series ''M(t)''<br />
* let's apply standard deviation with period P<sub>s</sub> on ''R(t)'' to get time series ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = (''R(t)'' - ''M(t)'') / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
* another common approach (to visualize) is to create bands and put it above the moving average ''M(t)'':<br />
** upper entry band ''U<sub>n</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>n</sub><br />
** lower entry band ''L<sub>n</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>n</sub><br />
** upper exit band ''U<sub>x</sub>(t)'' = ''M(t)'' + ''S(t)'' * E<sub>x</sub><br />
** lower exit band ''L<sub>x</sub>(t)'' = ''M(t)'' - ''S(t)'' * E<sub>x</sub><br />
** these bands are actually the same bands as in [http://en.wikipedia.org/wiki/Bollinger_Bands Bollinger Bands] indicator and we can use crossing of ''R(t)'' and bands as trade signals<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'')<br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'')<br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> (equivalent to ''R(t)'' >= ''U<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' < E<sub>n</sub> (same as ''R(t-1)'' < ''U<sub>n</sub>(t-1)'')<br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> (equivalent to ''R(t)'' <= ''L<sub>n</sub>(t)'') '''and''' ''Z(t-1)'' > -E<sub>n</sub> (same as ''R(t-1)'' > ''L<sub>n</sub>(t-1)'')<br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>x</sub><br />
** when using bands, it is the same as having ''R(t)'' < ''U<sub>n</sub>(t)'' '''and''' ''R(t-1)'' >= ''U<sub>n</sub>(t-1)'' '''and''' ''R(t)'' > ''U<sub>x</sub>(t)''<br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>x</sub><br />
** when using bands, it is the same as having ''R(t)'' > ''L<sub>n</sub>(t)'' '''and''' ''R(t-1)'' <= ''L<sub>n</sub>(t-1)'' '''and''' ''R(t)'' < ''L<sub>x</sub>(t)''<br />
<br />
Why do we have the '''simple entry mode'''? In normal situations and backtests, it gives same results as the '''uptick mode'''. But the difference comes up while trading multiple pairs in portfolio. The simple mode allows you to jump in the position immediately after a new slot is freed, regardless of the previous Z-scores.<br />
<br />
'''Which entry mode is better?''' Hard to tell, sometimes the uptick, sometimes the downtick. You have to do your homework and decide, which idea suits your trading style better. In general, uptick/simple mode is more aggressive, as it does not wait for first signs of spread mean reversion.<br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub> (equivalent to ''R(t)'' <= ''U<sub>x</sub>(t)'')<br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub> (equivalent to ''R(t)'' >= ''L<sub>x</sub>(t)'')<br />
<br />
=== Moving Average Types ===<br />
[[File:Rsi-illustration.png|right|650px|thumb|Ratio RSI Trading Example]]<br />
You can choose from these moving average algorithms:<br />
* [http://www.investopedia.com/terms/s/sma.asp Simple Moving Average] (SMA)<br />
* [http://www.investopedia.com/terms/e/ema.asp Exponential Moving Average] (EMA)<br />
* [http://fxtrade.oanda.com/learn/forex-indicators/weighted-moving-average Weighted Moving Average] (WMA)<br />
* [http://www.investopedia.com/terms/d/double-exponential-moving-average.asp Double Exponential Moving Average] (DEMA)<br />
* [http://www.investopedia.com/terms/t/triple-exponential-moving-average.asp Triple Exponential Moving Average] (TEMA)<br />
* [http://daytrading.about.com/od/indicators/a/Triangular.htm Triangular Moving Average] (TMA)<br />
* [http://etfhq.com/blog/2011/11/07/adaptive-moving-average-ama-aka-kaufman-adaptive-moving-average-kama Kaufman Adaptive Moving Average] (KAMA)<br />
* [http://www.mesasoftware.com/Papers/MAMA.pdf MESA Adaptive Moving Average] (MAMA)<br />
* [http://www.tradeescort.com/AgenaIndicatorHelp/AgenaIndicator.html?T3TripleExponentialMovingAverage.html Triple Exponential T3 Moving Average]<br />
<br />
Which one is the best? It depends, you have to test for yourself. They mostly differ in the term of memory and how fast they react to changes. Industry standard and the default is EMA. We suggest to try all of them on some sample pair to see how they work.<br />
<br />
=== RSI Filtering ===<br />
You can combine the Z-score/Bollinger Band model with [http://www.investopedia.com/terms/r/rsi.asp RSI indicator] applied on the ratio ''R(t)''. RSI filter will be automatically enabled if you set RSI threshold to other value than zero. RSI filter is combined with the Z-score rules using '''AND''' operator ('''both''' entry rule and RSI entry rule must be true to open a position).<br />
<br />
You can also change the RSI period if you want (default period = 15).<br />
<br />
RSI Threshold is value between 0 and 50. Because RSI indicator value oscillates between 0 and 100 (where 50 = mean), your threshold value is just used to set real thresholds for RSI:<br />
* let's assume RSI Period P<sub>r</sub> was entered<br />
* effective RSI value threshold is then 50+P<sub>r</sub> (upper) and 50-P<sub>r</sub> (lower)<br />
* you can see both thresholds in the example image at the right side<br />
<br />
Example:<br />
* RSI Threshold entered is 15<br />
* then, short positions are only opened, if RSI >= 65 (50+15)<br />
* long positions are only opened, if RSI <=35 (50-15)<br />
<br />
Useful hint: if you want to control entry rules just by RSI, you can set Entry Mode to simple and Entry Threshold to some low value.<br />
<br />
== Residual Model ==<br />
[[File:Residual-illustration2.png|right|650px|thumb|Residual Downtick Example]]<br />
Residual mode is based on [http://en.wikipedia.org/wiki/Linear_regression linear regression]. In literature it has been also referred to as the '''cointegration approach'''. Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the OLS method (Ordinary Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
This particular implementation is very simple. The regression is constructed using floating window of a fixed period, the same period is used for calculating the standard deviation.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''yes'''<br />
* in PTL Trader: '''yes''' (uptick and downtick modes are supported since v1.2.0)<br />
<br />
=== Model Neutrality ===<br />
We currently support only dollar-neutral version of this model, which means we allocate same amounts of margin to both legs based on current prices at the time of opening the position.<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <15, 300><br />
* entry mode (simple, uptick, downtick)<br />
<br />
=== Description ===<br />
* we trade pair of stocks A, B, having price series ''A(t)'', ''B(t)''<br />
* first we need to construct a linear regression between ''A(t)'', ''B(t)'' using OLS, where ''A(t)'' = ''&beta;'' * ''B(t)'' + ''&alpha;'' + ''R(t)''<br />
* because we use floating window of period P (we calculate new regression each day), we actually get new series ''&beta;(t)'', ''&alpha;(t)'', ''R(t)'', where ''&beta;(t)'', ''&alpha;(t)'' are series of regression coefficients and ''R(t)'' are residuals (prediction errors)<br />
* ''R(t)'' = ''A(t)'' - (''&beta;(t)'' * ''B(t)'' + ''&alpha;(t)'')<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>x</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>x</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub><br />
<br />
<br />
<br />
== Kalman Model ==<br />
This model is based on [http://en.wikipedia.org/wiki/Kalman_filter Kalman Filter]. In this case, the filter is used here instead of linear regression to determine proper hedge ratio, deviation from the mean and the standard deviation of the spread. The advantage is that this filter is superior when dealing with noise compared to OLS or TLS methods, also it does not have any lookback period to optimize.<br />
The disadvantage is that is has another parameters (like &delta;) to find out.<br />
<br />
We strongly recommend to read more details about Kalman filter applications in pair trading in [http://www.amazon.com/Algorithmic-Trading-Winning-Strategies-Rationale/dp/1118460146 this book].<br />
<br />
While Kalman Filter also estimates the standard deviation on the fly, our implementation also supports a possibility of having an auxiliary standard deviation indicator applied directly on the forecast error time series. The advantage is that you get less sensitivity on the &delta; parameter, but on the other hand a new lookback parameter is introduced.<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not yet'''<br />
* in PTL Trader: '''not yet'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter transition covariance &delta;, typical value is 0.0001 - '''unfortunately this model is very sensitive to this parameter especially when using standard deviation estimate coming from the Kalman filter itself'''<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
* Auxiliary standard deviation period - if equal to zero, Kalman filter is used to estimate standard deviation, if non-zero, auxiliary standard deviation indicator with this period is used (an the estimate from Kalman filter is ignored)<br />
* Unstable period = how many Kalman filter observations are ignored at the beginning (first observations are quite unstable)<br />
<br />
(section still under construction)<br />
<br />
'''This model is experimental.'''<br />
<br />
== Kalman-grid Model ==<br />
This model follows up on the Kalman model and it tries to fix its biggest flaw - sensitivity to &delta; parameter. How? By executing '''whole grid''' of Kalman filters with different &delta; settings at the same time.<br />
<br />
Then it evaluates separate backtests of underlying Kalman strategies and from their measures it creates weights for calculating &alpha;, &beta; and standard deviation. This process goes on incrementally (at each price observation!).<br />
<br />
(under construction)<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''not yet'''<br />
* in PTL Trader: '''not yet'''<br />
<br />
=== Model Neutrality ===<br />
For Kalman Model, we support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes.<br />
<br />
=== Model Parameters ===<br />
* Kalman filter observation covariance V<sub>e</sub>, typical value is 0.001<br />
<br />
<br />
'''This model is experimental.'''<br />
<br />
= Deprecated Models =<br />
These models are deprecated and no longer supported:<br />
== Orthogonal Model ==<br />
This model is similar to Residual model, but it uses [http://en.wikipedia.org/wiki/Total_least_squares orthogonal regression/total least squares] instead of OLS. In addition, it has some extra features.<br />
Linear regression of both stocks is constructed in order to fit a linear relationship between both instruments and estimate its best parameters using the TLS method (Total Least Squares). Then, [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] is applied on the regression residuals to estimate its statistical properties and calculate Z-scores.<br />
<br />
The regression is constructed using floating window of a fixed period. Standard deviation indicator is then applied to regression residuals (it can have a different period).<br />
<br />
=== Model Support ===<br />
* in PTL backtester: '''yes''' (max Z-score not supported)<br />
* in PTL portfolio backtester: '''no'''<br />
* in PTL Trader: '''no'''<br />
<br />
=== Model Neutrality ===<br />
We support both '''dollar neutral''' (equal dollar amount invested to each leg) and '''beta neutral''' regimes. In beta neutral mode, we determine the hedge ratio from TLS regression too, but using significantly longer floating period (240 or more).<br />
<br />
=== Model Parameters ===<br />
* entry threshold E<sub>n</sub> for Z-score, typical value range is <1.2, 2.5>, 1.5 is used most often<br />
* entry threshold E<sub>nd</sub> for Z-score, typical value range is slightly less than E<sub>n</sub> - used as another band for downtick mode only<br />
* exit threshold E<sub>x</sub> for Z-score, typical value is <-0.5, 0.5>, 0 is used most often...we allow positive values only for now<br />
* max Z-score E<sub>max</sub> (optional, to filter out extremes, typical value is >4 if used)<br />
* linear regression period P (floating window is used), typical range <50, 300><br />
* standard deviation period, typical range <25, 100><br />
* entry mode (simple, uptick, downtick)<br />
<br />
(section still under construction)<br />
<br />
'''Important: TLS is numerically far less stable than OLS, especially for floating-window case with short periods. For this model, you should use periods of 40 or more. But even in case that TLS provides unstable results (you can see it as spikes in &beta;), residuals are still stable.'''<br />
<br />
=== Description ===<br />
* see the math behind the regression itself [http://en.wikipedia.org/wiki/Deming_regression here] (Deming regression is a 2D case of TLS)<br />
* from the regression, we get coefficients ''&beta;'', ''&alpha;'' and orthogonal residuals ''R(t)''<br />
* then we apply standard deviation of period P on residuals ''R(t)'' and we put it to ''S(t)''<br />
* now we can create Z-score series ''Z(t)'' as ''Z(t)'' = ''R(t)'' / ''S(t)'', this time series can give us z-score to signal trading decision directly<br />
<br />
=== Entering Position ===<br />
There are certain possible approaches how to interpret model statistics in order to make trading decisions. For entering position, we used to call them '''entry modes'''. This is the list of them and description how they work:<br />
* entry mode = '''simple''':<br />
** to open short pair position, it is simple enough if the Z-score ''Z(t)'' >= E<sub>n</sub><br />
** to open long pair position, it is simple enough if the Z-score ''Z(t)'' <= -E<sub>n</sub><br />
* entry mode = '''uptick''': same as simple, but in addition, previous Z-score must be below the entry band (so we cross the band from inside to outside):<br />
** to open short pair position, we require ''Z(t)'' >= E<sub>n</sub> '''and''' ''Z(t-1)'' < E<sub>n</sub><br />
** to open long pair position, we require ''Z(t)'' <= -E<sub>n</sub> '''and''' ''Z(t-1)'' > -E<sub>n</sub><br />
* entry mode = '''downtick''': we wait for the Z-score crossing back the band from outside to inside:<br />
** to open short pair position, we require ''Z(t)'' < E<sub>n</sub> '''and''' ''Z(t-1)'' >= E<sub>n</sub> '''and''' ''Z(t)'' > E<sub>nd</sub><br />
** to open long pair position, we require ''Z(t)'' > -E<sub>n</sub> '''and''' ''Z(t-1)'' <= -E<sub>n</sub> '''and''' ''Z(t)'' < -E<sub>nd</sub><br />
<br />
=== Exiting Position ===<br />
For exiting position, we always use only these simple rules:<br />
* we exit short position when ''Z(t)'' <= E<sub>x</sub><br />
* we exit long position when ''Z(t)'' >= -E<sub>x</sub></div>Adminhttps://wiki.pairtradinglab.com/wiki/Trading_Pairs_Based_on_Inverse_ETFsTrading Pairs Based on Inverse ETFs2016-02-07T15:28:47Z<p>Admin: /* Hints & Tips */</p>
<hr />
<div>Pair Trading Lab now supports trading pairs assembled of negatively correlated instruments where typically one leg is an [http://www.investopedia.com/terms/i/inverse-etf.asp inverse ETF]. You could trade these pairs with regular strategies (sometimes cointegration is manifested in these cases and backtests look good), but there is one problem with this approach - the strategy is '''no longer market neutral'''.<br />
<br />
In order to stay market neutral one has to invert signals for one leg - so typically the strategy is either in long position for '''both legs''' or in short position for '''both legs''', unlike with the regular long/short or short/long scenario.<br />
<br />
For practical reasons to get meaningful signals from models, you typically also need to transform prices of one of legs (to "flip" the one of price streams). Pair Trading Lab now supports both features required. The transformation function is typically P<sub>new</sub> = A /P<sub>old</sub> + B. These coefficients don't really matter, default values are A=1000, B = 0.5. You just want to transform prices in the way the result looks like a real price and it is not too low or too high. In 99% cases you can stick with these defaults.<br />
<br />
In backtester you have separate settings for signal inversion and transformation - you can even try to apply the inversion only or the transformation only. Feel free to experiment. Some pairs work the best if you invert the same leg you transform, other pairs work better if you invert the other leg.<br />
<br />
== Inverted Strategies in Analyzer: Even & Odd ==<br />
While analyzing a pair, the engine now backtests inverted strategies with transformed prices too. It always transforms the first leg. Then it backtests strategies with the first leg inverted. These strategies are marked as '''even'''. Then it backtests strategies with the second leg inverted. These strategies are marked as '''odd'''. Feel free to experiment with analyzing and backtesting bunch of strategies and look what results you get.<br />
<br />
Hint: you can use the pair database to get list of pairs manifesting negative correlation.<br />
<br />
== Example Pair: EUM/EWH ==<br />
Now some example: Let's present EUM/EWH pair assembled from regular ETF and an inverse ETF: https://www.pairtradinglab.com/analyses/VrSMQuM8pGcteuiH.<br />
<br />
You can see prices manifesting negative correlation and &beta; coefficient is negative:<br />
[[File:Eum-ewh-prices.png|frame|center|EUM/EWH prices & correlation]]<br />
<br />
For this pair you can see that regular strategies do not appear to have much success:<br />
[[File:Eum-ewh-profit.png|center]]<br />
<br />
Now, let's try to look at inverted strategies with transformed prices:<br />
[[File:Eum-ewh-inverted-profit.png|center]]<br />
<br />
It looks much better! Lets try to backtest one of the strategies with details: https://www.pairtradinglab.com/backtests/VrSOmXaMl8UYci6D<br />
<br />
You can see quite stable performance for many years. Look at the price transformation:<br />
[[File:Eum-ewh-transformation.png|center|frame|EUR/EWH price transformation P<sub>new</sub> = 1000 / P<sub>old</sub> + 0.1]]<br />
You can notice how the price transformation created a virtual instrument leading to positive correlation in the pair. Now see how this combo is being traded with long/long or short/short positions:<br />
[[File:Eum-ewh-trade-log.png|center|frame|EUR/EWH trade log]]<br />
== Hints & Tips ==<br />
By using inverted strategies with transformed prices you can also achieve mean-reversion pairs trading '''without using real short positions whatsoever'''. Example here:<br />
https://www.pairtradinglab.com/backtests/Vrdh5bxv5LE53pQz</div>Adminhttps://wiki.pairtradinglab.com/wiki/Trading_Pairs_Based_on_Inverse_ETFsTrading Pairs Based on Inverse ETFs2016-02-07T15:28:19Z<p>Admin: </p>
<hr />
<div>Pair Trading Lab now supports trading pairs assembled of negatively correlated instruments where typically one leg is an [http://www.investopedia.com/terms/i/inverse-etf.asp inverse ETF]. You could trade these pairs with regular strategies (sometimes cointegration is manifested in these cases and backtests look good), but there is one problem with this approach - the strategy is '''no longer market neutral'''.<br />
<br />
In order to stay market neutral one has to invert signals for one leg - so typically the strategy is either in long position for '''both legs''' or in short position for '''both legs''', unlike with the regular long/short or short/long scenario.<br />
<br />
For practical reasons to get meaningful signals from models, you typically also need to transform prices of one of legs (to "flip" the one of price streams). Pair Trading Lab now supports both features required. The transformation function is typically P<sub>new</sub> = A /P<sub>old</sub> + B. These coefficients don't really matter, default values are A=1000, B = 0.5. You just want to transform prices in the way the result looks like a real price and it is not too low or too high. In 99% cases you can stick with these defaults.<br />
<br />
In backtester you have separate settings for signal inversion and transformation - you can even try to apply the inversion only or the transformation only. Feel free to experiment. Some pairs work the best if you invert the same leg you transform, other pairs work better if you invert the other leg.<br />
<br />
== Inverted Strategies in Analyzer: Even & Odd ==<br />
While analyzing a pair, the engine now backtests inverted strategies with transformed prices too. It always transforms the first leg. Then it backtests strategies with the first leg inverted. These strategies are marked as '''even'''. Then it backtests strategies with the second leg inverted. These strategies are marked as '''odd'''. Feel free to experiment with analyzing and backtesting bunch of strategies and look what results you get.<br />
<br />
Hint: you can use the pair database to get list of pairs manifesting negative correlation.<br />
<br />
== Example Pair: EUM/EWH ==<br />
Now some example: Let's present EUM/EWH pair assembled from regular ETF and an inverse ETF: https://www.pairtradinglab.com/analyses/VrSMQuM8pGcteuiH.<br />
<br />
You can see prices manifesting negative correlation and &beta; coefficient is negative:<br />
[[File:Eum-ewh-prices.png|frame|center|EUM/EWH prices & correlation]]<br />
<br />
For this pair you can see that regular strategies do not appear to have much success:<br />
[[File:Eum-ewh-profit.png|center]]<br />
<br />
Now, let's try to look at inverted strategies with transformed prices:<br />
[[File:Eum-ewh-inverted-profit.png|center]]<br />
<br />
It looks much better! Lets try to backtest one of the strategies with details: https://www.pairtradinglab.com/backtests/VrSOmXaMl8UYci6D<br />
<br />
You can see quite stable performance for many years. Look at the price transformation:<br />
[[File:Eum-ewh-transformation.png|center|frame|EUR/EWH price transformation P<sub>new</sub> = 1000 / P<sub>old</sub> + 0.1]]<br />
You can notice how the price transformation created a virtual instrument leading to positive correlation in the pair. Now see how this combo is being traded with long/long or short/short positions:<br />
[[File:Eum-ewh-trade-log.png|center|frame|EUR/EWH trade log]]<br />
== Hints & Tips ==<br />
Using inverted strategies with transformed prices you can also achieve mean-reversion pairs trading '''without using real short positions whatsoever'''. Example here:<br />
https://www.pairtradinglab.com/backtests/Vrdh5bxv5LE53pQz</div>Adminhttps://wiki.pairtradinglab.com/wiki/Trading_Pairs_Based_on_Inverse_ETFsTrading Pairs Based on Inverse ETFs2016-02-07T15:14:12Z<p>Admin: /* Inverted Strategies in Analyzer: Even or Odd */</p>
<hr />
<div>Pair Trading Lab now supports trading pairs assembled of negatively correlated instruments where typically one leg is an [http://www.investopedia.com/terms/i/inverse-etf.asp inverse ETF]. You could trade these pairs with regular strategies (sometimes cointegration is manifested in these cases and backtests look good), but there is one problem with this approach - the strategy is '''no longer market neutral'''.<br />
<br />
In order to stay market neutral one has to invert signals for one leg - so typically the strategy is either in long position for '''both legs''' or in short position for '''both legs''', unlike with the regular long/short or short/long scenario.<br />
<br />
For practical reasons to get meaningful signals from models, you typically also need to transform prices of one of legs (to "flip" the one of price streams). Pair Trading Lab now supports both features required. The transformation function is typically P<sub>new</sub> = A /P<sub>old</sub> + B. These coefficients don't really matter, default values are A=1000, B = 0.5. You just want to transform prices in the way the result looks like a real price and it is not too low or too high. In 99% cases you can stick with these defaults.<br />
<br />
In backtester you have separate settings for signal inversion and transformation - you can even try to apply the inversion only or the transformation only. Feel free to experiment. Some pairs work the best if you invert the same leg you transform, other pairs work better if you invert the other leg.<br />
<br />
== Inverted Strategies in Analyzer: Even & Odd ==<br />
While analyzing a pair, the engine now backtests inverted strategies with transformed prices too. It always transforms the first leg. Then it backtests strategies with the first leg inverted. These strategies are marked as '''even'''. Then it backtests strategies with the second leg inverted. These strategies are marked as '''odd'''. Feel free to experiment with analyzing and backtesting bunch of strategies and look what results you get.<br />
<br />
Hint: you can use the pair database to get list of pairs manifesting negative correlation.<br />
<br />
== Example Pair: EUM/EWH ==<br />
Now some example: Let's present EUM/EWH pair assembled from regular ETF and an inverse ETF: https://www.pairtradinglab.com/analyses/VrSMQuM8pGcteuiH.<br />
<br />
You can see prices manifesting negative correlation and &beta; coefficient is negative:<br />
[[File:Eum-ewh-prices.png|frame|center|EUM/EWH prices & correlation]]<br />
<br />
For this pair you can see that regular strategies do not appear to have much success:<br />
[[File:Eum-ewh-profit.png|center]]<br />
<br />
Now, let's try to look at inverted strategies with transformed prices:<br />
[[File:Eum-ewh-inverted-profit.png|center]]<br />
<br />
It looks much better! Lets try to backtest one of the strategies with details: https://www.pairtradinglab.com/backtests/VrSOmXaMl8UYci6D<br />
<br />
You can see quite stable performance for many years. Look at the price transformation:<br />
[[File:Eum-ewh-transformation.png|center|frame|EUR/EWH price transformation P<sub>new</sub> = 1000 / P<sub>old</sub> + 0.1]]<br />
You can notice how the price transformation created a virtual instrument leading to positive correlation in the pair. Now see how this combo is being traded with long/long or short/short positions:<br />
[[File:Eum-ewh-trade-log.png|center|frame|EUR/EWH trade log]]</div>Adminhttps://wiki.pairtradinglab.com/wiki/Trading_Pairs_Based_on_Inverse_ETFsTrading Pairs Based on Inverse ETFs2016-02-07T14:56:50Z<p>Admin: </p>
<hr />
<div>Pair Trading Lab now supports trading pairs assembled of negatively correlated instruments where typically one leg is an [http://www.investopedia.com/terms/i/inverse-etf.asp inverse ETF]. You could trade these pairs with regular strategies (sometimes cointegration is manifested in these cases and backtests look good), but there is one problem with this approach - the strategy is '''no longer market neutral'''.<br />
<br />
In order to stay market neutral one has to invert signals for one leg - so typically the strategy is either in long position for '''both legs''' or in short position for '''both legs''', unlike with the regular long/short or short/long scenario.<br />
<br />
For practical reasons to get meaningful signals from models, you typically also need to transform prices of one of legs (to "flip" the one of price streams). Pair Trading Lab now supports both features required. The transformation function is typically P<sub>new</sub> = A /P<sub>old</sub> + B. These coefficients don't really matter, default values are A=1000, B = 0.5. You just want to transform prices in the way the result looks like a real price and it is not too low or too high. In 99% cases you can stick with these defaults.<br />
<br />
In backtester you have separate settings for signal inversion and transformation - you can even try to apply the inversion only or the transformation only. Feel free to experiment. Some pairs work the best if you invert the same leg you transform, other pairs work better if you invert the other leg.<br />
<br />
== Inverted Strategies in Analyzer: Even or Odd ==<br />
While analyzing a pair, the engine now backtests inverted strategies with transformed prices too. It always transforms the first leg. Then it backtests strategies with the first leg inverted. These strategies are marked as '''even'''. Then it backtests strategies with the second leg inverted. These strategies are marked as '''odd'''. Feel free to experiment with analyzing and backtesting bunch of strategies and look what results you get.<br />
<br />
Hint: you can use the pair database to get list of pairs manifesting negative correlation.<br />
<br />
== Example Pair: EUM/EWH ==<br />
Now some example: Let's present EUM/EWH pair assembled from regular ETF and an inverse ETF: https://www.pairtradinglab.com/analyses/VrSMQuM8pGcteuiH.<br />
<br />
You can see prices manifesting negative correlation and &beta; coefficient is negative:<br />
[[File:Eum-ewh-prices.png|frame|center|EUM/EWH prices & correlation]]<br />
<br />
For this pair you can see that regular strategies do not appear to have much success:<br />
[[File:Eum-ewh-profit.png|center]]<br />
<br />
Now, let's try to look at inverted strategies with transformed prices:<br />
[[File:Eum-ewh-inverted-profit.png|center]]<br />
<br />
It looks much better! Lets try to backtest one of the strategies with details: https://www.pairtradinglab.com/backtests/VrSOmXaMl8UYci6D<br />
<br />
You can see quite stable performance for many years. Look at the price transformation:<br />
[[File:Eum-ewh-transformation.png|center|frame|EUR/EWH price transformation P<sub>new</sub> = 1000 / P<sub>old</sub> + 0.1]]<br />
You can notice how the price transformation created a virtual instrument leading to positive correlation in the pair. Now see how this combo is being traded with long/long or short/short positions:<br />
[[File:Eum-ewh-trade-log.png|center|frame|EUR/EWH trade log]]</div>Adminhttps://wiki.pairtradinglab.com/wiki/Trading_Pairs_Based_on_Inverse_ETFsTrading Pairs Based on Inverse ETFs2016-02-07T14:35:03Z<p>Admin: </p>
<hr />
<div>'''(this page is under construction)'''<br />
<br />
Pair Trading Lab now supports trading pairs assembled of negatively correlated instruments where typically one leg is an [http://www.investopedia.com/terms/i/inverse-etf.asp inverse ETF]. You could trade these pairs with regular strategies (sometimes cointegration is manifested in these cases and backtests look good), but there is one problem with this approach - the strategy is '''no longer market neutral'''.<br />
<br />
In order to stay market neutral one has to invert signals for one leg - so typically the strategy is either in long position for '''both legs''' or in short position for '''both legs''', unlike with the regular long/short or short/long scenario.<br />
<br />
For practical reasons to get meaningful signals from models, you typically also need to transform prices of one of legs (to "flip" the one of price streams). Pair Trading Lab now supports both features required. The transformation function is typically P<sub>new</sub> = A /P<sub>old</sub> + B. These coefficients don't really matter, default values are A=1000, B = 0.5. You just want to transform prices in the way the result looks like a real price and it is not too low or too high. In 99% cases you can stick with these defaults.<br />
== Example Pair: EUM/EWH ==<br />
Now some example: Let's present EUM/EWH pair assembled from regular ETF and an inverse ETF: https://www.pairtradinglab.com/analyses/VrSMQuM8pGcteuiH.<br />
<br />
You can see prices manifesting negative correlation and &beta; coefficient is negative:<br />
[[File:Eum-ewh-prices.png|frame|center|EUM/EWH prices & correlation]]<br />
<br />
For this pair you can see that regular strategies do not appear to have much success:<br />
[[File:Eum-ewh-profit.png|center]]<br />
<br />
Now, let's try to look at inverted strategies with transformed prices:<br />
[[File:Eum-ewh-inverted-profit.png|center]]<br />
<br />
It looks much better! Lets try to backtest one of the strategies with details: https://www.pairtradinglab.com/backtests/VrSOmXaMl8UYci6D<br />
<br />
You can see quite stable performance for many years. Look at the price transformation:<br />
[[File:Eum-ewh-transformation.png|center|frame|EUR/EWH price transformation P<sub>new</sub> = 1000 / P<sub>old</sub> + 0.1]]<br />
You can notice how the price transformation created a virtual instrument leading to positive correlation in the pair. Now see how this combo is being traded with long/long or short/short positions:<br />
[[File:Eum-ewh-trade-log.png|center|frame|EUR/EWH trade log]]</div>Adminhttps://wiki.pairtradinglab.com/wiki/File:Eum-ewh-trade-log.pngFile:Eum-ewh-trade-log.png2016-02-07T14:29:04Z<p>Admin: </p>
<hr />
<div></div>Adminhttps://wiki.pairtradinglab.com/wiki/Trading_Pairs_Based_on_Inverse_ETFsTrading Pairs Based on Inverse ETFs2016-02-07T14:28:54Z<p>Admin: </p>
<hr />
<div>'''(this page is under construction)'''<br />
<br />
Pair Trading Lab now supports trading pairs assembled of negatively correlated instruments where typically one leg is an [http://www.investopedia.com/terms/i/inverse-etf.asp inverse ETF]. You could trade these pairs with regular strategies (sometimes cointegration is manifested in these cases and backtests look good), but there is one problem with this approach - the strategy is '''no longer market neutral'''.<br />
<br />
In order to stay market neutral one has to invert signal for one leg - so typically one is either in long position for '''both legs''' or in short position for '''both legs''', unline with the regular long/short or short/long scenario.<br />
<br />
For practical reasons to get meaningful signals from models, you typically also need to transform prices of one of the leg (to "flip" prices). Pair Trading Lab now supports both features required. The transformation function is typically P<sub>new</sub> = A /P<sub>old</sub> + B. These coefficients don't really matter, default values are A=1000, B = 0.5. You just want to transform prices in the way the result looks like a real price and it is not too low or too high. In 99% cases you can stick with these defaults.<br />
<br />
Now some example: This is a pair EUM/EWH assembled from regular ETF and an inverse ETF: https://www.pairtradinglab.com/analyses/VrSMQuM8pGcteuiH.<br />
<br />
You can see prices manifesting negative correlation and &beta; coefficient is negative:<br />
[[File:Eum-ewh-prices.png|frame|center|EUM/EWH prices & correlation]]<br />
<br />
For this pair you can see that regular strategies do not appear to have much success:<br />
[[File:Eum-ewh-profit.png|center]]<br />
<br />
Now, let's try to look at inverted strategies with transformed prices:<br />
[[File:Eum-ewh-inverted-profit.png|center]]<br />
<br />
It looks much better! Lets try to backtest one of the strategies with details: https://www.pairtradinglab.com/backtests/VrSOmXaMl8UYci6D<br />
<br />
You can see quite stable performance for many years. Look at the price transformation:<br />
[[File:Eum-ewh-transformation.png|center|frame|EUR/EWH price transformation P<sub>new</sub> = 1000 / P<sub>old</sub> + 0.1]]<br />
You can notice how the price transformation created a virtual instrument leading to positive correlation in the pair. Now see how this combo is being traded with long/long or short/short positions:</div>Adminhttps://wiki.pairtradinglab.com/wiki/Trading_Pairs_Based_on_Inverse_ETFsTrading Pairs Based on Inverse ETFs2016-02-07T14:26:46Z<p>Admin: </p>
<hr />
<div>'''(this page is under construction)'''<br />
<br />
Pair Trading Lab now supports trading pairs assembled of negatively correlated instruments where typically one leg is an [http://www.investopedia.com/terms/i/inverse-etf.asp inverse ETF]. You could trade these pairs with regular strategies (sometimes cointegration is manifested in these cases and backtests look good), but there is one problem with this approach - the strategy is '''no longer market neutral'''.<br />
<br />
In order to stay market neutral one has to invert signal for one leg - so typically one is either in long position for '''both legs''' or in short position for '''both legs''', unline with the regular long/short or short/long scenario.<br />
<br />
For practical reasons to get meaningful signals from models, you typically also need to transform prices of one of the leg (to "flip" prices). Pair Trading Lab now supports both features required. The transformation function is typically P<sub>new</sub> = A /P<sub>old</sub> + B. These coefficients don't really matter, default values are A=1000, B = 0.5. You just want to transform prices in the way the result looks like a real price and it is not too low or too high. In 99% cases you can stick with these defaults.<br />
<br />
Now some example: This is a pair EUM/EWH assembled from regular ETF and an inverse ETF: https://www.pairtradinglab.com/analyses/VrSMQuM8pGcteuiH.<br />
<br />
You can see prices manifesting negative correlation and &beta; coefficient is negative:<br />
[[File:Eum-ewh-prices.png|frame|center|EUM/EWH prices & correlation]]<br />
<br />
For this pair you can see that regular strategies do not appear to have much success:<br />
[[File:Eum-ewh-profit.png|center]]<br />
<br />
Now, let's try to look at inverted strategies with transformed prices:<br />
[[File:Eum-ewh-inverted-profit.png|center]]<br />
<br />
It looks much better! Lets try to backtest one of the strategies with details: https://www.pairtradinglab.com/backtests/VrSOmXaMl8UYci6D<br />
<br />
You can see quite stable performance for many years. Look at the price transformation:<br />
[[File:Eum-ewh-transformation.png|center]]<br />
You can notice how the price transformation created a virtual instrument leading to positive correlation in the pair. Now see how this combo is being traded with long/long or short/short positions:</div>Adminhttps://wiki.pairtradinglab.com/wiki/File:Eum-ewh-transformation.pngFile:Eum-ewh-transformation.png2016-02-07T14:24:32Z<p>Admin: </p>
<hr />
<div></div>Adminhttps://wiki.pairtradinglab.com/wiki/Trading_Pairs_Based_on_Inverse_ETFsTrading Pairs Based on Inverse ETFs2016-02-07T14:23:49Z<p>Admin: </p>
<hr />
<div>'''(this page is under construction)'''<br />
<br />
Pair Trading Lab now supports trading pairs assembled of negatively correlated instruments where typically one leg is an [http://www.investopedia.com/terms/i/inverse-etf.asp inverse ETF]. You could trade these pairs with regular strategies (sometimes cointegration is manifested in these cases and backtests look good), but there is one problem with this approach - the strategy is '''no longer market neutral'''.<br />
<br />
In order to stay market neutral one has to invert signal for one leg - so typically one is either in long position for '''both legs''' or in short position for '''both legs''', unline with the regular long/short or short/long scenario.<br />
<br />
For practical reasons to get meaningful signals from models, you typically also need to transform prices of one of the leg (to "flip" prices). Pair Trading Lab now supports both features required. The transformation function is typically P<sub>new</sub> = A /P<sub>old</sub> + B. These coefficients don't really matter, default values are A=1000, B = 0.5. You just want to transform prices in the way the result looks like a real price and it is not too low or too high. In 99% cases you can stick with these defaults.<br />
<br />
Now some example: This is a pair EUM/EWH assembled from regular ETF and an inverse ETF: https://www.pairtradinglab.com/analyses/VrSMQuM8pGcteuiH.<br />
<br />
You can see prices manifesting negative correlation and &beta; coefficient is negative:<br />
[[File:Eum-ewh-prices.png|frame|center|EUM/EWH prices & correlation]]<br />
<br />
For this pair you can see that regular strategies do not appear to have much success:<br />
[[File:Eum-ewh-profit.png|center]]<br />
<br />
Now, let's try to look at inverted strategies with transformed prices:<br />
[[File:Eum-ewh-inverted-profit.png|center]]<br />
<br />
It looks much better! Lets try to backtest one of the strategies with details: https://www.pairtradinglab.com/backtests/VrSOmXaMl8UYci6D<br />
<br />
You can see quite stable performance for many years. Look at the price transformation:</div>Adminhttps://wiki.pairtradinglab.com/wiki/File:Eum-ewh-inverted-profit.pngFile:Eum-ewh-inverted-profit.png2016-02-07T14:13:40Z<p>Admin: </p>
<hr />
<div></div>Adminhttps://wiki.pairtradinglab.com/wiki/Trading_Pairs_Based_on_Inverse_ETFsTrading Pairs Based on Inverse ETFs2016-02-07T14:12:08Z<p>Admin: </p>
<hr />
<div>'''(this page is under construction)'''<br />
<br />
Pair Trading Lab now supports trading pairs assembled of negatively correlated instruments where typically one leg is an [http://www.investopedia.com/terms/i/inverse-etf.asp inverse ETF]. You could trade these pairs with regular strategies (sometimes cointegration is manifested in these cases and backtests look good), but there is one problem with this approach - the strategy is '''no longer market neutral'''.<br />
<br />
In order to stay market neutral one has to invert signal for one leg - so typically one is either in long position for '''both legs''' or in short position for '''both legs''', unline with the regular long/short or short/long scenario.<br />
<br />
For practical reasons to get meaningful signals from models, you typically also need to transform prices of one of the leg (to "flip" prices). Pair Trading Lab now supports both features required. The transformation function is typically P<sub>new</sub> = A /P<sub>old</sub> + B. These coefficients don't really matter, default values are A=1000, B = 0.5. You just want to transform prices in the way the result looks like a real price and it is not too low or too high. In 99% cases you can stick with these defaults.<br />
<br />
Now some example: This is a pair EUM/EWH assembled from regular ETF and an inverse ETF: https://www.pairtradinglab.com/analyses/VrSMQuM8pGcteuiH.<br />
<br />
You can see prices manifesting negative correlation and &beta; coefficient is negative:<br />
[[File:Eum-ewh-prices.png|center]]<br />
<br />
For this pair you can see that regular strategies do not appear to have much success:<br />
[[File:Eum-ewh-profit.png|center]]<br />
<br />
Now, let's try to look at inverted strategies with transformed prices:<br />
<br />
It looks much better.</div>Adminhttps://wiki.pairtradinglab.com/wiki/File:Eum-ewh-profit.pngFile:Eum-ewh-profit.png2016-02-07T14:11:23Z<p>Admin: </p>
<hr />
<div></div>Adminhttps://wiki.pairtradinglab.com/wiki/Trading_Pairs_Based_on_Inverse_ETFsTrading Pairs Based on Inverse ETFs2016-02-07T14:10:15Z<p>Admin: </p>
<hr />
<div>'''(this page is under construction)'''<br />
<br />
Pair Trading Lab now supports trading pairs assembled of negatively correlated instruments where typically one leg is an [http://www.investopedia.com/terms/i/inverse-etf.asp inverse ETF]. You could trade these pairs with regular strategies (sometimes cointegration is manifested in these cases and backtests look good), but there is one problem with this approach - the strategy is '''no longer market neutral'''.<br />
<br />
In order to stay market neutral one has to invert signal for one leg - so typically one is either in long position for '''both legs''' or in short position for '''both legs''', unline with the regular long/short or short/long scenario.<br />
<br />
For practical reasons to get meaningful signals from models, you typically also need to transform prices of one of the leg (to "flip" prices). Pair Trading Lab now supports both features required. The transformation function is typically P<sub>new</sub> = A /P<sub>old</sub> + B. These coefficients don't really matter, default values are A=1000, B = 0.5. You just want to transform prices in the way the result looks like a real price and it is not too low or too high. In 99% cases you can stick with these defaults.<br />
<br />
Now some example: This is a pair EUM/EWH assembled from regular ETF and an inverse ETF: https://www.pairtradinglab.com/analyses/VrSMQuM8pGcteuiH.<br />
<br />
You can see prices manifesting negative correlation and &beta; coefficient is negative:<br />
[[File:Eum-ewh-prices.png|center]]<br />
<br />
For this pair you can see that regular strategies do not appear to have much success:<br />
<br />
Now, let's try to look at inverted strategies with transformed prices:<br />
<br />
It looks much better.</div>Adminhttps://wiki.pairtradinglab.com/wiki/File:Eum-ewh-prices.pngFile:Eum-ewh-prices.png2016-02-07T14:07:47Z<p>Admin: </p>
<hr />
<div></div>Adminhttps://wiki.pairtradinglab.com/wiki/Trading_Pairs_Based_on_Inverse_ETFsTrading Pairs Based on Inverse ETFs2016-02-07T14:06:29Z<p>Admin: </p>
<hr />
<div>'''(this page is under construction)'''<br />
<br />
Pair Trading Lab now supports trading pairs assembled of negatively correlated instruments where typically one leg is an [http://www.investopedia.com/terms/i/inverse-etf.asp inverse ETF]. You could trade these pairs with regular strategies (sometimes cointegration is manifested in these cases and backtests look good), but there is one problem with this approach - the strategy is '''no longer market neutral'''.<br />
<br />
In order to stay market neutral one has to invert signal for one leg - so typically one is either in long position for '''both legs''' or in short position for '''both legs''', unline with the regular long/short or short/long scenario.<br />
<br />
For practical reasons to get meaningful signals from models, you typically also need to transform prices of one of the leg (to "flip" prices). Pair Trading Lab now supports both features required. The transformation function is typically P<sub>new</sub> = A /P<sub>old</sub> + B. These coefficients don't really matter, default values are A=1000, B = 0.5. You just want to transform prices in the way the result looks like a real price and it is not too low or too high. In 99% cases you can stick with these defaults.<br />
<br />
Now some example: This is a pair EUM/EWH assembled from regular ETF and an inverse ETF: https://www.pairtradinglab.com/analyses/VrSMQuM8pGcteuiH.<br />
<br />
You can see prices manifesting negative correlation and &beta; coefficient is negative:<br />
<br />
For this pair you can see that regular strategies do not appear to have much success:<br />
<br />
Now, let's try to look at inverted strategies with transformed prices:<br />
<br />
It looks much better.</div>Admin