Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pathlib import Path
- import pandas as pd
- from typing import Optional
- from dateutil.relativedelta import relativedelta
- from utils.plots import draw_growth_chart
- from utils.plots import draw_monte_carlo_simulation
- from utils.math import calc_returns
- from utils.math import calc_growth, calc_monte_carlo_simulations
- from utils.math import apply_monte_carlo_sim, calc_simulation_characteristics
- from utils.portfolio import GermanTaxModel, MAPortfolio, BelgianTaxModel, NullTaxModel
- from utils.math import calc_returns, calc_growth
- clean_data_path = Path("clean_data")
- input_path = clean_data_path / "etfs.xlsx"
- etfs = pd.read_excel(input_path, index_col=0)
- etfs.index = pd.to_datetime(etfs.index)
- assets_path = clean_data_path / "assets.xlsx"
- assets = pd.read_excel(assets_path, index_col = 0)
- assets.index = pd.to_datetime(assets.index)
- assets_daily_returns = calc_returns(assets, freq="D")
- borrowing = pd.read_excel(clean_data_path / "borrowing_rate.xlsx", index_col = 0)
- borrowing.index = pd.to_datetime(borrowing.index)
- borrowing = borrowing['borrowing_rate']
- def calc_letf(daily_returns, er, borrowing_rate=None, leverage = 1, adjustment_factor = 0, days_in_year = 365, percent=100):
- def gmean(x):
- return (x + 1)**(1 / days_in_year) - 1
- assert (leverage == 1) or (borrowing_rate is not None), "If leverage is not 1, you must provide the ffr argument!"
- if borrowing_rate is not None:
- daily_borrowing_rate = (leverage - 1) * gmean(-borrowing_rate/percent)
- else:
- daily_borrowing_rate = 0
- daily_adjustment_factor = gmean(adjustment_factor/percent)
- daily_er = gmean(-er/percent)
- return daily_returns * leverage + daily_er + daily_borrowing_rate + daily_adjustment_factor
- etfs['5x_sp500_eu'] = calc_growth(calc_letf(
- assets_daily_returns['sp500+div'],
- borrowing_rate=borrowing,
- leverage=5,
- er=0.7,
- adjustment_factor=-1.2,
- percent=100
- ), percent=1)
- p_hfea = MAPortfolio(
- {
- '3x_sp500_us': dict(dist=55),
- '3x_ltt_us': dict(dist=45),
- },
- start_value = 10000,
- rebalancing = relativedelta(months=3),
- rebalancing_offset = relativedelta(days=-8),
- spread = 0.002,
- tax_model=NullTaxModel(), # Change to GermanTaxModel() or BelgianTaxModel() to see the effect of taxes
- ).backtest(etfs)
- p_hfea5 = MAPortfolio(
- {
- '5x_sp500_eu': dict(dist=20),
- '1x_ltt_eu': dict(dist=80),
- },
- start_value = 10000,
- rebalancing = relativedelta(months=12),
- rebalancing_offset = relativedelta(days=-8),
- spread = 0.002,
- tax_model=NullTaxModel(), # Change to GermanTaxModel() or BelgianTaxModel() to see the effect of taxes
- ).backtest(etfs)
- def perform_monte_carlo_simulation(
- simulation_time: relativedelta,
- portfolio: pd.DataFrame,
- portfolio_name: str,
- start_value: float = 10000,
- monthly_rate: float = 0,
- portfolio_simulations: int = 100,
- reference: Optional[pd.DataFrame] = None,
- reference_name: Optional[str] = None,
- reference_simulations: int = 100,
- ):
- portfolio_simulation = calc_monte_carlo_simulations(portfolio, portfolio_simulations, simulation_time)
- portfolio_simulation = apply_monte_carlo_sim(
- portfolio_simulation,
- start_value = start_value,
- periodic_rate = monthly_rate,
- rate_interval = 30
- )
- kwargs = {}
- is_reference = reference is not None and reference_name is not None
- if is_reference:
- reference_simulation = calc_monte_carlo_simulations(reference, reference_simulations, simulation_time)
- reference_simulation = apply_monte_carlo_sim(
- reference_simulation,
- start_value = start_value,
- periodic_rate = monthly_rate,
- rate_interval = 30
- )
- char = calc_simulation_characteristics(reference_simulation)
- kwargs['reference']=char['mean']
- kwargs['reference_name']=reference_name
- draw_monte_carlo_simulation(
- calc_simulation_characteristics(portfolio_simulation),
- portfolio_name,
- reference_elaborate=char,
- draw_stddev = True,
- draw_minmax = False,
- **kwargs,
- )
- # FOR ME
- start_value = 50_000
- monthly_rate = 1200
- simulation_time = relativedelta(years = 20)
- p_sp500 = MAPortfolio(
- {
- '1x_sp500_eu': dict(dist=100),
- },
- start_value = 10000,
- spread=0.002,
- tax_model=NullTaxModel(),
- ).backtest(etfs)
- perform_monte_carlo_simulation(
- portfolio = p_hfea5,
- portfolio_name = 'HFEA 5x',
- reference = p_sp500,
- reference_name = 'sp500',
- simulation_time = simulation_time,
- start_value = start_value,
- monthly_rate = monthly_rate,
- portfolio_simulations = 150,
- reference_simulations = 150,
- )
- perform_monte_carlo_simulation(
- portfolio = p_hfea,
- portfolio_name = 'HFEA',
- reference = p_sp500,
- reference_name = 'sp500',
- simulation_time = simulation_time,
- start_value = start_value,
- monthly_rate = monthly_rate,
- portfolio_simulations = 150,
- reference_simulations = 150,
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement