@@ -322,10 +322,68 @@ def plot_monthly_returns_dist(returns, ax=None, **kwargs):
322
322
def plot_holdings (returns , positions , legend_loc = 'best' , ax = None , ** kwargs ):
323
323
"""
324
324
Plots total amount of stocks with an active position, either short
325
- or long.
325
+ or long. Displays daily total, daily average per month, and
326
+ all-time daily average.
326
327
327
- Displays daily total, daily average per month, and all-time daily
328
- average.
328
+ Parameters
329
+ ----------
330
+ returns : pd.Series
331
+ Daily returns of the strategy, noncumulative.
332
+ - See full explanation in tears.create_full_tear_sheet.
333
+ positions : pd.DataFrame, optional
334
+ Daily net position values.
335
+ - See full explanation in tears.create_full_tear_sheet.
336
+ legend_loc : matplotlib.loc, optional
337
+ The location of the legend on the plot.
338
+ ax : matplotlib.Axes, optional
339
+ Axes upon which to plot.
340
+ **kwargs, optional
341
+ Passed to plotting function.
342
+ Returns
343
+ -------
344
+ ax : matplotlib.Axes
345
+ The axes that were plotted on.
346
+ """
347
+
348
+ if ax is None :
349
+ ax = plt .gca ()
350
+
351
+ positions = positions .copy ().drop ('cash' , axis = 'columns' )
352
+ df_holdings = positions .apply (lambda x : np .sum (x != 0 ), axis = 'columns' )
353
+ df_holdings_by_month = df_holdings .resample ('1M' ).mean ()
354
+ df_holdings .plot (color = 'steelblue' , alpha = 0.6 , lw = 0.5 , ax = ax , ** kwargs )
355
+ df_holdings_by_month .plot (
356
+ color = 'orangered' ,
357
+ alpha = 0.5 ,
358
+ lw = 2 ,
359
+ ax = ax ,
360
+ ** kwargs )
361
+ ax .axhline (
362
+ df_holdings .values .mean (),
363
+ color = 'steelblue' ,
364
+ ls = '--' ,
365
+ lw = 3 ,
366
+ alpha = 1.0 )
367
+
368
+ ax .set_xlim ((returns .index [0 ], returns .index [- 1 ]))
369
+
370
+ ax .legend (['Daily holdings' ,
371
+ 'Average daily holdings, by month' ,
372
+ 'Average daily holdings, net' ],
373
+ loc = legend_loc )
374
+ ax .set_title ('Total holdings per pay' )
375
+ ax .set_ylabel ('Holdings' )
376
+ ax .set_xlabel ('' )
377
+ return ax
378
+
379
+
380
+ def plot_long_short_holdings (returns , positions ,
381
+ legend_loc = 'best' , ax = None , ** kwargs ):
382
+ """
383
+ Plots total amount of stocks with an active position, breaking out
384
+ short and long. Short positions will be shown below zero, while
385
+ long positions will be shown above zero. Displays daily total and
386
+ all-time daily average.
329
387
330
388
Parameters
331
389
----------
@@ -378,8 +436,8 @@ def plot_holdings(returns, positions, legend_loc='best', ax=None, **kwargs):
378
436
'Average daily long positions' ,
379
437
'Average daily short positions' ],
380
438
loc = legend_loc )
381
- ax .set_title ('Holdings per day' )
382
- ax .set_ylabel ('Amount of holdings per day ' )
439
+ ax .set_title ('Long and short holdings per day' )
440
+ ax .set_ylabel ('Holdings ' )
383
441
ax .set_xlabel ('' )
384
442
return ax
385
443
@@ -753,10 +811,9 @@ def cone(in_sample_returns (pd.Series),
753
811
754
812
cone_bounds = cone_bounds .set_index (oos_cum_returns .index )
755
813
for std in cone_std :
756
- cone_delta = oos_cum_returns [0 ] - 1
757
814
ax .fill_between (cone_bounds .index ,
758
- cone_bounds [float (std )] + cone_delta ,
759
- cone_bounds [float (- std )] + cone_delta ,
815
+ cone_bounds [float (std )],
816
+ cone_bounds [float (- std )],
760
817
color = 'steelblue' , alpha = 0.5 )
761
818
762
819
if legend_loc is not None :
@@ -1659,7 +1716,10 @@ def plot_round_trip_lifetimes(round_trips, disp_amount=16, lsize=18, ax=None):
1659
1716
if ax is None :
1660
1717
ax = plt .subplot ()
1661
1718
1662
- sample = round_trips .symbol .unique ().sample (n = disp_amount , random_state = 1 )
1719
+ symbols_sample = round_trips .symbol .unique ()
1720
+ np .random .seed (1 )
1721
+ sample = np .random .choice (round_trips .symbol .unique (), replace = False ,
1722
+ size = min (disp_amount , len (symbols_sample )))
1663
1723
sample_round_trips = round_trips [round_trips .symbol .isin (sample )]
1664
1724
1665
1725
symbol_idx = pd .Series (np .arange (len (sample )), index = sample )
0 commit comments