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.
104 lines
2.5 KiB
104 lines
2.5 KiB
package main |
|
|
|
import ( |
|
"sig-pub/internal/exchange/okx" |
|
"sig-pub/pkg/config" |
|
vmts "sig-pub/pkg/storage/tsdb/victoria_metrics" |
|
"sig-pub/pkg/types" |
|
"sig-pub/pkg/zlog" |
|
"testing" |
|
) |
|
|
|
func TestExchange(t *testing.T) { |
|
channel := okx.NewChannelCandle("candle-0", "http://192.168.1.5:7890") |
|
// channel := okx.NewChannelTickers("tickers-0", "http://192.168.1.5:7890") |
|
// channel := okx.NewChannelTrades("trades-0", "http://192.168.1.5:7890") |
|
// channel := okx.NewChannelBooks("books-0", "http://192.168.1.5:7890") |
|
if err := channel.Init(); err != nil { |
|
panic(err) |
|
} |
|
err := channel.Subscribe("DOGE-USDT") // "BTC-USDT-SWAP", |
|
if err != nil { |
|
panic(err) |
|
} |
|
|
|
C := channel.Consumer() |
|
|
|
vmdb := vmts.NewVictoriaMetricsTSDB(config.VictoriaMetricsConfig{Addr: "http://127.0.0.1:8428"}) |
|
|
|
var klines []*types.Kline |
|
inst := types.TradeInstance{ |
|
InstId: "DOGE-USDT", |
|
} |
|
for candle := range C { |
|
for _, kline := range candle.Klines { |
|
if kline.Confirm { |
|
klines = append(klines, kline) |
|
} |
|
} |
|
|
|
if len(klines) >= 10 { |
|
err := vmdb.SaveKlines(inst, klines) |
|
if err != nil { |
|
zlog.Error(err) |
|
} else { |
|
zlog.Infof("save klines :%d", len(klines)) |
|
} |
|
klines = klines[:0] |
|
} |
|
|
|
zlog.Infof("channel: %s, instId: %s, datas: %#v", candle.ExgInstId, candle.Exchange, candle.Klines[0]) |
|
} |
|
} |
|
|
|
// type ExchangeService struct { |
|
// pb.UnimplementedExchangeServiceServer |
|
// } |
|
|
|
// func NewExchangeService() *ExchangeService { |
|
// return &ExchangeService{} |
|
// } |
|
|
|
// func (ExchangeService) SubscribeKline(req *pb.ReqSubscribeKline, stream grpc.ServerStreamingServer[pb.StreamKline]) error { |
|
// ctx := stream.Context() |
|
|
|
// topic := req.Topic |
|
// log.Printf("Client subscribed to topic: %s", topic) |
|
|
|
// // go func() { |
|
// // <-time.After(time.Second * 20) |
|
// // // stream.Context().Done() |
|
// // // stream.CloseSend() |
|
|
|
// // fmt.Println("cenceled...") |
|
// // }() |
|
|
|
// for { |
|
// select { |
|
// case <-ctx.Done(): |
|
// // 客户端断开连接 |
|
// log.Printf("Client disconnected from topic: %s", topic) |
|
// return nil |
|
// default: |
|
// // 模拟事件生成 |
|
// event := &pb.StreamKline{ |
|
// InstId: "DOGE/USDT", |
|
// Exchange: pb.Exchange_OKX, |
|
// Klines: []*pb.Kline{ |
|
// { |
|
// Ts: time.Now().Unix(), |
|
// }, |
|
// }, |
|
// } |
|
|
|
// // 推送事件 |
|
// if err := stream.Send(event); err != nil { |
|
// log.Printf("Failed to send event to client: %v", err) |
|
// return err |
|
// } |
|
|
|
// // 模拟事件间隔 |
|
// time.Sleep(2 * time.Second) |
|
// } |
|
// } |
|
// }
|
|
|