File size: 2,482 Bytes
352f1aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
class StockSale:
    def __init__(self, name, sale_price, purchase_price, quantity, holding_period_months):
        self.name = name
        self.sale_price = sale_price
        self.purchase_price = purchase_price
        self.holding_period_months = holding_period_months
        self.quantity = quantity

    def calculate_capital_gain(self):
        if self.holding_period_months > 12:
            return self.calculate_ltcg()
        else:
            return self.calculate_stcg()

    def calculate_ltcg(self):
        # Long-term capital gain
        indexed_cost = self.purchase_price
        ltcg = (self.sale_price - indexed_cost) * self.quantity
        return ltcg

    def calculate_stcg(self):
        # Short-term capital gain
        stcg = (self.sale_price - self.purchase_price) * self.quantity
        return stcg

class Portfolio:
    def __init__(self):
        self.stocks = []

    def add_stock(self, stock_sale):
        self.stocks.append(stock_sale)

    def analyze_portfolio(self):
        total_stcg = 0
        total_ltcg = 0
        for stock in self.stocks:
            gain = stock.calculate_capital_gain()
            if stock.holding_period_months > 12:
                total_ltcg += gain
            else:
                total_stcg += gain
        return total_stcg, total_ltcg

    def apply_tax(self, total_stcg, total_ltcg):
        stcg_tax = max(0, total_stcg) * 0.15
        if total_ltcg > 100000:
            ltcg_tax = (total_ltcg - 100000) * 0.10
        else:
            ltcg_tax = 0
        total_tax = stcg_tax + ltcg_tax
        return stcg_tax, ltcg_tax, total_tax

class Stock:
    def __init__(self, name, purchase_price, current_price, quantity):
        self.name = name
        self.purchase_price = purchase_price
        self.current_price = current_price
        self.quantity = quantity

    def calculate_capital_gain(self):
        return (self.current_price - self.purchase_price) * self.quantity

    def calculate_capital_loss(self):
        return -self.calculate_capital_gain() if self.calculate_capital_gain() < 0 else 0

def optimize_taxes_selling_stocks(stocks):
    suggested_stocks_to_sell = []
    for stock in stocks:
        if stock.calculate_capital_gain() < 0:
            suggested_stocks_to_sell.append(stock)
    return suggested_stocks_to_sell

def calculate_total_loss(stocks):
    total_loss = 0
    for stock in stocks:
        total_loss += stock.calculate_capital_loss()
    return total_loss