Create utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class StockSale:
|
| 2 |
+
def __init__(self, name, sale_price, purchase_price, quantity, holding_period_months):
|
| 3 |
+
self.name = name
|
| 4 |
+
self.sale_price = sale_price
|
| 5 |
+
self.purchase_price = purchase_price
|
| 6 |
+
self.holding_period_months = holding_period_months
|
| 7 |
+
self.quantity = quantity
|
| 8 |
+
|
| 9 |
+
def calculate_capital_gain(self):
|
| 10 |
+
if self.holding_period_months > 12:
|
| 11 |
+
return self.calculate_ltcg()
|
| 12 |
+
else:
|
| 13 |
+
return self.calculate_stcg()
|
| 14 |
+
|
| 15 |
+
def calculate_ltcg(self):
|
| 16 |
+
# Long-term capital gain
|
| 17 |
+
indexed_cost = self.purchase_price
|
| 18 |
+
ltcg = (self.sale_price - indexed_cost) * self.quantity
|
| 19 |
+
return ltcg
|
| 20 |
+
|
| 21 |
+
def calculate_stcg(self):
|
| 22 |
+
# Short-term capital gain
|
| 23 |
+
stcg = (self.sale_price - self.purchase_price) * self.quantity
|
| 24 |
+
return stcg
|
| 25 |
+
|
| 26 |
+
class Portfolio:
|
| 27 |
+
def __init__(self):
|
| 28 |
+
self.stocks = []
|
| 29 |
+
|
| 30 |
+
def add_stock(self, stock_sale):
|
| 31 |
+
self.stocks.append(stock_sale)
|
| 32 |
+
|
| 33 |
+
def analyze_portfolio(self):
|
| 34 |
+
total_stcg = 0
|
| 35 |
+
total_ltcg = 0
|
| 36 |
+
for stock in self.stocks:
|
| 37 |
+
gain = stock.calculate_capital_gain()
|
| 38 |
+
if stock.holding_period_months > 12:
|
| 39 |
+
total_ltcg += gain
|
| 40 |
+
else:
|
| 41 |
+
total_stcg += gain
|
| 42 |
+
return total_stcg, total_ltcg
|
| 43 |
+
|
| 44 |
+
def apply_tax(self, total_stcg, total_ltcg):
|
| 45 |
+
stcg_tax = max(0, total_stcg) * 0.15
|
| 46 |
+
if total_ltcg > 100000:
|
| 47 |
+
ltcg_tax = (total_ltcg - 100000) * 0.10
|
| 48 |
+
else:
|
| 49 |
+
ltcg_tax = 0
|
| 50 |
+
total_tax = stcg_tax + ltcg_tax
|
| 51 |
+
return stcg_tax, ltcg_tax, total_tax
|
| 52 |
+
|
| 53 |
+
class Stock:
|
| 54 |
+
def __init__(self, name, purchase_price, current_price, quantity):
|
| 55 |
+
self.name = name
|
| 56 |
+
self.purchase_price = purchase_price
|
| 57 |
+
self.current_price = current_price
|
| 58 |
+
self.quantity = quantity
|
| 59 |
+
|
| 60 |
+
def calculate_capital_gain(self):
|
| 61 |
+
return (self.current_price - self.purchase_price) * self.quantity
|
| 62 |
+
|
| 63 |
+
def calculate_capital_loss(self):
|
| 64 |
+
return -self.calculate_capital_gain() if self.calculate_capital_gain() < 0 else 0
|
| 65 |
+
|
| 66 |
+
def optimize_taxes_selling_stocks(stocks):
|
| 67 |
+
suggested_stocks_to_sell = []
|
| 68 |
+
for stock in stocks:
|
| 69 |
+
if stock.calculate_capital_gain() < 0:
|
| 70 |
+
suggested_stocks_to_sell.append(stock)
|
| 71 |
+
return suggested_stocks_to_sell
|
| 72 |
+
|
| 73 |
+
def calculate_total_loss(stocks):
|
| 74 |
+
total_loss = 0
|
| 75 |
+
for stock in stocks:
|
| 76 |
+
total_loss += stock.calculate_capital_loss()
|
| 77 |
+
return total_loss
|