Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added example of json statistics usage to examples folder, added para… #364

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions examples/buy_and_hold_to_json_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import os

import pandas as pd
import pytz

from qstrader.alpha_model.fixed_signals import FixedSignalsAlphaModel
from qstrader.asset.equity import Equity
from qstrader.asset.universe.static import StaticUniverse
from qstrader.data.backtest_data_handler import BacktestDataHandler
from qstrader.data.daily_bar_csv import CSVDailyBarDataSource
from qstrader.statistics.json_statistics import JSONStatistics
from qstrader.trading.backtest import BacktestTradingSession


if __name__ == "__main__":
start_dt = pd.Timestamp('2004-11-19 14:30:00', tz=pytz.UTC)
end_dt = pd.Timestamp('2019-12-31 23:59:00', tz=pytz.UTC)

# Construct the symbol and asset necessary for the backtest
strategy_symbols = ['GLD']
strategy_assets = ['EQ:GLD']
strategy_universe = StaticUniverse(strategy_assets)

# To avoid loading all CSV files in the directory, set the
# data source to load only those provided symbols
csv_dir = os.environ.get('QSTRADER_CSV_DATA_DIR', '.')
data_source = CSVDailyBarDataSource(csv_dir, Equity, csv_symbols=strategy_symbols)
data_handler = BacktestDataHandler(strategy_universe, data_sources=[data_source])

# Construct an Alpha Model that simply provides a fixed
# signal for the single GLD ETF at 100% allocation
# with a backtest that does not rebalance
strategy_alpha_model = FixedSignalsAlphaModel({'EQ:GLD': 1.0})
strategy_backtest = BacktestTradingSession(
start_dt,
end_dt,
strategy_universe,
strategy_alpha_model,
rebalance='buy_and_hold',
long_only=True,
cash_buffer_percentage=0.01,
data_handler=data_handler
)
strategy_backtest.run()

# Performance Output to file in JSON Format
json_stats = JSONStatistics(
equity_curve=strategy_backtest.get_equity_curve(),
target_allocations=strategy_backtest.get_target_allocations()
)
json_stats.to_file()
2 changes: 2 additions & 0 deletions qstrader/statistics/json_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class JSONStatistics(object):
----------
equity_curve : `pd.DataFrame`
The equity curve DataFrame indexed by date-time.
target_allocations : `pd.DataFrame`
The target allocations DataFrame indexed by date-time.
strategy_id : `str`, optional
The optional ID string for the strategy to pass to
the statistics dict.
Expand Down