Skip to content

Instantly share code, notes, and snippets.

@ereli
Created August 9, 2023 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ereli/6724160073f9d58ee877edfd5663d588 to your computer and use it in GitHub Desktop.
Save ereli/6724160073f9d58ee877edfd5663d588 to your computer and use it in GitHub Desktop.
Try to calculate if you need to pay exit tax
import argparse
from datetime import datetime, timedelta
import yfinance as yf
def get_stock_price(ticker, date):
target_date = datetime.fromisoformat(date).replace(tzinfo=None)
start_date = (target_date - timedelta(days=7)).strftime("%Y-%m-%d")
end_date = (target_date + timedelta(days=7)).strftime("%Y-%m-%d")
stock = yf.Ticker(ticker)
historical_data = stock.history(period="1d", start=start_date, end=end_date)["Close"]
if not historical_data.empty:
closest_date = min(historical_data.index, key=lambda x: abs(x.replace(tzinfo=None) - target_date))
return historical_data[closest_date]
else:
print(f"No price data found for {ticker} on {date}.")
return None
def calculate_exit_tax(shares, price_at_grant, price_at_vest, price_at_sale, grant_date, leave_date, sale_date):
holding_period = (sale_date - grant_date).days
time_in_country = (leave_date - grant_date).days
capital_gain = shares * (price_at_sale - price_at_vest)
if holding_period >= 730: # 2 years in days
tax_rate = 0.25
else:
tax_rate = 0.50
proportion_in_country = time_in_country / holding_period
tax_amount = capital_gain * tax_rate * proportion_in_country
return tax_amount
def generate_markdown_report(
ticker: str,
grant_date: str,
sale_date: str,
leave_date: str,
holding_period: int,
time_in_country: int,
price_at_grant: float,
price_at_vest: float,
price_at_sale: float,
capital_gain: float,
tax_rate: float,
proportion_in_country: float,
tax: float,
) -> str:
report_template = (
"Exit Tax Calculation for {ticker}:\n"
"Grant Date: {grant_date}\n"
"Sale Date: {sale_date}\n"
"Leave Date: {leave_date}\n"
"Holding Period: {holding_period} days\n"
"Time in Country: {time_in_country} days\n"
"Price at Grant: {price_at_grant}\n"
"Price at Vest: {price_at_vest}\n"
"Price at Sale: {price_at_sale}\n"
"Capital Gain: {capital_gain}\n"
"Tax Rate: {tax_rate}%\n"
"Proportion in Country: {proportion_in_country}\n"
"Exit Tax: {tax} shekels\n"
)
report = report_template.format(
ticker=ticker,
grant_date=grant_date,
sale_date=sale_date,
leave_date=leave_date,
holding_period=holding_period,
time_in_country=time_in_country,
price_at_grant=price_at_grant,
price_at_vest=price_at_vest,
price_at_sale=price_at_sale,
capital_gain=capital_gain,
tax_rate=tax_rate * 100,
proportion_in_country=proportion_in_country,
tax=tax,
)
return report
def main():
parser = argparse.ArgumentParser(description="Calculate exit tax.")
parser.add_argument("ticker", help="Ticker symbol of the stock")
parser.add_argument("shares", type=int, help="Number of shares")
parser.add_argument("grant_date", help="Grant date in YYYY-MM-DD format")
parser.add_argument("leave_date", help="Date of leaving the country in YYYY-MM-DD format")
parser.add_argument("sale_date", help="Sale date in YYYY-MM-DD format")
args = parser.parse_args()
grant_date = datetime.fromisoformat(args.grant_date)
leave_date = datetime.fromisoformat(args.leave_date)
sale_date = datetime.fromisoformat(args.sale_date)
price_at_grant = get_stock_price(args.ticker, args.grant_date)
price_at_vest = get_stock_price(args.ticker, args.leave_date)
price_at_sale = get_stock_price(args.ticker, args.sale_date)
if price_at_grant is None or price_at_vest is None or price_at_sale is None:
print("Unable to calculate exit tax due to missing price data.")
return
tax = calculate_exit_tax(
args.shares, price_at_grant, price_at_vest, price_at_sale, grant_date, leave_date, sale_date
)
holding_period = (sale_date - grant_date).days
time_in_country = (leave_date - grant_date).days
capital_gain = args.shares * (price_at_sale - price_at_vest)
if holding_period >= 730:
tax_rate = 0.25
else:
tax_rate = 0.50
proportion_in_country = time_in_country / holding_period
tax_amount = capital_gain * tax_rate * proportion_in_country
tax = calculate_exit_tax(
args.shares, price_at_grant, price_at_vest, price_at_sale, grant_date, leave_date, sale_date
)
if tax > 0:
print(f"The exit tax amount to be paid is: {tax} shekels")
else:
print("No exit tax is due.")
markdown_report = generate_markdown_report(
args.ticker,
args.grant_date,
args.sale_date,
args.leave_date,
holding_period,
time_in_country,
price_at_grant,
price_at_vest,
price_at_sale,
capital_gain,
tax_rate,
proportion_in_country,
tax,
)
print(markdown_report)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment