|
2 | 2 | # Licensed under the MIT License.
|
3 | 3 | from qlib.backtest.exchange import Exchange
|
4 | 4 | from qlib.backtest.position import BasePosition
|
5 |
| -from typing import List, Union |
| 5 | +from typing import List, Tuple, Union |
6 | 6 |
|
7 | 7 | from ..model.base import BaseModel
|
8 | 8 | from ..data.dataset import DatasetH
|
@@ -158,6 +158,36 @@ def alter_outer_trade_decision(self, outer_trade_decision: BaseTradeDecision):
|
158 | 158 | # NOTE: normally, user should do something to the strategy due to the change of outer decision
|
159 | 159 | raise NotImplementedError(f"Please implement the `alter_outer_trade_decision` method")
|
160 | 160 |
|
| 161 | + # helper methods: not necessary but for convenience |
| 162 | + def get_data_cal_avail_range(self, rtype: str = "full") -> Tuple[int, int]: |
| 163 | + """ |
| 164 | + return data calendar's available decision range for `self` strategy |
| 165 | + the range consider following factors |
| 166 | + - data calendar in the charge of `self` strategy |
| 167 | + - trading range limitation from the decision of outer strategy |
| 168 | +
|
| 169 | +
|
| 170 | + related methods |
| 171 | + - TradeCalendarManager.get_data_cal_range |
| 172 | + - BaseTradeDecision.get_data_cal_range_limit |
| 173 | +
|
| 174 | + Parameters |
| 175 | + ---------- |
| 176 | + rtype: str |
| 177 | + - "full": return the available data index range of the strategy from `start_time` to `end_time` |
| 178 | + - "step": return the available data index range of the strategy of current step |
| 179 | +
|
| 180 | + Returns |
| 181 | + ------- |
| 182 | + Tuple[int, int]: |
| 183 | + the available range both sides are closed |
| 184 | + """ |
| 185 | + cal_range = self.trade_calendar.get_data_cal_range(rtype=rtype) |
| 186 | + if self.outer_trade_decision is None: |
| 187 | + raise ValueError(f"There is not limitation for strategy {self}") |
| 188 | + range_limit = self.outer_trade_decision.get_data_cal_range_limit(rtype=rtype) |
| 189 | + return max(cal_range[0], range_limit[0]), min(cal_range[1], range_limit[1]) |
| 190 | + |
161 | 191 |
|
162 | 192 | class ModelStrategy(BaseStrategy):
|
163 | 193 | """Model-based trading strategy, use model to make predictions for trading"""
|
|
0 commit comments