Skip to content

Commit

Permalink
remove offline data
Browse files Browse the repository at this point in the history
Signed-off-by: Lu Ken <[email protected]>
  • Loading branch information
kenplusplus committed Jan 3, 2025
1 parent 2889ed5 commit a6d1c10
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ store.watch(
(state) => state.currentOhlcv,
(value) => {
console.log('change ohlcv')
console.log(value)
candlestickSeries.setData(value)
}
)
Expand Down
107 changes: 74 additions & 33 deletions src/GenTradeAgent/src/renderer/src/components/TradingMain.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
<div class="pane-dashboard">
<div class="pane-dashboard-title">
<n-space horizontal size="small">
<n-select
v-model:value="currentMarket"
filterable
placeholder="Please select a market"
:options="optionsMarket"
size="tiny"
menu-size="tiny"
class="title-item"
style="width: 100px"
@update:value="handleUpdateCurrentMarket"
/>
<n-select
v-model:value="currentAsset"
filterable
Expand All @@ -13,7 +24,7 @@
size="tiny"
menu-size="tiny"
class="title-item"
style="width: 200px"
style="width: 150px"
@update:value="handleUpdateCurrentAsset"
/>
<n-select
Expand Down Expand Up @@ -52,19 +63,39 @@ import { ref } from 'vue'
import TradingDashboard from './TradingDashboard.vue'
import TradingChatAgent from './TradingChatAgent.vue'
import { useStore, getMarket } from '../store'
import { agentServer } from '@renderer/server'
import { useStore } from '../store'
import { agentServer, MARKET_CRYPTO } from '@renderer/server'
interface IOption {
label: string
value: string
}
let isConnect = false
const store = useStore()
const optionsAsset = ref<IOption[]>([])
const lastSelectedMarketId = localStorage.getItem('lastSelectedMarketId')
if (lastSelectedMarketId) {
store.state.currentMarket = lastSelectedMarketId
}
const lastSelectedAsset = localStorage.getItem('lastSelectedAsset')
if (lastSelectedAsset) {
store.state.currentAsset = lastSelectedAsset
}
const lastSelectedInterval = localStorage.getItem('lastSelectedInterval')
if (lastSelectedInterval) {
store.state.currentInterval = lastSelectedInterval
}
const currentMarket = ref(store.state.currentMarket)
const currentAsset = ref(store.state.currentAsset)
const currentInterval = ref('1h')
const currentInterval = ref('1d')
const optionsMarket = ref<IOption[]>([])
const optionsAsset = ref<IOption[]>([])
const optionsInterval = ref([
{
label: '1m',
Expand All @@ -88,31 +119,32 @@ const optionsInterval = ref([
}
])
store.watch(
(state) => state.ohlcvDB,
(value) => {
Object.keys(value).forEach((item) => {
optionsAsset.value.push({
label: getMarket(store.state, item) + ': ' + item,
value: item
})
const updateAssetOptions = (market_id) => {
const assertNames = Object.keys(agentServer.markets[market_id].assets)
optionsAsset.value.length = 0
assertNames.forEach((item) => {
optionsAsset.value.push({
label: item,
value: item
})
}
)
})
currentAsset.value = store.state.currentAsset
}
const handlerRefresh = () => {
store.commit('updateCurrentAsset', currentAsset.value)
setTimeout(handlerRefresh, 30000)
const handleUpdateCurrentMarket = (value: string) => {
store.commit('updateCurrentMarket', value)
localStorage.setItem('lastSelectedMarketId', value)
updateAssetOptions(value)
}
handlerRefresh()
const handleUpdateCurrentAsset = (value: string) => {
console.log('handleUpdateCurrentAsset:' + value)
store.commit('updateCurrentAsset', value)
localStorage.setItem('lastSelectedAsset', value)
}
const handleUpdateCurrentInterval = (value: string) => {
store.commit('updateCurrentInterval', value)
localStorage.setItem('lastSelectedInterval', value)
}
const callbackPing = (latency: number) => {
Expand All @@ -121,21 +153,10 @@ const callbackPing = (latency: number) => {
const handlerPingServer = () => {
agentServer.ping(callbackPing)
setTimeout(handlerPingServer, agentServer.pingInterval)
//setTimeout(handlerPingServer, agentServer.pingInterval)
}
handlerPingServer()
let isConnect = false
const onAssetFromServer = (retval) => {
optionsAsset.value.length = 0
Object.keys(retval).forEach((item) => {
optionsAsset.value.push({
label: item,
value: item
})
})
}
store.watch(
(state) => state.serverLatency,
(value) => {
Expand All @@ -145,7 +166,27 @@ store.watch(
if (!isConnect && value != -1) {
isConnect = true
agentServer.get_assets(onAssetFromServer)
store.commit('updateNotification', 'Reloading market data...')
agentServer.refreshAssets().then(() => {
setTimeout(() => {
store.commit('updateNotification', 'Reloading market data...Done')
// refresh market select UI
optionsMarket.value.length = 0
Object.keys(agentServer.markets).forEach((index) => {
optionsMarket.value.push({ label: agentServer.markets[index].name, value: index })
})
let lastSelectedMarketId = localStorage.getItem('lastSelectedMarketId')
if (lastSelectedMarketId == null || !(lastSelectedMarketId in agentServer.markets)) {
lastSelectedMarketId = MARKET_CRYPTO
}
currentMarket.value = agentServer.markets[lastSelectedMarketId].name
localStorage.setItem('lastSelectedMarketId', lastSelectedMarketId)
store.commit('updateCurrentMarket', lastSelectedMarketId)
updateAssetOptions(lastSelectedMarketId)
}, 500)
})
}
}
)
Expand Down
18 changes: 9 additions & 9 deletions src/GenTradeAgent/src/renderer/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ const app = createApp(App)
app.use(store, keyStore)
app.mount('#app')

window.electron.ipcRenderer.invoke('getCryptoAssetDB').then((response) => {
store.commit('updateCryptoAssetDB', response)
})
window.electron.ipcRenderer.invoke('getStockUSAssetDB').then((response) => {
store.commit('updateStockUSAssetDB', response)
})
window.electron.ipcRenderer.invoke('getOhlcvDB').then((response) => {
store.commit('updateOhlcvDB', response)
})
// window.electron.ipcRenderer.invoke('getCryptoAssetDB').then((response) => {
// store.commit('updateCryptoAssetDB', response)
// })
// window.electron.ipcRenderer.invoke('getStockUSAssetDB').then((response) => {
// store.commit('updateStockUSAssetDB', response)
// })
// window.electron.ipcRenderer.invoke('getOhlcvDB').then((response) => {
// store.commit('updateOhlcvDB', response)
// })
79 changes: 63 additions & 16 deletions src/GenTradeAgent/src/renderer/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
import axios, { AxiosError } from 'axios'

type Asset = {
name: string
quote: string
type: string
cik: number
base: string
symbol: string
}

interface IMarket {
name: string
type: string
assets: { [name: string]: Asset }
}

export const MARKET_CRYPTO = 'b13a4902-ad9d-11ef-a239-00155d3ba217'
export const MARKET_STOCK_US = '5784f1f5-d8f6-401d-8d24-f685a3812f2d'

class AgentServer {
static DEFAULT_SERVER_ADDRESS = 'http://47.100.216.225:8000/api/v1'
apiKey: string = 'e54d4431-5dab-474e-b71a-0db1fcb9e659'
tzName: string = ''
tzOffset: number = 0
pingInterval: number = 10000
markets: { [market_id: string]: IMarket } = {}

get serverAddress() {
const retval = localStorage.getItem('serverAddress')
Expand Down Expand Up @@ -39,10 +58,10 @@ class AgentServer {

ask_question(prompt: string, callback: (answer: string) => void) {
const address = this.serverAddress + '/agent/'
axios.interceptors.request.use((request) => {
console.log('Starting Request', JSON.stringify(request, null, 2))
return request
})
// axios.interceptors.request.use((request) => {
// console.log('Starting Request', JSON.stringify(request, null, 2))
// return request
// })

axios.defaults.headers['X-API-KEY'] = 'e54d4431-5dab-474e-b71a-0db1fcb9e659'
axios
Expand All @@ -60,17 +79,21 @@ class AgentServer {
})
}

get_assets(callback: (retval) => void) {
const address = this.serverAddress + '/public/assets/'
axios
.get(address)
.then((response) => {
console.log(response)
callback(response.data)
})
.catch((err: AxiosError) => {
console.log(err)
})
async get_assets(market_id: string, start: number = 0, limit: number = 0) {
const address = this.serverAddress + '/public/markets/' + market_id + '/assets'
const resp = await axios.get(address, {
params: {
start: start,
limit: limit
}
})
return resp.data
}

async get_markets() {
const address = this.serverAddress + '/public/markets/'
const resp = await axios.get(address)
return resp.data
}

get_ohlcv(assetName: string, interval: string, callback: (data) => void) {
Expand All @@ -89,13 +112,37 @@ class AgentServer {
}
})
.then((response) => {
console.log(response)
callback(response.data)
})
.catch((err: AxiosError) => {
console.log(err)
})
}

async refreshAssets() {
this.markets = await this.get_markets()
await Object.keys(this.markets).forEach(async (market_id) => {
this.markets[market_id].assets = {}
let start = 0
let isEnd = false
while (!isEnd) {
const assets = await this.get_assets(market_id, start, 1000)
if (assets.length < 1000) {
isEnd = true
break
}
start += 1000
assets.forEach(async (item) => {
if (market_id == MARKET_CRYPTO && item.quote == 'usdt' && item.type == 'spot') {
this.markets[market_id].assets[item.name] = item
}
if (market_id == MARKET_STOCK_US && item.type == 'stock') {
this.markets[market_id].assets[item.name] = item
}
})
}
})
}
}

export const agentServer = new AgentServer()
Loading

0 comments on commit a6d1c10

Please sign in to comment.