This commit is contained in:
2026-04-17 19:56:22 +08:00
parent 592c8e31dd
commit aa2c3fb84d
8 changed files with 271 additions and 29 deletions

View File

@@ -3,6 +3,7 @@ package logic
import (
"encoding/json"
"fmt"
"log"
"time"
"git.apinb.com/quant/collector/internal/impl"
@@ -182,24 +183,53 @@ func processOrderBook(accountID string, order types.Order, ymd int, now time.Tim
// OffsetFlag: 通常 0=买入, 1=卖出 (需要根据实际系统确认)
// 这里假设: OffsetFlag == 0 表示买入, OffsetFlag == 1 表示卖出
if order.OffsetFlag == 0 {
// 买入订单 - 创建新的订单簿记录
orderBook := models.OrderBook{
AccountID: accountID,
StockCode: order.StockCode,
Ymd: ymd,
BuyOrderID: order.OrderID,
BuyPrice: order.TradedPrice,
BuyVolume: order.TradedVolume,
BuyTime: order.OrderTime,
BuyCollectedAt: now,
IsClosed: false,
}
if order.OffsetFlag == types.FLAG_BUY {
// 先检查有无未闭合的订单簿记录
var existingOrderBook models.OrderBook
err := impl.DBService.Where(
"account_id = ? AND stock_code = ? AND is_closed = ?",
accountID, order.StockCode, false,
).Order("buy_time DESC").First(&existingOrderBook).Error
if err := impl.DBService.Create(&orderBook).Error; err != nil {
return fmt.Errorf("创建订单簿记录失败: %w", err)
if err == gorm.ErrRecordNotFound {
// 没有未闭合的订单簿记录,创建新的
orderBook := models.OrderBook{
AccountID: accountID,
StockCode: order.StockCode,
Ymd: ymd,
BuyOrderID: fmt.Sprintf("%d", order.OrderID),
BuyPrice: order.TradedPrice,
BuyVolume: order.TradedVolume,
BuyTime: order.OrderTime,
BuyCollectedAt: now,
IsClosed: false,
}
if err := impl.DBService.Create(&orderBook).Error; err != nil {
return fmt.Errorf("创建订单簿记录失败: %w", err)
}
log.Printf("新建订单簿: account=%s, stock=%s, orderID=%d, price=%.4f, volume=%d",
accountID, order.StockCode, order.OrderID, order.TradedPrice, order.TradedVolume)
} else if err != nil {
return fmt.Errorf("查询订单簿记录失败: %w", err)
} else {
// 存在未闭合的订单簿记录,更新数量和价格(加权平均)
totalVolume := existingOrderBook.BuyVolume + order.TradedVolume
totalAmount := existingOrderBook.BuyPrice*float64(existingOrderBook.BuyVolume) + order.TradedPrice*float64(order.TradedVolume)
newAvgPrice := totalAmount / float64(totalVolume)
existingOrderBook.BuyVolume = totalVolume
existingOrderBook.BuyPrice = newAvgPrice
// 追加订单ID到 BuyOrderID用逗号分隔
existingOrderBook.BuyOrderID = existingOrderBook.BuyOrderID + "," + fmt.Sprintf("%d", order.OrderID)
if err := impl.DBService.Save(&existingOrderBook).Error; err != nil {
return fmt.Errorf("更新订单簿记录失败: %w", err)
}
log.Printf("更新订单簿: account=%s, stock=%s, 新订单ID=%d, 总数量=%d, 平均价格=%.4f",
accountID, order.StockCode, order.OrderID, totalVolume, newAvgPrice)
}
} else if order.OffsetFlag == 1 {
} else if order.OffsetFlag == types.FLAG_SELL {
// 卖出订单 - 查找对应的买入记录并闭合
var orderBook models.OrderBook
// 查找同一账户、同一股票、未闭合的订单簿记录
@@ -216,17 +246,37 @@ func processOrderBook(accountID string, order types.Order, ymd int, now time.Tim
return fmt.Errorf("查询订单簿记录失败: %w", err)
}
// 计算盈亏
buyAmount := float64(orderBook.BuyVolume) * orderBook.BuyPrice
sellAmount := float64(order.TradedVolume) * order.TradedPrice
profit := sellAmount - buyAmount
// 计算盈亏(支持部分卖出)
sellVolume := order.TradedVolume
sellPrice := order.TradedPrice
buyPrice := orderBook.BuyPrice
// 如果卖出数量小于买入数量,按比例计算盈亏
var profit float64
var profitRate float64
if buyAmount > 0 {
profitRate = (profit / buyAmount) * 100
if sellVolume >= orderBook.BuyVolume {
// 全部卖出或超额卖出
buyAmount := float64(orderBook.BuyVolume) * buyPrice
sellAmount := float64(sellVolume) * sellPrice
profit = sellAmount - buyAmount
if buyAmount > 0 {
profitRate = (profit / buyAmount) * 100
}
} else {
// 部分卖出,按比例计算
sellRatio := float64(sellVolume) / float64(orderBook.BuyVolume)
buyAmount := float64(orderBook.BuyVolume) * buyPrice * sellRatio
sellAmount := float64(sellVolume) * sellPrice
profit = sellAmount - buyAmount
if buyAmount > 0 {
profitRate = (profit / buyAmount) * 100
}
}
// 更新订单簿记录
orderBook.SellOrderID = &order.OrderID
sellOrderID := fmt.Sprintf("%d", order.OrderID)
orderBook.SellOrderID = &sellOrderID
orderBook.SellPrice = &order.TradedPrice
orderBook.SellVolume = &order.TradedVolume
orderBook.SellTime = &order.OrderTime
@@ -238,6 +288,8 @@ func processOrderBook(accountID string, order types.Order, ymd int, now time.Tim
if err := impl.DBService.Save(&orderBook).Error; err != nil {
return fmt.Errorf("更新订单簿记录失败: %w", err)
}
log.Printf("闭合订单簿: account=%s, stock=%s, buyOrderID=%s, sellOrderID=%s, profit=%.2f",
accountID, order.StockCode, orderBook.BuyOrderID, sellOrderID, profit)
}
return nil