|
|
|
|
@ -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(
|
|
|
|
|
@ -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),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|