Files
2026-11-17 20:07:31 +01:00

286 lines
12 KiB
Python

"""Demo mode: the bot runs on its own, with fake money and no broker.
DEMO_DATA=real (default):
- Real candles from Yahoo Finance for every instrument in config, polled every few minutes.
- Real news headlines, judged by DeepSeek if a key is set, otherwise by a keyword score.
DEMO_DATA=simulated:
- Made-up prices (random walk with trending phases) and made-up news. Works offline,
and runs fast, so you can see many trades in a few minutes.
In both: the same strategy as pine/trend_strategy.pine runs in Python on each new candle,
and every order goes to the built-in paper broker.
"""
import math
import random
import threading
import time
from collections import deque
from dataclasses import dataclass, field
from .config import INSTRUMENTS
from .sentiment import Verdict
from .strategy import DEFAULT, adx, atr, diagnose, ema, signal # noqa: F401 (re-exported: the rules live in strategy.py)
# Rough starting levels and volatility per candle. Simulated, not live data.
START = {"BTCUSD": 67000, "ETHUSD": 3500, "SOLUSD": 180,
"ASML": 700, "ADYEN": 1400, "INGA": 18, "PHIA": 25, "MC": 600, "TTE": 55,
"SAP": 220, "SIE": 200, "ALV": 330, "SHEL": 27, "AZN": 120, "HSBA": 8,
"SPY": 600, "QQQ": 520, "AAPL": 230, "MSFT": 450, "NVDA": 180}
FAST, SLOW = DEFAULT.fast, DEFAULT.slow
@dataclass
class Series:
symbol: str
vol: float # stdev of returns per candle
price: float
regime: float = 0.0 # drift per candle
regime_left: int = 0
closes: deque = field(default_factory=lambda: deque(maxlen=300))
highs: deque = field(default_factory=lambda: deque(maxlen=300))
lows: deque = field(default_factory=lambda: deque(maxlen=300))
volumes: deque = field(default_factory=lambda: deque(maxlen=300))
in_position: bool = False
trail: float | None = None
last_ts: int = 0 # real mode: timestamp of the newest closed candle
currency: str = ""
error: str = ""
def step(self, rng: random.Random):
if self.regime_left <= 0: # pick a new market phase: uptrend, downtrend or sideways
kind = rng.choices(["up", "down", "flat"], [0.38, 0.30, 0.32])[0]
strength = rng.uniform(0.25, 0.6) * self.vol
self.regime = {"up": strength, "down": -strength, "flat": 0.0}[kind]
self.regime_left = rng.randint(40, 140)
self.regime_left -= 1
ret = self.regime + rng.gauss(0, self.vol)
o = self.price
c = max(o * math.exp(ret), 0.01)
wick = abs(rng.gauss(0, self.vol * 0.6))
# Volume rises with the size of the move, plus random spikes.
v = rng.lognormvariate(0, 0.35) * (1 + 0.8 * abs(ret) / self.vol)
if rng.random() < 0.08:
v *= rng.uniform(1.5, 2.5)
self.append(max(o, c) * (1 + wick), min(o, c) * (1 - wick), c, v)
def append(self, high, low, close, volume, ts=None):
self.highs.append(high)
self.lows.append(low)
self.closes.append(close)
self.volumes.append(volume)
self.price = close
if ts is not None:
self.last_ts = ts
def fake_news(asset: str, rng: random.Random = random) -> Verdict:
kind = rng.choices(["bullish", "neutral", "bearish"], [0.40, 0.35, 0.25])[0]
conf = round(rng.uniform(0.35, 0.9), 2)
text = {"bullish": "Simulated positive headlines.", "neutral": "Simulated mixed headlines.",
"bearish": "Simulated negative headlines."}[kind]
return Verdict(kind, conf, f"{text} (demo, not real news about {asset})", [])
class Simulator:
"""Runs the strategy on every instrument, on real (Yahoo) or simulated candles.
process(symbol, action, price_eur, reason) is the bot's normal decision path.
Prices passed to it are in euros, so the fake account is one clean euro balance:
US-dollar assets are converted with the live EUR/USD rate.
"""
def __init__(self, process, mark, mode: str = "real", interval: str = "1h",
poll_seconds: float = 300, tick_seconds: float = 5, seed: int | None = None,
fetch=None, fx=None, fallback_rates: dict | None = None,
on_price=None, on_equity=None, pause: float = 0.0):
from . import marketdata
self._md = marketdata
self.process, self.mark, self.mode, self.interval = process, mark, mode, interval
self.pause = pause # seconds between Yahoo requests
self.on_price, self.on_equity = on_price, on_equity # optional history recorders
self.wait = poll_seconds if mode == "real" else tick_seconds
self.fetch = fetch or (lambda sym: marketdata.fetch(sym, interval))
self.fx_fn = fx or marketdata.fx_rates
# Units of foreign currency per 1 euro. Live rates replace these when they load.
self.rates = dict(fallback_rates or {"USD": 1.10, "GBP": 0.87})
self.fx_checked = 0.0
self.rng = random.Random(seed)
self.series = {
sym: Series(sym, 0.012 if inst.asset_class == "crypto" else 0.006,
START.get(sym, 100.0) * self.rng.uniform(0.97, 1.03),
currency=inst.currency)
for sym, inst in INSTRUMENTS.items()
}
self.equity_history: deque = deque(maxlen=720)
self.candles = 0
self.last_poll = 0.0
self._stop = threading.Event()
if mode == "simulated":
for _ in range(SLOW + 40): # warm up indicators without trading
for s in self.series.values():
s.step(self.rng)
def to_eur(self, s: Series, price: float) -> float:
return self._md.to_eur(price, s.currency, self.rates)
def sync_positions(self, held: set[str]):
for s in self.series.values():
s.in_position = INSTRUMENTS[s.symbol].broker_symbol in held
if not s.in_position:
s.trail = None
def _record(self, s: Series, ts: float):
"""Remember the candle's price in euros, for the scorecard."""
if not self.on_price:
return
try:
self.on_price(s.symbol, ts, round(self.to_eur(s, s.price), 6))
except Exception:
pass
def _evaluate(self, s: Series, at: float | None = None):
sig = signal(s)
if not sig:
return
action, reason = sig
# Real data: judge "is the exchange open?" at the candle's own time, not now, because
# the last candle of a session is only confirmed after the exchange has closed.
# Simulated data runs around the clock, so the hours check is skipped there.
try:
price_eur = round(self.to_eur(s, s.price), 4)
except ValueError as e:
s.error = str(e)
return
r = self.process(s.symbol, action, price_eur, reason,
at=at, enforce_hours=self.mode == "real")
if r.get("decision") == "executed":
s.in_position = action == "buy"
if action == "sell":
s.trail = None
def _update_real(self, s: Series):
inst = INSTRUMENTS[s.symbol]
try:
candles, currency = self.fetch(inst.yahoo or s.symbol)
s.error = ""
except Exception as e:
s.error = f"price feed: {e}"[:160]
return
if currency:
s.currency = currency
new = [c for c in candles if c.ts > s.last_ts]
if not new:
return
first_load = s.last_ts == 0
for c in new:
s.append(c.high, c.low, c.close, c.volume, c.ts)
self._record(s, c.ts)
if not first_load: # history only warms up the indicators
self._evaluate(s, at=c.ts)
try:
self.mark(inst.broker_symbol, self.to_eur(s, s.price))
except ValueError as e:
s.error = str(e)
def run_once(self, equity_fn=None, held_fn=None):
if held_fn:
self.sync_positions(held_fn())
if self.mode == "real" and time.time() - self.fx_checked > 1800:
self.rates.update(self.fx_fn() or {}) # keep the old rate for any that fail
self.fx_checked = time.time()
for s in self.series.values():
if self.mode == "real":
self._update_real(s)
if self.pause:
time.sleep(self.pause)
else:
s.step(self.rng)
self.mark(INSTRUMENTS[s.symbol].broker_symbol, self.to_eur(s, s.price))
self._record(s, time.time())
self._evaluate(s)
self.candles += 1
self.last_poll = time.time()
if equity_fn:
eq = round(equity_fn(), 2)
self.equity_history.append((time.time(), eq))
if self.on_equity:
self.on_equity(time.time(), eq)
def prices(self) -> list[dict]:
out = []
for s in self.series.values():
c = list(s.closes)
row = {"symbol": s.symbol, "exchange": INSTRUMENTS[s.symbol].exchange,
"currency": s.currency, "error": s.error,
"last_candle": s.last_ts or None}
if c:
chg = (c[-1] / c[-21] - 1) * 100 if len(c) > 21 else 0.0
f, sl = ema(c, FAST)[-1], ema(c, SLOW)[-1]
row.update(price=round(s.price, 4), change_20=round(chg, 2),
trend="up" if f > sl else "down", in_position=s.in_position,
spark=[round(x, 4) for x in c[-60:]])
out.append(row)
return out
INTERVAL_SECONDS = {"15m": 900, "1h": 3600, "1d": 86400}
def health(self) -> dict:
"""Per-market checks for the dashboard: is the feed alive, and why is there no buy yet."""
from . import exchanges
now = time.time()
lag = self.INTERVAL_SECONDS.get(self.interval, 3600) * 3
rows = []
for s in self.series.values():
inst = INSTRUMENTS[s.symbol]
d = diagnose(s)
feed, note = "ok", ""
if s.error:
feed, note = "error", s.error
elif not s.closes:
feed, note = "waiting", "no data yet"
elif (self.mode == "real" and self.interval != "1d" and s.last_ts
and now - s.last_ts > lag and exchanges.is_open(exchanges.EXCHANGES[inst.exchange])):
feed, note = "stale", "exchange is open but no new candle for a while"
if d.get("no_volume") and feed == "ok":
note = "no volume data from the price source: this market cannot trigger a buy"
if s.in_position:
status = "holding"
elif d["ready"]:
status = "buy_signal"
elif d.get("warming"):
status = "warming"
else:
status = "waiting"
rows.append({"symbol": s.symbol, "exchange": inst.exchange, "feed": feed, "note": note,
"last_candle": s.last_ts or None, "candles": len(s.closes),
"status": status, "trend_up": d.get("trend_up", False),
"checks": d["checks"], "missing": d["missing"],
"warming": d.get("warming", False), "have": d.get("have"), "need": d.get("need")})
live = [r for r in rows if not r["warming"] and r["feed"] != "error"]
closest = sorted((r for r in live if r["status"] == "waiting"), key=lambda r: len(r["missing"]))[:3]
return {
"markets": rows,
"feed_ok": sum(r["feed"] == "ok" for r in rows), "total": len(rows),
"uptrend": sum(r["trend_up"] for r in rows),
"last_check": self.last_poll or None,
"next_check": (self.last_poll + self.wait) if self.last_poll else None,
"every_seconds": self.wait, "updates": self.candles,
"closest": [{"symbol": r["symbol"], "missing": r["missing"]} for r in closest],
"buy_ready": [r["symbol"] for r in rows if r["status"] == "buy_signal"],
}
def loop(self, equity_fn, held_fn):
while not self._stop.is_set():
try:
self.run_once(equity_fn, held_fn)
except Exception as e: # keep the demo alive; order errors are logged by process()
print("demo update failed:", e)
self._stop.wait(self.wait)
def start(self, equity_fn, held_fn):
threading.Thread(target=self.loop, args=(equity_fn, held_fn), daemon=True).start()
def stop(self):
self._stop.set()