1、新增dp策略
This commit is contained in:
@@ -19,11 +19,11 @@ class Strategy(ABC):
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
context: "BacktestContext",
|
||||
symbol: str,
|
||||
enable_log: bool = True,
|
||||
**params: Any,
|
||||
self,
|
||||
context: "BacktestContext",
|
||||
symbol: str,
|
||||
enable_log: bool = True,
|
||||
**params: Any,
|
||||
):
|
||||
"""
|
||||
Args:
|
||||
@@ -36,6 +36,9 @@ class Strategy(ABC):
|
||||
self.params = params
|
||||
self.enable_log = enable_log
|
||||
self.trading = False
|
||||
# 缓存指标用
|
||||
self._indicator_cache = None # type: Optional[Tuple[np.ndarray, ...]]
|
||||
self._cache_length = 0 # 上次缓存时数据长度
|
||||
|
||||
def on_init(self):
|
||||
"""
|
||||
@@ -74,6 +77,9 @@ class Strategy(ABC):
|
||||
"""
|
||||
pass
|
||||
|
||||
def on_start_trading(self):
|
||||
pass
|
||||
|
||||
# --- 新增/修改的辅助方法 ---
|
||||
|
||||
def send_order(self, order: "Order") -> Optional[Order]:
|
||||
@@ -83,7 +89,6 @@ class Strategy(ABC):
|
||||
"""
|
||||
if not self.trading:
|
||||
return None
|
||||
|
||||
if self.context.is_rollover_bar:
|
||||
self.log(f"当前是换月K线,禁止开仓订单")
|
||||
return None
|
||||
@@ -205,10 +210,22 @@ class Strategy(ABC):
|
||||
return self.context.get_price_history(key)
|
||||
|
||||
def get_indicator_tuple(self):
|
||||
close = np.array(self.get_price_history("close"))
|
||||
open = np.array(self.get_price_history("open"))
|
||||
"""获取价格数据的 numpy 数组元组,带缓存功能。"""
|
||||
close_data = self.get_price_history("close")
|
||||
current_length = len(close_data)
|
||||
|
||||
# 如果长度没有变化,直接返回缓存
|
||||
if self._indicator_cache is not None and current_length == self._cache_length:
|
||||
return self._indicator_cache
|
||||
|
||||
# 数据有变化,重新创建数组并更新缓存
|
||||
close = np.array(close_data)
|
||||
open_price = np.array(self.get_price_history("open"))
|
||||
high = np.array(self.get_price_history("high"))
|
||||
low = np.array(self.get_price_history("low"))
|
||||
volume = np.array(self.get_price_history("volume"))
|
||||
|
||||
return (close, open, high, low, volume)
|
||||
self._indicator_cache = (close, open_price, high, low, volume)
|
||||
self._cache_length = current_length
|
||||
|
||||
return self._indicator_cache
|
||||
|
||||
Reference in New Issue
Block a user