This commit is contained in:
2026-02-26 16:53:51 +08:00
parent f29265c0e4
commit 1e4a361f3c
5 changed files with 144 additions and 5 deletions

View File

@@ -6,6 +6,7 @@ import (
"git.apinb.com/bsm-sdk/core/utils"
"git.apinb.com/quant/gostock/internal/config"
"git.apinb.com/quant/gostock/internal/impl"
"git.apinb.com/quant/gostock/internal/logic/libs"
"git.apinb.com/quant/gostock/internal/models"
"github.com/jedib0t/go-pretty/v6/table"
)
@@ -20,7 +21,8 @@ func main() {
fmt.Println("")
ClosedTable()
UnclosedTable()
UnclosedTableByRealtime()
UnclosedTableByDay()
fmt.Println("")
}
@@ -28,7 +30,7 @@ func ClosedTable() {
tw := table.NewWriter()
tw.SetStyle(table.StyleLight)
tw.SetTitle("已清仓列表")
tw.AppendHeader(table.Row{"ID", "Code", "Name", "OpenDate", "OpenPrice", "CloseDate", "ClosePrice", "PNL/Per", "PNLRate(%)"})
tw.AppendHeader(table.Row{"ID", "Code", "Name", "Open_Date", "Open_Price", "Close_Date", "Close_Price", "PNL/Per", "PNLRate(%)"})
var data []models.MockPosition
impl.DBService.Where("status=?", 1).Find(&data)
var tPNL, tPNLR, cost, sell float64
@@ -54,11 +56,49 @@ func ClosedTable() {
fmt.Println(tw.Render())
}
func UnclosedTable() {
func UnclosedTableByRealtime() {
tw := table.NewWriter()
tw.SetStyle(table.StyleLight)
tw.SetTitle("未平仓列表")
tw.AppendHeader(table.Row{"ID", "Code", "Name", "OpenDate", "OpenPrice", "TodayPrice", "PNL/Per", "PNLRate(%)"})
tw.SetTitle("未平仓列表(实时)")
tw.AppendHeader(table.Row{"ID", "Code", "Name", "Open_Date", "Open_Price", "Realtime_Price", "PNL/Per", "PNLRate(%)"})
var data []models.MockPosition
impl.DBService.Where("status=?", 0).Find(&data)
var tPNL, tPNLR, cost, sell float64
for idx, item := range data {
var stock models.StockBasic
impl.DBService.Where("ts_code=?", item.Code).First(&stock)
currentPrice, err := libs.GetSinaStockPrice(item.Code)
if err != nil {
var daily models.StockDaily
impl.DBService.Model(&models.StockDaily{}).Where("ts_code=?", item.Code).Order("id desc").Limit(1).First(&daily)
currentPrice = daily.High
}
high := utils.FloatRound(currentPrice*0.995, 2)
pnl := utils.FloatRound(high-item.OpenPrice, 2)
pnlRate := utils.FloatRound(pnl/item.OpenPrice*100, 2)
tPNL = tPNL + pnl
cost = cost + item.OpenPrice
sell = sell + high
tw.AppendRow(table.Row{idx + 1, item.Code, stock.Name, item.CreatedAt.Format("2006-01-02"), item.OpenPrice, high, pnl, pnlRate})
}
tPNLR = utils.FloatRound(((sell-cost)/cost)*100, 2)
tw.AppendFooter(table.Row{"", "", "", "TOTAL", utils.FloatRound(cost, 2), utils.FloatRound(sell, 2), utils.FloatRound(tPNL, 2), tPNLR})
fmt.Println(tw.Render())
}
func UnclosedTableByDay() {
tw := table.NewWriter()
tw.SetStyle(table.StyleLight)
tw.SetTitle("未平仓列表(日线)")
tw.AppendHeader(table.Row{"ID", "Code", "Name", "Open_Date", "Open_Price", "Today_Price", "PNL/Per", "PNLRate(%)"})
var data []models.MockPosition
impl.DBService.Where("status=?", 0).Find(&data)
var tPNL, tPNLR, cost, sell float64

17
cmd/rk/main.go Normal file
View File

@@ -0,0 +1,17 @@
package main
import (
"fmt"
"git.apinb.com/quant/gostock/internal/logic/libs"
)
func main() {
// 示例获取贵州茅台sh600519的最新价
price, err := libs.GetSinaStockPrice("600519.SH")
if err != nil {
fmt.Printf("获取失败: %v\n", err)
return
}
fmt.Printf("贵州茅台最新价: %.2f\n", price)
}