diff --git a/compare.py b/compare.py index 89484f9..888e341 100644 --- a/compare.py +++ b/compare.py @@ -30,7 +30,7 @@ def get_data(stocks, start, end): def human_readable_size(size, decimal_places=3): for unit in ["$", "K$", "M$", "G$", "T$", "P$"]: if size < 1000.0 or unit == "P$": - return f"{size:.{decimal_places}f} {unit}" + return f"{size:.{decimal_places}f}{unit}" break size /= 1000.0 @@ -121,23 +121,25 @@ def simulate(stockData, monthly_params): (actualEnd - actualStart).days / 365, ) ) - # print(df[["annual%", "froi%", "cost", "total_value", "max_dd%", "max_md"]]) - print(df) + with pd.option_context("display.max_rows", None, "display.max_columns", None): + # print(df[["annual%", "froi%", "cost", "total_value", "max_dd%", "max_md"]]) + print(df) if __name__ == "__main__": stockList = ["^GSPC"] - monthly_params = dict(sum=1000, coef=1.001) + monthly_params = dict(sum=1000, coef=1, t_rate=1 + 0.02 / 12) # startDate = datetime.datetime.fromisoformat("2000-01-01") # endDate = datetime.datetime.fromisoformat("2022-01-01") print(stockList[0]) - for period_years in (1, 2, 5, 10, 20, 50, 100): + """ + for period_years in (10, 20): end_date = datetime.datetime.now() start_date = end_date - datetime.timedelta(days=period_years * 365) stockData = get_data(stockList[0], start_date, end_date) simulate(stockData, monthly_params) """ - period_years = 10 + period_years = 5 stockData = get_data( stockList[0], datetime.datetime.fromisoformat("1900-01-01"), @@ -147,9 +149,8 @@ if __name__ == "__main__": end_date = start_date + datetime.timedelta(days=period_years * 365) while start_date < datetime.datetime.today(): stockData = get_data(stockList[0], start_date, end_date) - simulate(stockData) + simulate(stockData, monthly_params) start_date, end_date = ( end_date, end_date + datetime.timedelta(days=period_years * 365), ) - """ diff --git a/strategies.py b/strategies.py index a1ad3a3..36e46aa 100644 --- a/strategies.py +++ b/strategies.py @@ -41,7 +41,7 @@ class PercentageCommisionScheme(bt.CommInfoBase): ) def _getcommission(self, size, price, pseudoexec): - return abs(size * price * self.p.commission) + 4 # 290rub/month + return abs(size * price * self.p.commission) + 4 # 290rub/month + 0.04% class Investing(bt.Strategy): @@ -136,18 +136,13 @@ class VA(FormulaInvesting): def formula(self): target_value = ( min( - self.months * self.monthly_cash, + self.monthly_cash * self.months, self.broker.get_value(), ) - reserve ) self.order_target_value(target=target_value), - - -class DCA(FormulaInvesting): - def formula(self): - target_value = self.broker.get_value() - reserve - self.order_target_value(target=target_value) + self.broker.set_cash(self.broker.get_cash() * self.monthly_params["t_rate"]) class QVA(VA): @@ -156,6 +151,12 @@ class QVA(VA): super().formula() +class DCA(FormulaInvesting): + def formula(self): + target_value = self.broker.get_value() - reserve + self.order_target_value(target=target_value) + + class QDCA(DCA): def formula(self): if not self.months % 3: