거래 빠르게 안티에서 병신만든거 커서로
feat: Implement backtest source management and enhance candle data handling Changes: - Introduced a new function `_apply_backtest_source_env_from_request` to manage the environment variables for candle, tick, and order book sources based on incoming requests. - Added a teardown function `_teardown_backtest_source_env` to ensure that environment variables do not persist between requests, enhancing the stability of the backtesting environment. - Refactored existing code to utilize the new source management functions, improving code readability and maintainability. - Added new utility functions in `bt_candle_source.py` for fetching and managing candle data, ensuring consistency with live trading data sources. Impact: - These changes improve the flexibility and reliability of the backtesting framework, allowing for better management of data sources and reducing the risk of cross-request contamination.
This commit is contained in:
@@ -118,6 +118,7 @@ def prepend_scalp_candle_warmup(
|
||||
)
|
||||
ps = str(period_start_key)[:12]
|
||||
total_prepended = 0
|
||||
from kis_trader.backtest.bt_candle_source import fetch_ws_candles_warmup_before
|
||||
for code, rows in list(candles_by_code.items()):
|
||||
if not rows:
|
||||
continue
|
||||
@@ -135,16 +136,12 @@ def prepend_scalp_candle_warmup(
|
||||
first_ct = str(rows[first_period_idx].get("candle_time") or "")
|
||||
if not first_ct:
|
||||
continue
|
||||
warm_rows = db.conn.execute(
|
||||
"SELECT candle_time, open, high, low, close, volume "
|
||||
"FROM ws_candles WHERE timeframe=1 AND code=%s "
|
||||
"AND candle_time < %s AND is_confirmed=1 "
|
||||
"ORDER BY candle_time DESC LIMIT %s",
|
||||
[code, first_ct, wb],
|
||||
).fetchall()
|
||||
warm_rows = fetch_ws_candles_warmup_before(
|
||||
db, code, 1, first_ct, wb,
|
||||
)
|
||||
if not warm_rows:
|
||||
continue
|
||||
prefix = [dict(r) for r in reversed(warm_rows)]
|
||||
prefix = warm_rows
|
||||
candles_by_code[code] = prefix + [dict(r) for r in rows]
|
||||
total_prepended += len(prefix)
|
||||
return total_prepended
|
||||
@@ -176,29 +173,24 @@ def load_scalp_candles_by_code(
|
||||
)
|
||||
return candles_by_code, total_candles
|
||||
|
||||
candle_src = os.environ.get("CANDLE_SOURCE", "kis") or "kis"
|
||||
from kis_trader.backtest.bt_candle_source import (
|
||||
fetch_ws_candles_for_code,
|
||||
list_ws_candle_codes,
|
||||
)
|
||||
|
||||
codes_raw = db.conn.execute(
|
||||
"SELECT DISTINCT code FROM ws_candles WHERE timeframe=1 "
|
||||
"AND candle_time >= %s AND candle_time <= %s ORDER BY code",
|
||||
[start_key, end_key],
|
||||
).fetchall()
|
||||
codes = [r["code"] for r in codes_raw]
|
||||
codes = list_ws_candle_codes(db, 1, start_key, end_key)
|
||||
|
||||
candles_by_code: Dict[str, List[Dict]] = {}
|
||||
total_candles = 0
|
||||
|
||||
for code in codes:
|
||||
rows = db.conn.execute(
|
||||
"SELECT candle_time, open, high, low, close, volume "
|
||||
"FROM ws_candles WHERE timeframe=1 AND code=%s "
|
||||
"AND candle_time >= %s AND candle_time <= %s AND is_confirmed=1 "
|
||||
"ORDER BY candle_time ASC",
|
||||
[code, start_key, end_key],
|
||||
).fetchall()
|
||||
rows = fetch_ws_candles_for_code(
|
||||
db, code, 1, start_key, end_key,
|
||||
confirmed_only=True,
|
||||
)
|
||||
if len(rows) < min_bars:
|
||||
continue
|
||||
candles_by_code[code] = [dict(r) for r in rows]
|
||||
candles_by_code[code] = rows
|
||||
total_candles += len(rows)
|
||||
|
||||
prepend_scalp_candle_warmup(db, candles_by_code, str(start_key)[:12])
|
||||
|
||||
Reference in New Issue
Block a user