File size: 4,810 Bytes
590a501 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { marketApi, accountApi, positionApi, orderApi, tradeApi, riskApi } from '../api'
export const useTradingStore = defineStore('trading', () => {
const quotes = ref([])
const contracts = ref([])
const categories = ref({})
const exchanges = ref({})
const marketMode = ref('simulated')
const account = ref({
total_balance: 1000000, available_balance: 1000000,
used_margin: 0, unrealized_pnl: 0, realized_pnl: 0, positions: []
})
const positions = ref([])
const orders = ref([])
const trades = ref([])
const riskMetrics = ref({})
const selectedSymbol = ref('')
const selectedCategory = ref('')
const wsConnection = ref(null)
const filteredQuotes = computed(() => {
let result = [...quotes.value]
if (selectedCategory.value) {
result = result.filter(q => q.category === selectedCategory.value)
}
return result.sort((a, b) => (a.name || a.symbol).localeCompare(b.name || b.symbol, 'zh'))
})
const categoryList = computed(() => {
return [{ key: '', label: '全部' }, ...Object.entries(categories.value).map(([k, v]) => ({ key: k, label: v }))]
})
const symbolList = computed(() => contracts.value.map(c => ({ symbol: c.symbol, name: c.name_cn, exchange: c.exchange, category: c.category })))
async function fetchContracts(params) {
try {
const { data } = await marketApi.getContracts(params)
contracts.value = data
} catch (e) { console.error('Failed to fetch contracts:', e) }
}
async function fetchCategories() {
try {
const { data } = await marketApi.getCategories()
categories.value = data
} catch (e) { console.error('Failed to fetch categories:', e) }
}
async function fetchExchanges() {
try {
const { data } = await marketApi.getExchanges()
exchanges.value = data
} catch (e) { console.error('Failed to fetch exchanges:', e) }
}
async function fetchMarketMode() {
try {
const { data } = await marketApi.getMode()
marketMode.value = data.mode
} catch (e) { console.error('Failed to fetch mode:', e) }
}
async function setMarketMode(mode) {
try {
const { data } = await marketApi.setMode(mode)
marketMode.value = data.mode
} catch (e) { console.error('Failed to set mode:', e) }
}
async function fetchQuotes(params) {
try {
const { data } = await marketApi.getQuotes(params)
quotes.value = data
} catch (e) { console.error('Failed to fetch quotes:', e) }
}
async function fetchAccount() {
try {
const { data } = await accountApi.getAccount()
account.value = data
} catch (e) { console.error('Failed to fetch account:', e) }
}
async function fetchPositions() {
try {
const { data } = await positionApi.getPositions()
positions.value = data
} catch (e) { console.error('Failed to fetch positions:', e) }
}
async function fetchOrders() {
try {
const { data } = await orderApi.getOrders()
orders.value = data
} catch (e) { console.error('Failed to fetch orders:', e) }
}
async function fetchTrades() {
try {
const { data } = await tradeApi.getTrades()
trades.value = data
} catch (e) { console.error('Failed to fetch trades:', e) }
}
async function fetchRisk() {
try {
const { data } = await riskApi.getMetrics()
riskMetrics.value = data
} catch (e) { console.error('Failed to fetch risk:', e) }
}
function connectWebSocket() {
if (wsConnection.value) return
const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'
const ws = new WebSocket(`${wsProtocol}//${window.location.host}/ws/market`)
ws.onmessage = (event) => {
const msg = JSON.parse(event.data)
if (msg.type === 'market_data') {
const idx = quotes.value.findIndex(q => q.symbol === msg.data.symbol)
if (idx >= 0) {
quotes.value[idx] = { ...quotes.value[idx], ...msg.data }
} else {
quotes.value.push(msg.data)
}
}
}
ws.onclose = () => {
wsConnection.value = null
setTimeout(connectWebSocket, 3000)
}
wsConnection.value = ws
}
function disconnectWebSocket() {
if (wsConnection.value) {
wsConnection.value.close()
wsConnection.value = null
}
}
return {
quotes, filteredQuotes, contracts, categories, exchanges, categoryList, symbolList,
marketMode, account, positions, orders, trades, riskMetrics,
selectedSymbol, selectedCategory,
fetchContracts, fetchCategories, fetchExchanges, fetchMarketMode, setMarketMode,
fetchQuotes, fetchAccount, fetchPositions, fetchOrders, fetchTrades, fetchRisk,
connectWebSocket, disconnectWebSocket,
}
})
|