Skip to content

Commit b5b8508

Browse files
committed
rename GainEntry fields
1 parent 371fa67 commit b5b8508

File tree

3 files changed

+101
-100
lines changed

3 files changed

+101
-100
lines changed

casparser/analysis/gains.py

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ class GainEntry:
4646
fy: str
4747
fund: Fund
4848
type: str
49-
buy_date: date
50-
buy_price: Decimal
49+
purchase_date: date
50+
purchase_value: Decimal
5151
stamp_duty: Decimal
52-
sell_date: date
53-
sell_price: Decimal
52+
sale_date: date
53+
sale_value: Decimal
5454
stt: Decimal
5555
units: Decimal
5656

@@ -67,44 +67,44 @@ def __update_nav(self):
6767
def gain_type(self):
6868
"""Identify gain type based on the current fund type, buy and sell dates."""
6969
ltcg = {
70-
FundType.EQUITY.name: self.buy_date + relativedelta(years=1),
71-
FundType.DEBT.name: self.buy_date + relativedelta(years=3),
70+
FundType.EQUITY.name: self.purchase_date + relativedelta(years=1),
71+
FundType.DEBT.name: self.purchase_date + relativedelta(years=3),
7272
}
7373

74-
return GainType.LTCG if self.sell_date > ltcg[self.type] else GainType.STCG
74+
return GainType.LTCG if self.sale_date > ltcg[self.type] else GainType.STCG
7575

7676
@property
7777
def gain(self) -> Decimal:
78-
return Decimal(round(self.sell_price - self.buy_price, 2))
78+
return Decimal(round(self.sale_value - self.purchase_value, 2))
7979

8080
@property
8181
def fmv(self) -> Decimal:
8282
if self.fund.isin != self._cached_isin:
8383
self.__update_nav()
8484
if self._cached_nav is None:
85-
return self.buy_price
85+
return self.purchase_value
8686
return self._cached_nav * self.units
8787

8888
@property
8989
def index_ratio(self) -> Decimal:
9090
return Decimal(
91-
round(CII[get_fin_year(self.sell_date)] / CII[get_fin_year(self.buy_date)], 2)
91+
round(CII[get_fin_year(self.sale_date)] / CII[get_fin_year(self.purchase_date)], 2)
9292
)
9393

9494
@property
9595
def coa(self) -> Decimal:
9696
if self.fund.type == FundType.DEBT.name:
97-
return Decimal(round(self.buy_price * self.index_ratio, 2))
98-
if self.buy_date < self.__cutoff_date:
99-
if self.sell_date < self.__sell_cutoff_date:
100-
return self.sell_price
101-
return max(self.buy_price, min(self.fmv, self.sell_price))
102-
return self.buy_price
97+
return Decimal(round(self.purchase_value * self.index_ratio, 2))
98+
if self.purchase_date < self.__cutoff_date:
99+
if self.sale_date < self.__sell_cutoff_date:
100+
return self.sale_value
101+
return max(self.purchase_value, min(self.fmv, self.sale_value))
102+
return self.purchase_value
103103

104104
@property
105105
def ltcg_taxable(self) -> Decimal:
106106
if self.gain_type == GainType.LTCG:
107-
return Decimal(round(self.sell_price - self.coa, 2))
107+
return Decimal(round(self.sale_value - self.coa, 2))
108108
return Decimal(0.0)
109109

110110
@property
@@ -211,16 +211,16 @@ def sell(self, sell_date: date, quantity: Decimal, nav: Decimal, tax: Decimal):
211211
original_quantity = abs(quantity)
212212
pending_units = original_quantity
213213
while pending_units > 0:
214-
buy_date, units, buy_nav, buy_tax = self.transactions.popleft()
214+
purchase_date, units, purchase_nav, purchase_tax = self.transactions.popleft()
215215

216216
if units <= pending_units:
217217
gain_units = units
218218
else:
219219
gain_units = pending_units
220220

221-
buy_price = round(gain_units * buy_nav, 2)
222-
sell_price = round(gain_units * nav, 2)
223-
stamp_duty = round(buy_tax * gain_units / units, 2)
221+
purchase_value = round(gain_units * purchase_nav, 2)
222+
sale_value = round(gain_units * nav, 2)
223+
stamp_duty = round(purchase_tax * gain_units / units, 2)
224224
stt = round(tax * gain_units / original_quantity, 2)
225225

226226
pending_units -= units
@@ -229,19 +229,21 @@ def sell(self, sell_date: date, quantity: Decimal, nav: Decimal, tax: Decimal):
229229
fy=fin_year,
230230
fund=self._fund,
231231
type=self.fund_type.name,
232-
buy_date=buy_date,
233-
buy_price=buy_price,
232+
purchase_date=purchase_date,
233+
purchase_value=purchase_value,
234234
stamp_duty=stamp_duty,
235-
sell_date=sell_date,
236-
sell_price=sell_price,
235+
sale_date=sell_date,
236+
sale_value=sale_value,
237237
stt=stt,
238238
units=gain_units,
239239
)
240240
self.gains.append(ge)
241-
if pending_units < 0 and buy_nav is not None:
241+
if pending_units < 0 and purchase_nav is not None:
242242
# Sale is partially matched against the last buy transactions
243243
# Re-add the remaining units to the FIFO queue
244-
self.transactions.appendleft((buy_date, -1 * pending_units, buy_nav, buy_tax))
244+
self.transactions.appendleft(
245+
(purchase_date, -1 * pending_units, purchase_nav, purchase_tax)
246+
)
245247

246248

247249
class CapitalGainsReport:
@@ -254,7 +256,7 @@ def __init__(self, data: CASParserDataType):
254256

255257
@property
256258
def gains(self) -> List[GainEntry]:
257-
return list(sorted(self._gains, key=lambda x: (x.fy, x.fund, x.sell_date)))
259+
return list(sorted(self._gains, key=lambda x: (x.fy, x.fund, x.sale_date)))
258260

259261
def process_data(self):
260262
self._gains = []
@@ -305,12 +307,12 @@ def get_gains_csv_data(self) -> str:
305307
"ISIN",
306308
"Type",
307309
"Units",
308-
"Buy Date",
309-
"Buy Value",
310+
"Purchase Date",
311+
"Purchase Value",
310312
"Stamp Duty",
311313
"Acquisition Value",
312-
"Sell Date",
313-
"Sell Value",
314+
"Sale Date",
315+
"Sale Value",
314316
"STT",
315317
"LTCG",
316318
"LTCG Taxable",
@@ -327,12 +329,12 @@ def get_gains_csv_data(self) -> str:
327329
gain.fund.isin,
328330
gain.type,
329331
gain.units,
330-
gain.buy_date,
331-
gain.buy_price,
332+
gain.purchase_date,
333+
gain.purchase_value,
332334
gain.stamp_duty,
333335
gain.coa,
334-
gain.sell_date,
335-
gain.sell_price,
336+
gain.sale_date,
337+
gain.sale_value,
336338
gain.stt,
337339
gain.ltcg,
338340
gain.ltcg_taxable,

0 commit comments

Comments
 (0)