-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgain_loss.py
115 lines (94 loc) · 3.27 KB
/
gain_loss.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
import copy
from datetime import datetime
class GainLoss(dict):
def __init__(self):
super(GainLoss, self).__init__()
self.aud = 0
self.left_date = self.right_date = datetime.now()
@property
def discountable(self):
return (self['aud'] > 0 and (self.right_date - self.left_date).days >= 365)
@property
def aud(self):
""" Gain or loss amount in AUD """
return self['aud']
@aud.setter
def aud(self, value):
self['aud'] = value
@property
def gain(self):
return self.aud >= 0
@property
def transaction(self):
return self['transaction']
@transaction.setter
def transaction(self, value):
self['transaction'] = copy.deepcopy(value)
@property
def position(self):
return self['position']
@position.setter
def position(self, value):
self['position'] = copy.deepcopy(value)
@property
def left_date(self):
return self['left_date']
@left_date.setter
def left_date(self, value):
self['left_date'] = value
@property
def right_date(self):
return self['right_date']
@right_date.setter
def right_date(self, value):
self['right_date'] = value
@property
def description(self):
return self['description']
@description.setter
def description(self, value):
self['description'] = value
@property
def matched(self):
return self['matched']
@matched.setter
def matched(self, value):
self['matched'] = value
@property
def brief(self):
return dict(
aud=self.aud,
position=self.position.brief,
buy_transaction=self.position.transaction.brief,
sell_transaction=self.transaction.brief)
@property
def brief_csv(self):
position = self.position.brief
buy_transaction = self.position.transaction.brief
sell_transaction = self.transaction.brief
row = [
'{}'.format('gain' if self.gain else 'loss'), # gain_or_loss
'{}'.format(self.aud), # aud
'{}'.format('Yes' if self.discountable else 'No' if self.
gain else 'N/A'), # discountable
'{}'.format(buy_transaction['aud']), # buy_transaction.aud
'{}'.format(buy_transaction['volume']), # buy_transaction.volume
'{}'.format(buy_transaction['datetime']), # buy_transaction.datetime
'{}'.format(buy_transaction['operation']), # buy_transaction.operation
'{}'.format(buy_transaction['pair']), # buy_transaction.pair
'{}'.format(buy_transaction['usd']), # buy_transaction.usd
'{}'.format(position['asset']), # position.asset
'{}'.format(position['aud']), # position.aud
'{}'.format(position['initial_volume']), # position.initial_volume
'{}'.format(position['price']), # position.price
'{}'.format(position['volume']), # position.volume
'{}'.format(self.matched), # matched
'{}'.format(sell_transaction['aud']), # sell_transaction.aud
'{}'.format(sell_transaction['volume']), # sell_transaction.volume
'{}'.format(sell_transaction['datetime']), # sell_transaction.datetime
'{}'.format(
sell_transaction['operation']), # sell_transaction.operation
'{}'.format(sell_transaction['pair']), # sell_transaction.pair
'{}'.format(sell_transaction['usd']) # sell_transaction.usd
]
return ','.join(row)