2020-08-03 05:17:17 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2020-08-03 07:25:06 +00:00
|
|
|
"context"
|
2020-09-05 08:22:46 +00:00
|
|
|
"time"
|
|
|
|
|
2020-08-03 05:17:17 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2020-08-03 07:25:06 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2020-09-05 08:22:46 +00:00
|
|
|
|
2020-10-05 06:25:58 +00:00
|
|
|
"github.com/c9s/bbgo/types"
|
2020-08-03 05:17:17 +00:00
|
|
|
)
|
|
|
|
|
2020-08-03 07:25:06 +00:00
|
|
|
type TradeSync struct {
|
2020-10-09 05:21:42 +00:00
|
|
|
Service *TradeService
|
2020-08-03 07:25:06 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 07:01:10 +00:00
|
|
|
func (s *TradeSync) Sync(ctx context.Context, exchange types.Exchange, symbol string, startTime time.Time) error {
|
2020-08-03 07:25:06 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-10-10 04:02:06 +00:00
|
|
|
batch := &types.ExchangeBatchProcessor{Exchange: exchange}
|
|
|
|
trades, err := batch.BatchQueryTrades(ctx, symbol, &types.TradeQueryOptions{
|
2020-08-03 07:25:06 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-08-03 05:17:17 +00:00
|
|
|
type TradeService struct {
|
2020-08-03 07:25:06 +00:00
|
|
|
DB *sqlx.DB
|
2020-08-03 05:17:17 +00:00
|
|
|
}
|
|
|
|
|
2020-08-03 07:25:06 +00:00
|
|
|
func NewTradeService(db *sqlx.DB) *TradeService {
|
2020-08-03 05:17:17 +00:00
|
|
|
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{}{
|
2020-08-03 05:17:17 +00:00
|
|
|
"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()
|
2020-08-03 05:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 05:17:17 +00:00
|
|
|
|
2020-08-03 07:25:06 +00:00
|
|
|
return nil, rows.Err()
|
2020-08-03 05:17:17 +00:00
|
|
|
}
|
|
|
|
|
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-10-09 05:21:42 +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{}{
|
2020-08-03 05:17:17 +00:00
|
|
|
"symbol": symbol,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
2020-08-03 12:06:33 +00:00
|
|
|
return s.scanRows(rows)
|
|
|
|
}
|
|
|
|
|
2020-10-09 05:21:42 +00:00
|
|
|
func (s *TradeService) scanRows(rows *sqlx.Rows) (trades []types.Trade, err error) {
|
2020-08-03 05:17:17 +00:00
|
|
|
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
|
|
|
|
2020-08-03 05:17:17 +00:00
|
|
|
trades = append(trades, trade)
|
|
|
|
}
|
|
|
|
|
2020-08-03 07:25:06 +00:00
|
|
|
return trades, rows.Err()
|
2020-08-03 05:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TradeService) Insert(trade types.Trade) error {
|
2020-08-03 07:25:06 +00:00
|
|
|
_, err := s.DB.NamedExec(`
|
2020-08-03 05:17:17 +00:00
|
|
|
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
|
|
|
|
}
|