feat: GridBot als zweite Paper-Engine — No-Stop-XRP-Grid live

processGridCycle (Paritätstest gegen runGridBacktest), GridEngine mit
DB-Recovery (grid_state/grid_lots, bot_state id=2), bot-Spalte in
paper_trades/equity_snapshots, /api/grid, Dashboard-Panel.
Bewusster Paper-Probelauf trotz Gate-Fail (User-Entscheidung 2026-06-10).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-06-10 07:29:42 +00:00
parent f754b91acd
commit 021049b259
12 changed files with 1267 additions and 17 deletions

View File

@@ -1,12 +1,14 @@
import { and, desc, eq, gte } from 'drizzle-orm';
import { db } from '../db/client';
import { botState, candles, decisionLogs, equitySnapshots, paperTrades, positions } from '../db/schema';
import { botState, candles, decisionLogs, equitySnapshots, gridLots, gridState, paperTrades, positions } from '../db/schema';
import { aggregate4h } from '../market/aggregate';
import { computeMetrics, type EquityPoint } from '../backtest/metrics';
import type { ClosedTrade } from '../engine/portfolio';
import type { Pair } from '../types';
import { PAIRS } from '../types';
import type { LiveEngine } from '../live/engine';
import type { GridEngine } from '../live/grid-engine';
import { GRID_CYCLE_CONFIG } from '../live/grid-engine';
function json(data: unknown, status = 200): Response {
return new Response(JSON.stringify(data), { status, headers: { 'content-type': 'application/json' } });
@@ -66,7 +68,7 @@ async function getPortfolio(engine: LiveEngine) {
async function getStats() {
const [state] = await db.select().from(botState).where(eq(botState.id, 1));
const tradeRows = await db.select().from(paperTrades);
const tradeRows = await db.select().from(paperTrades).where(eq(paperTrades.bot, 'trend'));
const trades: ClosedTrade[] = tradeRows.map((t) => ({
pair: t.pair as Pair,
entryTs: t.entryTs.getTime(),
@@ -79,7 +81,11 @@ async function getStats() {
exitReason: t.exitReason as ClosedTrade['exitReason'],
side: t.side as 'long' | 'short',
}));
const curveRows = await db.select().from(equitySnapshots).orderBy(equitySnapshots.ts);
const curveRows = await db
.select()
.from(equitySnapshots)
.where(eq(equitySnapshots.bot, 'trend'))
.orderBy(equitySnapshots.ts);
const curve: EquityPoint[] = curveRows.map((r) => ({ ts: r.ts.getTime(), equity: r.equity }));
const start = state?.startCapital ?? 1000;
const metrics = computeMetrics(trades, curve, start);
@@ -104,7 +110,78 @@ async function getStats() {
return { ...metrics, startCapital: start, equityCurve: curve, btcBuyHoldPct };
}
export function createServer(engine: LiveEngine, port: number) {
async function getGrid(gridEngine: GridEngine) {
const [state] = await db.select().from(botState).where(eq(botState.id, 2));
const stateRows = await db.select().from(gridState);
const lotRows = await db.select().from(gridLots);
const tradeRows = await db
.select()
.from(paperTrades)
.where(eq(paperTrades.bot, 'grid'))
.orderBy(desc(paperTrades.exitTs))
.limit(30);
const closes = await latestCloses();
let equity = state?.cash ?? 0;
const lots = lotRows.map((l) => {
const last = closes.get(l.pair as Pair) ?? l.entryPrice;
const value = l.qty * last;
equity += value;
return {
pair: l.pair,
levelIdx: l.levelIdx,
qty: l.qty,
entryTs: l.entryTs.getTime(),
entryPrice: l.entryPrice,
lastPrice: last,
value,
unrealizedPnl: value - l.entryCost,
};
});
const wins = tradeRows.filter((t) => t.pnl > 0);
const grossWin = wins.reduce((s, t) => s + t.pnl, 0);
const grossLoss = tradeRows.filter((t) => t.pnl < 0).reduce((s, t) => s - t.pnl, 0);
const curveRows = await db
.select()
.from(equitySnapshots)
.where(eq(equitySnapshots.bot, 'grid'))
.orderBy(equitySnapshots.ts);
return {
equity,
cash: state?.cash ?? 0,
startCapital: state?.startCapital ?? 0,
cursorTs: state?.cursorTs.getTime() ?? null,
config: {
pairs: GRID_CYCLE_CONFIG.pairs,
spacingAtrMult: GRID_CYCLE_CONFIG.params.spacingAtrMult,
gridLevels: GRID_CYCLE_CONFIG.params.gridLevels,
noStop: !GRID_CYCLE_CONFIG.params.hardStop,
},
grids: stateRows.map((s) => ({
pair: s.pair,
center: s.center,
spacing: s.spacing,
lowerBound: s.lowerBound,
upperBound: s.upperBound,
budgetPerLevel: s.budgetPerLevel,
activatedTs: s.activatedTs.getTime(),
lastPrice: closes.get(s.pair as Pair) ?? null,
})),
lots,
trades: tradeRows,
stats: {
trades: tradeRows.length,
winRate: tradeRows.length ? wins.length / tradeRows.length : 0,
profitFactor: grossLoss > 0 ? grossWin / grossLoss : tradeRows.length ? Infinity : 0,
totalPnl: tradeRows.reduce((s, t) => s + t.pnl, 0),
},
equityCurve: curveRows.map((r) => ({ ts: r.ts.getTime(), equity: r.equity })),
engine: gridEngine.status,
};
}
export function createServer(engine: LiveEngine, gridEngine: GridEngine, port: number) {
const indexHtml = Bun.file(new URL('../../../public/index.html', import.meta.url));
return Bun.serve({
@@ -124,7 +201,13 @@ export function createServer(engine: LiveEngine, port: number) {
return json(await getPortfolio(engine));
case '/api/trades': {
const limit = clampLimit(url, 100, 500);
const rows = await db.select().from(paperTrades).orderBy(desc(paperTrades.exitTs)).limit(limit);
const bot = url.searchParams.get('bot') ?? 'trend';
const rows = await db
.select()
.from(paperTrades)
.where(eq(paperTrades.bot, bot))
.orderBy(desc(paperTrades.exitTs))
.limit(limit);
return json(rows);
}
case '/api/decisions': {
@@ -136,6 +219,8 @@ export function createServer(engine: LiveEngine, port: number) {
}
case '/api/stats':
return json(await getStats());
case '/api/grid':
return json(await getGrid(gridEngine));
case '/api/candles': {
const pair = url.searchParams.get('pair') ?? 'BTC_USDT';
if (!(PAIRS as readonly string[]).includes(pair)) return json({ error: 'unbekanntes Pair' }, 400);

View File

@@ -29,6 +29,7 @@ export const positions = pgTable('positions', {
export const paperTrades = pgTable('paper_trades', {
id: serial('id').primaryKey(),
bot: text('bot').notNull().default('trend'), // 'trend' | 'grid'
pair: varchar('pair', { length: 16 }).notNull(),
side: text('side').notNull(),
entryTs: timestamp('entry_ts', { withTimezone: true }).notNull(),
@@ -69,10 +70,38 @@ export const botState = pgTable('bot_state', {
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
});
export const equitySnapshots = pgTable('equity_snapshots', {
ts: timestamp('ts', { withTimezone: true }).primaryKey(), // 4h-Bucket
equity: doublePrecision('equity').notNull(),
cash: doublePrecision('cash').notNull(),
export const equitySnapshots = pgTable(
'equity_snapshots',
{
bot: text('bot').notNull().default('trend'),
ts: timestamp('ts', { withTimezone: true }).notNull(), // 4h-Bucket
equity: doublePrecision('equity').notNull(),
cash: doublePrecision('cash').notNull(),
},
(t) => [primaryKey({ columns: [t.bot, t.ts] })],
);
/** Aktives Grid je Pair (No-Stop-GridBot). */
export const gridState = pgTable('grid_state', {
pair: varchar('pair', { length: 16 }).primaryKey(),
center: doublePrecision('center').notNull(),
spacing: doublePrecision('spacing').notNull(),
lowerBound: doublePrecision('lower_bound').notNull(),
upperBound: doublePrecision('upper_bound').notNull(),
budgetPerLevel: doublePrecision('budget_per_level').notNull(),
activatedTs: timestamp('activated_ts', { withTimezone: true }).notNull(),
});
/** Offene Grid-Lots (Inventar). */
export const gridLots = pgTable('grid_lots', {
id: serial('id').primaryKey(),
pair: varchar('pair', { length: 16 }).notNull(),
levelIdx: integer('level_idx').notNull(),
qty: doublePrecision('qty').notNull(),
entryTs: timestamp('entry_ts', { withTimezone: true }).notNull(),
entryPrice: doublePrecision('entry_price').notNull(),
entryCost: doublePrecision('entry_cost').notNull(),
riskAmount: doublePrecision('risk_amount').notNull(),
});
export const backtestRuns = pgTable('backtest_runs', {

View File

@@ -1,13 +1,21 @@
import { env } from './config';
import { LiveEngine } from './live/engine';
import { GridEngine } from './live/grid-engine';
import { createServer } from './api/server';
const CYCLE_MS = 5 * 60 * 1000;
const engine = new LiveEngine();
const gridEngine = new GridEngine();
await engine.init();
createServer(engine, env.PORT);
console.log(`trade-kuns Live-Paper-Engine läuft auf :${env.PORT}`);
await gridEngine.init();
createServer(engine, gridEngine, env.PORT);
console.log(`trade-kuns Live-Paper-Engines (trend + grid) laufen auf :${env.PORT}`);
void engine.runCycle();
setInterval(() => void engine.runCycle(), CYCLE_MS);
// Grid läuft nach der Trend-Engine — deren Gap-Fetch füllt die Candle-DB für beide.
const cycle = async () => {
await engine.runCycle();
await gridEngine.runCycle();
};
void cycle();
setInterval(() => void cycle(), CYCLE_MS);

View File

@@ -186,8 +186,11 @@ export class LiveEngine {
.onConflictDoNothing();
}
for (const s of result.equitySnapshots) {
const row = { ts: new Date(s.ts), equity: s.equity, cash: s.cash };
await tx.insert(equitySnapshots).values(row).onConflictDoUpdate({ target: equitySnapshots.ts, set: row });
const row = { bot: 'trend', ts: new Date(s.ts), equity: s.equity, cash: s.cash };
await tx
.insert(equitySnapshots)
.values(row)
.onConflictDoUpdate({ target: [equitySnapshots.bot, equitySnapshots.ts], set: row });
}
await tx
.update(botState)

View File

@@ -0,0 +1,85 @@
import { describe, expect, test } from 'bun:test';
import type { Candle, Pair } from '../types';
import { DEFAULT_EXEC } from '../engine/portfolio';
import { runGridBacktest, DEFAULT_GRID_PARAMS } from '../backtest/grid';
import { processGridCycle, type GridCycleConfig, type GridLiveState } from './grid-cycle';
const M15 = 15 * 60 * 1000;
const PAIR: Pair = 'XRP_USDT';
const T0 = Date.UTC(2025, 0, 1);
// Live-Spec: No-Stop, ADX-Filter aus, 3×ATR, 8 Levels
const PARAMS = { ...DEFAULT_GRID_PARAMS, spacingAtrMult: 3, gridLevels: 8, adxMax: 100, hardStop: false };
const CFG: GridCycleConfig = { exec: DEFAULT_EXEC, params: PARAMS, minNotionalUsdt: 10, pairs: [PAIR] };
/** Volatile Serie: Trend + zwei Crash-Erholungs-Zyklen — füllt Levels und löst TPs aus. */
function synthetic(): Candle[] {
const out: Candle[] = [];
let price = 1.0;
for (let k = 0; k < 16 * 400; k++) {
const phase = Math.floor(k / 16);
let drift = 0.0002;
if ((phase > 120 && phase < 150) || (phase > 260 && phase < 290)) drift = -0.004; // Crashes
if ((phase >= 150 && phase < 220) || (phase >= 290 && phase < 360)) drift = 0.0025; // Erholungen
const open = price;
price = Math.max(0.1, price + drift + 0.01 * Math.sin(k / 5));
out.push({
ts: T0 + k * M15, open, high: Math.max(open, price) + 0.005,
low: Math.min(open, price) - 0.005, close: price, volume: 1,
});
}
return out;
}
function freshState(c15: Candle[]): GridLiveState {
return { cash: 1000, grids: new Map(), cursorTs: c15[0].ts - 1 };
}
describe('processGridCycle', () => {
test('Parität mit runGridBacktest (identische grid_tp-Trades)', () => {
const c15 = synthetic();
const map = new Map([[PAIR, c15]]);
const live = processGridCycle(map, freshState(c15), CFG);
const bt = runGridBacktest(map, {
startCapital: 1000, exec: DEFAULT_EXEC, params: PARAMS, minNotionalUsdt: 10,
tradeFrom: 0, tradeTo: c15[c15.length - 1].ts + M15,
});
const btTps = bt.trades.filter((t) => t.exitReason === 'grid_tp');
expect(live.closedTrades.length).toBeGreaterThanOrEqual(3);
expect(live.closedTrades).toEqual(btTps);
});
test('Split-Äquivalenz: ein Lauf ≡ zwei Läufe mit Cut', () => {
const c15 = synthetic();
const map = new Map([[PAIR, c15]]);
const full = processGridCycle(map, freshState(c15), CFG);
const cut = c15[Math.floor(c15.length * 0.6)].ts;
const r1 = processGridCycle(new Map([[PAIR, c15.filter((c) => c.ts <= cut)]]), freshState(c15), CFG);
const r2 = processGridCycle(map, { cash: r1.cash, grids: r1.grids, cursorTs: r1.cursorTs }, CFG);
expect(r2.cursorTs).toBe(full.cursorTs);
expect(r2.cash).toBeCloseTo(full.cash, 8);
expect([...r1.closedTrades, ...r2.closedTrades]).toEqual(full.closedTrades);
expect(JSON.stringify([...r2.grids.entries()])).toBe(JSON.stringify([...full.grids.entries()]));
});
test('Idempotenz: zweiter Lauf ohne neue Candles ist No-op', () => {
const c15 = synthetic();
const map = new Map([[PAIR, c15]]);
const r1 = processGridCycle(map, freshState(c15), CFG);
const r2 = processGridCycle(map, { cash: r1.cash, grids: r1.grids, cursorTs: r1.cursorTs }, CFG);
expect(r2.closedTrades).toEqual([]);
expect(r2.cash).toBe(r1.cash);
expect(r2.cursorTs).toBe(r1.cursorTs);
});
test('No-Stop-Invariante: kein Trade mit Verlust außer end_of_data', () => {
const c15 = synthetic();
const res = processGridCycle(new Map([[PAIR, c15]]), freshState(c15), CFG);
for (const t of res.closedTrades) {
expect(t.exitReason).toBe('grid_tp');
expect(t.pnl).toBeGreaterThan(0);
}
});
});

View File

@@ -0,0 +1,189 @@
import type { Candle, Pair } from '../types';
import { PAIRS } from '../types';
import { aggregateTf } from '../market/aggregate';
import { atr } from '../indicators/atr';
import { adx } from '../indicators/adx';
import type { ClosedTrade, ExecConfig } from '../engine/portfolio';
import type { GridParams } from '../backtest/grid';
import type { EquitySnapshot } from './process-cycle';
export interface GridLot {
levelIdx: number;
qty: number;
entryTs: number;
entryPrice: number;
entryCost: number;
riskAmount: number;
}
export interface GridStateForPair {
center: number;
spacing: number;
lowerBound: number; // center (N+1)·spacing
upperBound: number; // center + (N+1)·spacing
budgetPerLevel: number;
activatedTs: number;
lots: (GridLot | null)[]; // Index = Level
}
export interface GridLiveState {
cash: number;
grids: Map<Pair, GridStateForPair>;
cursorTs: number;
}
export interface GridCycleConfig {
exec: ExecConfig;
params: GridParams; // hardStop muss false sein (No-Stop-Design)
minNotionalUsdt: number;
pairs: Pair[];
}
export interface GridCycleResult {
cash: number;
grids: Map<Pair, GridStateForPair>;
cursorTs: number;
closedTrades: ClosedTrade[];
equitySnapshots: EquitySnapshot[];
equity: number;
}
/**
* Cursor-inkrementelle Variante von runGridBacktest (No-Stop-Semantik):
* Tf-Close: Aktivierung / verlustfreies Re-Center bei leerem Grid außerhalb der Range;
* 15m: Sells (nur Lots von vor diesem Bar) vor Buys. Lots werden nie mit Verlust
* verkauft. Pure Funktion — Paritätstest gegen runGridBacktest erzwingt Gleichheit.
*/
export function processGridCycle(
candles15ByPair: Map<Pair, Candle[]>,
state: GridLiveState,
cfg: GridCycleConfig,
): GridCycleResult {
const { exec, params: p } = cfg;
let cash = state.cash;
const grids = new Map<Pair, GridStateForPair>();
for (const [pair, g] of state.grids) grids.set(pair, { ...g, lots: g.lots.map((l) => (l ? { ...l } : null)) });
const trades: ClosedTrade[] = [];
const equitySnapshots: EquitySnapshot[] = [];
const lastClose = new Map<Pair, number>();
const cursorBucket = Math.floor(state.cursorTs / p.tfMs) * p.tfMs;
const pairs = cfg.pairs.filter((pr) => candles15ByPair.has(pr));
const equity = (): number => {
let eq = cash;
for (const [pair, g] of grids) {
const last = lastClose.get(pair) ?? 0;
for (const lot of g.lots) if (lot) eq += lot.qty * last;
}
return eq;
};
const contexts = pairs.map((pair) => {
const c15 = candles15ByPair.get(pair)!;
const c4h = aggregateTf(c15, p.tfMs);
let next4h = 0;
while (next4h < c4h.length && c4h[next4h].ts < cursorBucket) next4h++;
for (const c of c15) {
if (c.ts > state.cursorTs) break;
lastClose.set(pair, c.close);
}
return { pair, c4h, atr: atr(c4h, p.atrPeriod), adx: adx(c4h, p.atrPeriod), next4h };
});
const byPair = new Map(contexts.map((c) => [c.pair, c]));
const timeline: { ts: number; pair: Pair; candle: Candle }[] = [];
for (const ctx of contexts) {
for (const candle of candles15ByPair.get(ctx.pair)!) {
if (candle.ts > state.cursorTs) timeline.push({ ts: candle.ts, pair: ctx.pair, candle });
}
}
timeline.sort((a, b) => a.ts - b.ts || PAIRS.indexOf(a.pair) - PAIRS.indexOf(b.pair));
const sell = (pair: Pair, ts: number, price: number, lot: GridLot, reason: ClosedTrade['exitReason']): void => {
const fill = price * (1 - exec.slippage);
const proceeds = lot.qty * fill;
const fee = proceeds * exec.feeRate;
cash += proceeds - fee;
const pnl = proceeds - fee - lot.entryCost;
trades.push({
pair, entryTs: lot.entryTs, entryPrice: lot.entryPrice, exitTs: ts, exitPrice: fill,
qty: lot.qty, pnl, r: pnl / lot.riskAmount, exitReason: reason, side: 'long',
});
};
let cursorTs = state.cursorTs;
let lastEquityBucket = -1;
for (const { ts, pair, candle } of timeline) {
const ctx = byPair.get(pair)!;
const bucket = Math.floor(ts / p.tfMs) * p.tfMs;
// 1) Neu abgeschlossene Tf-Bars: Aktivierung / verlustfreies Re-Center
while (ctx.next4h < ctx.c4h.length && ctx.c4h[ctx.next4h].ts < bucket) {
const i = ctx.next4h++;
const bar = ctx.c4h[i];
const g = grids.get(pair);
if (g) {
const outOfRange = bar.close < g.lowerBound || bar.close > g.upperBound;
if (outOfRange && g.lots.every((l) => !l)) grids.delete(pair);
} else if (!Number.isNaN(ctx.atr[i]) && !Number.isNaN(ctx.adx[i]) && ctx.adx[i] < p.adxMax) {
const spacing = p.spacingAtrMult * ctx.atr[i];
const budgetPerLevel = equity() / pairs.length / p.gridLevels;
if (spacing > 0 && budgetPerLevel >= cfg.minNotionalUsdt) {
grids.set(pair, {
center: bar.close,
spacing,
lowerBound: bar.close - (p.gridLevels + 1) * spacing,
upperBound: bar.close + (p.gridLevels + 1) * spacing,
budgetPerLevel,
activatedTs: bar.ts + p.tfMs,
lots: Array(p.gridLevels).fill(null),
});
}
}
}
// 2) 15m-Fills: Sells zuerst (nur Lots von vor diesem Bar), dann Buys
const g = grids.get(pair);
if (g) {
for (let k = 0; k < g.lots.length; k++) {
const lot = g.lots[k];
if (!lot || lot.entryTs >= ts) continue;
const tp = g.center - k * g.spacing;
if (candle.high >= tp) {
sell(pair, ts, tp, lot, 'grid_tp');
g.lots[k] = null;
}
}
for (let k = 0; k < g.lots.length; k++) {
const levelPrice = g.center - (k + 1) * g.spacing;
if (!g.lots[k] && candle.low <= levelPrice) {
const fill = levelPrice * (1 + exec.slippage);
const qty = g.budgetPerLevel / fill;
const cost = qty * fill;
const fee = cost * exec.feeRate;
if (cash >= cost + fee) {
cash -= cost + fee;
g.lots[k] = {
levelIdx: k, qty, entryTs: ts, entryPrice: fill, entryCost: cost + fee,
riskAmount: Math.max((levelPrice - g.lowerBound) * qty, 1e-9),
};
}
}
}
}
lastClose.set(pair, candle.close);
cursorTs = Math.max(cursorTs, ts);
// 3) Equity-Punkt einmal pro Tf-Bucket
if (bucket !== lastEquityBucket) {
lastEquityBucket = bucket;
equitySnapshots.push({ ts: bucket, equity: equity(), cash });
}
}
return { cash, grids, cursorTs, closedTrades: trades, equitySnapshots, equity: equity() };
}

View File

@@ -0,0 +1,181 @@
import { eq } from 'drizzle-orm';
import { db } from '../db/client';
import { botState, equitySnapshots, gridLots, gridState, paperTrades } from '../db/schema';
import { getCandles } from '../market/candle-store';
import { H4 } from '../market/aggregate';
import { DEFAULT_GRID_PARAMS } from '../backtest/grid';
import { DEFAULT_EXEC } from '../engine/portfolio';
import type { Candle, Pair } from '../types';
import {
processGridCycle,
type GridCycleConfig,
type GridCycleResult,
type GridLiveState,
type GridStateForPair,
} from './grid-cycle';
const M15 = 15 * 60 * 1000;
const BOT_STATE_ID = 2; // id=1 gehört der Trend-Engine
const START_CAPITAL = 1000;
/** ATR/ADX(14) brauchen nur ~30 Bars — 200 ist reichlich Warmup. */
const WARMUP_TF_BARS = 200;
/** Live-Spec aus dem Walk-Forward: No-Stop, XRP only, 3×ATR, 8 Levels, kein ADX-Filter. */
export const GRID_CYCLE_CONFIG: GridCycleConfig = {
exec: DEFAULT_EXEC,
params: { ...DEFAULT_GRID_PARAMS, spacingAtrMult: 3, gridLevels: 8, adxMax: 100, hardStop: false },
minNotionalUsdt: 10,
pairs: ['XRP_USDT'],
};
export interface GridEngineStatus {
lastCycleAt: number | null;
lastCycleOk: boolean;
lastError: string | null;
cursorTs: number | null;
}
/**
* Zweite Paper-Engine (No-Stop-Grid). Holt selbst keine Candles —
* läuft im Zyklus NACH der Trend-Engine, deren Gap-Fetch die DB füllt.
*/
export class GridEngine {
status: GridEngineStatus = { lastCycleAt: null, lastCycleOk: true, lastError: null, cursorTs: null };
private cycling = false;
async init(): Promise<void> {
const [row] = await db.select().from(botState).where(eq(botState.id, BOT_STATE_ID));
if (row) {
this.status.cursorTs = row.cursorTs.getTime();
return;
}
const cursor = Math.floor(Date.now() / M15) * M15 - M15;
await db.insert(botState).values({
id: BOT_STATE_ID,
cash: START_CAPITAL,
startCapital: START_CAPITAL,
cursorTs: new Date(cursor),
});
this.status.cursorTs = cursor;
}
async runCycle(): Promise<void> {
if (this.cycling) return;
this.cycling = true;
try {
const state = await this.loadState();
const tfMs = GRID_CYCLE_CONFIG.params.tfMs;
const from = Math.floor(state.cursorTs / tfMs) * tfMs - WARMUP_TF_BARS * tfMs;
const candles15 = new Map<Pair, Candle[]>();
for (const pair of GRID_CYCLE_CONFIG.pairs) {
candles15.set(pair, await getCandles(pair, from));
}
const result = processGridCycle(candles15, state, GRID_CYCLE_CONFIG);
await this.persist(state, result);
this.status.lastCycleAt = Date.now();
this.status.lastCycleOk = true;
this.status.lastError = null;
this.status.cursorTs = result.cursorTs;
} catch (err) {
this.status.lastCycleAt = Date.now();
this.status.lastCycleOk = false;
this.status.lastError = err instanceof Error ? err.message : String(err);
console.error('Grid-Zyklus fehlgeschlagen:', err);
} finally {
this.cycling = false;
}
}
private async loadState(): Promise<GridLiveState> {
const [row] = await db.select().from(botState).where(eq(botState.id, BOT_STATE_ID));
if (!row) throw new Error('bot_state (grid) fehlt — init() nicht gelaufen?');
const stateRows = await db.select().from(gridState);
const lotRows = await db.select().from(gridLots);
const grids = new Map<Pair, GridStateForPair>();
for (const s of stateRows) {
const levels = GRID_CYCLE_CONFIG.params.gridLevels;
const lots: GridStateForPair['lots'] = Array(levels).fill(null);
for (const l of lotRows) {
if (l.pair === s.pair && l.levelIdx < levels) {
lots[l.levelIdx] = {
levelIdx: l.levelIdx,
qty: l.qty,
entryTs: l.entryTs.getTime(),
entryPrice: l.entryPrice,
entryCost: l.entryCost,
riskAmount: l.riskAmount,
};
}
}
grids.set(s.pair as Pair, {
center: s.center,
spacing: s.spacing,
lowerBound: s.lowerBound,
upperBound: s.upperBound,
budgetPerLevel: s.budgetPerLevel,
activatedTs: s.activatedTs.getTime(),
lots,
});
}
return { cash: row.cash, grids, cursorTs: row.cursorTs.getTime() };
}
private async persist(prev: GridLiveState, result: GridCycleResult): Promise<void> {
await db.transaction(async (tx) => {
// Grid-State + Lots vollständig ersetzen (kleine Mengen: ≤1 Grid, ≤8 Lots)
await tx.delete(gridLots);
await tx.delete(gridState);
for (const [pair, g] of result.grids) {
await tx.insert(gridState).values({
pair,
center: g.center,
spacing: g.spacing,
lowerBound: g.lowerBound,
upperBound: g.upperBound,
budgetPerLevel: g.budgetPerLevel,
activatedTs: new Date(g.activatedTs),
});
for (const lot of g.lots) {
if (!lot) continue;
await tx.insert(gridLots).values({
pair,
levelIdx: lot.levelIdx,
qty: lot.qty,
entryTs: new Date(lot.entryTs),
entryPrice: lot.entryPrice,
entryCost: lot.entryCost,
riskAmount: lot.riskAmount,
});
}
}
if (result.closedTrades.length > 0) {
await tx.insert(paperTrades).values(
result.closedTrades.map((t) => ({
bot: 'grid',
pair: t.pair,
side: t.side,
entryTs: new Date(t.entryTs),
entryPrice: t.entryPrice,
exitTs: new Date(t.exitTs),
exitPrice: t.exitPrice,
qty: t.qty,
pnl: t.pnl,
r: t.r,
exitReason: t.exitReason,
})),
);
}
for (const s of result.equitySnapshots) {
const row = { bot: 'grid', ts: new Date(s.ts), equity: s.equity, cash: s.cash };
await tx
.insert(equitySnapshots)
.values(row)
.onConflictDoUpdate({ target: [equitySnapshots.bot, equitySnapshots.ts], set: row });
}
await tx
.update(botState)
.set({ cash: result.cash, cursorTs: new Date(result.cursorTs), updatedAt: new Date() })
.where(eq(botState.id, BOT_STATE_ID));
});
}
}