convert time struct for sqlite driver

This commit is contained in:
c9s 2021-02-06 12:32:21 +08:00
parent 32117af4b0
commit 3abdb3dd7b
10 changed files with 80 additions and 15 deletions

View File

@ -2,6 +2,7 @@ package pnl
import (
"strings"
"time"
"github.com/c9s/bbgo/pkg/types"
)
@ -96,7 +97,7 @@ func (c *AverageCostCalculator) Calculate(symbol string, trades []types.Trade, c
Symbol: symbol,
CurrentPrice: currentPrice,
NumTrades: len(trades),
StartTime: trades[0].Time,
StartTime: time.Time(trades[0].Time),
BuyVolume: bidVolume,
SellVolume: askVolume,

View File

@ -9,6 +9,7 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/datatype"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
@ -235,7 +236,7 @@ func (m *SimplePriceMatching) newTradeFromOrder(order types.Order, isMaker bool)
Side: order.Side,
IsBuyer: order.Side == types.SideTypeBuy,
IsMaker: isMaker,
Time: m.CurrentTime,
Time: datatype.Time(m.CurrentTime),
Fee: fee,
FeeCurrency: feeCurrency,
}

59
pkg/datatype/time.go Normal file
View File

@ -0,0 +1,59 @@
package datatype
import (
"database/sql/driver"
"fmt"
"time"
)
type Time time.Time
var layout = "2006-01-02 15:04:05.999Z07:00"
func (t Time) String() string {
return time.Time(t).String()
}
// driver.Valuer interface
// see http://jmoiron.net/blog/built-in-interfaces/
func (t Time) Value() (driver.Value, error) {
return time.Time(t), nil
}
func (t *Time) Scan(src interface{}) error {
switch d := src.(type) {
case *time.Time:
*t = Time(*d)
return nil
case time.Time:
*t = Time(d)
return nil
case string:
// 2020-12-16 05:17:12.994+08:00
tt, err := time.Parse(layout, d)
if err != nil {
return err
}
*t = Time(tt)
return nil
case []byte:
// 2019-10-20 23:01:43.77+08:00
tt, err := time.Parse(layout, string(d))
if err != nil {
return err
}
*t = Time(tt)
return nil
default:
}
return fmt.Errorf("datatype.Time scan error, type: %T is not supported, value; %+v", src, src)
}

View File

@ -8,6 +8,7 @@ import (
"github.com/adshao/go-binance/v2"
"github.com/pkg/errors"
"github.com/c9s/bbgo/pkg/datatype"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util"
)
@ -115,7 +116,7 @@ func ToGlobalTrade(t binance.TradeV3, isMargin bool) (*types.Trade, error) {
Fee: fee,
FeeCurrency: t.CommissionAsset,
QuoteQuantity: quoteQuantity,
Time: millisecondTime(t.Time),
Time: datatype.Time(millisecondTime(t.Time)),
IsMargin: isMargin,
IsIsolated: t.IsIsolated,
}, nil

View File

@ -9,6 +9,7 @@ import (
"github.com/adshao/go-binance/v2"
"github.com/valyala/fastjson"
"github.com/c9s/bbgo/pkg/datatype"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util"
@ -132,7 +133,7 @@ func (e *ExecutionReportEvent) Trade() (*types.Trade, error) {
QuoteQuantity: util.MustParseFloat(e.LastQuoteAssetTransactedQuantity),
IsBuyer: e.Side == "BUY",
IsMaker: e.IsMaker,
Time: tt,
Time: datatype.Time(tt),
Fee: util.MustParseFloat(e.CommissionAmount),
FeeCurrency: e.CommissionAsset,
}, nil

View File

@ -6,6 +6,7 @@ import (
"strings"
"time"
"github.com/c9s/bbgo/pkg/datatype"
"github.com/c9s/bbgo/pkg/exchange/max/maxapi"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
@ -211,7 +212,7 @@ func toGlobalTrade(t max.Trade) (*types.Trade, error) {
Fee: fee,
FeeCurrency: toGlobalCurrency(t.FeeCurrency),
QuoteQuantity: quoteQuantity,
Time: mts,
Time: datatype.Time(mts),
}, nil
}

View File

@ -7,6 +7,7 @@ import (
"github.com/gorilla/websocket"
"github.com/c9s/bbgo/pkg/datatype"
max "github.com/c9s/bbgo/pkg/exchange/max/maxapi"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
@ -198,7 +199,7 @@ func convertWebSocketTrade(t max.TradeUpdate) (*types.Trade, error) {
Fee: fee,
FeeCurrency: toGlobalCurrency(t.FeeCurrency),
QuoteQuantity: quoteQuantity,
Time: mts,
Time: datatype.Time(mts),
}, nil
}

View File

@ -84,7 +84,7 @@ func (s *SyncService) SyncTrades(ctx context.Context, exchange types.Exchange, s
var lastID int64 = 0
if lastTrade != nil {
lastID = lastTrade.ID
startTime = lastTrade.Time
startTime = time.Time(lastTrade.Time)
logrus.Infof("found last trade, start from lastID = %d since %s", lastID, startTime)
}

View File

@ -155,7 +155,7 @@ func (e ExchangeBatchProcessor) BatchQueryTrades(ctx context.Context, symbol str
logrus.Infof("returned %d trades", len(trades))
startTime = trades[len(trades)-1].Time
startTime = time.Time(trades[len(trades)-1].Time)
for _, t := range trades {
// ignore the first trade if last TradeID is given
if t.ID == lastTradeID {

View File

@ -3,10 +3,10 @@ package types
import (
"fmt"
"sync"
"time"
"github.com/slack-go/slack"
"github.com/c9s/bbgo/pkg/datatype"
"github.com/c9s/bbgo/pkg/util"
)
@ -49,12 +49,12 @@ type Trade struct {
QuoteQuantity float64 `json:"quoteQuantity" db:"quote_quantity"`
Symbol string `json:"symbol" db:"symbol"`
Side SideType `json:"side" db:"side"`
IsBuyer bool `json:"isBuyer" db:"is_buyer"`
IsMaker bool `json:"isMaker" db:"is_maker"`
Time time.Time `json:"tradedAt" db:"traded_at"`
Fee float64 `json:"fee" db:"fee"`
FeeCurrency string `json:"feeCurrency" db:"fee_currency"`
Side SideType `json:"side" db:"side"`
IsBuyer bool `json:"isBuyer" db:"is_buyer"`
IsMaker bool `json:"isMaker" db:"is_maker"`
Time datatype.Time `json:"tradedAt" db:"traded_at"`
Fee float64 `json:"fee" db:"fee"`
FeeCurrency string `json:"feeCurrency" db:"fee_currency"`
IsMargin bool `json:"isMargin" db:"is_margin"`
IsIsolated bool `json:"isIsolated" db:"is_isolated"`