test_export_freqtrade_data.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import importlib.util
  2. import json
  3. import sys
  4. from pathlib import Path
  5. from okx_codex_trader.models import Candle
  6. def load_export_module():
  7. path = Path(__file__).resolve().parents[1] / "scripts" / "export_freqtrade_data.py"
  8. spec = importlib.util.spec_from_file_location("export_freqtrade_data", path)
  9. assert spec is not None
  10. module = importlib.util.module_from_spec(spec)
  11. assert spec.loader is not None
  12. sys.modules[spec.name] = module
  13. spec.loader.exec_module(module)
  14. return module
  15. def test_export_ohlcv_json_writes_freqtrade_futures_filename(tmp_path):
  16. module = load_export_module()
  17. cache_dir = tmp_path / "cache"
  18. explore = __import__("scripts.explore_ultrashort", fromlist=["save_cached_candles"])
  19. explore.save_cached_candles(
  20. cache_dir,
  21. "BTC-USDT-SWAP",
  22. "15m",
  23. [
  24. Candle("BTC-USDT-SWAP", 1_700_000_000_000, 100.0, 101.0, 99.0, 100.5, 10.0),
  25. Candle("BTC-USDT-SWAP", 1_700_000_900_000, 100.5, 102.0, 100.0, 101.5, 11.0),
  26. ],
  27. history_exhausted=True,
  28. )
  29. output_file = module.export_ohlcv_json(
  30. symbol="BTC-USDT-SWAP",
  31. bar="15m",
  32. cache_dir=cache_dir,
  33. output_dir=tmp_path / "freqtrade",
  34. )
  35. assert output_file.name == "BTC_USDT_USDT-15m-futures.json"
  36. assert json.loads(output_file.read_text()) == [
  37. [1_700_000_000_000, 100.0, 101.0, 99.0, 100.5, 10.0],
  38. [1_700_000_900_000, 100.5, 102.0, 100.0, 101.5, 11.0],
  39. ]