mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 08:45:16 +00:00
convert time struct for sqlite driver
This commit is contained in:
parent
32117af4b0
commit
3abdb3dd7b
|
@ -2,6 +2,7 @@ package pnl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
)
|
)
|
||||||
|
@ -96,7 +97,7 @@ func (c *AverageCostCalculator) Calculate(symbol string, trades []types.Trade, c
|
||||||
Symbol: symbol,
|
Symbol: symbol,
|
||||||
CurrentPrice: currentPrice,
|
CurrentPrice: currentPrice,
|
||||||
NumTrades: len(trades),
|
NumTrades: len(trades),
|
||||||
StartTime: trades[0].Time,
|
StartTime: time.Time(trades[0].Time),
|
||||||
|
|
||||||
BuyVolume: bidVolume,
|
BuyVolume: bidVolume,
|
||||||
SellVolume: askVolume,
|
SellVolume: askVolume,
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/c9s/bbgo/pkg/datatype"
|
||||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
)
|
)
|
||||||
|
@ -235,7 +236,7 @@ func (m *SimplePriceMatching) newTradeFromOrder(order types.Order, isMaker bool)
|
||||||
Side: order.Side,
|
Side: order.Side,
|
||||||
IsBuyer: order.Side == types.SideTypeBuy,
|
IsBuyer: order.Side == types.SideTypeBuy,
|
||||||
IsMaker: isMaker,
|
IsMaker: isMaker,
|
||||||
Time: m.CurrentTime,
|
Time: datatype.Time(m.CurrentTime),
|
||||||
Fee: fee,
|
Fee: fee,
|
||||||
FeeCurrency: feeCurrency,
|
FeeCurrency: feeCurrency,
|
||||||
}
|
}
|
||||||
|
|
59
pkg/datatype/time.go
Normal file
59
pkg/datatype/time.go
Normal 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)
|
||||||
|
}
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"github.com/adshao/go-binance/v2"
|
"github.com/adshao/go-binance/v2"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
|
"github.com/c9s/bbgo/pkg/datatype"
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
"github.com/c9s/bbgo/pkg/util"
|
"github.com/c9s/bbgo/pkg/util"
|
||||||
)
|
)
|
||||||
|
@ -115,7 +116,7 @@ func ToGlobalTrade(t binance.TradeV3, isMargin bool) (*types.Trade, error) {
|
||||||
Fee: fee,
|
Fee: fee,
|
||||||
FeeCurrency: t.CommissionAsset,
|
FeeCurrency: t.CommissionAsset,
|
||||||
QuoteQuantity: quoteQuantity,
|
QuoteQuantity: quoteQuantity,
|
||||||
Time: millisecondTime(t.Time),
|
Time: datatype.Time(millisecondTime(t.Time)),
|
||||||
IsMargin: isMargin,
|
IsMargin: isMargin,
|
||||||
IsIsolated: t.IsIsolated,
|
IsIsolated: t.IsIsolated,
|
||||||
}, nil
|
}, nil
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/adshao/go-binance/v2"
|
"github.com/adshao/go-binance/v2"
|
||||||
"github.com/valyala/fastjson"
|
"github.com/valyala/fastjson"
|
||||||
|
|
||||||
|
"github.com/c9s/bbgo/pkg/datatype"
|
||||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
"github.com/c9s/bbgo/pkg/util"
|
"github.com/c9s/bbgo/pkg/util"
|
||||||
|
@ -132,7 +133,7 @@ func (e *ExecutionReportEvent) Trade() (*types.Trade, error) {
|
||||||
QuoteQuantity: util.MustParseFloat(e.LastQuoteAssetTransactedQuantity),
|
QuoteQuantity: util.MustParseFloat(e.LastQuoteAssetTransactedQuantity),
|
||||||
IsBuyer: e.Side == "BUY",
|
IsBuyer: e.Side == "BUY",
|
||||||
IsMaker: e.IsMaker,
|
IsMaker: e.IsMaker,
|
||||||
Time: tt,
|
Time: datatype.Time(tt),
|
||||||
Fee: util.MustParseFloat(e.CommissionAmount),
|
Fee: util.MustParseFloat(e.CommissionAmount),
|
||||||
FeeCurrency: e.CommissionAsset,
|
FeeCurrency: e.CommissionAsset,
|
||||||
}, nil
|
}, nil
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/c9s/bbgo/pkg/datatype"
|
||||||
"github.com/c9s/bbgo/pkg/exchange/max/maxapi"
|
"github.com/c9s/bbgo/pkg/exchange/max/maxapi"
|
||||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
|
@ -211,7 +212,7 @@ func toGlobalTrade(t max.Trade) (*types.Trade, error) {
|
||||||
Fee: fee,
|
Fee: fee,
|
||||||
FeeCurrency: toGlobalCurrency(t.FeeCurrency),
|
FeeCurrency: toGlobalCurrency(t.FeeCurrency),
|
||||||
QuoteQuantity: quoteQuantity,
|
QuoteQuantity: quoteQuantity,
|
||||||
Time: mts,
|
Time: datatype.Time(mts),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
|
||||||
|
"github.com/c9s/bbgo/pkg/datatype"
|
||||||
max "github.com/c9s/bbgo/pkg/exchange/max/maxapi"
|
max "github.com/c9s/bbgo/pkg/exchange/max/maxapi"
|
||||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
|
@ -198,7 +199,7 @@ func convertWebSocketTrade(t max.TradeUpdate) (*types.Trade, error) {
|
||||||
Fee: fee,
|
Fee: fee,
|
||||||
FeeCurrency: toGlobalCurrency(t.FeeCurrency),
|
FeeCurrency: toGlobalCurrency(t.FeeCurrency),
|
||||||
QuoteQuantity: quoteQuantity,
|
QuoteQuantity: quoteQuantity,
|
||||||
Time: mts,
|
Time: datatype.Time(mts),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -84,7 +84,7 @@ func (s *SyncService) SyncTrades(ctx context.Context, exchange types.Exchange, s
|
||||||
var lastID int64 = 0
|
var lastID int64 = 0
|
||||||
if lastTrade != nil {
|
if lastTrade != nil {
|
||||||
lastID = lastTrade.ID
|
lastID = lastTrade.ID
|
||||||
startTime = lastTrade.Time
|
startTime = time.Time(lastTrade.Time)
|
||||||
|
|
||||||
logrus.Infof("found last trade, start from lastID = %d since %s", lastID, startTime)
|
logrus.Infof("found last trade, start from lastID = %d since %s", lastID, startTime)
|
||||||
}
|
}
|
||||||
|
|
|
@ -155,7 +155,7 @@ func (e ExchangeBatchProcessor) BatchQueryTrades(ctx context.Context, symbol str
|
||||||
|
|
||||||
logrus.Infof("returned %d trades", len(trades))
|
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 {
|
for _, t := range trades {
|
||||||
// ignore the first trade if last TradeID is given
|
// ignore the first trade if last TradeID is given
|
||||||
if t.ID == lastTradeID {
|
if t.ID == lastTradeID {
|
||||||
|
|
|
@ -3,10 +3,10 @@ package types
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/slack-go/slack"
|
"github.com/slack-go/slack"
|
||||||
|
|
||||||
|
"github.com/c9s/bbgo/pkg/datatype"
|
||||||
"github.com/c9s/bbgo/pkg/util"
|
"github.com/c9s/bbgo/pkg/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ type Trade struct {
|
||||||
Side SideType `json:"side" db:"side"`
|
Side SideType `json:"side" db:"side"`
|
||||||
IsBuyer bool `json:"isBuyer" db:"is_buyer"`
|
IsBuyer bool `json:"isBuyer" db:"is_buyer"`
|
||||||
IsMaker bool `json:"isMaker" db:"is_maker"`
|
IsMaker bool `json:"isMaker" db:"is_maker"`
|
||||||
Time time.Time `json:"tradedAt" db:"traded_at"`
|
Time datatype.Time `json:"tradedAt" db:"traded_at"`
|
||||||
Fee float64 `json:"fee" db:"fee"`
|
Fee float64 `json:"fee" db:"fee"`
|
||||||
FeeCurrency string `json:"feeCurrency" db:"fee_currency"`
|
FeeCurrency string `json:"feeCurrency" db:"fee_currency"`
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user