<jupyter_start><jupyter_text>Import Packages<jupyter_code>from ndfinance.brokers.backtest import *
from ndfinance.core import BacktestEngine
from ndfinance.analysis.backtest import BacktestAnalyzer
from ndfinance.strategies import PeriodicRebalancingStrategy
from ndfinance.visualizers.backtest_visualizer import BasicVisualizer
%matplotlib inline
import matplotlib.pyplot as plt<jupyter_output>2020-10-10 13:22:13,815	INFO resource_spec.py:212 -- Starting Ray with 15.38 GiB memory available for workers and up to 7.7 GiB for objects. You can adjust these settings with ray.init(memory=<bytes>, object_store_memory=<bytes>).
2020-10-10 13:22:14,051	WARNING services.py:923 -- Redis failed to start, retrying now.
2020-10-10 13:22:14,252	INFO services.py:1165 -- View the Ray dashboard at [1m[32mlocalhost:8265[39m[22m<jupyter_text>build strategy<jupyter_code>class AllWeatherPortfolio(PeriodicRebalancingStrategy):
    def __init__(self, weight_dict, rebalance_period):
        super(AllWeatherPortfolio, self).__init__(rebalance_period)
        self.weight_dict = weight_dict
        
        
    def _logic(self):
        self.broker.order(Rebalance(self.weight_dict.keys(), self.weight_dict.values()))<jupyter_output><empty_output><jupyter_text>set portfolio elements, weights, rebalance periodyou can adjust and play in your own!<jupyter_code>PORTFOLIO = {
    "GLD" : 0.05, 
    "SPY" : 0.5,
    "SPTL" : 0.15,
    "BWZ" : 0.15,
    "SPHY": 0.15,
}
REBALANCE_PERIOD = TimeFrames.day * 365<jupyter_output><empty_output><jupyter_text>Make data provider<jupyter_code>dp = BacktestDataProvider()
dp.add_yf_tickers(*PORTFOLIO.keys())<jupyter_output><empty_output><jupyter_text>Make time indexer<jupyter_code>indexer = TimeIndexer(dp.get_shortest_timestamp_seq())
dp.set_indexer(indexer)
dp.cut_data()<jupyter_output><empty_output><jupyter_text>Make broker and add assets<jupyter_code>brk = BacktestBroker(dp, initial_margin=10000)
_ = [brk.add_asset(Asset(ticker=ticker)) for ticker in PORTFOLIO.keys()]<jupyter_output><empty_output><jupyter_text>Initialize strategy<jupyter_code>strategy = AllWeatherPortfolio(PORTFOLIO, rebalance_period=REBALANCE_PERIOD)<jupyter_output><empty_output><jupyter_text>Initialize backtest engine<jupyter_code>engine = BacktestEngine()
engine.register_broker(brk)
engine.register_strategy(strategy)<jupyter_output><empty_output><jupyter_text>run<jupyter_code>log = engine.run()<jupyter_output>[ENGINE]: 100%|██████████| 2090/2090 [00:00<00:00, 11637.76it/s]<jupyter_text>run analysis<jupyter_code>analyzer = BacktestAnalyzer(log)
analyzer.print()<jupyter_output>-------------------------------------------------- [BACKTEST RESULT] --------------------------------------------------
CAGR:10.644
MDD:19.49
CAGR_MDD_ratio:0.546
win_trade_count:24
lose_trade_count:11
total_trade_count:75
win_rate_percentage:32.0
lose_rate_percentage:14.667
sharpe_ratio:0.279
sortino_ratio:0.361
pnl_ratio_sum:13.677
pnl_ratio:6.269
average_realized_pnl:90.262
max_realized_pnl:1255.897
min_realized_pnl:-232.054
average_realized_pnl_percentage:2.427
max_realized_pnl_percentage:26.859
min_realized_pnl_percentage:-13.873
average_realized_pnl_percentage_weighted:0.661
max_realized_pnl_percentage_weighted:9.432
min_realized_pnl_percentage_weighted:-1.999
average_portfolio_value_total:12706.284
max_portfolio_value_total:18406.706
min_portfolio_value_total:9613.284
average_portfolio_value:12706.284
max_portfolio_value:18406.706
min_portfolio_value:9613.284
average_leverage:0.88
max_leverage:1.0
min_leverage:0.0
average_leverage_total:0.88
max_leverage_total:1.0
min_leverage[...]<jupyter_text>visualize<jupyter_code>visualizer = BasicVisualizer()
visualizer.plot_log(log)<jupyter_output><empty_output><jupyter_text>Export<jupyter_code>visualizer.export(EXPORT_PATH)
analyzer.export(EXPORT_PATH)<jupyter_output>-------------------------------------------------- [EXPORTING FIGURES] --------------------------------------------------
exporting figure to:  ./bt_results/all_weather_portfolio/plot/mdd.png
exporting figure to:  ./bt_results/all_weather_portfolio/plot/cagr.png
exporting figure to:  ./bt_results/all_weather_portfolio/plot/sharpe.png
exporting figure to:  ./bt_results/all_weather_portfolio/plot/sortino.png
exporting figure to:  ./bt_results/all_weather_portfolio/plot/portfolio_value.png
exporting figure to:  ./bt_results/all_weather_portfolio/plot/portfolio_value_cum_pnl_perc.png
exporting figure to:  ./bt_results/all_weather_portfolio/plot/portfolio_value_total.png
exporting figure to:  ./bt_results/all_weather_portfolio/plot/portfolio_value_total_cum_pnl_perc.png
exporting figure to:  ./bt_results/all_weather_portfolio/plot/realized_pnl_percentage_hist.png
exporting figure to:  ./bt_results/all_weather_portfolio/plot/realized_pnl_percentage_weighted_hist.png
exporting figure to:  .[...]
{'fraction_non_alphanumeric': 0.13877973112719752, 'fraction_numerical': 0.0562564632885212, 'mean_word_length': 5.908571428571428, 'pattern_counts': {'":': 1, '<': 48, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '433', 'license': 'MIT', 'n_tokens_mistral': 1955, 'n_tokens_neox': 1742, 'n_words': 231, 'path': 'examples/all_weather_portfolio.ipynb', 'repo_name': 'gomtinQQ/NDFinance', 'size': '4835'}