forked from robcarver17/python-uk-trading-tax-calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrades.py
352 lines (243 loc) · 11.2 KB
/
trades.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
"""
Python UK trading tax calculator
Copyright (C) 2015 Robert Carver
You may copy, modify and redistribute this file as allowed in the license agreement
but you must retain this header
See README.txt
"""
import datetime
from copy import copy
from utils import type_and_sense_check_arguments, signs_match, repr_class,next_letter_code, pretty
THRESHOLD=0.0001
class Trade(object):
def _possible_args(self):
return self._required_columns()+self._optional_columns()
def _required_columns(self):
return ['Code', 'Commission', 'Price', 'Quantity', 'Tax', 'Date', 'Currency']
def _optional_columns(self):
return self._optional_columns_misc()+self._optional_columns_values()+self._optional_columns_preprocess()+ \
self._optional_columns_allocation()
def _optional_columns_misc(self):
return ['AssetClass', 'parent', 'nextchild','original']
def _optional_columns_values(self):
return [ 'BS']
def _optional_columns_preprocess(self):
return ['FXRate', 'TradeID', 'Value', 'SignQuantity']
def _optional_columns_allocation(self):
return ['tradetype', 'pseudotrade','sharedtrade']
def _has_preprocess_data(self):
return all([x in self.argsused for x in self._optional_columns_preprocess()])
def _has_allocation_data(self):
return all([x in self.argsused for x in self._optional_columns_allocation()]) and \
"SignQuantity" in self.argsused
def _ready_for_split(self):
"""
Can this trade be split?
"""
return self._has_preprocess_data() and self._has_allocation_data()
def _type_check(self):
arg_types=dict(Code=str, Commission=float, Price=float, Quantity=float, Tax=float, Date=datetime.datetime,
BS=str, Currency=str, Value=float,
SignQuantity=float, FXRate=float, AssetClass=str,
tradetype=str, pseudotrade=bool, sharedtrade=bool, TradeID=str, parent=Trade, nextchild=str,
original=Trade)
return arg_types
def _check_inputs(self):
if self.Commission<0.0:
raise Exception("can't have negative commssion")
if self.Tax<0.0:
raise Exception("can't have negative tax")
if self.Quantity<0:
raise Exception("Quantity can't be negative (you're confusing with SignQuantity")
if "SignQuantity" in self.argsused:
if "Quantity" in self.argsused and "BS" in self.argsused:
checksignquant = self._signed_quantity()
if checksignquant!=self.SignQuantity:
raise Exception("Signed quantity of %d not consistent with quantity of %d and BS of %s" %
(self.SignQuantity, self.Quantity, self.BS))
if "typestring" in self.argsused:
assert self.typestring in ["Open", "Close", "OverClose"]
if "Value" in self.argsused:
if not self.Value==0.0 and self.SignQuantity==0.0:
assert not signs_match(self.Value, self.SignQuantity)
def __init__(self, **kwargs):
'''
Constructor
'''
type_and_sense_check_arguments(self, kwargs)
argsused=[]
for key in kwargs:
argsused.append(key)
setattr(self, key, kwargs[key])
setattr(self, 'argsused', argsused)
self._check_inputs()
def modify(self, **kwargs):
modorderfill=type_and_sense_check_arguments(self, kwargs, checkrequired=False)
argsused=self.argsused
for key in modorderfill:
setattr(self, key, modorderfill[key])
argsused.append(key)
argsused=list(set(argsused))
setattr(self, 'argsused', argsused)
self._check_inputs()
def __repr__(self):
return "ID %s Code %s Date %s Quantity %s Price %s Value per block %s" % \
(getattr(self, "TradeID",""), self.Code, str(self.Date), pretty(self.SignQuantity),
pretty(self.Price), pretty(abs(self.Value/self.SignQuantity)))
def brief(self):
return "ID %s Quantity %s" % (getattr(self, "TradeID",""), pretty(self.SignQuantity))
def full(self):
print(repr_class(self))
def add_value(self, raiseerror=True):
if "Value" in self.argsused and raiseerror:
raise Exception("Can't add_value on trade as Value field already set")
if not "SignQuantity" in self.argsused:
self.add_signed_quantity()
## Cash flow method. Negative means buy ...
value= - self.Price * self.SignQuantity
self.modify(Value=value)
return self
def bslabel(self):
if "BS" in self.argsused:
return self.BS
if "SignQuantity" in self.argsused:
if self.SignQuantity>0:
return "BUY"
elif self.SignQuantity<0:
return "SELL"
return "Unknown"
def _signed_quantity(self):
if "BS" not in self.argsused:
raise Exception("can't add signed quantity without BUY or SELL")
if self.BS=="BUY":
multiplier=1
elif self.BS=="SELL":
multiplier=-1
else:
raise Exception("BS can't be %s" % self.BS)
sign_quantity=multiplier * self.Quantity
return sign_quantity
def add_signed_quantity(self):
if "SignQuantity" in self.argsused:
raise Exception("Already have signed quantity")
sign_quantity=self._signed_quantity()
self.modify(SignQuantity=sign_quantity)
return self
def _init_allocation(self, tradetype):
assert self._has_preprocess_data()
self.modify(tradetype=tradetype, pseudotrade=False, sharedtrade=False)
def _share_of_trade(self, share=None, pro_rata=None):
"""
Returns a trade, a clones of self, with quantity share or a pro_rata proportion
"""
assert not (share is None and pro_rata is None)
newtrade=copy(self)
oldquantity=copy(self.SignQuantity)
if pro_rata is None:
assert type(share) is float
assert signs_match(share, oldquantity)
assert abs(share)<=abs(oldquantity)
pro_rata=share/oldquantity
if share is None:
assert type(pro_rata) is float
assert pro_rata>=0.0 and pro_rata<=1.0
share=oldquantity*pro_rata
if pro_rata==0.0:
newtrade.modify(Value=0.0, Commission=0.0, Tax=0.0,
SignQuantity=0.0, Quantity=0.0,
)
else:
newtrade.modify(Value=self.Value*pro_rata, Commission=self.Commission*pro_rata, Tax=self.Tax*pro_rata,
SignQuantity=share, Quantity=abs(share),
)
return newtrade
def spawn_pseudo_trades(self, tradetoclose):
"""
Returns a new clone trade with tradetoclose, and a residual clone trade
"""
assert self._ready_for_split()
assert not self.pseudotrade
oldquantity=copy(self.SignQuantity)
residualtrade=oldquantity - tradetoclose
assert type(tradetoclose) is float
assert signs_match(tradetoclose, oldquantity)
assert abs(tradetoclose)<=abs(oldquantity)
assert abs(tradetoclose)>0.0
neworder=self._share_of_trade(share=tradetoclose)
changedorder=self._share_of_trade(share=residualtrade)
neworder.modify(tradetype="Close")
changedorder.modify(tradetype="Open")
neworder.modify(pseudotrade=True)
changedorder.modify(pseudotrade=True)
oldtradeid=self.TradeID
neworder.modify(TradeID=oldtradeid+":1")
changedorder.modify(TradeID=oldtradeid+":2")
## To avoid duplications we make the opening order a second after the old one
newdate=changedorder.Date+datetime.timedelta(seconds=1)
changedorder.modify(Date=newdate)
return [changedorder, neworder]
def spawn_child_trade(self, share=None, pro_rata=None):
"""
Return a child order, which is a shared trade with a letter suffix, and the parent trade in attribute
and a smaller parent order.
If this is an only child, then the child becomes the parent
"""
assert self._ready_for_split()
assert not self.sharedtrade
thischildid=(getattr(self, "nextchild",None))
if thischildid is None:
firstchild=True
thischildid="a"
else:
firstchild=False
if share is not None:
residual_share = self.SignQuantity - share
residual_pro_rata=None
elif pro_rata is not None:
if pro_rata==1.0:
residual_pro_rata=0.0
else:
residual_pro_rata = 1.0 - pro_rata
residual_share=None
else:
raise Exception("Tried to spawn child order without share or pro rata")
original_trade=(getattr(self, "original",None))
if original_trade is None:
## We keep this so we know the original size of the allocation
original_trade=copy(self)
self.modify(original=original_trade)
child_trade=self._share_of_trade(share=share, pro_rata=pro_rata)
parent_trade=self._share_of_trade(share=residual_share, pro_rata=residual_pro_rata)
parent_trade.modify(nextchild=next_letter_code(thischildid))
if firstchild and self._last_child(share=share, pro_rata=pro_rata):
## only child- will be same as parent
childid=parent_trade.TradeID
child_trade.modify(sharedtrade=False, TradeID=childid)
else:
childid=parent_trade.TradeID+thischildid
child_trade.modify(sharedtrade=True, TradeID=childid, parent=original_trade)
return (parent_trade, child_trade)
def _last_child(self,share=None, pro_rata=None):
oldquantity=self.SignQuantity
if pro_rata is not None:
assert type(pro_rata) is float
assert pro_rata>=0.0 and pro_rata<=1.0
if abs(pro_rata - 1.0)<THRESHOLD:
return True
else:
return False
if share is not None:
assert type(share) is float
assert signs_match(share, oldquantity)
assert abs(share)<=abs(oldquantity)
if abs(share - oldquantity)<THRESHOLD:
return True
else:
return False
raise Exception("no share or pro rata")
def total_mine_or_parent(self):
parent=(getattr(self, "parent",None))
if parent is None:
return self.SignQuantity
else:
return parent.SignQuantity