Refactor code for improved readability and consistency
- Cleaned up whitespace and formatting in various files including http.py, language.py, logger.py, safe_exec.py, and SQL migration scripts. - Consolidated import statements and removed unnecessary blank lines. - Updated logging configuration for better clarity. - Enhanced the safe execution code with improved error handling and logging. - Removed commented-out code and unnecessary variables in backfill_zero_trades.py and other scripts. - Added a pyproject.toml for Ruff and Vulture configuration. - Introduced requirements-dev.txt for development dependencies. - Removed commented-out stock entries in init.sql for cleaner migration scripts.
This commit is contained in:
@@ -19,9 +19,8 @@ Notes:
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any, Dict, Optional, Tuple, List
|
||||
from datetime import datetime
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from app.utils.db import get_db_connection
|
||||
|
||||
@@ -96,7 +95,9 @@ def _find_best_order_match(
|
||||
if not cand:
|
||||
return None
|
||||
# If the closest candidates are tied, such as identical executed_at, treat it as ambiguous and skip it
|
||||
if len(cand) >= 2 and abs(int(cand[0]["executed_at"]) - trade_ts) == abs(int(cand[1]["executed_at"]) - trade_ts):
|
||||
if len(cand) >= 2 and abs(int(cand[0]["executed_at"]) - trade_ts) == abs(
|
||||
int(cand[1]["executed_at"]) - trade_ts
|
||||
):
|
||||
return None
|
||||
return cand[0]
|
||||
|
||||
@@ -132,14 +133,22 @@ def main() -> None:
|
||||
ap.add_argument("--until", type=str, required=True, help="YYYY-MM-DD or YYYY/MM/DD")
|
||||
ap.add_argument("--window-sec", type=int, default=600, help="Match window, default ±600 seconds")
|
||||
ap.add_argument("--limit", type=int, default=500, help="Maximum number of trades to process")
|
||||
ap.add_argument("--apply", action="store_true", help="Actually write to the database; default is dry-run output only")
|
||||
ap.add_argument(
|
||||
"--apply", action="store_true", help="Actually write to the database; default is dry-run output only"
|
||||
)
|
||||
args = ap.parse_args()
|
||||
|
||||
since_ts = _parse_date_to_ts(args.since)
|
||||
until_ts = _parse_date_to_ts(args.until) + 24 * 3600 - 1 if len(args.until.strip()) <= 10 else _parse_date_to_ts(args.until)
|
||||
until_ts = (
|
||||
_parse_date_to_ts(args.until) + 24 * 3600 - 1
|
||||
if len(args.until.strip()) <= 10
|
||||
else _parse_date_to_ts(args.until)
|
||||
)
|
||||
|
||||
trades = _fetch_bad_trades(args.strategy_id, since_ts, until_ts, args.limit)
|
||||
print(f"[scan] bad_trades={len(trades)} strategy_id={args.strategy_id} since={since_ts} until={until_ts} apply={args.apply}")
|
||||
print(
|
||||
f"[scan] bad_trades={len(trades)} strategy_id={args.strategy_id} since={since_ts} until={until_ts} apply={args.apply}"
|
||||
)
|
||||
|
||||
fixed = 0
|
||||
skipped = 0
|
||||
@@ -173,5 +182,3 @@ def main() -> None:
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
|
||||
|
||||
@@ -7,10 +7,11 @@ Usage:
|
||||
AI_CALIBRATION_MARKET=Crypto python scripts/run_calibration.py
|
||||
AI_CALIBRATION_MARKETS=Crypto,USStock python scripts/run_calibration.py
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
|
||||
import os
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
||||
|
||||
from app.services.ai_calibration import AICalibrationService
|
||||
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
import sys
|
||||
import os
|
||||
import sys
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Add the backend directory to the Python path so app.* can be imported
|
||||
# The app package lives under backend_api_python/app while this script is under backend_api_python/scripts
|
||||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||
|
||||
from app.services.reflection import ReflectionService
|
||||
|
||||
|
||||
def main():
|
||||
# Load backend envs for DATABASE_URL, reflection switches, etc.
|
||||
# This script may be run locally, so we must load .env explicitly.
|
||||
backend_env_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '.env'))
|
||||
root_env_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '.env'))
|
||||
backend_env_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".env"))
|
||||
root_env_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", ".env"))
|
||||
if os.path.exists(root_env_path):
|
||||
load_dotenv(root_env_path, override=False)
|
||||
if os.path.exists(backend_env_path):
|
||||
@@ -28,6 +30,6 @@ def main():
|
||||
print("Reflection stats:", stats)
|
||||
print("Task Completed.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ import os
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, List, Optional
|
||||
from typing import Any, Dict, List
|
||||
|
||||
|
||||
def _ensure_backend_on_syspath() -> None:
|
||||
@@ -70,17 +70,18 @@ def _make_klines_1m(base: float = 3000.0, n: int = 200) -> List[Dict[str, Any]]:
|
||||
else:
|
||||
price *= 1.008 # +0.8% per bar
|
||||
|
||||
o = price * 0.999
|
||||
c = price
|
||||
h = max(o, c) * 1.0005
|
||||
l = min(o, c) * 0.9995
|
||||
open_price = price * 0.999
|
||||
close_price = price
|
||||
high_price = max(open_price, close_price) * 1.0005
|
||||
low_price = min(open_price, close_price) * 0.9995
|
||||
|
||||
klines.append(
|
||||
{
|
||||
"time": int(ts),
|
||||
"open": float(o),
|
||||
"high": float(h),
|
||||
"low": float(l),
|
||||
"close": float(c),
|
||||
"open": float(open_price),
|
||||
"high": float(high_price),
|
||||
"low": float(low_price),
|
||||
"close": float(close_price),
|
||||
"volume": 1.0,
|
||||
}
|
||||
)
|
||||
@@ -179,20 +180,22 @@ def _find_klines_with_signal(ex: TradingExecutor, indicator_code: str) -> List[D
|
||||
price *= float(down_mult)
|
||||
else:
|
||||
price *= float(up_mult)
|
||||
o = price * 0.999
|
||||
c = price
|
||||
h = max(o, c) * 1.0005
|
||||
l = min(o, c) * 0.9995
|
||||
row["open"] = float(o)
|
||||
row["high"] = float(h)
|
||||
row["low"] = float(l)
|
||||
row["close"] = float(c)
|
||||
open_price = price * 0.999
|
||||
close_price = price
|
||||
high_price = max(open_price, close_price) * 1.0005
|
||||
low_price = min(open_price, close_price) * 0.9995
|
||||
row["open"] = float(open_price)
|
||||
row["high"] = float(high_price)
|
||||
row["low"] = float(low_price)
|
||||
row["close"] = float(close_price)
|
||||
|
||||
cnt = _count_signals_for_klines(ex, indicator_code, kl, "both", 5, 1000.0)
|
||||
if cnt["buy"] > 0 or cnt["sell"] > 0:
|
||||
kl2 = _trim_klines_to_last_signal(ex, indicator_code, kl, keep_before=220, keep_after=0)
|
||||
cnt2 = _count_signals_for_klines(ex, indicator_code, kl2, "both", 5, 1000.0)
|
||||
print(f"[OK] Found signals with pattern down={down_mult}, up={up_mult}, split={split}: {cnt} -> trimmed={cnt2}, bars={len(kl2)}")
|
||||
print(
|
||||
f"[OK] Found signals with pattern down={down_mult}, up={up_mult}, split={split}: {cnt} -> trimmed={cnt2}, bars={len(kl2)}"
|
||||
)
|
||||
return kl2
|
||||
print(f"[MISS] Pattern down={down_mult}, up={up_mult}, split={split}: {cnt}")
|
||||
|
||||
@@ -390,5 +393,3 @@ def main() -> None:
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user