1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-11 23:06:58 +08:00

Cash Update (#559)

* fix negative cash

* update order test

* fix bug

* update file_order_test
This commit is contained in:
wangwenxi-handsome
2021-08-12 23:44:22 +08:00
committed by GitHub
parent 05b9fb5a47
commit 735153a50d
4 changed files with 87 additions and 209 deletions

View File

@@ -16,6 +16,8 @@ class FileStrTest(TestAutoData):
EXAMPLE_FILE = DIRNAME / "order_example.csv"
DEAL_NUM_FOR_1000 = 123.47105436976445
def _gen_orders(self) -> pd.DataFrame:
headers = [
"datetime",
@@ -24,11 +26,18 @@ class FileStrTest(TestAutoData):
"direction",
]
orders = [
["20200102", self.TEST_INST, "1000", "sell"],
# test cash limit for buying
["20200103", self.TEST_INST, "1000", "buy"],
# test min_cost for buying
["20200103", self.TEST_INST, "1", "buy"],
# test held stock limit for selling
["20200106", self.TEST_INST, "1000", "sell"],
["20200106", self.TEST_INST, "1000", "buy"],
["20200106", self.TEST_INST, "949.7773413058803", "sell"],
# test cash limit for buying
["20200107", self.TEST_INST, "1000", "buy"],
# test min_cost for selling
["20200108", self.TEST_INST, "1", "sell"],
# test selling all stocks
["20200110", self.TEST_INST, str(self.DEAL_NUM_FOR_1000), "sell"],
]
return pd.DataFrame(orders, columns=headers).set_index(["datetime", "instrument"])
@@ -54,7 +63,7 @@ class FileStrTest(TestAutoData):
backtest_config = {
"start_time": start_time,
"end_time": end_time,
"account": 100000000,
"account": 30000,
"benchmark": None, # benchmark is not required here for trading
"exchange_kwargs": {
"freq": freq,
@@ -62,9 +71,9 @@ class FileStrTest(TestAutoData):
"deal_price": "close",
"open_cost": 0.0005,
"close_cost": 0.0015,
"min_cost": 5,
"min_cost": 500,
"codes": codes,
"trade_unit": 100,
"trade_unit": 2,
},
# "pos_type": "InfPosition" # Position with infinitive position
}
@@ -80,7 +89,16 @@ class FileStrTest(TestAutoData):
},
},
}
backtest(executor=executor_config, strategy=strategy_config, **backtest_config)
report_dict, indicator_dict = backtest(executor=executor_config, strategy=strategy_config, **backtest_config)
# ffr valid
ffr_dict = indicator_dict["1day"]["ffr"].to_dict()
ffr_dict = {str(date).split()[0]: ffr_dict[date] for date in ffr_dict}
assert ffr_dict["2020-01-03"] == 0
assert ffr_dict["2020-01-06"] == self.DEAL_NUM_FOR_1000 / 1000
assert ffr_dict["2020-01-07"] == self.DEAL_NUM_FOR_1000 / 1000
assert ffr_dict["2020-01-08"] == 0
assert ffr_dict["2020-01-10"] == 1
self.EXAMPLE_FILE.unlink()

View File

@@ -1,119 +0,0 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import unittest
import qlib
from qlib.backtest import backtest, order
from qlib.tests import TestAutoData
from qlib.backtest.order import TradeDecisionWO, TradeRangeByTime
import pandas as pd
from pathlib import Path
class FileStrTest(TestAutoData):
TEST_INST = "SH600519"
def init_qlib(self):
provider_uri_day = "/nfs_data1/stock_data/huaxia_1d_qlib"
provider_uri_1min = "/nfs_data1/stock_data/huaxia_1min_qlib"
provider_uri_map = {"1min": provider_uri_1min, "day": provider_uri_day}
client_config = {
"calendar_provider": {
"class": "LocalCalendarProvider",
"module_path": "qlib.data.data",
"kwargs": {
"backend": {
"class": "FileCalendarStorage",
"module_path": "qlib.data.storage.file_storage",
"kwargs": {"provider_uri_map": provider_uri_map},
}
},
},
"feature_provider": {
"class": "LocalFeatureProvider",
"module_path": "qlib.data.data",
"kwargs": {
"backend": {
"class": "FileFeatureStorage",
"module_path": "qlib.data.storage.file_storage",
"kwargs": {"provider_uri_map": provider_uri_map},
}
},
},
}
qlib.init(provider_uri=provider_uri_day, **client_config, expression_cache=None, dataset_cache=None)
def test_file_str(self):
freq = "1min"
inst = ["SH600000", "SH600011"]
start_time = "2020-01-01"
end_time = "2020-01-15 15:00"
strategy_config = {
"class": "RandomOrderStrategy",
"module_path": "qlib.contrib.strategy.rule_strategy",
"kwargs": {
"trade_range": TradeRangeByTime("9:30", "15:00"),
"sample_ratio": 1.0,
"volume_ratio": 0.01,
"market": inst,
},
}
position_dict = {
"cash": 100000000,
"SH600000": {"amount": 100},
"SH600011": {"amount": 101},
}
backtest_config = {
"start_time": start_time,
"end_time": end_time,
"account": position_dict,
"benchmark": None, # benchmark is not required here for trading
"exchange_kwargs": {
"freq": freq,
"limit_threshold": 0.095,
"deal_price": "close",
"open_cost": 0.0005,
"close_cost": 0.0015,
"min_cost": 5,
"codes": inst,
},
"pos_type": "Position", # Position with infinitive position
}
executor_config = {
"class": "NestedExecutor",
"module_path": "qlib.backtest.executor",
"kwargs": {
"time_per_step": "day",
"inner_executor": {
"class": "SimulatorExecutor",
"module_path": "qlib.backtest.executor",
"kwargs": {
"time_per_step": freq,
"generate_report": False,
"verbose": False,
# "verbose": True,
"indicator_config": {
"show_indicator": False,
},
},
},
"inner_strategy": {
"class": "TWAPStrategy",
"module_path": "qlib.contrib.strategy.rule_strategy",
},
"track_data": True,
"generate_report": True,
"indicator_config": {
"show_indicator": True,
},
},
}
self.init_qlib()
backtest(executor=executor_config, strategy=strategy_config, **backtest_config)
if __name__ == "__main__":
unittest.main()