FRAMA Part II: Replicating A Simple Strategy

[This article was first published on QuantStrat TradeR » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This post will begin the investigation into FRAMA strategies, with the aim of ultimately finding a FRAMA trading strategy with less market exposure, fewer whipsaw trades, and fewer counter-trend trades. This post will also introduce new analytics regarding trade duration.

To begin the investigation into developing strategies based on the previously-introduced FRAMA, I’m going to replicate the simple strategy from ETFHQ — use a 126 day FRAMA with a fast constant of 4 (that is, an EMA that goes as fast as a 4-day EMA), and all the way up to a slow constant of 300. For my ATR order-sizing, which, once again, was inspired by Andreas Clenow in the post on leverage being pointless, I’m going to use 2 percent of notional capital, with a 10 day ATR for my order sizing (ATR 20 and 30 display slightly weaker results, but nevertheless, are very close in performance).

Once again, let’s start by looking at the strategy, using our same 30 instruments as with our TVI demos (I thought about testing on mutual funds, but due to the obnoxious fees that mutual funds charge for trying to trade with them, I feel that I’d have to employ too much magical thinking to neglect their obscene trading transaction costs):

require(DSTrading)
require(IKTrading)
require(quantstrat)

initDate="1990-01-01"
from="2003-01-01"
to="2010-12-31"
options(width=70)

#to rerun the strategy, rerun everything below this line
source("demoData.R") #contains all of the data-related boilerplate.

#trade sizing and initial equity settings
tradeSize <- 10000
initEq <- tradeSize*length(symbols)

strategy.st <- portfolio.st <- account.st <- "FRAMA_I"
rm.strat(portfolio.st)
rm.strat(strategy.st)
initPortf(portfolio.st, symbols=symbols, initDate=initDate, currency='USD')
initAcct(account.st, portfolios=portfolio.st, initDate=initDate, currency='USD',initEq=initEq)
initOrders(portfolio.st, initDate=initDate)
strategy(strategy.st, store=TRUE)

#Parameters

FC=4
SC=300
n=126
triggerLag=1
pctATR=.02
period=10

#indicators

add.indicator(strategy.st, name="FRAMA", 
              arguments=list(HLC=quote(HLC(mktdata)),n=n, 
                             FC=FC, SC=SC, triggerLag=triggerLag),
              label="frama")

add.indicator(strategy.st, name="lagATR", 
              arguments=list(HLC=quote(HLC(mktdata)), n=period), 
              label="atrX")


#signals

add.signal(strategy.st, name="sigCrossover",
           arguments=list(columns=c("Close", "FRAMA.frama"), relationship="gte"),
           label="longEntry")

add.signal(strategy.st, name="sigCrossover",
           arguments=list(columns=c("Close", "FRAMA.frama"), relationship="lt"),
           label="longExit")

#rules

add.rule(strategy.st, name="ruleSignal", 
         arguments=list(sigcol="longEntry", sigval=TRUE, ordertype="market", 
                        orderside="long", replace=FALSE, prefer="Open", osFUN=osDollarATR,
                        tradeSize=tradeSize, pctATR=pctATR, atrMod="X"), 
         type="enter", path.dep=TRUE)

add.rule(strategy.st, name="ruleSignal", 
         arguments=list(sigcol="longExit", sigval=TRUE, orderqty="all", ordertype="market", 
                        orderside="long", replace=FALSE, prefer="Open"), 
         type="exit", path.dep=TRUE)

#apply strategy
t1 <- Sys.time()
out <- applyStrategy(strategy=strategy.st,portfolios=portfolio.st)
t2 <- Sys.time()
print(t2-t1)


#set up analytics
updatePortf(portfolio.st)
dateRange <- time(getPortfolio(portfolio.st)$summary)[-1]
updateAcct(portfolio.st,dateRange)
updateEndEq(account.st)

It’s a fairly simple strategy–buy the next day’s open when the price crosses above the indicator, and vice versa. In other words, it’s about as simple a strategy as you can get as its sole purpose was to demonstrate the effectiveness of the indicator. And while someone interested can peruse through ETFHQ to find all of the indicator relative tests, the general gist is that adaptive moving averages work very well, and the FRAMA (the fractal adaptive moving average) works slightly better than the rest. Ultimately though, if one believes ETFHQ’s analysis (and I do), then even one good trend-following indicator will be sufficient.

Here are the trade statistics:

                         EFA       EPP       EWA      EWC       EWG
Num.Txns              289.00    245.00    281.00   219.00    248.00
Num.Trades            145.00    123.00    140.00   109.00    121.00
Net.Trading.PL       9233.11  17020.32  14917.84 14298.85  13603.78
Avg.Trade.PL           63.68    138.38    106.56   131.18    112.43
Med.Trade.PL          -46.29    -26.79    -24.81   -21.06    -28.14
Largest.Winner       2454.58   4499.10   3301.82  4031.95   3582.43
Largest.Loser        -523.15   -847.71   -607.41  -613.76   -531.39
Gross.Profits       23874.49  29202.05  28060.29 23541.10  24820.41
Gross.Losses       -14641.38 -12181.73 -13142.45 -9242.26 -11216.63
Std.Dev.Trade.PL      420.81    624.11    518.71   565.75    565.31
Percent.Positive       42.07     46.34     46.43    46.79     46.28
Percent.Negative       57.93     53.66     53.57    53.21     53.72
Profit.Factor           1.63      2.40      2.14     2.55      2.21
Avg.Win.Trade         391.39    512.32    431.70   461.59    443.22
Med.Win.Trade         199.18    238.18    220.98   236.81    179.68
Avg.Losing.Trade     -174.30   -184.57   -175.23  -159.35   -172.56
Med.Losing.Trade     -152.31   -121.13   -159.74  -133.38   -128.70
Avg.Daily.PL           63.07    137.42    105.72   128.59    112.43
Med.Daily.PL          -49.26    -29.12    -27.68   -22.15    -28.14
Std.Dev.Daily.PL      422.22    626.59    520.49   567.74    565.31
Ann.Sharpe              2.37      3.48      3.22     3.60      3.16
Max.Drawdown        -2338.49  -1945.85  -3026.93 -2001.41  -2047.83
Profit.To.Max.Draw      3.95      8.75      4.93     7.14      6.64
Avg.WinLoss.Ratio       2.25      2.78      2.46     2.90      2.57
Med.WinLoss.Ratio       1.31      1.97      1.38     1.78      1.40
Max.Equity           9902.70  17058.92  14996.93 14298.85  14604.43
Min.Equity           -136.30   -189.12   -159.59  -233.07   -315.54
End.Equity           9233.11  17020.32  14917.84 14298.85  13603.78

                         EWH       EWJ       EWS      EWT      EWU
Num.Txns              271.00    243.00    275.00   199.00   227.00
Num.Trades            135.00    119.00    133.00    96.00   111.00
Net.Trading.PL      11548.79   5965.14  12676.51 12221.36 12044.63
Avg.Trade.PL           85.55     50.13     95.31   127.31   108.51
Med.Trade.PL          -27.05    -26.14    -12.78    14.04    -7.08
Largest.Winner       2668.92   1872.67   2773.40  2749.00  2521.64
Largest.Loser        -572.88   -521.96   -593.95  -537.06  -603.96
Gross.Profits       23173.00  17577.09  23801.66 19413.97 21927.72
Gross.Losses       -11624.21 -11611.96 -11125.16 -7192.61 -9883.09
Std.Dev.Trade.PL      501.77    395.06    470.11   506.25   512.20
Percent.Positive       44.44     43.70     46.62    53.12    49.55
Percent.Negative       55.56     56.30     53.38    46.88    50.45
Profit.Factor           1.99      1.51      2.14     2.70     2.22
Avg.Win.Trade         386.22    338.02    383.90   380.67   398.69
Med.Win.Trade         125.17    140.16    212.49   172.84   195.04
Avg.Losing.Trade     -154.99   -173.31   -156.69  -159.84  -176.48
Med.Losing.Trade     -111.58   -146.64   -115.28  -138.75  -158.23
Avg.Daily.PL           86.78     38.81     94.45   108.42   108.20
Med.Daily.PL          -25.57    -32.55    -17.73    12.24    -7.80
Std.Dev.Daily.PL      503.45    376.87    471.79   473.73   514.53
Ann.Sharpe              2.74      1.63      3.18     3.63     3.34
Max.Drawdown        -2298.32  -3445.56  -2017.06 -2764.34 -2071.61
Profit.To.Max.Draw      5.02      1.73      6.28     4.42     5.81
Avg.WinLoss.Ratio       2.49      1.95      2.45     2.38     2.26
Med.WinLoss.Ratio       1.12      0.96      1.84     1.25     1.23
Max.Equity          12708.66   6336.41  13177.76 12221.36 12640.57
Min.Equity           -272.68   -307.36   -331.04     0.00   -57.47
End.Equity          11548.79   5965.14  12676.51 12221.36 12044.63

                         EWY       EWZ       EZU      IEF       IGE
Num.Txns              261.00    265.00    270.00   216.00    249.00
Num.Trades            130.00    133.00    134.00   107.00    125.00
Net.Trading.PL      10513.27  14496.80  11233.90 13370.63  12428.65
Avg.Trade.PL           80.87    109.00     83.84   124.96     99.43
Med.Trade.PL           21.32    -42.35    -34.59   -41.49    -93.07
Largest.Winner       1550.67   2633.38   3163.76  2799.11   3710.24
Largest.Loser        -614.98   -682.75   -553.54  -542.02   -576.53
Gross.Profits       23615.06  28466.25  25227.96 22877.10  26829.04
Gross.Losses       -13101.78 -13969.45 -13994.06 -9506.47 -14400.39
Std.Dev.Trade.PL      417.96    548.34    538.74   546.44    660.65
Percent.Positive       50.77     43.61     42.54    42.06     32.80
Percent.Negative       49.23     56.39     57.46    57.94     67.20
Profit.Factor           1.80      2.04      1.80     2.41      1.86
Avg.Win.Trade         357.80    490.80    442.60   508.38    654.37
Med.Win.Trade         207.28    210.90    194.52   221.16    367.42
Avg.Losing.Trade     -204.72   -186.26   -181.74  -153.33   -171.43
Med.Losing.Trade     -187.43   -163.60   -144.77  -129.05   -154.06
Avg.Daily.PL           77.86    105.49     83.84   124.96     77.08
Med.Daily.PL           19.08    -44.57    -34.59   -41.49    -93.37
Std.Dev.Daily.PL      418.17    548.93    538.74   546.44    614.05
Ann.Sharpe              2.96      3.05      2.47     3.63      1.99
Max.Drawdown        -2462.61  -2188.41  -2310.88 -2650.59  -3045.12
Profit.To.Max.Draw      4.27      6.62      4.86     5.04      4.08
Avg.WinLoss.Ratio       1.75      2.64      2.44     3.32      3.82
Med.WinLoss.Ratio       1.11      1.29      1.34     1.71      2.38
Max.Equity          10513.27  14760.11  12629.07 14339.37  12428.65
Min.Equity           -339.01   -214.04   -203.63  -278.08   -268.60
End.Equity          10513.27  14496.80  11233.90 13370.63  12428.65

                         IYR       IYZ      LQD       RWR       SHY
Num.Txns              253.00    257.00   222.00    267.00    300.00
Num.Trades            125.00    127.00   108.00    133.00    143.00
Net.Trading.PL       9702.92   8599.09 13277.24  12090.68  15021.96
Avg.Trade.PL           77.62     67.71   122.94     90.91    105.05
Med.Trade.PL          -60.97    -47.00   -17.31    -63.38    -41.73
Largest.Winner       2602.17   2511.55  2526.23   2810.42   1987.06
Largest.Loser        -864.63   -619.97  -339.49   -758.94   -971.00
Gross.Profits       22264.43  20356.47 20777.57  25087.09  30006.10
Gross.Losses       -12561.51 -11757.38 -7500.32 -12996.41 -14984.14
Std.Dev.Trade.PL      488.33    426.35   486.90    528.23    499.11
Percent.Positive       36.80     43.31    46.30     39.10     44.06
Percent.Negative       63.20     56.69    53.70     60.90     55.94
Profit.Factor           1.77      1.73     2.77      1.93      2.00
Avg.Win.Trade         484.01    370.12   415.55    482.44    476.29
Med.Win.Trade         313.81    182.80   139.12    228.24    229.73
Avg.Losing.Trade     -159.01   -163.30  -129.32   -160.45   -187.30
Med.Losing.Trade     -126.56   -112.84  -103.66   -147.59   -145.26
Avg.Daily.PL           75.21     66.83   122.94     88.59    105.05
Med.Daily.PL          -61.69    -50.50   -17.31    -65.60    -41.73
Std.Dev.Daily.PL      489.56    427.93   486.90    529.57    499.11
Ann.Sharpe              2.44      2.48     4.01      2.66      3.34
Max.Drawdown        -5488.96  -2253.39 -1261.10  -5062.88  -2852.91
Profit.To.Max.Draw      1.77      3.82    10.53      2.39      5.27
Avg.WinLoss.Ratio       3.04      2.27     3.21      3.01      2.54
Med.WinLoss.Ratio       2.48      1.62     1.34      1.55      1.58
Max.Equity          13313.44   9225.39 14084.98  15192.88  16570.24
Min.Equity           -152.91   -522.27  -347.48   -394.76  -1036.81
End.Equity           9702.92   8599.09 13277.24  12090.68  15021.96

                         TLT       XLB       XLE       XLF       XLI
Num.Txns              238.00    247.00    229.00    245.00    261.00
Num.Trades            118.00    122.00    115.00    121.00    130.00
Net.Trading.PL       7117.62   6227.48  10335.40     -8.45   6351.34
Avg.Trade.PL           60.32     51.04     89.87     -0.07     48.86
Med.Trade.PL          -71.33    -62.80    -65.87    -56.76    -45.18
Largest.Winner       2548.77   2083.24   2407.81   1895.31   1571.81
Largest.Loser        -509.75   -544.43   -484.90   -626.99   -465.20
Gross.Profits       20099.36  19500.08  21827.84  12869.00  17364.07
Gross.Losses       -12981.73 -13272.60 -11492.44 -12877.45 -11012.73
Std.Dev.Trade.PL      506.64    421.98    566.48    345.43    379.26
Percent.Positive       31.36     36.89     34.78     35.54     41.54
Percent.Negative       68.64     63.11     65.22     64.46     58.46
Profit.Factor           1.55      1.47      1.90      1.00      1.58
Avg.Win.Trade         543.23    433.34    545.70    299.28    321.56
Med.Win.Trade         230.04    248.59    241.31    153.68    129.61
Avg.Losing.Trade     -160.27   -172.37   -153.23   -165.10   -144.90
Med.Losing.Trade     -130.27   -159.19   -148.44   -134.74   -131.29
Avg.Daily.PL           60.32     47.96     68.35     -8.00     34.56
Med.Daily.PL          -71.33    -65.49    -66.25    -58.07    -46.28
Std.Dev.Daily.PL      506.64    422.35    519.59    335.64    343.76
Ann.Sharpe              1.89      1.80      2.09     -0.38      1.60
Max.Drawdown        -4938.54  -3692.51  -2780.65  -5704.14  -3013.95
Profit.To.Max.Draw      1.44      1.69      3.72      0.00      2.11
Avg.WinLoss.Ratio       3.39      2.51      3.56      1.81      2.22
Med.WinLoss.Ratio       1.77      1.56      1.63      1.14      0.99
Max.Equity           9318.18   6780.72  10335.40   4191.68   6357.94
Min.Equity           -693.51   -213.25   -557.67  -1512.46   -795.23
End.Equity           7117.62   6227.48  10335.40     -8.45   6351.34

                         XLK       XLP       XLU       XLV       XLY
Num.Txns              254.00    280.00    241.00    218.00    220.00
Num.Trades            127.00    137.00    121.00    107.00    109.00
Net.Trading.PL       2940.29   2543.61   7904.42   3189.25   6727.03
Avg.Trade.PL           23.15     18.57     65.33     29.81     61.72
Med.Trade.PL          -72.35    -85.92    -38.84    -88.65    -71.80
Largest.Winner       2050.23   1782.84   1240.83   1927.28   2667.81
Largest.Loser        -749.16   -679.39   -598.65   -662.12   -559.61
Gross.Profits       16321.02  17361.88  18269.97  14178.93  18237.47
Gross.Losses       -13380.73 -14818.27 -10365.55 -10989.67 -11510.44
Std.Dev.Trade.PL      389.23    363.63    363.21    418.10    525.74
Percent.Positive       35.43     32.12     43.80     28.97     39.45
Percent.Negative       64.57     67.88     56.20     71.03     60.55
Profit.Factor           1.22      1.17      1.76      1.29      1.58
Avg.Win.Trade         362.69    394.59    344.72    457.38    424.13
Med.Win.Trade         158.36    236.63    180.99    238.62    159.38
Avg.Losing.Trade     -163.18   -159.34   -152.43   -144.60   -174.40
Med.Losing.Trade     -135.65   -131.38   -124.59   -122.90   -146.41
Avg.Daily.PL           23.15     18.57     63.70     29.81     61.72
Med.Daily.PL          -72.35    -85.92    -41.32    -88.65    -71.80
Std.Dev.Daily.PL      389.23    363.63    364.29    418.10    525.74
Ann.Sharpe              0.94      0.81      2.78      1.13      1.86
Max.Drawdown        -3074.10  -4531.59  -2545.84  -4623.78  -4041.34
Profit.To.Max.Draw      0.96      0.56      3.10      0.69      1.66
Avg.WinLoss.Ratio       2.22      2.48      2.26      3.16      2.43
Med.WinLoss.Ratio       1.17      1.80      1.45      1.94      1.09
Max.Equity           2981.10   2993.23   8632.88   3902.68   6838.89
Min.Equity          -1716.68  -1538.36   -165.00  -1213.50   -310.63
End.Equity           2940.29   2543.61   7904.42   3189.25   6727.03

And the aggregate trade statistics:

> (aggPF <- sum(tStats$Gross.Profits)/-sum(tStats$Gross.Losses))
[1] 1.828178
> (aggCorrect <- mean(tStats$Percent.Positive))
[1] 41.55233
> (numTrades <- sum(tStats$Num.Trades))
[1] 3704
> (meanAvgWLR <- mean(tStats$Avg.WinLoss.Ratio))
[1] 2.619

Far from spectacular. Less than 50% hit rate, the profit factor definitely indicates that there is massive room for improvement as well.

Here are the daily statistics:

                         EFA       EPP       EWA       EWC       EWG
Total.Net.Profit     9233.11  17020.32  14917.84  14298.85  13603.78
Total.Days           1280.00   1330.00   1330.00   1296.00   1264.00
Winning.Days          690.00    729.00    725.00    719.00    702.00
Losing.Days           590.00    601.00    605.00    577.00    562.00
Avg.Day.PL              7.21     12.80     11.22     11.03     10.76
Med.Day.PL             12.85     16.84     17.06     18.59     21.33
Largest.Winner        725.53    521.47    428.25    648.73    510.14
Largest.Loser        -973.81  -1162.04   -722.38   -505.11   -841.33
Gross.Profits       72427.12  85143.30  80490.48  70817.88  75036.35
Gross.Losses       -63194.01 -68122.98 -65572.64 -56519.03 -61432.57
Std.Dev.Daily.PL      139.51    153.35    140.28    125.46    141.11
Percent.Positive       53.91     54.81     54.51     55.48     55.54
Percent.Negative       46.09     45.19     45.49     44.52     44.46
Profit.Factor           1.15      1.25      1.23      1.25      1.22
Avg.Win.Day           104.97    116.79    111.02     98.49    106.89
Med.Win.Day            83.48     95.05     90.88     83.65     84.31
Avg.Losing.Day       -107.11   -113.35   -108.38    -97.95   -109.31
Med.Losing.Day        -81.61    -83.94    -84.18    -72.76    -84.31
Avg.Daily.PL            7.21     12.80     11.22     11.03     10.76
Med.Daily.PL           12.85     16.84     17.06     18.59     21.33
Std.Dev.Daily.PL.1    139.51    153.35    140.28    125.46    141.11
Ann.Sharpe              0.82      1.32      1.27      1.40      1.21
Max.Drawdown        -2338.49  -1945.85  -3026.93  -2001.41  -2047.83
Profit.To.Max.Draw      3.95      8.75      4.93      7.14      6.64
Avg.WinLoss.Ratio       0.98      1.03      1.02      1.01      0.98
Med.WinLoss.Ratio       1.02      1.13      1.08      1.15      1.00
Max.Equity           9902.70  17058.92  14996.93  14298.85  14604.43
Min.Equity           -136.30   -189.12   -159.59   -233.07   -315.54
End.Equity           9233.11  17020.32  14917.84  14298.85  13603.78

                         EWH       EWJ       EWS       EWT       EWU
Total.Net.Profit    11548.79   5965.14  12676.51  12221.36  12044.63
Total.Days           1221.00   1130.00   1300.00   1138.00   1252.00
Winning.Days          644.00    588.00    712.00    610.00    689.00
Losing.Days           577.00    542.00    588.00    528.00    563.00
Avg.Day.PL              9.46      5.28      9.75     10.74      9.62
Med.Day.PL             11.07     13.74     17.01     18.92     18.71
Largest.Winner        456.34    640.14    578.24    780.22    596.56
Largest.Loser        -495.32   -496.23  -1058.32   -835.42   -812.47
Gross.Profits       71689.04  67809.75  70413.47  71565.95  71295.50
Gross.Losses       -60140.25 -61844.61 -57736.96 -59344.59 -59250.87
Std.Dev.Daily.PL      138.92    148.13    130.19    150.04    132.82
Percent.Positive       52.74     52.04     54.77     53.60     55.03
Percent.Negative       47.26     47.96     45.23     46.40     44.97
Profit.Factor           1.19      1.10      1.22      1.21      1.20
Avg.Win.Day           111.32    115.32     98.90    117.32    103.48
Med.Win.Day            89.52     94.38     79.32     95.13     87.31
Avg.Losing.Day       -104.23   -114.10    -98.19   -112.40   -105.24
Med.Losing.Day        -81.34    -92.34    -75.96    -85.69    -82.84
Avg.Daily.PL            9.46      5.28      9.75     10.74      9.62
Med.Daily.PL           11.07     13.74     17.01     18.92     18.71
Std.Dev.Daily.PL.1    138.92    148.13    130.19    150.04    132.82
Ann.Sharpe              1.08      0.57      1.19      1.14      1.15
Max.Drawdown        -2298.32  -3445.56  -2017.06  -2764.34  -2071.61
Profit.To.Max.Draw      5.02      1.73      6.28      4.42      5.81
Avg.WinLoss.Ratio       1.07      1.01      1.01      1.04      0.98
Med.WinLoss.Ratio       1.10      1.02      1.04      1.11      1.05
Max.Equity          12708.66   6336.41  13177.76  12221.36  12640.57
Min.Equity           -272.68   -307.36   -331.04      0.00    -57.47
End.Equity          11548.79   5965.14  12676.51  12221.36  12044.63

                         EWY       EWZ       EZU       IEF       IGE
Total.Net.Profit    10513.27  14496.80  11233.90  13370.63  12428.65
Total.Days           1297.00   1351.00   1282.00   1186.00   1362.00
Winning.Days          714.00    743.00    705.00    628.00    730.00
Losing.Days           583.00    608.00    577.00    558.00    632.00
Avg.Day.PL              8.11     10.73      8.76     11.27      9.13
Med.Day.PL             15.39     16.83     16.09     13.04     12.81
Largest.Winner        461.13    661.59    726.99    660.63    666.40
Largest.Loser        -819.21   -837.40  -1254.82   -555.37   -643.72
Gross.Profits       77069.26  77329.78  74286.39  79557.53  81524.20
Gross.Losses       -66555.99 -62832.98 -63052.49 -66186.90 -69095.55
Std.Dev.Daily.PL      146.05    135.63    142.76    158.58    142.55
Percent.Positive       55.05     55.00     54.99     52.95     53.60
Percent.Negative       44.95     45.00     45.01     47.05     46.40
Profit.Factor           1.16      1.23      1.18      1.20      1.18
Avg.Win.Day           107.94    104.08    105.37    126.68    111.68
Med.Win.Day            88.89     81.86     84.23     99.02     92.92
Avg.Losing.Day       -114.16   -103.34   -109.28   -118.61   -109.33
Med.Losing.Day        -80.09    -78.39    -82.94    -94.81    -90.22
Avg.Daily.PL            8.11     10.73      8.76     11.27      9.13
Med.Daily.PL           15.39     16.83     16.09     13.04     12.81
Std.Dev.Daily.PL.1    146.05    135.63    142.76    158.58    142.55
Ann.Sharpe              0.88      1.26      0.97      1.13      1.02
Max.Drawdown        -2462.61  -2188.41  -2310.88  -2650.59  -3045.12
Profit.To.Max.Draw      4.27      6.62      4.86      5.04      4.08
Avg.WinLoss.Ratio       0.95      1.01      0.96      1.07      1.02
Med.WinLoss.Ratio       1.11      1.04      1.02      1.04      1.03
Max.Equity          10513.27  14760.11  12629.07  14339.37  12428.65
Min.Equity           -339.01   -214.04   -203.63   -278.08   -268.60
End.Equity          10513.27  14496.80  11233.90  13370.63  12428.65

                         IYR       IYZ       LQD       RWR       SHY
Total.Net.Profit     9702.92   8599.09  13277.24  12090.68  15021.96
Total.Days           1272.00   1232.00   1231.00   1306.00   1350.00
Winning.Days          689.00    656.00    687.00    708.00    738.00
Losing.Days           583.00    576.00    544.00    598.00    612.00
Avg.Day.PL              7.63      6.98     10.79      9.26     11.13
Med.Day.PL             12.50     11.72     14.81     11.46     20.75
Largest.Winner        550.83    601.69   1475.28    648.22    591.25
Largest.Loser        -749.58   -803.69   -724.79   -960.12   -988.98
Gross.Profits       68544.80  62158.11  60933.53  74573.82  80464.45
Gross.Losses       -58841.88 -53559.03 -47656.28 -62483.14 -65442.49
Std.Dev.Daily.PL      131.85    123.46    119.48    139.10    140.36
Percent.Positive       54.17     53.25     55.81     54.21     54.67
Percent.Negative       45.83     46.75     44.19     45.79     45.33
Profit.Factor           1.16      1.16      1.28      1.19      1.23
Avg.Win.Day            99.48     94.75     88.70    105.33    109.03
Med.Win.Day            82.65     72.40     70.23     83.85     88.76
Avg.Losing.Day       -100.93    -92.98    -87.60   -104.49   -106.93
Med.Losing.Day        -72.35    -70.97    -69.84    -78.11    -88.06
Avg.Daily.PL            7.63      6.98     10.79      9.26     11.13
Med.Daily.PL           12.50     11.72     14.81     11.46     20.75
Std.Dev.Daily.PL.1    131.85    123.46    119.48    139.10    140.36
Ann.Sharpe              0.92      0.90      1.43      1.06      1.26
Max.Drawdown        -5488.96  -2253.39  -1261.10  -5062.88  -2852.91
Profit.To.Max.Draw      1.77      3.82     10.53      2.39      5.27
Avg.WinLoss.Ratio       0.99      1.02      1.01      1.01      1.02
Med.WinLoss.Ratio       1.14      1.02      1.01      1.07      1.01
Max.Equity          13313.44   9225.39  14084.98  15192.88  16570.24
Min.Equity           -152.91   -522.27   -347.48   -394.76  -1036.81
End.Equity           9702.92   8599.09  13277.24  12090.68  15021.96

                         TLT       XLB       XLE       XLF       XLI
Total.Net.Profit     7117.62   6227.48  10335.40     -8.45   6351.34
Total.Days           1126.00   1252.00   1336.00   1208.00   1291.00
Winning.Days          576.00    679.00    732.00    626.00    694.00
Losing.Days           550.00    573.00    604.00    582.00    597.00
Avg.Day.PL              6.32      4.97      7.74     -0.01      4.92
Med.Day.PL              7.01     12.34     15.29      6.50     11.06
Largest.Winner        706.10    586.14    525.30    425.95    479.01
Largest.Loser        -609.88   -815.74   -501.16   -446.61   -384.75
Gross.Profits       70845.43  64778.65  73739.30  55919.96  61941.72
Gross.Losses       -63727.80 -58551.17 -63403.90 -55928.41 -55590.37
Std.Dev.Daily.PL      158.57    130.49    131.97    122.51    116.99
Percent.Positive       51.15     54.23     54.79     51.82     53.76
Percent.Negative       48.85     45.77     45.21     48.18     46.24
Profit.Factor           1.11      1.11      1.16      1.00      1.11
Avg.Win.Day           123.00     95.40    100.74     89.33     89.25
Med.Win.Day            93.07     72.97     79.85     66.21     73.41
Avg.Losing.Day       -115.87   -102.18   -104.97    -96.10    -93.12
Med.Losing.Day        -89.17    -77.13    -79.13    -69.41    -72.87
Avg.Daily.PL            6.32      4.97      7.74     -0.01      4.92
Med.Daily.PL            7.01     12.34     15.29      6.50     11.06
Std.Dev.Daily.PL.1    158.57    130.49    131.97    122.51    116.99
Ann.Sharpe              0.63      0.61      0.93      0.00      0.67
Max.Drawdown        -4938.54  -3692.51  -2780.65  -5704.14  -3013.95
Profit.To.Max.Draw      1.44      1.69      3.72      0.00      2.11
Avg.WinLoss.Ratio       1.06      0.93      0.96      0.93      0.96
Med.WinLoss.Ratio       1.04      0.95      1.01      0.95      1.01
Max.Equity           9318.18   6780.72  10335.40   4191.68   6357.94
Min.Equity           -693.51   -213.25   -557.67  -1512.46   -795.23
End.Equity           7117.62   6227.48  10335.40     -8.45   6351.34

                         XLK       XLP       XLU       XLV       XLY
Total.Net.Profit     2940.29   2543.61   7904.42   3189.25   6727.03
Total.Days           1210.00   1326.00   1322.00   1153.00   1179.00
Winning.Days          664.00    704.00    710.00    593.00    611.00
Losing.Days           546.00    622.00    612.00    560.00    568.00
Avg.Day.PL              2.43      1.92      5.98      2.77      5.71
Med.Day.PL             14.64     11.03     12.12      5.31      8.48
Largest.Winner        437.09    537.52    546.32    453.96    450.42
Largest.Loser        -765.80   -750.98   -855.10   -726.49   -447.69
Gross.Profits       57610.32  62334.98  61795.49  53165.92  58852.52
Gross.Losses       -54670.02 -59791.38 -53891.07 -49976.67 -52125.49
Std.Dev.Daily.PL      122.10    118.49    115.44    117.37    120.29
Percent.Positive       54.88     53.09     53.71     51.43     51.82
Percent.Negative       45.12     46.91     46.29     48.57     48.18
Profit.Factor           1.05      1.04      1.15      1.06      1.13
Avg.Win.Day            86.76     88.54     87.04     89.66     96.32
Med.Win.Day            71.15     73.84     70.10     72.61     78.75
Avg.Losing.Day       -100.13    -96.13    -88.06    -89.24    -91.77
Med.Losing.Day        -72.57    -74.82    -65.52    -71.56    -70.88
Avg.Daily.PL            2.43      1.92      5.98      2.77      5.71
Med.Daily.PL           14.64     11.03     12.12      5.31      8.48
Std.Dev.Daily.PL.1    122.10    118.49    115.44    117.37    120.29
Ann.Sharpe              0.32      0.26      0.82      0.37      0.75
Max.Drawdown        -3074.10  -4531.59  -2545.84  -4623.78  -4041.34
Profit.To.Max.Draw      0.96      0.56      3.10      0.69      1.66
Avg.WinLoss.Ratio       0.87      0.92      0.99      1.00      1.05
Med.WinLoss.Ratio       0.98      0.99      1.07      1.01      1.11
Max.Equity           2981.10   2993.23   8632.88   3902.68   6838.89
Min.Equity          -1716.68  -1538.36   -165.00  -1213.50   -310.63
End.Equity           2940.29   2543.61   7904.42   3189.25   6727.03

Now, I’d like to introduce some new analytics found in my IKTrading package–namely, trade duration statistics. This is a simple little function that breaks down the trades by duration, in aggregate, winners, and losers, using the usual five-number summary and the mean time. It’s programmed under the assumption that the units are days. Once you start drilling into more frequent trading, one would likely need a bit more refined analysis. However, as most freely available data occurs at the daily frequency, this function should be sufficient for most analyses.

Here is the input and output for this strategy:

durStats <- durationStatistics(Portfolio=portfolio.st, Symbols=sort(symbols))
print(t(durStats))


      EFA EPP EWA EWC EWG EWH EWJ EWS EWT EWU EWY EWZ EZU IEF IGE IYR
Min     1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
Q1      2   2   2   3   2   2   2   2   2   2   2   3   2   2   2   3
Med     6   6   5   7   6   4   5   6   6   6   6   6   6   4   6   6
Mean   12  14  13  16  14  12  12  13  16  15  13  13  12  15  14  13
Q3     14  15  14  17  13  12  11  14  18  14  15  14  13  17  14  12
Max   119 132 135 122 126 185 130 139 106 132 105  95 131 144 125 128
WMin    1   1   1   1   1   1   1   1   1   1   1   1   1   1   3   1
WQ1     9   7   3   8   3   2   2   6   3   6   5   7   5   6   9   7
WMed   14  14  14  18  12  11   8  13  14  14  13  14  13  17  19  15
WMean  22  25  22  28  24  21  20  23  24  27  21  24  23  28  30  28
WQ3    20  28  26  34  25  20  25  23  38  28  27  37  22  38  42  33
WMax  119 132 135 122 126 185 130 139 106 132 105  95 131 144 125 128
LMin    1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
LQ1     1   1   1   2   1   2   2   1   1   1   1   1   1   1   1   2
LMed    3   4   3   4   3   3   3   3   3   3   3   3   3   3   3   4
LMean   4   5   5   6   5   5   6   5   7   4   5   5   5   5   6   5
LQ3     5   7   6   7   7   6   8   6   6   6   6   6   6   5   6   6
LMax   30  22  22  34  28  30  25  41  64  17  28  43  22  40  62  40

      IYZ LQD RWR SHY TLT XLB XLE XLF XLI XLK XLP XLU XLV XLY
Min     1   1   1   1   1   1   1   1   1   1   1   1   1   1
Q1      3   1   2   2   2   3   2   2   2   2   2   3   2   3
Med     6   4   5   6   5   7   6   5   7   6   4   7   5   8
Mean   13  15  13  12  12  14  15  13  12  13  13  15  14  15
Q3     13  14  12  14  12  15  16  15  13  13  13  14  13  14
Max   115 151 128 109  90 105 113 130  99 116 117 121 132 123
WMin    1   1   1   1   1   6   1   1   1   1   1   1   1   1
WQ1     6   3   7   6   9  12   9   9   6   6   5   7   9  11
WMed   13  13  14  14  21  16  19  17  12  14  17  16  15  14
WMean  23  27  27  22  29  28  31  27  21  27  29  27  35  28
WQ3    34  28  27  28  43  34  48  32  20  38  45  40  44  28
WMax  115 151 128 109  90 105 113 130  99 116 117 121 132 123
LMin    1   1   1   1   1   1   1   1   1   1   1   1   1   1
LQ1     2   1   1   2   1   1   1   2   1   1   1   1   1   2
LMed    4   2   3   4   3   3   3   3   4   4   4   4   4   4
LMean   5   5   4   5   5   6   6   6   7   5   6   5   6   6
LQ3     6   5   6   6   6   7   7   7   8   7   7   7   7   8
LMax   33  25  32  42  31  36  38  41  36  19  41  39  69  35

From top to bottom in this transposed table (or left to right in the original), we have aggregate trade duration statistics, the same statistics on winners, and finally, losers. This paints a picture of this current strategy as having a profile of a classic trend follower: let your winners run, and cut your losses. Or, to set it to a higher standard, the occasional winner at the price of plenty of small whipsaws, with the occasional long-duration loser. Note that this loser may not have been a loser the entire time–it could very well have been a trade that was going sideways for a long time and finally sunk into negative territory near the end, but it seems that every instrument has had at least one somewhat long-running trade that wound up losing at least a penny.

On the winning side of the trade, the winning trades stay in the market for long berths, with the best ones riding waves that last for around half a year. Ultimately, this is the appeal of trend following–the idea of just getting in once, and just having the market pay you. The goal of investigating the FRAMA (and potentially other trend indicators), is to try and locate those long profit waves while avoiding the whipsaws, countertrends, and so on.

One other interesting piece of analytics I’m incorporating into this demo is the idea of market exposure–or what percentage of the time that the strategy is actually *in* the market. Here is the code and the results:

#market exposure
tmp <- list()
length(tmp) <- length(symbols)
for(i in 1:nrow(dStats)) {
  totalDays <- nrow(get(rownames(dStats)[i]))
  mktExposure <- dStats$Total.Days[i]/totalDays
  tmp[[i]] <- c(rownames(dStats)[i], round(mktExposure, 3))
}
mktExposure <- data.frame(do.call(rbind, tmp))
colnames(mktExposure) <- c("Symbol","MktExposure")
print(mktExposure)

Essentially, this little piece of code takes advantage of the daily statistics output to compute market exposure. Here’s the output:

   Symbol MktExposure
1     EFA       0.635
2     EPP        0.66
3     EWA        0.66
4     EWC       0.643
5     EWG       0.627
6     EWH       0.606
7     EWJ       0.561
8     EWS       0.645
9     EWT       0.565
10    EWU       0.621
11    EWY       0.644
12    EWZ        0.67
13    EZU       0.636
14    IEF       0.589
15    IGE       0.676
16    IYR       0.631
17    IYZ       0.611
18    LQD       0.611
19    RWR       0.648
20    SHY        0.67
21    TLT       0.559
22    XLB       0.621
23    XLE       0.663
24    XLF         0.6
25    XLI       0.641
26    XLK         0.6
27    XLP       0.658
28    XLU       0.656
29    XLV       0.572
30    XLY       0.585

So basically, around 60-66% of the time spent in the market, most of which are short, sporadic, losing trades, with the occasional long winner.

Here’s what the equity curve looks like:

As can be seen during the crisis, this baseline strategy is taking lots of trades…for no reason at all.

And here are the three aggregate portfolio statistics:

> SharpeRatio.annualized(portfRets)
                                    [,1]
Annualized Sharpe Ratio (Rf=0%) 1.334861
> Return.annualized(portfRets)
                       [,1]
Annualized Return 0.1288973
> maxDrawdown(portfRets)
[1] 0.1562275

The largest drawdown occurs during the crisis, but beyond that, the annualized returns are solid. What I find most impressive is that the annualized Sharpe Ratio over the course of this backtest, even with what seems to be a period of drawdowns that can be removed with what seems to be relative ease (most market timing trend-followers seem to do a decent job of avoiding most of the brunt of the crisis).

And finally, to demonstrate the indicator and investigate areas for improvement, here’s the equity curve of XLF (not XLB this time), since XLF lost eight dollars over the course of the backtest.

One advantage that I think the FRAMA has over the Trend Vigor is that as it is an indicator that’s superimposed directly on the price action, it’s easier to understand visually. (Also, the math is definitely more intuitive.) As we can see from the XLF equity curve, the base strategy presented by ETFHQ could certainly use a confirmatory, slower-moving indicator to keep out counter-trend trading. After all, while the tiny little whipsaw trades are undoubtedly a nuisance, correcting counter-trend trading is a lower-hanging fruit, and would seem to be more profitable in addressing.

Thanks for reading.


To leave a comment for the author, please follow the link and comment on their blog: QuantStrat TradeR » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)