How to set region in backtest ?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 48.7k
- Forks
- 7.7k
- PR merge metrics
- No merged PRs in 30d
Description
I'm currently using the following code for testing over US data, I have a question about trading cost. Based on what I've learned, US market has different rule of trading cost compared to Chinese market. US market's trading cost is based on share, while Chinese market's trading cost is based on trading value, so I wonder how to set the trading cost if I'm going to test on US market. Thanks!
`from pprint import pprint
import qlib
import pandas as pd
from qlib.utils.time import Freq
from qlib.utils import flatten_dict
from qlib.backtest import backtest, executor
from qlib.contrib.evaluate import risk_analysis
from qlib.contrib.strategy import TopkDropoutStrategy
if name == "main":
qlib.init(provider_uri=r"../../benchmark/us_data/qlib_data/")
score_df = pd.read_csv("../gbrt.csv")
score_df['instrument'] = score_df['instrument'].astype(str)
print(score_df)
start = "2023-01-01"
end = "2023-12-31"
score_df = score_df[(score_df['datetime'] >= start) & (score_df['datetime'] <= end)]
score_df.sort_values(by=['datetime', 'instrument'], inplace=True)
score_df["datetime"] = pd.to_datetime(score_df["datetime"])
pred_score = score_df.set_index(["datetime", "instrument"])["score"]
SP500_BENCH = "SPX"
FREQ = "day"
STRATEGY_CONFIG = {
"topk": 50,
"n_drop": 10,
"signal": pred_score,
# "class": "TopkDropoutStrategy",
# "module_path": "qlib.contrib.strategy",
# "kwargs": {"signal": pred_score, "topk": 50, "n_drop": 5},
}
EXECUTOR_CONFIG = {
"time_per_step": "day",
"generate_portfolio_metrics": True,
"verbose": True,
# "class": "SimulatorExecutor",
# "module_path": "qlib.backtest.executor",
# "kwargs": {
# "time_per_step": "day",
# "generate_portfolio_metrics": True,
# },
}
backtest_config = {
"start_time": start,
"end_time": end,
"account": 100000000,
"benchmark": SP500_BENCH,
"exchange_kwargs": {
"trade_unit": 1,
"freq": FREQ,
"limit_threshold": None,
"deal_price": "close",
"open_cost": 0.0000229,
"close_cost": 0,
"min_cost": 0.01,
},
}
strategy_obj = TopkDropoutStrategy(**STRATEGY_CONFIG)
executor_obj = executor.SimulatorExecutor(**EXECUTOR_CONFIG)
portfolio_metric_dict, indicator_dict = backtest(executor=executor_obj, strategy=strategy_obj, **backtest_config)
analysis_freq = "{0}{1}".format(*Freq.parse(FREQ))
report_normal, positions_normal = portfolio_metric_dict.get(analysis_freq)
analysis = dict()
analysis["excess_return_without_cost"] = risk_analysis(
report_normal["return"] - report_normal["bench"], freq=analysis_freq
)
analysis["excess_return_with_cost"] = risk_analysis(
report_normal["return"] - report_normal["bench"] - report_normal["cost"], freq=analysis_freq
)
analysis_df = pd.concat(analysis) # type: pd.DataFrame
# log metrics
analysis_dict = flatten_dict(analysis_df["risk"].unstack().T.to_dict())
# print out results
pprint(f"The following are analysis results of benchmark return({analysis_freq}).")
pprint(risk_analysis(report_normal["bench"], freq=analysis_freq))
pprint(f"The following are analysis results of the excess return without cost({analysis_freq}).")
pprint(analysis["excess_return_without_cost"])
pprint(f"The following are analysis results of the excess return with cost({analysis_freq}).")
pprint(analysis["excess_return_with_cost"])
`
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the qlib.backtest.backtest entry point and the exchange_kwargs block shown in the issue, especially the trading-cost settings. Trace how those values are consumed during the simulator run and determine whether regional cost rules can be configured. Done means the US cost model is supported or the supported configuration is clearly documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100