tax / utils.py
jarvisx17's picture
Create utils.py
352f1aa verified
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