Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
657c473e82 | ||
|
|
50b22df948 |
No files matched your search
+26
-59
@@ -9,7 +9,6 @@ import MultiStrategyPanel from './components/MultiStrategyPanel.vue'
|
|||||||
import OrdersTable from './components/OrdersTable.vue'
|
import OrdersTable from './components/OrdersTable.vue'
|
||||||
import PositionsTable from './components/PositionsTable.vue'
|
import PositionsTable from './components/PositionsTable.vue'
|
||||||
import StatusCards from './components/StatusCards.vue'
|
import StatusCards from './components/StatusCards.vue'
|
||||||
import StrategyPanel from './components/StrategyPanel.vue'
|
|
||||||
import SweepPanel from './components/SweepPanel.vue'
|
import SweepPanel from './components/SweepPanel.vue'
|
||||||
|
|
||||||
const status = ref({
|
const status = ref({
|
||||||
@@ -35,6 +34,27 @@ const strategiesLoading = ref(true)
|
|||||||
const presets = ref([])
|
const presets = ref([])
|
||||||
const multiStatus = ref({ running: false, strategies: [], total_pnl: 0, equity: 0 })
|
const multiStatus = ref({ running: false, strategies: [], total_pnl: 0, equity: 0 })
|
||||||
const liveCheck = ref({ ok: true, mode: 'paper', blockers: [] })
|
const liveCheck = ref({ ok: true, mode: 'paper', blockers: [] })
|
||||||
|
|
||||||
|
const runningLabel = computed(() => {
|
||||||
|
const list = multiStatus.value.strategies || []
|
||||||
|
if (!list.length) return 'Idle'
|
||||||
|
if (list.length === 1) {
|
||||||
|
return `Running: ${list[0].strategy_name || list[0].strategy_key}`
|
||||||
|
}
|
||||||
|
return `Running: ${list.length} strategies`
|
||||||
|
})
|
||||||
|
|
||||||
|
const scopeLabel = computed(() => {
|
||||||
|
const list = multiStatus.value.strategies || []
|
||||||
|
if (!list.length) {
|
||||||
|
return status.value.symbol ? `${status.value.symbol} ${status.value.timeframe}` : ''
|
||||||
|
}
|
||||||
|
const first = list[0]
|
||||||
|
const sameScope = list.every(
|
||||||
|
(s) => s.symbol === first.symbol && s.timeframe === first.timeframe,
|
||||||
|
)
|
||||||
|
return sameScope ? `${first.symbol} ${first.timeframe}` : `${list.length} pairs`
|
||||||
|
})
|
||||||
const positions = ref([])
|
const positions = ref([])
|
||||||
const orders = ref([])
|
const orders = ref([])
|
||||||
const events = ref([])
|
const events = ref([])
|
||||||
@@ -98,25 +118,6 @@ async function handleDeletePreset(id) {
|
|||||||
await loadPresets()
|
await loadPresets()
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleStart(payload) {
|
|
||||||
if (liveCheck.value.mode === 'live' && !liveCheck.value.ok) {
|
|
||||||
status.value = {
|
|
||||||
...status.value,
|
|
||||||
error: 'Live trading blocked: ' + liveCheck.value.blockers.join('; '),
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
busy.value = true
|
|
||||||
try {
|
|
||||||
status.value = await api.startBot(payload)
|
|
||||||
await refreshAll()
|
|
||||||
} catch (error) {
|
|
||||||
status.value = { ...status.value, error: error.response?.data?.detail || error.message }
|
|
||||||
} finally {
|
|
||||||
busy.value = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function refreshMulti() {
|
async function refreshMulti() {
|
||||||
multiStatus.value = await api.getMultiStatus()
|
multiStatus.value = await api.getMultiStatus()
|
||||||
}
|
}
|
||||||
@@ -168,29 +169,6 @@ async function handleKillMany() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleStop() {
|
|
||||||
busy.value = true
|
|
||||||
try {
|
|
||||||
status.value = await api.stopBot()
|
|
||||||
await refreshMulti()
|
|
||||||
await refreshAll()
|
|
||||||
} finally {
|
|
||||||
busy.value = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function handleKill() {
|
|
||||||
busy.value = true
|
|
||||||
try {
|
|
||||||
status.value = await api.killBot('manual kill switch')
|
|
||||||
await refreshAll()
|
|
||||||
} catch (error) {
|
|
||||||
status.value = { ...status.value, error: error.response?.data?.detail || error.message }
|
|
||||||
} finally {
|
|
||||||
busy.value = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function handleClose(id) {
|
async function handleClose(id) {
|
||||||
try {
|
try {
|
||||||
status.value = await api.closePosition(id)
|
status.value = await api.closePosition(id)
|
||||||
@@ -314,9 +292,8 @@ onUnmounted(() => {
|
|||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="text-xs text-slate-400">
|
<div class="text-xs text-slate-400">
|
||||||
<span v-if="status.strategy_name">Running: {{ status.strategy_name }}</span>
|
<span>{{ runningLabel }}</span>
|
||||||
<span v-else>Idle</span>
|
<span v-if="scopeLabel"> · {{ scopeLabel }}</span>
|
||||||
<span v-if="status.symbol"> · {{ status.symbol }} {{ status.timeframe }}</span>
|
|
||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
@@ -344,27 +321,17 @@ onUnmounted(() => {
|
|||||||
|
|
||||||
<StatusCards :status="status" />
|
<StatusCards :status="status" />
|
||||||
|
|
||||||
<StrategyPanel
|
|
||||||
:strategies="strategies"
|
|
||||||
:status="status"
|
|
||||||
:busy="busy"
|
|
||||||
:presets="presets"
|
|
||||||
:loading="strategiesLoading"
|
|
||||||
@start="handleStart"
|
|
||||||
@stop="handleStop"
|
|
||||||
@kill="handleKill"
|
|
||||||
@save-preset="handleSavePreset"
|
|
||||||
@delete-preset="handleDeletePreset"
|
|
||||||
/>
|
|
||||||
|
|
||||||
<MultiStrategyPanel
|
<MultiStrategyPanel
|
||||||
:strategies="strategies"
|
:strategies="strategies"
|
||||||
:multi-status="multiStatus"
|
:multi-status="multiStatus"
|
||||||
|
:presets="presets"
|
||||||
:busy="busy"
|
:busy="busy"
|
||||||
:loading="strategiesLoading"
|
:loading="strategiesLoading"
|
||||||
@start-many="handleStartMany"
|
@start-many="handleStartMany"
|
||||||
@stop="handleStopMany"
|
@stop="handleStopMany"
|
||||||
@kill="handleKillMany"
|
@kill="handleKillMany"
|
||||||
|
@save-preset="handleSavePreset"
|
||||||
|
@delete-preset="handleDeletePreset"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<BacktestPanel :strategies="strategies" :status="status" :loading="strategiesLoading" />
|
<BacktestPanel :strategies="strategies" :status="status" :loading="strategiesLoading" />
|
||||||
|
|||||||
@@ -7,10 +7,11 @@ import StrategyRowEditor from './StrategyRowEditor.vue'
|
|||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
strategies: { type: Array, required: true },
|
strategies: { type: Array, required: true },
|
||||||
multiStatus: { type: Object, required: true },
|
multiStatus: { type: Object, required: true },
|
||||||
|
presets: { type: Array, default: () => [] },
|
||||||
busy: { type: Boolean, default: false },
|
busy: { type: Boolean, default: false },
|
||||||
loading: { type: Boolean, default: false },
|
loading: { type: Boolean, default: false },
|
||||||
})
|
})
|
||||||
const emit = defineEmits(['start-many', 'stop', 'kill'])
|
const emit = defineEmits(['start-many', 'stop', 'kill', 'save-preset', 'delete-preset'])
|
||||||
|
|
||||||
const startingBalance = ref(10000)
|
const startingBalance = ref(10000)
|
||||||
// Each row: { strategy_key, symbol, timeframe, weight, params }
|
// Each row: { strategy_key, symbol, timeframe, weight, params }
|
||||||
@@ -64,6 +65,32 @@ function removeRow(index) {
|
|||||||
rows.value.splice(index, 1)
|
rows.value.splice(index, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load a saved preset as an additional row.
|
||||||
|
function loadPreset(event) {
|
||||||
|
const preset = props.presets.find((p) => p.id === Number(event.target.value))
|
||||||
|
event.target.value = ''
|
||||||
|
if (!preset) return
|
||||||
|
rows.value.push({
|
||||||
|
strategy_key: preset.strategy_key,
|
||||||
|
symbol: preset.symbol,
|
||||||
|
timeframe: preset.timeframe,
|
||||||
|
weight: 1,
|
||||||
|
params: { ...preset.params },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveRow(row) {
|
||||||
|
const name = window.prompt('Preset name', row.strategy_key)
|
||||||
|
if (name === null) return
|
||||||
|
emit('save-preset', {
|
||||||
|
name: name || row.strategy_key,
|
||||||
|
strategy_key: row.strategy_key,
|
||||||
|
symbol: row.symbol,
|
||||||
|
timeframe: row.timeframe,
|
||||||
|
params: row.params,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
function start() {
|
function start() {
|
||||||
emit('start-many', {
|
emit('start-many', {
|
||||||
strategies: rows.value.map((r) => ({
|
strategies: rows.value.map((r) => ({
|
||||||
@@ -85,7 +112,7 @@ function confirmKill() {
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<CollapsiblePanel title="Strategies (multi)" storage-key="multi" :summary="summary">
|
<CollapsiblePanel title="Strategies" storage-key="strategies" :summary="summary">
|
||||||
<div class="space-y-3">
|
<div class="space-y-3">
|
||||||
<div class="flex flex-wrap items-end gap-3">
|
<div class="flex flex-wrap items-end gap-3">
|
||||||
<label class="space-y-1">
|
<label class="space-y-1">
|
||||||
@@ -102,6 +129,15 @@ function confirmKill() {
|
|||||||
<button class="btn-muted" :disabled="running || loading" @click="addRow">
|
<button class="btn-muted" :disabled="running || loading" @click="addRow">
|
||||||
Add strategy
|
Add strategy
|
||||||
</button>
|
</button>
|
||||||
|
<label v-if="presets.length" class="space-y-1">
|
||||||
|
<span class="label">Load preset</span>
|
||||||
|
<select class="input" :disabled="running || loading" @change="loadPreset">
|
||||||
|
<option value="">—</option>
|
||||||
|
<option v-for="p in presets" :key="p.id" :value="p.id">
|
||||||
|
{{ p.name || `${p.strategy_key} #${p.id}` }}
|
||||||
|
</option>
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
<button
|
<button
|
||||||
class="btn-primary"
|
class="btn-primary"
|
||||||
:disabled="busy || running || loading || !rows.length"
|
:disabled="busy || running || loading || !rows.length"
|
||||||
@@ -127,11 +163,10 @@ function confirmKill() {
|
|||||||
:key="i"
|
:key="i"
|
||||||
v-model="rows[i]"
|
v-model="rows[i]"
|
||||||
:strategies="strategies"
|
:strategies="strategies"
|
||||||
:default-symbol="multiStatus.symbol"
|
|
||||||
:default-timeframe="multiStatus.timeframe"
|
|
||||||
:disabled="running"
|
:disabled="running"
|
||||||
:allocation="allocations[i]"
|
:allocation="allocations[i]"
|
||||||
@remove="removeRow(i)"
|
@remove="removeRow(i)"
|
||||||
|
@save-preset="saveRow"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<p v-else class="text-sm text-slate-400">
|
<p v-else class="text-sm text-slate-400">
|
||||||
|
|||||||
@@ -1,58 +0,0 @@
|
|||||||
<script setup>
|
|
||||||
import { computed } from 'vue'
|
|
||||||
import CollapsiblePanel from './CollapsiblePanel.vue'
|
|
||||||
import StrategyForm from './StrategyForm.vue'
|
|
||||||
|
|
||||||
const props = defineProps({
|
|
||||||
strategies: { type: Array, required: true },
|
|
||||||
status: { type: Object, required: true },
|
|
||||||
busy: { type: Boolean, default: false },
|
|
||||||
presets: { type: Array, default: () => [] },
|
|
||||||
loading: { type: Boolean, default: false },
|
|
||||||
})
|
|
||||||
const emit = defineEmits(['start', 'stop', 'kill', 'save-preset', 'delete-preset'])
|
|
||||||
|
|
||||||
const summary = computed(() => {
|
|
||||||
const name = props.status.strategy_name || 'none'
|
|
||||||
return `${name} · ${props.status.running ? 'running' : 'stopped'}`
|
|
||||||
})
|
|
||||||
|
|
||||||
function confirmKill() {
|
|
||||||
if (window.confirm('Kill switch: flatten the position and halt trading?')) {
|
|
||||||
emit('kill')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
|
||||||
<CollapsiblePanel title="Strategy" storage-key="strategy" :summary="summary">
|
|
||||||
<div class="space-y-3">
|
|
||||||
<StrategyForm
|
|
||||||
:strategies="strategies"
|
|
||||||
:disabled="status.running"
|
|
||||||
:busy="busy"
|
|
||||||
submit-label="Start bot"
|
|
||||||
:symbol="status.symbol"
|
|
||||||
:timeframe="status.timeframe"
|
|
||||||
:presets="presets"
|
|
||||||
:loading="loading"
|
|
||||||
@submit="emit('start', $event)"
|
|
||||||
@save-preset="emit('save-preset', $event)"
|
|
||||||
@delete-preset="emit('delete-preset', $event)"
|
|
||||||
/>
|
|
||||||
|
|
||||||
<div class="flex flex-wrap gap-2">
|
|
||||||
<button class="btn-danger" :disabled="busy || !status.running" @click="emit('stop')">
|
|
||||||
Stop bot
|
|
||||||
</button>
|
|
||||||
<button
|
|
||||||
class="btn bg-rose-700 font-bold uppercase text-white hover:bg-rose-600"
|
|
||||||
:disabled="busy || (!status.running && status.open_positions === 0)"
|
|
||||||
@click="confirmKill"
|
|
||||||
>
|
|
||||||
Kill switch
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</CollapsiblePanel>
|
|
||||||
</template>
|
|
||||||
@@ -4,13 +4,11 @@ import { computed, ref, watch } from 'vue'
|
|||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
modelValue: { type: Object, required: true },
|
modelValue: { type: Object, required: true },
|
||||||
strategies: { type: Array, required: true },
|
strategies: { type: Array, required: true },
|
||||||
defaultSymbol: { type: String, default: 'BTC/USDT' },
|
|
||||||
defaultTimeframe: { type: String, default: '5m' },
|
|
||||||
disabled: { type: Boolean, default: false },
|
disabled: { type: Boolean, default: false },
|
||||||
// Share of the total balance this row will receive (0..1), for display.
|
// Share of the total balance this row will receive (0..1), for display.
|
||||||
allocation: { type: Number, default: 0 },
|
allocation: { type: Number, default: 0 },
|
||||||
})
|
})
|
||||||
const emit = defineEmits(['update:modelValue', 'remove'])
|
const emit = defineEmits(['update:modelValue', 'remove', 'save-preset'])
|
||||||
|
|
||||||
const row = computed(() => props.modelValue)
|
const row = computed(() => props.modelValue)
|
||||||
const selected = computed(() => props.strategies.find((s) => s.key === row.value.strategy_key))
|
const selected = computed(() => props.strategies.find((s) => s.key === row.value.strategy_key))
|
||||||
@@ -115,7 +113,8 @@ watch(
|
|||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="mt-2 flex justify-end">
|
<div class="mt-2 flex justify-end gap-2">
|
||||||
|
<button class="btn-muted" :disabled="disabled" @click="emit('save-preset', row)">Save</button>
|
||||||
<button class="btn-muted" :disabled="disabled" @click="emit('remove')">Remove</button>
|
<button class="btn-muted" :disabled="disabled" @click="emit('remove')">Remove</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
import { mount } from '@vue/test-utils'
|
import { mount } from '@vue/test-utils'
|
||||||
import { describe, expect, it } from 'vitest'
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||||
import MultiStrategyPanel from './components/MultiStrategyPanel.vue'
|
import MultiStrategyPanel from './components/MultiStrategyPanel.vue'
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
vi.restoreAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
const STRATEGIES = [
|
const STRATEGIES = [
|
||||||
{
|
{
|
||||||
key: 'sma_cross',
|
key: 'sma_cross',
|
||||||
@@ -19,9 +23,20 @@ const STRATEGIES = [
|
|||||||
|
|
||||||
const EMPTY_STATUS = { running: false, strategies: [], total_pnl: 0, equity: 0 }
|
const EMPTY_STATUS = { running: false, strategies: [], total_pnl: 0, equity: 0 }
|
||||||
|
|
||||||
function mountPanel(multiStatus = EMPTY_STATUS) {
|
const PRESETS = [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
name: 'BTC fast',
|
||||||
|
strategy_key: 'ema_cross',
|
||||||
|
symbol: 'ETH/USDT',
|
||||||
|
timeframe: '15m',
|
||||||
|
params: { fast: 3 },
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
function mountPanel(multiStatus = EMPTY_STATUS, presets = []) {
|
||||||
return mount(MultiStrategyPanel, {
|
return mount(MultiStrategyPanel, {
|
||||||
props: { strategies: STRATEGIES, multiStatus, busy: false, loading: false },
|
props: { strategies: STRATEGIES, multiStatus, presets, busy: false, loading: false },
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,6 +104,39 @@ describe('MultiStrategyPanel', () => {
|
|||||||
expect(text).toContain('running')
|
expect(text).toContain('running')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('loads a preset as an additional row', async () => {
|
||||||
|
const wrapper = mountPanel(EMPTY_STATUS, PRESETS)
|
||||||
|
const presetSelect = wrapper.findAll('select').find((s) => s.text().includes('BTC fast'))
|
||||||
|
expect(presetSelect).toBeTruthy()
|
||||||
|
|
||||||
|
await presetSelect.setValue('1')
|
||||||
|
|
||||||
|
// A second row now exists, seeded from the preset.
|
||||||
|
expect(wrapper.findAll('select').length).toBeGreaterThan(1)
|
||||||
|
const inputs = wrapper.findAll('input')
|
||||||
|
expect(inputs.some((i) => i.element.value === 'ETH/USDT')).toBe(true)
|
||||||
|
expect(inputs.some((i) => i.element.value === '15m')).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('emits save-preset with the row config', async () => {
|
||||||
|
vi.spyOn(window, 'prompt').mockReturnValue('my preset')
|
||||||
|
const wrapper = mountPanel()
|
||||||
|
const save = wrapper.findAll('button').find((b) => b.text() === 'Save')
|
||||||
|
await save.trigger('click')
|
||||||
|
const payload = wrapper.emitted('save-preset')[0][0]
|
||||||
|
expect(payload.name).toBe('my preset')
|
||||||
|
expect(payload.strategy_key).toBe('sma_cross')
|
||||||
|
expect(payload.params).toEqual({ fast: 10 })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('does not save when the prompt is cancelled', async () => {
|
||||||
|
vi.spyOn(window, 'prompt').mockReturnValue(null)
|
||||||
|
const wrapper = mountPanel()
|
||||||
|
const save = wrapper.findAll('button').find((b) => b.text() === 'Save')
|
||||||
|
await save.trigger('click')
|
||||||
|
expect(wrapper.emitted('save-preset')).toBeUndefined()
|
||||||
|
})
|
||||||
|
|
||||||
it('marks a halted strategy', () => {
|
it('marks a halted strategy', () => {
|
||||||
const wrapper = mountPanel({
|
const wrapper = mountPanel({
|
||||||
running: true,
|
running: true,
|
||||||
|
|||||||
@@ -1,48 +0,0 @@
|
|||||||
import { mount } from '@vue/test-utils'
|
|
||||||
import { describe, expect, it } from 'vitest'
|
|
||||||
import StrategyPanel from './components/StrategyPanel.vue'
|
|
||||||
|
|
||||||
function mountPanel() {
|
|
||||||
return mount(StrategyPanel, {
|
|
||||||
props: {
|
|
||||||
strategies: [
|
|
||||||
{
|
|
||||||
key: 'sma_cross',
|
|
||||||
name: 'SMA Crossover',
|
|
||||||
description: 'test',
|
|
||||||
params: [{ name: 'fast', type: 'int', default: 10, description: '' }],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
status: { running: false, symbol: 'BTC/USDT', timeframe: '5m', open_positions: 0 },
|
|
||||||
busy: false,
|
|
||||||
presets: [],
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
describe('StrategyPanel collapse', () => {
|
|
||||||
it('shows the strategy form by default', () => {
|
|
||||||
const wrapper = mountPanel()
|
|
||||||
expect(wrapper.find('select').exists()).toBe(true)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('hides the form when the header is clicked', async () => {
|
|
||||||
const wrapper = mountPanel()
|
|
||||||
const header = wrapper.find('button')
|
|
||||||
expect(header.attributes('aria-expanded')).toBe('true')
|
|
||||||
|
|
||||||
await header.trigger('click')
|
|
||||||
|
|
||||||
expect(wrapper.find('button').attributes('aria-expanded')).toBe('false')
|
|
||||||
// The form container is v-show-hidden, so the select is no longer visible.
|
|
||||||
const hidden = wrapper.findAll('div').some((d) => d.element.style.display === 'none')
|
|
||||||
expect(hidden).toBe(true)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('picks the first strategy so the dropdown is populated', () => {
|
|
||||||
const wrapper = mountPanel()
|
|
||||||
const select = wrapper.find('select')
|
|
||||||
expect(select.element.value).toBe('sma_cross')
|
|
||||||
expect(select.findAll('option')).toHaveLength(1)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
Reference in new issue
Block a user