130 lines
4.1 KiB
Go
130 lines
4.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"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"
|
|
)
|
|
|
|
var (
|
|
ServiceKey = "gostock"
|
|
)
|
|
|
|
func main() {
|
|
config.New(ServiceKey)
|
|
impl.NewImpl()
|
|
|
|
fmt.Println("")
|
|
ClosedTable()
|
|
UnclosedTableByRealtime()
|
|
UnclosedTableByDay()
|
|
fmt.Println("")
|
|
}
|
|
|
|
func ClosedTable() {
|
|
tw := table.NewWriter()
|
|
tw.SetStyle(table.StyleLight)
|
|
tw.SetTitle("已清仓列表")
|
|
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).Order("id asc").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)
|
|
|
|
pnl := utils.FloatRound(item.ClosePrice-item.OpenPrice, 2)
|
|
pnlRate := utils.FloatRound(pnl/item.OpenPrice*100, 2)
|
|
|
|
tPNL = tPNL + pnl
|
|
cost = cost + item.OpenPrice
|
|
sell = sell + item.ClosePrice
|
|
|
|
tw.AppendRow(table.Row{idx + 1, item.Code, stock.Name, item.CreatedAt.Format("2006-01-02"), item.OpenPrice, item.UpdatedAt.Format("2006-01-02"), item.ClosePrice, 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 UnclosedTableByRealtime() {
|
|
tw := table.NewWriter()
|
|
tw.SetStyle(table.StyleLight)
|
|
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).Order("id asc").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).Order("id asc").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)
|
|
|
|
var daily models.StockDaily
|
|
impl.DBService.Model(&models.StockDaily{}).Where("ts_code=?", item.Code).Order("id desc").Limit(1).First(&daily)
|
|
|
|
high := utils.FloatRound(daily.High*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())
|
|
}
|