|
|
|
@ -6,14 +6,13 @@ import matplotlib.pyplot as plt
|
|
|
|
from pandas_datareader import data as pdr
|
|
|
|
from pandas_datareader import data as pdr
|
|
|
|
import backtrader as bt
|
|
|
|
import backtrader as bt
|
|
|
|
|
|
|
|
|
|
|
|
stockList = ["SWPPX"]
|
|
|
|
|
|
|
|
month_sum = 500 # usd
|
|
|
|
|
|
|
|
reserve = 5 # usd for comms etc
|
|
|
|
|
|
|
|
# startDate = datetime.datetime.fromisoformat("2000-01-01")
|
|
|
|
|
|
|
|
# endDate = datetime.datetime.fromisoformat("2022-01-01")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import strategies as st
|
|
|
|
import strategies as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# import data
|
|
|
|
|
|
|
|
def get_data(stocks, start, end):
|
|
|
|
|
|
|
|
stockData = pdr.get_data_yahoo(stocks, start, end)
|
|
|
|
|
|
|
|
return stockData
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run(strategy, data, fund_mode=False):
|
|
|
|
def run(strategy, data, fund_mode=False):
|
|
|
|
if fund_mode:
|
|
|
|
if fund_mode:
|
|
|
|
@ -36,7 +35,7 @@ def run(strategy, data, fund_mode=False):
|
|
|
|
if fund_mode:
|
|
|
|
if fund_mode:
|
|
|
|
cerebro.broker.set_fundmode(True)
|
|
|
|
cerebro.broker.set_fundmode(True)
|
|
|
|
|
|
|
|
|
|
|
|
cerebro.broker.set_cash(month_sum)
|
|
|
|
cerebro.broker.set_cash(st.month_sum)
|
|
|
|
cerebro.addanalyzer(bt.analyzers.DrawDown, _name="drawdown")
|
|
|
|
cerebro.addanalyzer(bt.analyzers.DrawDown, _name="drawdown")
|
|
|
|
cerebro.addanalyzer(bt.analyzers.VWR, _name="returns")
|
|
|
|
cerebro.addanalyzer(bt.analyzers.VWR, _name="returns")
|
|
|
|
cerebro.addanalyzer(
|
|
|
|
cerebro.addanalyzer(
|
|
|
|
@ -44,31 +43,31 @@ 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if __name__ == "__main__":
|
|
|
|
df = pd.DataFrame(
|
|
|
|
df = pd.DataFrame(
|
|
|
|
columns=[
|
|
|
|
columns=[
|
|
|
|
"annual%",
|
|
|
|
|
|
|
|
"froi",
|
|
|
|
"froi",
|
|
|
|
"cost",
|
|
|
|
"cost",
|
|
|
|
"total_value",
|
|
|
|
"total_value",
|
|
|
|
"times",
|
|
|
|
"times",
|
|
|
|
"units",
|
|
|
|
"units",
|
|
|
|
"comms",
|
|
|
|
"comms",
|
|
|
|
|
|
|
|
"annual%",
|
|
|
|
"max_dd_len",
|
|
|
|
"max_dd_len",
|
|
|
|
"max_dd",
|
|
|
|
"max_dd",
|
|
|
|
"max_md",
|
|
|
|
"max_md",
|
|
|
|
"twr",
|
|
|
|
"twr",
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
# import data
|
|
|
|
|
|
|
|
def get_data(stocks, start, end):
|
|
|
|
|
|
|
|
stockData = pdr.get_data_yahoo(stocks, start, end)
|
|
|
|
|
|
|
|
return stockData
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
stockList = ["SWPPX"]
|
|
|
|
|
|
|
|
# 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 (1, 2, 5, 10, 15, 20, 25):
|
|
|
|
endDate = datetime.datetime.now()
|
|
|
|
endDate = datetime.datetime.now()
|
|
|
|
startDate = endDate - datetime.timedelta(days=period_years * 365)
|
|
|
|
startDate = endDate - datetime.timedelta(days=period_years * 365)
|
|
|
|
@ -83,7 +82,12 @@ if __name__ == "__main__":
|
|
|
|
# tr = thestrat.analyzers.tr
|
|
|
|
# tr = thestrat.analyzers.tr
|
|
|
|
# print(next(reversed(tr.get_analysis())))
|
|
|
|
# print(next(reversed(tr.get_analysis())))
|
|
|
|
# print(tr.get_analysis())
|
|
|
|
# print(tr.get_analysis())
|
|
|
|
|
|
|
|
params = therun.calc_params()
|
|
|
|
|
|
|
|
annual = 100 * (
|
|
|
|
|
|
|
|
(1 + params[0] / 100) ** (365 / (endDate - actualStart).days) - 1
|
|
|
|
|
|
|
|
)
|
|
|
|
df.loc[strategy.__name__] = therun.calc_params() + (
|
|
|
|
df.loc[strategy.__name__] = therun.calc_params() + (
|
|
|
|
|
|
|
|
annual,
|
|
|
|
dd.get_analysis().max.len,
|
|
|
|
dd.get_analysis().max.len,
|
|
|
|
dd.get_analysis().max.drawdown,
|
|
|
|
dd.get_analysis().max.drawdown,
|
|
|
|
dd.get_analysis().max.moneydown,
|
|
|
|
dd.get_analysis().max.moneydown,
|
|
|
|
@ -91,5 +95,5 @@ if __name__ == "__main__":
|
|
|
|
)
|
|
|
|
)
|
|
|
|
print("Starting from:", actualStart)
|
|
|
|
print("Starting from:", actualStart)
|
|
|
|
print("Time in Market: {:.1f} years".format((endDate - actualStart).days / 365))
|
|
|
|
print("Time in Market: {:.1f} years".format((endDate - actualStart).days / 365))
|
|
|
|
# print(df[["annual%", "froi", "cost", "total_value"]])
|
|
|
|
# print(df[["annual%", "froi", "cost", "total_value", "max_dd", "max_md"]])
|
|
|
|
print(df)
|
|
|
|
print(df)
|
|
|
|
|