Add configurable tick interval (time between trades)
- Seed tick_interval_seconds setting (default 60s) - Expose tick_interval_seconds via GET/PATCH /api/settings - startScheduler reads the setting at start time; enforces 5s minimum - getSchedulerStatus returns intervalSeconds for the UI - Main page: "Every [__] s" input in idle bar — saves on blur, persists across restarts; running state displays "every Ns" next to symbol/action Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
39d7c5761c
commit
245a15666e
@@ -1,7 +1,7 @@
|
|||||||
import { NextRequest, NextResponse } from 'next/server';
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
import { getSetting, setSetting } from '@/lib/db';
|
import { getSetting, setSetting } from '@/lib/db';
|
||||||
|
|
||||||
const VALID_KEYS = ['max_concurrent_accounts'] as const;
|
const VALID_KEYS = ['max_concurrent_accounts', 'tick_interval_seconds'] as const;
|
||||||
type SettingKey = typeof VALID_KEYS[number];
|
type SettingKey = typeof VALID_KEYS[number];
|
||||||
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
|
|||||||
+33
-2
@@ -256,6 +256,7 @@ interface SchedulerStatus {
|
|||||||
action: 'Buy' | 'Sell' | 'Random';
|
action: 'Buy' | 'Sell' | 'Random';
|
||||||
symbol: string;
|
symbol: string;
|
||||||
lastRun: string | null;
|
lastRun: string | null;
|
||||||
|
intervalSeconds: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Home() {
|
export default function Home() {
|
||||||
@@ -270,10 +271,11 @@ export default function Home() {
|
|||||||
const [sortDir, setSortDir] = useState<SortDir>('asc');
|
const [sortDir, setSortDir] = useState<SortDir>('asc');
|
||||||
|
|
||||||
// Trade controls
|
// Trade controls
|
||||||
const [scheduler, setScheduler] = useState<SchedulerStatus>({ running: false, action: 'Buy', symbol: 'NQ', lastRun: null });
|
const [scheduler, setScheduler] = useState<SchedulerStatus>({ running: false, action: 'Buy', symbol: 'NQ', lastRun: null, intervalSeconds: 60 });
|
||||||
const [enabledSymbols, setEnabledSymbols] = useState<string[]>([]);
|
const [enabledSymbols, setEnabledSymbols] = useState<string[]>([]);
|
||||||
const [tradeSymbol, setTradeSymbol] = useState('');
|
const [tradeSymbol, setTradeSymbol] = useState('');
|
||||||
const [tradeAction, setTradeAction] = useState<'Buy' | 'Sell' | 'Random'>('Buy');
|
const [tradeAction, setTradeAction] = useState<'Buy' | 'Sell' | 'Random'>('Buy');
|
||||||
|
const [tickInterval, setTickInterval] = useState('60');
|
||||||
const [tradeLoading, setTradeLoading] = useState(false);
|
const [tradeLoading, setTradeLoading] = useState(false);
|
||||||
|
|
||||||
const handleSort = (col: SortKey) => {
|
const handleSort = (col: SortKey) => {
|
||||||
@@ -314,6 +316,13 @@ export default function Home() {
|
|||||||
})
|
})
|
||||||
.catch(() => {});
|
.catch(() => {});
|
||||||
|
|
||||||
|
fetch('/api/settings')
|
||||||
|
.then((r) => r.json())
|
||||||
|
.then((s: { tick_interval_seconds?: string | null }) => {
|
||||||
|
if (s.tick_interval_seconds != null) setTickInterval(s.tick_interval_seconds);
|
||||||
|
})
|
||||||
|
.catch(() => {});
|
||||||
|
|
||||||
fetchConfig();
|
fetchConfig();
|
||||||
fetchScheduler();
|
fetchScheduler();
|
||||||
|
|
||||||
@@ -456,9 +465,10 @@ export default function Home() {
|
|||||||
<span className="text-sm font-semibold text-green-700">
|
<span className="text-sm font-semibold text-green-700">
|
||||||
{scheduler.symbol} · {scheduler.action}
|
{scheduler.symbol} · {scheduler.action}
|
||||||
</span>
|
</span>
|
||||||
|
<span className="text-xs text-slate-400">every {scheduler.intervalSeconds}s</span>
|
||||||
{scheduler.lastRun && (
|
{scheduler.lastRun && (
|
||||||
<span className="text-xs text-slate-400">
|
<span className="text-xs text-slate-400">
|
||||||
last run {new Date(scheduler.lastRun).toLocaleTimeString()}
|
· last run {new Date(scheduler.lastRun).toLocaleTimeString()}
|
||||||
</span>
|
</span>
|
||||||
)}
|
)}
|
||||||
<button
|
<button
|
||||||
@@ -501,6 +511,27 @@ export default function Home() {
|
|||||||
Random
|
Random
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
<div className="flex items-center gap-1.5 ml-1">
|
||||||
|
<label className="text-xs text-slate-400 whitespace-nowrap">Every</label>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
min={5}
|
||||||
|
max={3600}
|
||||||
|
value={tickInterval}
|
||||||
|
onChange={(e) => setTickInterval(e.target.value)}
|
||||||
|
onBlur={() => {
|
||||||
|
const secs = Math.max(5, parseInt(tickInterval, 10) || 60);
|
||||||
|
setTickInterval(String(secs));
|
||||||
|
fetch('/api/settings', {
|
||||||
|
method: 'PATCH',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ tick_interval_seconds: secs }),
|
||||||
|
}).catch(() => {});
|
||||||
|
}}
|
||||||
|
className="w-16 rounded-lg border border-slate-200 bg-slate-50 px-2 py-1.5 text-sm text-right font-mono text-slate-800 focus:outline-none focus:ring-2 focus:ring-blue-500"
|
||||||
|
/>
|
||||||
|
<span className="text-xs text-slate-400">s</span>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<button
|
<button
|
||||||
onClick={handleStart}
|
onClick={handleStart}
|
||||||
|
|||||||
+4
-2
@@ -336,8 +336,9 @@ export function startScheduler(action: 'Buy' | 'Sell' | 'Random', symbol: string
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
state.intervalId = setInterval(tick, 60_000);
|
const intervalSecs = Math.max(5, parseInt(getSetting('tick_interval_seconds') ?? '60', 10));
|
||||||
console.log(`[scheduler] started — ${action} ${symbol} every 60s`);
|
state.intervalId = setInterval(tick, intervalSecs * 1_000);
|
||||||
|
console.log(`[scheduler] started — ${action} ${symbol} every ${intervalSecs}s`);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function stopScheduler() {
|
export function stopScheduler() {
|
||||||
@@ -357,5 +358,6 @@ export function getSchedulerStatus() {
|
|||||||
action: state.action,
|
action: state.action,
|
||||||
symbol: state.symbol,
|
symbol: state.symbol,
|
||||||
lastRun: state.lastRun,
|
lastRun: state.lastRun,
|
||||||
|
intervalSeconds: parseInt(getSetting('tick_interval_seconds') ?? '60', 10),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -161,6 +161,7 @@ db.exec(`
|
|||||||
// Seed defaults if missing
|
// Seed defaults if missing
|
||||||
const seedSetting = db.prepare(`INSERT OR IGNORE INTO settings (key, value) VALUES (?, ?)`);
|
const seedSetting = db.prepare(`INSERT OR IGNORE INTO settings (key, value) VALUES (?, ?)`);
|
||||||
seedSetting.run('max_concurrent_accounts', '5');
|
seedSetting.run('max_concurrent_accounts', '5');
|
||||||
|
seedSetting.run('tick_interval_seconds', '60');
|
||||||
|
|
||||||
export function getSetting(key: string): string | null {
|
export function getSetting(key: string): string | null {
|
||||||
const row = db.prepare('SELECT value FROM settings WHERE key = ?').get(key) as { value: string } | undefined;
|
const row = db.prepare('SELECT value FROM settings WHERE key = ?').get(key) as { value: string } | undefined;
|
||||||
|
|||||||
Reference in New Issue
Block a user