-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgain_loss.py
More file actions
131 lines (110 loc) · 4.2 KB
/
Copy pathgain_loss.py
File metadata and controls
131 lines (110 loc) · 4.2 KB
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
import copy
from position import Position
from transaction import Transaction
from shared_def import LOCALE_FIAT
class GainLoss(dict):
def __init__(self):
super(GainLoss, self).__init__()
self.fiat = 0
self.left_date = self.right_date = None
self.position = None
self.transaction = None
self.matched = 0
self.description = ''
@property
def discountable(self):
return (self.fiat > 0 and (self.right_date - self.left_date).days > 365)
@property
def fiat(self):
""" Gain or loss amount in local fiat currency """
return self['fiat']
@fiat.setter
def fiat(self, value):
self['fiat'] = value
@property
def gain(self):
return self.fiat > 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 matched(self):
return self['matched']
@matched.setter
def matched(self, value):
self['matched'] = value
@property
def description(self):
return self['description']
@description.setter
def description(self, value):
self['description'] = value
@property
def is_cgt(self):
""" Return if this gain/loss is under capital gain tax category"""
return self.matched != 0
@property
def brief(self):
locale_fiat_lower = LOCALE_FIAT.lower()
return dict(
**{locale_fiat_lower: self.fiat},
position=self.position.brief,
buy_transaction=self.position.transaction.brief,
sell_transaction=self.transaction.brief)
@property
def brief_csv(self):
position = self.position.brief if self.position else Position.create_na_brief()
buy_transaction = self.position.transaction.brief if self.position else Transaction.create_na_brief()
sell_transaction = self.transaction.brief if self.transaction else Transaction.create_na_brief()
locale_fiat_lower = LOCALE_FIAT.lower()
row = [
'{}'.format('gain' if self.gain else 'loss'), # gain_or_loss
'{}'.format(self.right_date), # datetime
'{}'.format(self.fiat), # fiat amount
'{}'.format('Yes' if self.discountable else 'No' if self.
gain else 'N/A'), # discountable
'{}'.format(self.description), # description
'{}'.format(buy_transaction.get(locale_fiat_lower, 'N/A')), # buy_transaction.fiat
'{}'.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(buy_transaction['comments']), # buy_transaction.comments
'{}'.format(position['asset']), # position.asset
'{}'.format(position.get(locale_fiat_lower, 'N/A')), # position.fiat (uses locale-specific key)
'{}'.format(position['initial_volume']), # position.initial_volume
'{}'.format(position['price']), # position.price
'{}'.format(position['volume']), # position.volume
'{}'.format(self.matched), # matched
'{}'.format(sell_transaction.get(locale_fiat_lower, 'N/A')), # sell_transaction.fiat
'{}'.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
'"{}"'.format(sell_transaction['comments']) # sell_transaction.comments
]
return ','.join(row)