From 11a49b5ea861576008f9b013fbde58d5f9f609f5 Mon Sep 17 00:00:00 2001 From: Dmitry Maylarov Date: Mon, 31 Jan 2022 13:03:48 +0300 Subject: [PATCH] add smacross, increment investment, fix va --- compare.py | 8 ++++---- strategies.py | 39 +++++++++++++++++++++++++++++++++------ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/compare.py b/compare.py index 2d15221..cf4dbaa 100644 --- a/compare.py +++ b/compare.py @@ -43,7 +43,7 @@ def run(strategy, data, fund_mode=False): ) thestrats = cerebro.run() thestrat = thestrats[0] - # cerebro.plot(iplot=False, style="candlestick") + cerebro.plot(iplot=False, style="candlestick") return thestrat @@ -65,18 +65,18 @@ if __name__ == "__main__": ] ) - stockList = ["SWPPX"] + stockList = ["^GSPC"] # 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, 15, 20, 25): + for period_years in (20, 50): endDate = datetime.datetime.now() startDate = endDate - datetime.timedelta(days=period_years * 365) stockData = get_data(stockList[0], startDate, endDate) actualStart: datetime.datetime = stockData.index[0] data = bt.feeds.PandasData(dataname=stockData) - for i, strategy in enumerate((st.DCA, st.QDCA, st.VA, st.QVA)): + for i, strategy in enumerate((st.SmaCross,)): therun = run(strategy, data) dd = therun.analyzers.drawdown ret = therun.analyzers.returns diff --git a/strategies.py b/strategies.py index 5eced69..2d489ce 100644 --- a/strategies.py +++ b/strategies.py @@ -7,9 +7,31 @@ from pandas_datareader import data as pdr import backtrader as bt reserve = 5 # usd for comms etc cause I cant math -month_sum = 500 # usd +month_sum = 5000 # usd +class SmaCross(bt.Strategy): + # list of parameters which are configurable for the strategy + params = dict( + pfast=50, # period for the fast moving average + pslow=200, # period for the slow moving average + ) + + def __init__(self): + sma1 = bt.ind.SMA(period=self.p.pfast) # fast moving average + sma2 = bt.ind.SMA(period=self.p.pslow) # slow moving average + self.crossover = bt.ind.CrossOver(sma1, sma2) # crossover signal + + def next(self): + if not self.position: # not in the market + if self.crossover > 0: # if fast crosses slow to the upside + self.buy() # enter long + + elif self.crossover < 0: # in the market & cross to the downside + self.close() # close long position + + +""" class LSI(bt.Strategy): def start(self): self.val_start = self.broker.get_cash() @@ -21,7 +43,6 @@ class LSI(bt.Strategy): def stop(self): # calculate actual returns self.roi = (self.broker.get_value() / self.val_start) - 1 - """ print("Starting Value: ${:,.2f}".format(self.val_start)) print("ROI: {:.2f}%".format(self.roi * 100.0)) print( @@ -32,7 +53,7 @@ class LSI(bt.Strategy): print( "Gross Return: ${:,.2f}".format(self.broker.get_value() - self.val_start) ) - """ +""" class PercentageCommisionScheme(bt.CommInfoBase): @@ -111,7 +132,7 @@ class FormulaInvesting(bt.Strategy): return ( self.froi, self.cost, - self.broker.get_value() + self.broker.get_cash(), + self.broker.get_value(), self.times, self.units, self.comms, @@ -119,13 +140,19 @@ class FormulaInvesting(bt.Strategy): def notify_timer(self, timer, when, *args): self.months += 1 - self.broker.add_cash(month_sum) + self.broker.add_cash(month_sum * 1.005 ** (self.months)) self.formula() class VA(FormulaInvesting): def formula(self): - target_value = min(self.months * month_sum, self.broker.get_value()) - reserve + target_value = ( + min( + self.months * month_sum * 1.005 ** (self.months), + self.broker.get_value(), + ) + - reserve + ) self.order_target_value(target=target_value),