@@ -135,29 +135,39 @@ def extract_pos(positions, cash):
135
135
return values
136
136
137
137
138
- def turnover ( transactions_df , backtest_data_df , period = 'M' ):
138
+ def get_turnover ( transactions , positions , period = None ):
139
139
"""
140
- Calculates the percent absolute value portfolio turnover.
140
+ Portfolio Turnover Rate:
141
+
142
+ Average value of purchases and sales divided
143
+ by the average portfolio value for the period.
144
+
145
+ If no period is provided the period is one time step.
141
146
142
147
Parameters
143
148
----------
144
149
transactions_df : pd.DataFrame
145
- Contains transactional data.
146
- backtest_data_df : pd.DataFrame
147
- Contains backtest data, like positions.
150
+ Contains transactions data.
151
+ - See full explanation in tears.create_full_tear_sheet
152
+ positions : pd.DataFrame
153
+ Contains daily position values including cash
154
+ - See full explanation in tears.create_full_tear_sheet
148
155
period : str, optional
149
156
Takes the same arguments as df.resample.
150
157
151
158
Returns
152
159
-------
153
- turnoverpct : pd.DataFrame
154
- The number of shares traded for a period as a fraction of total shares .
160
+ turnover_rate : pd.Series
161
+ timeseries of portfolio turnover rates .
155
162
"""
156
163
157
- turnover = transactions_df .apply (
158
- lambda z : z .apply (
159
- lambda r : abs (r ))).resample (period , 'sum' ).sum (axis = 1 )
160
- portfolio_value = backtest_data_df .portfolio_value .resample (period , 'mean' )
161
- turnoverpct = turnover / portfolio_value
162
- turnoverpct = turnoverpct .fillna (0 )
163
- return turnoverpct
164
+ traded_value = transactions .txn_volume
165
+ portfolio_value = positions .sum (axis = 1 )
166
+ if period is not None :
167
+ traded_value = traded_value .resample (period , how = 'sum' )
168
+ portfolio_value = portfolio_value .resample (period , how = 'mean' )
169
+ # traded_value contains the summed value from buys and sells;
170
+ # this is divided by 2.0 to get the average of the two.
171
+ turnover = traded_value / 2.0
172
+ turnover_rate = turnover / portfolio_value
173
+ return turnover_rate
0 commit comments