bbgo_origin/service/trade.go

133 lines
3.0 KiB
Go
Raw Normal View History

package service
import (
2020-08-03 07:25:06 +00:00
"context"
"github.com/c9s/bbgo/pkg/bbgo/exchange/binance"
"github.com/c9s/bbgo/pkg/bbgo/types"
"github.com/jmoiron/sqlx"
2020-08-03 07:25:06 +00:00
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"time"
)
2020-08-03 07:25:06 +00:00
type TradeSync struct {
Service *TradeService
Exchange *binance.Exchange
}
func (s *TradeSync) Sync(ctx context.Context, symbol string, startTime time.Time) error {
lastTrade, err := s.Service.QueryLast(symbol)
if err != nil {
return err
}
var lastID int64 = 0
if lastTrade != nil {
lastID = lastTrade.ID
startTime = lastTrade.Time
log.Infof("found last trade, start from lastID = %d since %s", lastTrade.ID, startTime)
}
trades, err := s.Exchange.BatchQueryTrades(ctx, symbol, &binance.TradeQueryOptions{
StartTime: &startTime,
2020-08-04 05:50:27 +00:00
Limit: 200,
2020-08-03 07:25:06 +00:00
LastTradeID: lastID,
})
if err != nil {
return err
}
for _, trade := range trades {
if err := s.Service.Insert(trade); err != nil {
return err
}
}
return nil
}
type TradeService struct {
2020-08-03 07:25:06 +00:00
DB *sqlx.DB
}
2020-08-03 07:25:06 +00:00
func NewTradeService(db *sqlx.DB) *TradeService {
return &TradeService{db}
}
// QueryLast queries the last trade from the database
2020-08-03 07:25:06 +00:00
func (s *TradeService) QueryLast(symbol string) (*types.Trade, error) {
log.Infof("querying last trade symbol = %s", symbol)
rows, err := s.DB.NamedQuery(`SELECT * FROM trades WHERE symbol = :symbol ORDER BY gid DESC LIMIT 1`, map[string]interface{}{
"symbol": symbol,
})
if err != nil {
2020-08-03 07:25:06 +00:00
return nil, errors.Wrap(err, "query last trade error")
}
if rows.Err() != nil {
return nil, rows.Err()
}
defer rows.Close()
2020-08-03 07:25:06 +00:00
if rows.Next() {
var trade types.Trade
err = rows.StructScan(&trade)
return &trade, err
}
2020-08-03 07:25:06 +00:00
return nil, rows.Err()
}
2020-08-04 01:47:54 +00:00
func (s *TradeService) QueryForTradingFeeCurrency(symbol string, feeCurrency string) ([]types.Trade, error) {
rows, err := s.DB.NamedQuery(`SELECT * FROM trades WHERE symbol = :symbol OR fee_currency = :fee_currency ORDER BY traded_at ASC`, map[string]interface{}{
2020-08-03 12:06:33 +00:00
"symbol": symbol,
2020-08-04 01:47:54 +00:00
"fee_currency": feeCurrency,
2020-08-03 12:06:33 +00:00
})
if err != nil {
return nil, err
}
defer rows.Close()
return s.scanRows(rows)
}
func (s *TradeService) Query(symbol string) ([]types.Trade, error) {
2020-08-03 07:25:06 +00:00
rows, err := s.DB.NamedQuery(`SELECT * FROM trades WHERE symbol = :symbol ORDER BY gid ASC`, map[string]interface{}{
"symbol": symbol,
})
if err != nil {
return nil, err
}
defer rows.Close()
2020-08-03 12:06:33 +00:00
return s.scanRows(rows)
}
func (s *TradeService) scanRows(rows *sqlx.Rows) (trades []types.Trade, err error) {
for rows.Next() {
var trade types.Trade
if err := rows.StructScan(&trade); err != nil {
return nil, err
}
2020-08-03 07:25:06 +00:00
trades = append(trades, trade)
}
2020-08-03 07:25:06 +00:00
return trades, rows.Err()
}
2020-08-03 12:06:33 +00:00
func (s *TradeService) Insert(trade types.Trade) error {
2020-08-03 07:25:06 +00:00
_, err := s.DB.NamedExec(`
INSERT INTO trades (id, exchange, symbol, price, quantity, quote_quantity, side, is_buyer, is_maker, fee, fee_currency, traded_at)
VALUES (:id, :exchange, :symbol, :price, :quantity, :quote_quantity, :side, :is_buyer, :is_maker, :fee, :fee_currency, :traded_at)`,
trade)
return err
}