You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

100 lines
2.8 KiB

package service
import (
"fmt"
"net/http"
"sig-pub/internal/admin/args"
repository "sig-pub/internal/admin/repoitory"
"sig-pub/pkg/data"
"sig-pub/pkg/resp"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
)
type BacktestService struct {
repo *repository.BacktestRepository
}
func NewBacktestService(repo *repository.BacktestRepository) *BacktestService {
return &BacktestService{
repo: repo,
}
}
func (svc *BacktestService) Route(group *gin.RouterGroup) {
group.POST("listBacktest", svc.ListBacktest) // 回测记录分页
group.GET("getBacktest", svc.GetBacktest) // 回测记录详情
group.POST("listBacktestTrades", svc.ListBacktestTrades) // 回测记录交易订单分页
group.GET("testEquities", svc.TestEquities) // 回测记录资金曲线
}
func (svc *BacktestService) ListBacktest(ctx *gin.Context) {
page := data.PageArgs(ctx)
total, tests, err := svc.repo.ListBacktest(10001, page)
if err != nil {
ctx.JSON(http.StatusInternalServerError, resp.Error(err.Error()))
return
}
ctx.JSON(http.StatusOK, resp.Success(resp.H{
"total": total,
"tests": tests,
}))
}
func (svc *BacktestService) GetBacktest(ctx *gin.Context) {
backtestId, err := cast.ToInt64E(ctx.Query("backtestId"))
if backtestId == 0 || err != nil {
ctx.JSON(http.StatusBadRequest, resp.Error("param backtestId format error"))
return
}
test, err := svc.repo.GetBacktest(10001, backtestId)
if err != nil {
ctx.JSON(http.StatusInternalServerError, resp.Error(err.Error()))
return
}
if test == nil || test.Id == 0 {
ctx.JSON(http.StatusInternalServerError, resp.Error(fmt.Sprintf("backtest %d not exists", backtestId)))
return
}
ctx.JSON(http.StatusOK, resp.Success(test))
}
func (svc *BacktestService) ListBacktestTrades(ctx *gin.Context) {
page := data.PageArgs(ctx)
arg := new(args.ListBacktestLogTradesReq)
if err := ctx.ShouldBindJSON(arg); err != nil {
ctx.JSON(http.StatusBadRequest, resp.Fail(err.Error()))
return
}
total, trades, err := svc.repo.ListBacktestTrades(10001, arg.BacktestId, page)
if err != nil {
ctx.JSON(http.StatusInternalServerError, resp.Error(err.Error()))
return
}
ctx.JSON(http.StatusOK, resp.Success(resp.H{
"total": total,
"trades": trades,
}))
}
// TestEquityCurve 回测记录资金曲线
func (svc *BacktestService) TestEquities(ctx *gin.Context) {
backtestId, err := cast.ToInt64E(ctx.Query("backtestId"))
if backtestId == 0 || err != nil {
ctx.JSON(http.StatusBadRequest, resp.Error("param backtestId format error"))
return
}
times, equities, tradeIds, err := svc.repo.BacktestEquities(10001, backtestId)
if err != nil {
ctx.JSON(http.StatusInternalServerError, resp.Error(err.Error()))
return
}
ctx.JSON(http.StatusOK, resp.Success(resp.H{
"time": times,
"equity": equities,
"tradeId": tradeIds,
}))
}