Build a Real‑Time Go Stock Analyzer with WebSocket & Vue Frontend
This article details version 3.0 of a Go‑based stock analysis system that adds sector classification, persistent watchlists, scheduled strategy evaluation, real‑time WebSocket market feeds, K‑line charting with technical indicators, a Vue front‑end, deployment steps, future roadmap, and a public GitHub repository.
System Overview
The Go Stock Analyzer 3.0 adds four market sectors (Main Board, ChiNext, STAR, Sci‑Tech), a persistent watchlist, scheduled daily strategy analysis, real‑time market data push via WebSocket, and per‑stock K‑line charts with overlayed technical indicators.
Architecture
go-stock-analyzer/
├── backend/
│ ├── fetcher/ # Stock data fetching (list, daily K, real‑time)
│ ├── storage/ # SQLite persistence
│ ├── strategy/ # Strategy modules (MA, MACD, DSL, etc.)
│ ├── scheduler/ # Daily 15:00 scheduled analysis
│ ├── realtime/ # WebSocket real‑time market push
│ ├── web/ # Gin Web API
│ └── main.go
└── frontend/
├── views/
│ ├── BoardView.vue # Sector‑wise stock browsing
│ ├── StockPoolView.vue # Watchlist management
│ ├── KlineModal.vue # Stock K‑line popup
│ └── RealtimeView.vue # Real‑time market page
└── App.vue / main.tsStartup Logic
On launch the application checks whether the SQLite database already contains stock data. If the database is empty, it fetches the full list of A‑shares from the Sina Finance API and stores only the four defined sectors.
func classifyBoard(symbol, code string) string {
if strings.HasPrefix(symbol, "sh") {
if strings.HasPrefix(code, "688") { return "科创板" }
if strings.HasPrefix(code, "6") { return "上证主板" }
}
if strings.HasPrefix(symbol, "sz") {
if strings.HasPrefix(code, "300") || strings.HasPrefix(code, "301") { return "创业板" }
if strings.HasPrefix(code, "000") { return "深证主板" }
}
return ""
}Watchlist & Scheduled Strategy Analysis
Users add individual stocks to a watchlist via the front‑end. Every day at 15:00 the backend triggers StartDailyTask, reads the watchlist, and runs selected strategies such as MA, MACD, and custom DSL cross‑overs.
func StartDailyTask() {
for {
next := nextTriggerTime(15, 0)
time.Sleep(time.Until(next))
runAnalysis() // reads watchlist as strategy input
}
}Analysis results are saved back to SQLite and can be viewed in the web UI as historical selection records.
Real‑Time Market & WebSocket Push
The real‑time module polls Sina's live API and broadcasts the latest tick data to connected clients using a hub‑based WebSocket server.
// Client subscription example
{ "action": "subscribe", "symbols": ["sz000001", "sh600000"] }
// Server push example
{ "code": "sz000001", "name": "平安银行", "price": 10.23, "time": "14:58:00" }The hub supports multiple client subscriptions and streams data per stock code.
K‑Line & Indicator Calculation
Daily K‑line data are fetched automatically, and technical indicators such as MA5/10/20/30 and MACD (DIF, DEA, MACD) are computed and stored.
type KLine struct {
Code, Date string
Open, Close, High, Low, Volume float64
MA5, MA10, MA20, MA30 float64
DIF, DEA, MACD float64
}The front‑end uses ECharts to render interactive K‑line charts with overlayed indicator lines inside a modal window.
Front‑End Feature Summary
Sector stock list: browse by sector and add stocks to the watchlist with one click.
Watchlist page: view saved stocks and open their K‑line charts.
Stock detail page: shows basic info and daily K‑line chart.
Running the Application
# Start backend
cd backend
go mod tidy
go run main.go
# Start frontend
cd frontend
npm install
npm run devOpen a browser and navigate to http://localhost:5173 to access the UI.
Future Roadmap
Multi‑user system with authentication and isolated watchlists.
Combine real‑time tick chart with daily K‑line for dynamic visualization.
Historical back‑testing and strategy optimization module.
Strategy extensions based on market sentiment, index correlation, and volume‑price relationships.
Open‑Source Repository
GitHub project repository: https://github.com/louis-xie-programmer/go-stock-analyzer
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Code Wrench
Focuses on code debugging, performance optimization, and real-world engineering, sharing efficient development tips and pitfall guides. We break down technical challenges in a down-to-earth style, helping you craft handy tools so every line of code becomes a problem‑solving weapon. 🔧💻
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
