From 4b26f89e88ac72816a75959ca8a890c65aa9287d Mon Sep 17 00:00:00 2001 From: Dmitry Maylarov Date: Mon, 31 Jan 2022 16:17:26 +0300 Subject: [PATCH] sma_va --- compare.py | 8 +++++--- strategies.py | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/compare.py b/compare.py index cf4dbaa..a9682b5 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 @@ -69,14 +69,16 @@ if __name__ == "__main__": # startDate = datetime.datetime.fromisoformat("2000-01-01") # endDate = datetime.datetime.fromisoformat("2022-01-01") print(stockList[0]) - for period_years in (20, 50): + for period_years in (1, 2, 5): 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.SmaCross,)): + for i, strategy in enumerate( + (st.SmaCross, st.DCA, st.QDCA, st.VA, st.QVA, st.SmaVA) + ): therun = run(strategy, data) dd = therun.analyzers.drawdown ret = therun.analyzers.returns diff --git a/strategies.py b/strategies.py index 24a26e3..d59fc7e 100644 --- a/strategies.py +++ b/strategies.py @@ -37,7 +37,7 @@ class LSI(bt.Strategy): class PercentageCommisionScheme(bt.CommInfoBase): params = ( - ("commission", 0.004), + ("commission", 0.0004), ("stocklike", True), ("commtype", bt.CommInfoBase.COMM_PERC), ) @@ -174,7 +174,17 @@ class SmaCross(Investing): 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 + self.order_target_value(target=self.broker.get_cash()) # enter long elif self.crossover < 0: # in the market & cross to the downside self.close() # close long position + + +class SmaVA(SmaCross): + def next(self): + if not self.position: # not in the market + if self.crossover > 0: # if fast crosses slow to the upside + self.order_target_value(target=self.broker.get_cash()) # enter long + + elif self.crossover < 0: # in the market & cross to the downside + self.order_target_value(target=self.broker.get_value() / 2) # close half