Usage

Feeding a portfolio analytics one observation at a time

  • A portfolio analytics object can be feeded using fit! function.

  • It's feeded one observation at a time.

Showing sample data

Some sample data are provided for testing purpose.

julia> using OnlinePortfolioAnalytics
julia> using OnlinePortfolioAnalytics.SampleData: dates, TSLA, NFLX, MSFT, weights

julia> dates
Dates.Date("2020-12-31"):Dates.Month(1):Dates.Date("2021-12-31")

julia> TSLA
13-element Vector{Float64}:
 235.22
 264.51
 225.16
 222.64
 236.48
 208.4
 226.56
 229.06
 245.24
 258.49
 371.33
 381.58
 352.26

julia> NFLX
13-element Vector{Float64}:
 540.73
 532.39
 538.85
 521.66
 513.47
 502.81
 528.21
 517.57
 569.19
 610.34
 690.31
 641.9
 602.44

julia> MSFT
13-element Vector{Float64}:
 222.42
 231.96
 232.38
 235.77
 252.18
 249.68
 270.9
 284.91
 301.88
 281.92
 331.62
 330.59
 336.32

julia> weights
3-element Vector{Float64}:
 0.4
 0.4
 0.2

Calculate returns (from TSLA prices)

julia> stat = SimpleAssetReturn{Float64}()
SimpleAssetReturn: n=0 | value=missing

julia> fit!(stat, 235.22)
SimpleAssetReturn: n=1 | value=missing

julia> fit!(stat, 264.51)
SimpleAssetReturn: n=2 | value=0.124522

julia> value(stat)
0.12452172434316806

Calculate returns (natural log method)

julia> stat = LogAssetReturn{Float64}()
LogAssetReturn: n=0 | value=missing

julia> fit!(stat, 235.22)
LogAssetReturn: n=1 | value=missing

julia> fit!(stat, 264.51)
LogAssetReturn: n=2 | value=0.117358

Others

See API usage

Portfolio analytics with Tables.jl compatible sources

Here is an example showing how to use OnlinePortfolioAnalytics with Tables.jl compatible sources.

Sample data

julia> using TSFrames

julia> prices_ts = TSFrame([TSLA NFLX MSFT], dates, colnames = [:TSLA, :NFLX, :MSFT])
13×3 TSFrame with Date Index
 Index       TSLA     NFLX     MSFT
 Date        Float64  Float64  Float64
───────────────────────────────────────
 2020-12-31   235.22   540.73   222.42
 2021-01-31   264.51   532.39   231.96
 2021-02-28   225.16   538.85   232.38
 2021-03-31   222.64   521.66   235.77
 2021-04-30   236.48   513.47   252.18
 2021-05-31   208.4    502.81   249.68
 2021-06-30   226.56   528.21   270.9
 2021-07-31   229.06   517.57   284.91
 2021-08-31   245.24   569.19   301.88
 2021-09-30   258.49   610.34   281.92
 2021-10-31   371.33   690.31   331.62
 2021-11-30   381.58   641.9    330.59
 2021-12-31   352.26   602.44   336.3

Returns

julia> returns = SimpleAssetReturn(prices_ts)
13×3 TSFrame with Date Index
 Index       TSLA             MSFT              NFLX
 Date        Float64?         Float64?          Float64?
────────────────────────────────────────────────────────────────
 2020-12-31  missing          missing           missing
 2021-01-31        0.124522         0.0428918        -0.0154236
 2021-02-28       -0.148766         0.00181066        0.012134
 2021-03-31       -0.011192         0.0145882        -0.0319013
 2021-04-30        0.0621631        0.0696017        -0.0156999
 2021-05-31       -0.118742        -0.00991355       -0.0207607
 2021-06-30        0.0871401        0.0849888         0.0505161
 2021-07-31        0.0110346        0.0517165        -0.0201435
 2021-08-31        0.0706365        0.0595627         0.0997353
 2021-09-30        0.0540287       -0.066119          0.0722957
 2021-10-31        0.436535         0.176291          0.131025
 2021-11-30        0.0276035       -0.00310596       -0.0701279
 2021-12-31       -0.0768384        0.0173326        -0.0614737

Remove missing from returns

julia> using DataFrames

julia> returns = dropmissing(returns.coredata) |> TSFrame
12×3 TSFrame with Date Index
 Index       TSLA        MSFT         NFLX
 Date        Float64     Float64      Float64
─────────────────────────────────────────────────
 2021-01-31   0.124522    0.0428918   -0.0154236
 2021-02-28  -0.148766    0.00181066   0.012134
 2021-03-31  -0.011192    0.0145882   -0.0319013
 2021-04-30   0.0621631   0.0696017   -0.0156999
 2021-05-31  -0.118742   -0.00991355  -0.0207607
 2021-06-30   0.0871401   0.0849888    0.0505161
 2021-07-31   0.0110346   0.0517165   -0.0201435
 2021-08-31   0.0706365   0.0595627    0.0997353
 2021-09-30   0.0540287  -0.066119     0.0722957
 2021-10-31   0.436535    0.176291     0.131025
 2021-11-30   0.0276035  -0.00310596  -0.0701279
 2021-12-31  -0.0768384   0.0173326   -0.0614737

Calculate standard deviation of returns

julia> stddev = StdDev(returns)
12×3 TSFrame with Date Index
 Index       TSLA       MSFT       NFLX
 Date        Float64    Float64    Float64
─────────────────────────────────────────────
 2021-01-31  1.0        1.0        1.0
 2021-02-28  0.193243   0.0290488  0.0194861
 2021-03-31  0.136645   0.0210239  0.0222487
 2021-04-30  0.117541   0.0302594  0.0182741
 2021-05-31  0.116225   0.0322773  0.016229
 2021-06-30  0.11253    0.0381779  0.0301918
 2021-07-31  0.102823   0.0354894  0.0282681
 2021-08-31  0.0983386  0.0338512  0.0456038
 2021-09-30  0.0931719  0.047328   0.0478437
 2021-10-31  0.159765   0.064804   0.0582396
 2021-11-30  0.15182    0.0630003  0.0624165
 2021-12-31  0.149608   0.0603753  0.0637211

Calculate arithmetic mean returns

julia> amr = ArithmeticMeanReturn(returns)
12×3 TSFrame with Date Index
 Index       TSLA          MSFT       NFLX
 Date        Float64       Float64    Float64
──────────────────────────────────────────────────
 2021-01-31   0.124522     0.0428918  -0.0154236
 2021-02-28  -0.012122     0.0223512  -0.00164482
 2021-03-31  -0.011812     0.0197636  -0.0117303
 2021-04-30   0.00668179   0.0322231  -0.0127227
 2021-05-31  -0.0184029    0.0237958  -0.0143303
 2021-06-30  -0.000812376  0.0339946  -0.00352257
 2021-07-31   0.00088005   0.0365263  -0.00589699
 2021-08-31   0.00959961   0.0394058   0.00730705
 2021-09-30   0.0145362    0.0276809   0.014528
 2021-10-31   0.0567361    0.0425419   0.0261777
 2021-11-30   0.0540877    0.0383921   0.0174227
 2021-12-31   0.0431772    0.0366371   0.010848

Calculate geometric mean returns

julia> gmr = GeometricMeanReturn(returns)
12×3 TSFrame with Date Index
 Index       TSLA         MSFT       NFLX
 Date        Float64      Float64    Float64
─────────────────────────────────────────────────
 2021-01-31   0.124522    0.0428918  -0.0154236
 2021-02-28  -0.0216179   0.0221449  -0.0017399
 2021-03-31  -0.0181549   0.0196197  -0.0118967
 2021-04-30   0.00133649  0.0318919  -0.0128489
 2021-05-31  -0.0239216   0.0233919  -0.0144363
 2021-06-30  -0.0062324   0.0334097  -0.00389675
 2021-07-31  -0.00378386  0.0360053  -0.0062341
 2021-08-31   0.00522813  0.0389211   0.00643238
 2021-09-30   0.0105369   0.0266895   0.013546
 2021-10-31   0.0467153   0.0407506   0.0247227
 2021-11-30   0.0449633   0.0366852   0.0157142
 2021-12-31   0.0342267   0.0350585   0.00904634

Calculate asset log returns from prices

julia> log_returns = LogAssetReturn(prices_ts)
13×3 TSFrame with Date Index
 Index       TSLA             MSFT              NFLX
 Date        Float64?         Float64?          Float64?
────────────────────────────────────────────────────────────────
 2020-12-31  missing          missing           missing
 2021-01-31        0.117358         0.0419975        -0.0155438
 2021-02-28       -0.161068         0.00180902        0.0120609
 2021-03-31       -0.0112551        0.0144828        -0.0324212
 2021-04-30        0.0603075        0.0672864        -0.0158244
 2021-05-31       -0.126404        -0.00996302       -0.0209792
 2021-06-30        0.0835505        0.0815697         0.0492816
 2021-07-31        0.0109742        0.0504236        -0.0203492
 2021-08-31        0.0682533        0.0578562         0.0950695
 2021-09-30        0.0526197       -0.0684062         0.0698019
 2021-10-31        0.362234         0.162366          0.123125
 2021-11-30        0.0272294       -0.0031108        -0.0727082
 2021-12-31       -0.079951         0.0171842        -0.0634445

Calculate cumulative returns

julia> cum_returns = CumulativeReturn(returns)
12×3 TSFrame with Date Index
 Index       TSLA      MSFT     NFLX
 Date        Float64   Float64  Float64
─────────────────────────────────────────
 2021-01-31  1.12452   1.04289  0.984576
 2021-02-28  0.957232  1.04478  0.996523
 2021-03-31  0.946518  1.06002  0.964733
 2021-04-30  1.00536   1.1338   0.949587
 2021-05-31  0.885979  1.12256  0.929873
 2021-06-30  0.963183  1.21797  0.976846
 2021-07-31  0.973812  1.28095  0.957169
 2021-08-31  1.0426    1.35725  1.05263
 2021-09-30  1.09893   1.26751  1.12873
 2021-10-31  1.57865   1.49096  1.27663
 2021-11-30  1.62223   1.48633  1.1871
 2021-12-31  1.49758   1.51209  1.11412

Calculate Drawdowns

julia> dd = DrawDowns(returns)
12×3 TSFrame with Date Index
 Index       TSLA        MSFT         NFLX
 Date        Float64     Float64      Float64
─────────────────────────────────────────────────
 2021-01-31   0.0         0.0          0.0
 2021-02-28  -0.148766    0.0          0.0
 2021-03-31  -0.158293    0.0         -0.0319013
 2021-04-30  -0.10597     0.0         -0.0471003
 2021-05-31  -0.212128   -0.00991355  -0.0668832
 2021-06-30  -0.143473    0.0         -0.0197458
 2021-07-31  -0.134021    0.0         -0.0394915
 2021-08-31  -0.0728517   0.0          0.0
 2021-09-30  -0.0227591  -0.066119     0.0
 2021-10-31   0.0         0.0          0.0
 2021-11-30   0.0        -0.00310596  -0.0701279
 2021-12-31  -0.0768384   0.0         -0.127291

Calculate Drawdowns (Arithmetic method)

julia> arith_dd = ArithmeticDrawDowns(returns)
12×3 TSFrame with Date Index
 Index       TSLA        MSFT         NFLX
 Date        Float64     Float64      Float64
─────────────────────────────────────────────────
 2021-01-31   0.0         0.0          0.0
 2021-02-28  -0.132292    0.0          0.0
 2021-03-31  -0.142245    0.0         -0.0320066
 2021-04-30  -0.0869655   0.0         -0.0477583
 2021-05-31  -0.192558   -0.00878166  -0.0685875
 2021-06-30  -0.115068    0.0         -0.0179047
 2021-07-31  -0.105255    0.0         -0.0381146
 2021-08-31  -0.0424401   0.0          0.0
 2021-09-30   0.0        -0.0502712    0.0
 2021-10-31   0.0         0.0          0.0
 2021-11-30   0.0        -0.00217898  -0.0555787
 2021-12-31  -0.0481756   0.0         -0.104299

Calculate statistical moments of returns

julia> moments = AssetReturnMoments(returns)
12×3 TSFrame with Date Index
 Index       TSLA                               MSFT                               NFLX
 Date        Any                                Any                                Any
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 2021-01-31  (mean = 0.124522, std = NaN, ske…  (mean = 0.0428918, std = NaN, sk…  (mean = -0.0154236, std = NaN, s…
 2021-02-28  (mean = -0.012122, std = 0.19324…  (mean = 0.0223512, std = 0.02904…  (mean = -0.00164482, std = 0.019…
 2021-03-31  (mean = -0.011812, std = 0.13664…  (mean = 0.0197636, std = 0.02102…  (mean = -0.0117303, std = 0.0222…
 2021-04-30  (mean = 0.00668179, std = 0.1175…  (mean = 0.0322231, std = 0.03025…  (mean = -0.0127227, std = 0.0182…
 2021-05-31  (mean = -0.0184029, std = 0.1162…  (mean = 0.0237958, std = 0.03227…  (mean = -0.0143303, std = 0.0162…
 2021-06-30  (mean = -0.000812376, std = 0.11…  (mean = 0.0339946, std = 0.03817…  (mean = -0.00352257, std = 0.030…
 2021-07-31  (mean = 0.00088005, std = 0.1028…  (mean = 0.0365263, std = 0.03548…  (mean = -0.00589699, std = 0.028…
 2021-08-31  (mean = 0.00959961, std = 0.0983…  (mean = 0.0394058, std = 0.03385…  (mean = 0.00730705, std = 0.0456…
 2021-09-30  (mean = 0.0145362, std = 0.09317…  (mean = 0.0276809, std = 0.04732…  (mean = 0.014528, std = 0.047843…
 2021-10-31  (mean = 0.0567361, std = 0.15976…  (mean = 0.0425419, std = 0.06480…  (mean = 0.0261777, std = 0.05823…
 2021-11-30  (mean = 0.0540877, std = 0.15182…  (mean = 0.0383921, std = 0.06300…  (mean = 0.0174227, std = 0.06241…
 2021-12-31  (mean = 0.0431772, std = 0.14960…  (mean = 0.0366371, std = 0.06037…  (mean = 0.010848, std = 0.063721…

Calculate Sharpe ratio (from returns)

julia> sharpe = Sharpe(returns, period = 1)
12×3 TSFrame with Date Index
 Index       TSLA         MSFT       NFLX
 Date        Float64      Float64    Float64
────────────────────────────────────────────────
 2021-01-31   0.124522    0.0428918  -0.0154236
 2021-02-28  -0.062729    0.769438   -0.0844096
 2021-03-31  -0.086443    0.940052   -0.527235
 2021-04-30   0.0568464   1.0649     -0.696214
 2021-05-31  -0.158339    0.73723    -0.883007
 2021-06-30  -0.00721917  0.890425   -0.116673
 2021-07-31   0.00855887  1.02922    -0.208609
 2021-08-31   0.0976179   1.16409     0.160229
 2021-09-30   0.156015    0.584873    0.303656
 2021-10-31   0.355123    0.65647     0.449484
 2021-11-30   0.356261    0.609396    0.279136
 2021-12-31   0.288602    0.606824    0.170242

Calculate Sortino ratio (from returns)

julia> sortino = Sortino(returns)
12×3 TSFrame with Date Index
 Index       TSLA       MSFT       NFLX
 Date        Float64    Float64    Float64
───────────────────────────────────────────────
 2021-01-31   1.97672    0.680887   -0.244842
 2021-02-28  -0.19243    0.354815   -0.0261106
 2021-03-31  -1.92754    0.313737  -15.9819
 2021-04-30   1.09037    0.511526  -21.4069
 2021-05-31  -4.03861    0.377746  -29.527
 2021-06-30  -0.17828    0.539648   -7.25811
 2021-07-31   0.193132   0.579837  -14.0099
 2021-08-31   2.10669    0.625548   17.3599
 2021-09-30   3.19005   11.0565     34.5153
 2021-10-31  12.4511    16.9924     62.1925
 2021-11-30  11.8698    17.6228     13.163
 2021-12-31  11.4992    16.8173      7.56288