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.
 
 

134 lines
3.4 KiB

package trading
import (
"context"
"sig-pub/api/pb"
"sig-pub/pkg/types"
"sig-pub/pkg/utils/lang"
"sig-pub/pkg/utils/times"
"sig-pub/pkg/zlog"
"google.golang.org/protobuf/types/known/structpb"
)
type TradingGrpcServer struct {
pb.UnimplementedTradingServiceServer
tradingService *TradingService
}
func NewTradingGrpcServer(tradingService *TradingService) *TradingGrpcServer {
return &TradingGrpcServer{
tradingService: tradingService,
}
}
func (svr *TradingGrpcServer) Init() (err error) {
return
}
func (svr *TradingGrpcServer) IndicatorPlots(ctx context.Context, req *pb.ReqIndicatorPlots) (rsp *pb.RspIndicatorPlots, err error) {
indPlots, err := svr.tradingService.IndicatorPlots(req.Indicators...)
if err != nil {
return
}
rsp = new(pb.RspIndicatorPlots)
for _, indName := range req.Indicators {
plots := indPlots[indName]
p := &pb.IndicatorPlot{Indicator: indName}
for _, plot := range plots {
ps := &pb.IndicatorPlotSeries{
State: plot.State,
Type: int32(plot.Type),
}
if ps.Props, err = structpb.NewStruct(plot.Props); err != nil {
return
}
for _, exp := range plot.Exps {
pe := &pb.IndicatorPlotExp{
Exp: exp.Exp,
}
if pe.Props, err = structpb.NewStruct(exp.Props); err != nil {
return
}
ps.Exps = append(ps.Exps, pe)
}
p.Plots = append(p.Plots, ps)
}
rsp.Plots = append(rsp.Plots, p)
}
return
}
func (svr *TradingGrpcServer) IndicatorSeries(ctx context.Context, req *pb.ReqIndicatorSeries) (rsp *pb.RspIndicatorSeries, err error) {
// s, err := structpb.NewStruct(map[string]any{})
input := req.Input.AsMap()
matrix, times, states, err := svr.tradingService.IndicatorSeries(ctx, req.Indicator, req.Digit, types.Input(input), req.Series)
if err != nil {
return
}
rsp = &pb.RspIndicatorSeries{}
rsp.Matrix = matrix
rsp.Times = times
for state, value := range states {
rsp.States = append(rsp.States, &pb.IndicatorState{
State: state,
Matrix: value,
})
}
return
}
func (svr *TradingGrpcServer) StrategySeries(ctx context.Context, req *pb.ReqStrategySeries) (rsp *pb.RspStrategySeries, err error) {
rsp = &pb.RspStrategySeries{}
err = svr.tradingService.StrategySeries(ctx, req, rsp)
return
}
func (svr *TradingGrpcServer) Backtest(ctx context.Context, req *pb.ReqBacktest) (rsp *pb.RspBacktest, err error) {
stime, err := times.ParseFORMAT(req.Stime)
if err != nil {
return
}
etime, err := times.ParseFORMAT(req.Etime)
if err != nil {
return
}
err = svr.tradingService.Backtest(ctx, req.PlanId, stime.UnixMilli(), etime.UnixMilli())
if err != nil {
return
}
rsp = new(pb.RspBacktest)
return
}
// BacktestLog 回测记录查询
func (svr *TradingGrpcServer) BacktestLog(ctx context.Context, req *pb.ReqBacktestLog) (rsp *pb.RspBacktestLog, err error) {
logs, err := svr.tradingService.BacktestLog(ctx, 10001)
if err != nil {
return
}
rsp = new(pb.RspBacktestLog)
for _, l := range logs {
log := &pb.BacktestLog{
PlanId: l.PlanId,
Stime: l.SeriesBefore,
Etime: l.SeriesAfter,
BacktestId: l.Id,
}
rsp.Logs = append(rsp.Logs, log)
}
return
}
// BacktestRace 交易计划参数调试回测
func (svr *TradingGrpcServer) BacktestRace(ctx context.Context, req *pb.ReqBacktestRace) (rsp *pb.RspBacktestRace, err error) {
rsp = new(pb.RspBacktestRace)
lang.SafeGo(func() {
c := context.Background()
err := svr.tradingService.BacktestRace(c, req)
if err != nil {
zlog.Error("BacktestRace error:", err)
}
})
return
}