Skip to content

Commit

Permalink
Refine (#7)
Browse files Browse the repository at this point in the history
* create local store class

Signed-off-by: Lu Ken <[email protected]>

* display ohlcv data from csv file

Signed-off-by: Lu Ken <[email protected]>

---------

Signed-off-by: Lu Ken <[email protected]>
  • Loading branch information
kenplusplus authored Dec 22, 2024
1 parent 348dfa9 commit 039a6c4
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 51 deletions.
1 change: 1 addition & 0 deletions src/GenTradeAgent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@vue/eslint-config-typescript": "^13.0.0",
"@vueuse/components": "^12.0.0",
"@vueuse/core": "^12.0.0",
"csv-parse": "^5.6.0",
"electron": "^31.0.2",
"electron-builder": "^24.13.3",
"electron-vite": "^2.3.0",
Expand Down
89 changes: 79 additions & 10 deletions src/GenTradeAgent/src/main/data.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,89 @@
//
// Manage the local cache data
//

import { readFileSync } from 'fs'
import { join } from 'path'
import { parse } from 'csv-parse'

interface ohlcvData {
time: number | string
open: number | string
high: number | string
low: number | string
close: number | string
vol: number | string
}
export class LocalStore {
private rootDir: string
private cryptoAssets: object | null = null
private stockUSAssets: object | null = null
private ohlcBTC_1h: ohlcvData[] = []
public constructor(cacheDir: string) {
this.rootDir = cacheDir
}

let crypto_assets
private loadJsonData(filePath: string): object {
const data = readFileSync(filePath, 'utf-8')
return JSON.parse(data)
}

export function handlerLoadCache(cache_dir: string) {
console.log('Cache path: ' + cache_dir)
private loadCsvData(filePath: string, dataArray: ohlcvData[]): void {
const data = readFileSync(filePath, { encoding: 'utf-8' })
const headers = ['time', 'open', 'high', 'low', 'close', 'vol']
parse(
data,
{
delimiter: ',',
columns: headers
},
(error, result: ohlcvData[]) => {
if (error) {
console.error(error)
}
result.shift()

const crypto_assets_data = readFileSync(join(cache_dir, 'Binance/crypto_assets.json'), 'utf-8')
crypto_assets = JSON.parse(crypto_assets_data)
return crypto_assets
}
result.forEach((element) => {
if (typeof element.time === 'string') {
element.time = parseInt(element.time, 10)
}
if (typeof element.open === 'string') {
element.open = parseInt(element.open, 10)
}
if (typeof element.high === 'string') {
element.high = parseInt(element.high, 10)
}
if (typeof element.low === 'string') {
element.low = parseInt(element.low, 10)
}
if (typeof element.close === 'string') {
element.close = parseInt(element.close, 10)
}
if (typeof element.vol === 'string') {
element.vol = parseInt(element.vol, 10)
}
})
dataArray.push(...result)
}
)
}

public init(): boolean {
this.cryptoAssets = this.loadJsonData(join(this.rootDir, 'Binance/crypto_assets.json'))
this.stockUSAssets = this.loadJsonData(join(this.rootDir, 'StockUS/stock_us_ticker.json'))
this.loadCsvData(join(this.rootDir, 'Binance/btc_usdt-1hour.csv'), this.ohlcBTC_1h)
// setTimeout(() => console.log(this.ohlcBTC_1h), 200)
return true
}

public getCryptoAssets(): object | null {
return this.cryptoAssets
}

public getStockUSAssets(): object | null {
return this.stockUSAssets
}

export function handlerGetCryptoAssets() {
return crypto_assets
public getOhlcvBTC(): ohlcvData[] {
return this.ohlcBTC_1h
}
}
12 changes: 6 additions & 6 deletions src/GenTradeAgent/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { app, shell, BrowserWindow, ipcMain } from 'electron'
import { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'
import { handlerGetCryptoAssets, handlerLoadCache } from './data'
import { LocalStore } from './data'

function createWindow(): void {
// Create the browser window.
Expand Down Expand Up @@ -50,11 +50,11 @@ app.whenReady().then(() => {
optimizer.watchWindowShortcuts(window)
})

// IPC test
ipcMain.on('ping', () => console.log('pong'))

ipcMain.on('loadCache', () => handlerLoadCache(join(app.getAppPath(), '../../data')))
ipcMain.handle('getCryptoAssets', () => handlerGetCryptoAssets())
const store = new LocalStore(join(app.getAppPath(), '../../data'))
ipcMain.on('loadCache', () => store.init())
ipcMain.handle('getCryptoAssets', () => store.getCryptoAssets())
ipcMain.handle('getStockUSAssets', () => store.getStockUSAssets())
ipcMain.handle('getOhlcvBTC', () => store.getOhlcvBTC())

createWindow()

Expand Down
2 changes: 1 addition & 1 deletion src/GenTradeAgent/src/renderer/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import TradingMain from './components/TradingMain.vue'
import StatusBar from './components/StatusBar.vue'
import 'vfonts/Lato.css'
// const ipcHandle = () => window.electron.ipcRenderer.send('ping')
</script>

<template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ const prompt = ref('')
onMounted(() => {
window.electron.ipcRenderer.invoke('getCryptoAssets').then((response) => {
console.log(response)
//console.log(response)
console.log(Object.keys(response))
//placeholder_output.value = Object.keys(response).toString
placeholder_output.value = JSON.stringify(response)
//placeholder_output.value = JSON.stringify(response)
})
})
</script>
Expand Down
50 changes: 18 additions & 32 deletions src/GenTradeAgent/src/renderer/src/components/TradingDashboard.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div class="dashboard-all">
<div id="trading-chart" v-resize-observer="resizeHandler" class="trading-chart" ></div>
<div id="trading-chart" v-resize-observer="resizeHandler" class="trading-chart"></div>
</div>
</template>

Expand All @@ -21,8 +21,6 @@ const resizeHandler = () => {
}
}
// const ipcHandle = () => window.electron.ipcRenderer.invoke('getCache')
onMounted(() => {
window.electron.ipcRenderer.invoke('getCryptoAssets').then((response) => {
console.log(response)
Expand All @@ -38,42 +36,30 @@ onMounted(() => {
chartElement = document.getElementById('trading-chart')
if (chartElement) {
chartObj = createChart(chartElement, chartOptions)
const areaSeries = chartObj.addAreaSeries({
lineColor: '#2962FF',
topColor: '#2962FF',
bottomColor: 'rgba(41, 98, 255, 0.28)'
})
areaSeries.setData([
{ time: '2018-12-22', value: 32.51 },
{ time: '2018-12-23', value: 31.11 },
{ time: '2018-12-24', value: 27.02 },
{ time: '2018-12-25', value: 27.32 },
{ time: '2018-12-26', value: 25.17 },
{ time: '2018-12-27', value: 28.89 },
{ time: '2018-12-28', value: 25.46 },
{ time: '2018-12-29', value: 23.92 },
{ time: '2018-12-30', value: 22.68 },
{ time: '2018-12-31', value: 22.67 }
])
const candlestickSeries = chartObj.addCandlestickSeries({
upColor: '#26a69a',
downColor: '#ef5350',
borderVisible: false,
wickUpColor: '#26a69a',
wickDownColor: '#ef5350'
})
candlestickSeries.setData([
{ time: '2018-12-22', open: 75.16, high: 82.84, low: 36.16, close: 45.72 },
{ time: '2018-12-23', open: 45.12, high: 53.9, low: 45.12, close: 48.09 },
{ time: '2018-12-24', open: 60.71, high: 60.71, low: 53.39, close: 59.29 },
{ time: '2018-12-25', open: 68.26, high: 68.26, low: 59.04, close: 60.5 },
{ time: '2018-12-26', open: 67.71, high: 105.85, low: 66.67, close: 91.04 },
{ time: '2018-12-27', open: 91.04, high: 121.4, low: 82.7, close: 111.4 },
{ time: '2018-12-28', open: 111.51, high: 142.83, low: 103.34, close: 131.25 },
{ time: '2018-12-29', open: 131.33, high: 151.17, low: 77.68, close: 96.43 },
{ time: '2018-12-30', open: 106.33, high: 110.2, low: 90.39, close: 98.1 },
{ time: '2018-12-31', open: 109.87, high: 114.69, low: 85.66, close: 111.26 }
])
// candlestickSeries.setData([
// { time: '2018-12-22', open: 75.16, high: 82.84, low: 36.16, close: 45.72 },
// { time: '2018-12-23', open: 45.12, high: 53.9, low: 45.12, close: 48.09 },
// { time: '2018-12-24', open: 60.71, high: 60.71, low: 53.39, close: 59.29 },
// { time: '2018-12-25', open: 68.26, high: 68.26, low: 59.04, close: 60.5 },
// { time: '2018-12-26', open: 67.71, high: 105.85, low: 66.67, close: 91.04 },
// { time: '2018-12-27', open: 91.04, high: 121.4, low: 82.7, close: 111.4 },
// { time: '2018-12-28', open: 111.51, high: 142.83, low: 103.34, close: 131.25 },
// { time: '2018-12-29', open: 131.33, high: 151.17, low: 77.68, close: 96.43 },
// { time: '2018-12-30', open: 106.33, high: 110.2, low: 90.39, close: 98.1 },
// { time: '2018-12-31', open: 109.87, high: 114.69, low: 85.66, close: 111.26 }
// ])
window.electron.ipcRenderer.invoke('getOhlcvBTC').then((response) => {
console.log(response)
candlestickSeries.setData(response)
})
chartObj.timeScale().fitContent()
window.addEventListener('resize', () => resizeHandler())
Expand Down

0 comments on commit 039a6c4

Please sign in to comment.