[R-SIG-Finance] Bug in tradeStats function?

Nick White n-e-w at qtradr.net
Mon Nov 24 00:14:51 CET 2014


Hi all,

Not sure if I've found a bug here, but I've had some issues with tradeStats
in getting a correct number of trades (as opposed to number of
transactions).

For reproducibility, I'm using the "AMZN" demo from Rev 1317 -- latest code
here:
https://r-forge.r-project.org/scm/viewvc.php/pkg/blotter/demo/amzn_test.R?view=markup&revision=1317&root=blotter
)

So I set `Sys.setenv(TZ="UTC")` and then run the demo script.

I then call:

> getTxns("amzn_port","amzn")

                    Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost
Net.Txn.Realized.PL
2010-01-14 00:00:00       0      0.00        0         0         0.00
            0
2010-01-14 15:18:50    -400    127.49        0    -50996       127.49
        50996
2010-01-14 15:18:53     400    127.49        0     50996       127.49
            0
2010-01-14 15:21:50    -300    127.26        0    -38178       127.26
        38178
2010-01-14 15:21:53     300    127.26        0     38178       127.26
            0
2010-01-14 15:23:07     100    127.17        0     12717       127.17
       -12717
2010-01-14 15:23:10    -100    127.19        0    -12719       127.19
            2
2010-01-14 15:37:56    -400    127.12        0    -50848       127.12
        50848
2010-01-14 15:37:59     400    127.12        0     50848       127.12
            0
2010-01-14 15:38:32    -500    127.23        0    -63615       127.23
        63615
2010-01-14 15:38:35     500    127.23        0     63615       127.23
            0
2010-01-14 15:38:37     300    127.27        0     38181       127.27
       -38181
2010-01-14 15:38:40    -300    127.30        0    -38190       127.30
            9
2010-01-14 15:46:08   -1300    126.95        0   -165035       126.95
       165035
2010-01-14 15:46:11    1300    126.95        0    165035       126.95
            0

and we see 14 transactions comprising 7 flat-to-flat trades (though, of
course, there is the portfolio initiation at midnight on the 14th, which
brings it up to 15 entries)

However, when I call

   > t(tradeStats("amzn_port"))
                   amzn
Portfolio          "amzn_port"
Symbol             "amzn"
Num.Txns           "14"
Num.Trades         "9"
Net.Trading.PL     "11"
Avg.Trade.PL       "35309.44"
Med.Trade.PL       "38178"
Largest.Winner     "165035"
Largest.Loser      "-38181"
Gross.Profits      "368683"
Gross.Losses       "-50898"
Std.Dev.Trade.PL   "59503.98"
Percent.Positive   "77.77778"
Percent.Negative   "22.22222"
Profit.Factor      "7.243566"
Avg.Win.Trade      "52669"
Med.Win.Trade      "50848"
Avg.Losing.Trade   "-25449"
Med.Losing.Trade   "-25449"
Avg.Daily.PL       "317785"
Med.Daily.PL       "317785"
Std.Dev.Daily.PL   NA
Ann.Sharpe         NA
Max.Drawdown       "-109.0012"
Profit.To.Max.Draw "0.1009164"
Avg.WinLoss.Ratio  "2.06959"
Med.WinLoss.Ratio  "1.998035"
Max.Equity         "102.0036"
Min.Equity         "-37.00018"
End.Equity         "11"

Where we get the correct number of trades (14) , but the incorrect number
of trades (9). This also stands contrary to `perTradeStats` which gets it
'right' with 7 line items (apols for formatting)

> perTradeStats("amzn_port")
                Start                 End Init.Pos Max.Pos Num.Txns
Max.Notional.Cost Net.Trading.PL       MAE      MFE Pct.Net.Trading.PL
  Pct.MAE      Pct.MFE tick.Net.Trading.PL   tick.MAE  tick.MFE
1 2010-01-14 15:18:50 2010-01-14 15:18:53     -400    -400        2
   -50996              0   0.00000 72.00098       0.0000000000
 0.0000000000 0.0014118946                   0   0.000000 18.000244
2 2010-01-14 15:21:50 2010-01-14 15:21:53     -300    -300        2
   -38178              0 -33.00082  0.00000       0.0000000000
-0.0008643937 0.0000000000                   0 -11.000275  0.000000
3 2010-01-14 15:23:07 2010-01-14 15:23:10      100     100        2
    12717              2   0.00000 12.94995       0.0001572698
 0.0000000000 0.0010183181                   2   0.000000 12.949951
4 2010-01-14 15:37:56 2010-01-14 15:37:59     -400    -400        2
   -50848              0 -32.04150  0.00000       0.0000000000
-0.0006301429 0.0000000000                   0  -8.010376  0.000000
5 2010-01-14 15:38:32 2010-01-14 15:38:35     -500    -500        2
   -63615              0   0.00000 45.00031       0.0000000000
 0.0000000000 0.0007073851                   0   0.000000  9.000061
6 2010-01-14 15:38:37 2010-01-14 15:38:40      300     300        2
    38181              9 -39.00018  9.00000       0.0002357193
-0.0010214553 0.0002357193                   3 -13.000061  3.000000
7 2010-01-14 15:46:08 2010-01-14 15:46:11    -1300   -1300        2
  -165035              0   0.00000 91.00357       0.0000000000
 0.0000000000 0.0005514198                   0   0.000000  7.000275


In my own data set of some fills from Friday I get the following (XYZ is
the dummy ticker)

> t(perTradeStats(b.strategy,"XYZ"))
                    [,1]
Start               "2014-11-21 14:30:01"
End                 "2014-11-21 20:42:26"
Init.Pos            "200"
Max.Pos             "50263"
Num.Txns            "95"
***SNIP***


So `perTradeStats` has rightly calculated the position as being flat to
flat with 95 transactions comprising one single trade starting at 14:30 and
ending at 20:42 UTC....BUT tradeStats makes the error again


Portfolio          "list_demo"
Symbol             "XYZ"
Num.Txns           "95"
Num.Trades         "94"

This is not the first time I've bumped up against this. In other examples
for my own trades, I've found that tradeStats is sensitive to how you've
indexed the data (for example, it doesn't seem to like it if you have some
things as "Date" and others as "POSIXct". But in the above case, ensuring
all indexes are classed the same way still produces the transactions /
trades error.

I think there might perhaps be something assumed in the `tradeDef` function
that may need to be made more explicit depending on system options for time
zone or index class...but thought I'd get some initial thoughts from the
community before proceeding further to reconcile.

Any ideas?

n.

> sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United
States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C
                  LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] blotter_0.9.1637              PerformanceAnalytics_1.4.3541
FinancialInstrument_1.1       quantmod_0.4-2                TTR_0.22-0.1
               xts_0.9-7                     zoo_1.7-12

loaded via a namespace (and not attached):
[1] grid_3.1.1          lattice_0.20-29     quantstrat_0.9.1632 tools_3.1.1

	[[alternative HTML version deleted]]



More information about the R-SIG-Finance mailing list