fix exchange interface

This commit is contained in:
c9s 2020-09-18 18:15:45 +08:00
parent 0b58033bfb
commit a9b995a362
6 changed files with 31 additions and 17 deletions

View File

@ -323,14 +323,7 @@ func (e *Exchange) QueryKLines(ctx context.Context, symbol, interval string, opt
return kLines, nil return kLines, nil
} }
type TradeQueryOptions struct { func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) (trades []types.Trade, err error) {
StartTime *time.Time
EndTime *time.Time
Limit int
LastTradeID int64
}
func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *TradeQueryOptions) (trades []types.Trade, err error) {
req := e.Client.NewListTradesService(). req := e.Client.NewListTradesService().
Limit(1000). Limit(1000).
Symbol(symbol) Symbol(symbol)
@ -368,7 +361,7 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *Trad
return trades, nil return trades, nil
} }
func (e *Exchange) BatchQueryTrades(ctx context.Context, symbol string, options *TradeQueryOptions) (allTrades []types.Trade, err error) { func (e *Exchange) BatchQueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) (allTrades []types.Trade, err error) {
var startTime = time.Now().Add(-7 * 24 * time.Hour) var startTime = time.Now().Add(-7 * 24 * time.Hour)
if options.StartTime != nil { if options.StartTime != nil {
startTime = *options.StartTime startTime = *options.StartTime
@ -378,7 +371,7 @@ func (e *Exchange) BatchQueryTrades(ctx context.Context, symbol string, options
var lastTradeID = options.LastTradeID var lastTradeID = options.LastTradeID
for { for {
trades, err := e.QueryTrades(ctx, symbol, &TradeQueryOptions{ trades, err := e.QueryTrades(ctx, symbol, &types.TradeQueryOptions{
StartTime: &startTime, StartTime: &startTime,
Limit: options.Limit, Limit: options.Limit,
LastTradeID: lastTradeID, LastTradeID: lastTradeID,

View File

@ -242,7 +242,6 @@ func ParseEvent(message string) (interface{}, error) {
return nil, fmt.Errorf("unsupported message: %s", message) return nil, fmt.Errorf("unsupported message: %s", message)
} }
// KLine uses binance's kline as the standard structure
type KLine struct { type KLine struct {
StartTime int64 `json:"t"` StartTime int64 `json:"t"`
EndTime int64 `json:"T"` EndTime int64 `json:"T"`

View File

@ -35,9 +35,9 @@ type OrderProcessor struct {
MinProfitSpread float64 `json:"minProfitSpread"` MinProfitSpread float64 `json:"minProfitSpread"`
MaxOrderAmount float64 `json:"maxOrderAmount"` MaxOrderAmount float64 `json:"maxOrderAmount"`
Exchange types.Exchange
Trader *Trader Exchange types.Exchange `json:"-"`
Trader *Trader `json:"-"`
} }
func (p *OrderProcessor) Submit(ctx context.Context, order *types.SubmitOrder) error { func (p *OrderProcessor) Submit(ctx context.Context, order *types.SubmitOrder) error {
@ -46,6 +46,9 @@ func (p *OrderProcessor) Submit(ctx context.Context, order *types.SubmitOrder) e
market := order.Market market := order.Market
quantity := order.Quantity quantity := order.Quantity
tradingCtx.Lock()
defer tradingCtx.Unlock()
switch order.Side { switch order.Side {
case types.SideTypeBuy: case types.SideTypeBuy:

View File

@ -31,7 +31,7 @@ func (s *TradeSync) Sync(ctx context.Context, symbol string, startTime time.Time
log.Infof("found last trade, start from lastID = %d since %s", lastTrade.ID, startTime) log.Infof("found last trade, start from lastID = %d since %s", lastTrade.ID, startTime)
} }
trades, err := s.Exchange.BatchQueryTrades(ctx, symbol, &binance.TradeQueryOptions{ trades, err := s.Exchange.BatchQueryTrades(ctx, symbol, &types.TradeQueryOptions{
StartTime: &startTime, StartTime: &startTime,
Limit: 200, Limit: 200,
LastTradeID: lastID, LastTradeID: lastID,

View File

@ -304,7 +304,18 @@ func (trader *Trader) reportPnL() {
func (trader *Trader) SubmitOrder(ctx context.Context, order *types.SubmitOrder) { func (trader *Trader) SubmitOrder(ctx context.Context, order *types.SubmitOrder) {
trader.Notifier.Notify(":memo: Submitting %s %s %s order with quantity: %s", order.Symbol, order.Type, order.Side, order.QuantityString, order) trader.Notifier.Notify(":memo: Submitting %s %s %s order with quantity: %s", order.Symbol, order.Type, order.Side, order.QuantityString, order)
err := trader.Exchange.SubmitOrder(ctx, order) orderProcessor := &OrderProcessor{
MinQuoteBalance: 0,
MaxAssetBalance: 0,
MinAssetBalance: 0,
MinProfitSpread: 0,
MaxOrderAmount: 0,
Exchange: trader.Exchange,
Trader: trader,
}
err := orderProcessor.Submit(ctx, order)
if err != nil { if err != nil {
log.WithError(err).Errorf("order create error: side %s quantity: %s", order.Side, order.QuantityString) log.WithError(err).Errorf("order create error: side %s quantity: %s", order.Side, order.QuantityString)
return return

View File

@ -6,8 +6,16 @@ import (
) )
type Exchange interface { type Exchange interface {
QueryKLines(interval string, startFrom time.Time, endTo time.Time) []KLineOrWindow QueryKLines(ctx context.Context, symbol string, interval string, options KLineQueryOptions) ([]KLine, error)
QueryTrades(symbol string, startFrom time.Time) []Trade QueryTrades(ctx context.Context, symbol string, options *TradeQueryOptions) ([]Trade, error)
SubmitOrder(ctx context.Context, order *SubmitOrder) error SubmitOrder(ctx context.Context, order *SubmitOrder) error
} }
type TradeQueryOptions struct {
StartTime *time.Time
EndTime *time.Time
Limit int
LastTradeID int64
}