Skip to content

Commit 623d43f

Browse files
committed
gains: calculate net invested amount
1 parent 09fec27 commit 623d43f

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

casparser/analysis/gains.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ def __init__(self, fund: Fund, transactions: List[TransactionDataType]):
162162
self._merged_transactions = self.merge_transactions()
163163

164164
self.transactions = deque()
165+
self.invested = Decimal(0.0)
166+
self.balance = Decimal(0.0)
165167
self.gains: List[GainEntry] = []
166168

167169
self.process()
@@ -205,6 +207,8 @@ def process(self):
205207

206208
def buy(self, txn_date: date, quantity: Decimal, nav: Decimal, tax: Decimal):
207209
self.transactions.append((txn_date, quantity, nav, tax))
210+
self.invested += quantity * nav
211+
self.balance += quantity
208212

209213
def sell(self, sell_date: date, quantity: Decimal, nav: Decimal, tax: Decimal):
210214
fin_year = get_fin_year(sell_date)
@@ -223,8 +227,6 @@ def sell(self, sell_date: date, quantity: Decimal, nav: Decimal, tax: Decimal):
223227
stamp_duty = round(purchase_tax * gain_units / units, 2)
224228
stt = round(tax * gain_units / original_quantity, 2)
225229

226-
pending_units -= units
227-
228230
ge = GainEntry(
229231
fy=fin_year,
230232
fund=self._fund,
@@ -238,6 +240,11 @@ def sell(self, sell_date: date, quantity: Decimal, nav: Decimal, tax: Decimal):
238240
units=gain_units,
239241
)
240242
self.gains.append(ge)
243+
244+
self.balance -= gain_units
245+
self.invested -= purchase_value
246+
247+
pending_units -= units
241248
if pending_units < 0 and purchase_nav is not None:
242249
# Sale is partially matched against the last buy transactions
243250
# Re-add the remaining units to the FIFO queue
@@ -252,6 +259,8 @@ class CapitalGainsReport:
252259
def __init__(self, data: CASParserDataType):
253260
self._data: CASParserDataType = data
254261
self._gains: List[GainEntry] = []
262+
self.invested_amount = Decimal(0.0)
263+
self.current_value = Decimal(0.0)
255264
self.process_data()
256265

257266
@property
@@ -273,6 +282,8 @@ def process_data(self):
273282
fifo = FIFOUnits(
274283
Fund(name=name, isin=scheme["isin"], type=scheme["type"]), transactions
275284
)
285+
self.invested_amount += fifo.invested
286+
self.current_value += scheme["valuation"]["value"]
276287
self._gains.extend(fifo.gains)
277288

278289
def get_summary(self):

casparser/cli.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ def print_gains(data, output_file_path=None):
194194
fp.write(cg.get_gains_csv_data())
195195
console.print(f"Detailed gains report saved : [bold]{fname}[/]")
196196

197+
console.print(f"\n[bold]PnL[/] as of [bold]{data['statement_period']['to']}[/]")
198+
console.print(f"{'Total Invested':20s}: [bold]₹{cg.invested_amount:,.2f}[/]")
199+
console.print(f"{'Current Valuation':20s}: [bold]₹{cg.current_value:,.2f}[/]")
200+
pnl = cg.current_value - cg.invested_amount
201+
console.print(f"{'Absolute PnL':20s}: [bold {get_color(pnl)}]₹{pnl:,.2f}[/]")
202+
197203

198204
@click.command(name="casparser", context_settings=CONTEXT_SETTINGS)
199205
@click.option(
@@ -235,7 +241,7 @@ def cli(output, summary, password, include_all, gains, force_pdfminer, filename)
235241
if output is not None:
236242
output_ext = os.path.splitext(output)[-1].lower()
237243

238-
if not (summary or output_ext in (".csv", ".json")):
244+
if not (summary or gains or output_ext in (".csv", ".json")):
239245
summary = True
240246
try:
241247
with Progress(

0 commit comments

Comments
 (0)