package trading import ( "context" "sig-pub/api/pb" "sig-pub/pkg/types" "sig-pub/pkg/utils/times" ) 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 } for _, plot := range plots { _ = plot } 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, Value: 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 }