add smacross, increment investment, fix va

master
Dmitry Maylarov 4 years ago
parent 2392692f2a
commit 11a49b5ea8

@ -43,7 +43,7 @@ def run(strategy, data, fund_mode=False):
) )
thestrats = cerebro.run() thestrats = cerebro.run()
thestrat = thestrats[0] thestrat = thestrats[0]
# cerebro.plot(iplot=False, style="candlestick") cerebro.plot(iplot=False, style="candlestick")
return thestrat return thestrat
@ -65,18 +65,18 @@ if __name__ == "__main__":
] ]
) )
stockList = ["SWPPX"] stockList = ["^GSPC"]
# startDate = datetime.datetime.fromisoformat("2000-01-01") # startDate = datetime.datetime.fromisoformat("2000-01-01")
# endDate = datetime.datetime.fromisoformat("2022-01-01") # endDate = datetime.datetime.fromisoformat("2022-01-01")
print(stockList[0]) print(stockList[0])
for period_years in (1, 2, 5, 10, 15, 20, 25): for period_years in (20, 50):
endDate = datetime.datetime.now() endDate = datetime.datetime.now()
startDate = endDate - datetime.timedelta(days=period_years * 365) startDate = endDate - datetime.timedelta(days=period_years * 365)
stockData = get_data(stockList[0], startDate, endDate) stockData = get_data(stockList[0], startDate, endDate)
actualStart: datetime.datetime = stockData.index[0] actualStart: datetime.datetime = stockData.index[0]
data = bt.feeds.PandasData(dataname=stockData) 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) therun = run(strategy, data)
dd = therun.analyzers.drawdown dd = therun.analyzers.drawdown
ret = therun.analyzers.returns ret = therun.analyzers.returns

@ -7,9 +7,31 @@ from pandas_datareader import data as pdr
import backtrader as bt import backtrader as bt
reserve = 5 # usd for comms etc cause I cant math 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): class LSI(bt.Strategy):
def start(self): def start(self):
self.val_start = self.broker.get_cash() self.val_start = self.broker.get_cash()
@ -21,7 +43,6 @@ class LSI(bt.Strategy):
def stop(self): def stop(self):
# calculate actual returns # calculate actual returns
self.roi = (self.broker.get_value() / self.val_start) - 1 self.roi = (self.broker.get_value() / self.val_start) - 1
"""
print("Starting Value: ${:,.2f}".format(self.val_start)) print("Starting Value: ${:,.2f}".format(self.val_start))
print("ROI: {:.2f}%".format(self.roi * 100.0)) print("ROI: {:.2f}%".format(self.roi * 100.0))
print( print(
@ -111,7 +132,7 @@ class FormulaInvesting(bt.Strategy):
return ( return (
self.froi, self.froi,
self.cost, self.cost,
self.broker.get_value() + self.broker.get_cash(), self.broker.get_value(),
self.times, self.times,
self.units, self.units,
self.comms, self.comms,
@ -119,13 +140,19 @@ class FormulaInvesting(bt.Strategy):
def notify_timer(self, timer, when, *args): def notify_timer(self, timer, when, *args):
self.months += 1 self.months += 1
self.broker.add_cash(month_sum) self.broker.add_cash(month_sum * 1.005 ** (self.months))
self.formula() self.formula()
class VA(FormulaInvesting): class VA(FormulaInvesting):
def formula(self): 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), self.order_target_value(target=target_value),

Loading…
Cancel
Save