-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
txn.py
217 lines (177 loc) · 6.5 KB
/
txn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#
# Copyright 2015 Quantopian, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import division
from collections import defaultdict
import pandas as pd
def map_transaction(txn):
"""
Maps a single transaction row to a dictionary.
Parameters
----------
txn : pd.DataFrame
A single transaction object to convert to a dictionary.
Returns
-------
dict
Mapped transaction.
"""
# sid can either be just a single value or a SID descriptor
if isinstance(txn['sid'], dict):
sid = txn['sid']['sid']
symbol = txn['sid']['symbol']
else:
sid = txn['sid']
symbol = None
return {'sid': sid,
'symbol': symbol,
'price': txn['price'],
'order_id': txn['order_id'],
'amount': txn['amount'],
'commission': txn['commission'],
'dt': txn['dt']}
def make_transaction_frame(transactions):
"""
Formats a transaction DataFrame.
Parameters
----------
transactions : pd.DataFrame
Contains improperly formatted transactional data.
Returns
-------
df : pd.DataFrame
Daily transaction volume and dollar ammount.
- See full explanation in tears.create_full_tear_sheet.
"""
transaction_list = []
for dt in transactions.index:
txns = transactions.loc[dt]
if len(txns) == 0:
continue
for txn in txns:
txn = map_transaction(txn)
transaction_list.append(txn)
df = pd.DataFrame(sorted(transaction_list, key=lambda x: x['dt']))
df['txn_dollars'] = -df['amount'] * df['price']
df.index = list(map(pd.Timestamp, df.dt.values))
return df
def get_txn_vol(transactions):
"""Extract daily transaction data from set of transaction objects.
Parameters
----------
transactions : pd.DataFrame
Time series containing one row per symbol (and potentially
duplicate datetime indices) and columns for amount and
price.
Returns
-------
pd.DataFrame
Daily transaction volume and number of shares.
- See full explanation in tears.create_full_tear_sheet.
"""
amounts = transactions.amount.abs()
prices = transactions.price
values = amounts * prices
daily_amounts = amounts.groupby(amounts.index).sum()
daily_values = values.groupby(values.index).sum()
daily_amounts.name = "txn_shares"
daily_values.name = "txn_volume"
return pd.concat([daily_values, daily_amounts], axis=1)
def adjust_returns_for_slippage(returns, turnover, slippage_bps):
"""Apply a slippage penalty for every dollar traded.
Parameters
----------
returns : pd.Series
Time series of daily returns.
turnover: pd.Series
Time series of daily total of buys and sells
divided by portfolio value.
- See txn.get_turnover.
slippage_bps: int/float
Basis points of slippage to apply.
Returns
-------
pd.Series
Time series of daily returns, adjusted for slippage.
"""
slippage = 0.0001 * slippage_bps
# Only include returns in the period where the algo traded.
trim_returns = returns.loc[turnover.index]
return trim_returns - turnover * slippage
def create_txn_profits(transactions):
"""
Compute per-trade profits.
Generates a new transactions DataFrame with a profits column
Parameters
----------
transactions : pd.DataFrame
Daily transaction volume and number of shares.
- See full explanation in tears.create_full_tear_sheet.
Returns
-------
profits_dts : pd.DataFrame
DataFrame containing transactions and their profits, datetimes,
amounts, current prices, prior prices, and symbols.
"""
txn_descr = defaultdict(list)
for symbol, transactions_sym in transactions.groupby('symbol'):
transactions_sym = transactions_sym.reset_index()
for i, (amount, price, dt) in transactions_sym.iloc[1:][
['amount', 'price', 'date_time_utc']].iterrows():
prev_amount, prev_price, prev_dt = transactions_sym.loc[
i - 1, ['amount', 'price', 'date_time_utc']]
profit = (price - prev_price) * -amount
txn_descr['profits'].append(profit)
txn_descr['dts'].append(dt - prev_dt)
txn_descr['amounts'].append(amount)
txn_descr['prices'].append(price)
txn_descr['prev_prices'].append(prev_price)
txn_descr['symbols'].append(symbol)
profits_dts = pd.DataFrame(txn_descr)
return profits_dts
def get_turnover(transactions, positions, period=None, average=True):
"""
Portfolio Turnover Rate:
Value of purchases and sales divided
by the average portfolio value for the period.
If no period is provided the period is one time step.
Parameters
----------
transactions_df : pd.DataFrame
Contains transactions data.
- See full explanation in tears.create_full_tear_sheet
positions : pd.DataFrame
Contains daily position values including cash
- See full explanation in tears.create_full_tear_sheet
period : str, optional
Takes the same arguments as df.resample.
average : bool
if True, return the average of purchases and sales divided
by portfolio value. If False, return the sum of
purchases and sales divided by portfolio value.
Returns
-------
turnover_rate : pd.Series
timeseries of portfolio turnover rates.
"""
traded_value = transactions.txn_volume
portfolio_value = positions.sum(axis=1)
if period is not None:
traded_value = traded_value.resample(period, how='sum')
portfolio_value = portfolio_value.resample(period, how='mean')
# traded_value contains the summed value from buys and sells;
# this is divided by 2.0 to get the average of the two.
turnover = traded_value / 2.0 if average else traded_value
turnover_rate = turnover.div(portfolio_value, axis='index')
return turnover_rate