package trading import ( "context" "sig-pub/api/pb" "sig-pub/pkg/indicator" "sig-pub/pkg/types" "sig-pub/pkg/utils/times" "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) { plots, err := svr.tradingService.IndicatorPlots(req.Indicators...) if err != nil { return } rsp = new(pb.RspIndicatorPlots) for name, plot := range plots { p := &pb.IndicatorPlot{Indicator: name} p.Series, err = mappingIndicatorPlotSeries(plot.Series) if err != nil { return nil, err } for _, series := range plot.StateSeries { ps, err := mappingIndicatorPlotSeries(series) if err != nil { return rsp, err } p.StateSeries = append(p.StateSeries, ps) } rsp.Plots = append(rsp.Plots, p) } return } func mappingIndicatorPlotSeries(ps indicator.PlotSeries) (splot *pb.IndicatorPlotSeries, err error) { seriesProps, err := structpb.NewStruct(ps.Props) if err != nil { return } splot = &pb.IndicatorPlotSeries{ State: ps.State, Type: int32(ps.Type), Props: seriesProps, StateEnumProps: nil, } for state, valueProps := range ps.State2Props { s2p := &pb.IndicatorStateEnumProps{ State: state, EnumProps: make(map[int32]*structpb.Struct), } for v, props := range valueProps { s2p.EnumProps[int32(v)], err = structpb.NewStruct(props) if err != nil { return } } splot.StateEnumProps = append(splot.StateEnumProps, s2p) } 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 }