Files
hyper/fetch_history.py
DiTus 1a95fe1caa Clean up unused files, organize structure, update docs
- Delete obsolete files: data_fetcher_old.py, market_old.py, base_strategy.py (root),
  strategy_sma_cross.py, and old architecture remnants (address_monitor.py,
  position_monitor.py, trade_log.py, wallet_data.py, whale_tracker.py)
- Delete zero-byte Docker artifacts and runtime files (clp_hedger.log,
  clp_hedger/hedge_status.json)
- Move one-off utility scripts to scripts/ directory
- Move example/template files to .temp/ directory
- Update .gitignore: add entries for clp_hedger.log, clp_hedger/hedge_status.json,
  Docker layer hash files, Using, Running, and backups/
- Update .dockerignore: add clp_hedger.log, clp_hedger/hedge_status.json, backups/
- Create example config files: _data/strategies.json.example,
  _data/backtesting_conf.json.example, _data/coin_precision.json.example
- Update GEMINI.md: remove outdated session summaries and duplicate review section
- Update review.md: add cleanup status section, update remaining recommendations
- Update MIGRATION_PLAN.md: mark completed phases, update file references
- Update DOCKER_MIGRATION_GUIDE.md: update import_csv.py path reference
2026-08-05 09:50:36 +02:00

85 lines
2.7 KiB
Python

import os
import requests
import json
import db
import time
from datetime import datetime, timezone
DB_PATH = os.environ.get("PG_CONN_STR", "postgresql://hyper:hyper@localhost:5432/hyper")
URL = "https://api.hyperliquid.xyz/info"
def fetch_historical_candles(coin, start_ms, end_ms, interval="1m"):
"""Fetch historical candles using the raw HTTP API."""
candles = []
current_start = start_ms
while current_start < end_ms:
payload = {
"type": "candleSnapshot",
"req": {
"coin": coin,
"interval": interval,
"startTime": current_start,
"endTime": end_ms
}
}
resp = requests.post(URL, json=payload)
batch = resp.json()
if not batch:
break
for candle in batch:
candle['coin'] = coin
candles.append(candle)
last_ts = batch[-1]['t']
if last_ts < current_start:
break
current_start = last_ts + 1
time.sleep(0.5)
return candles
def write_candles_to_db(coin, candles, interval="1m"):
"""Write candles to the database."""
table_name = db.sanitize_table_name(coin, interval)
conn = db.get_connection()
db.create_candle_table(conn, table_name)
records = []
for candle in candles:
record = (
datetime.fromtimestamp(candle['t'] / 1000, tz=timezone.utc).strftime('%Y-%m-%d %H:%M:%S'),
candle['t'],
candle.get('o'), candle.get('h'), candle.get('l'), candle.get('c'),
candle.get('v'), candle.get('n')
)
records.append(record)
db.upsert_candles(conn, table_name, records)
conn.close()
def get_last_timestamp(coin):
"""Get the most recent timestamp from the database."""
table_name = db.sanitize_table_name(coin, "1m")
conn = db.get_connection()
try:
return db.get_last_timestamp(conn, table_name)
except:
return None
finally:
conn.close()
coins = ["mkts:USTECH", "xyz:XYZ100"]
now_ms = int(time.time() * 1000)
seven_days_ms = 7 * 24 * 60 * 60 * 1000
for coin in coins:
for tf in ["1m", "1d"]:
start_ts = now_ms - seven_days_ms
if start_ts >= now_ms:
print(f"{coin} ({tf}): Already up to date")
continue
print(f"{coin} ({tf}): Fetching historical candles from {datetime.fromtimestamp(start_ts/1000, tz=timezone.utc)} to {datetime.fromtimestamp(now_ms/1000, tz=timezone.utc)}...")
candles = fetch_historical_candles(coin, start_ts, now_ms, interval=tf)
print(f"{coin} ({tf}): Fetched {len(candles)} candles")
write_candles_to_db(coin, candles, interval=tf)
print(f"{coin} ({tf}): Written to database")
print("Done!")