mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
Merge pull request #116 from c9s/feature/sqlite3
convert time struct for sqlite driver
This commit is contained in:
commit
597dd21865
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
@ -436,7 +437,7 @@ func (m *SimplePriceMatching) newOrder(o types.SubmitOrder, orderID uint64) type
|
|||
Status: types.OrderStatusNew,
|
||||
ExecutedQuantity: 0,
|
||||
IsWorking: true,
|
||||
CreationTime: m.CurrentTime,
|
||||
UpdateTime: m.CurrentTime,
|
||||
CreationTime: datatype.Time(m.CurrentTime),
|
||||
UpdateTime: datatype.Time(m.CurrentTime),
|
||||
}
|
||||
}
|
||||
|
|
71
pkg/datatype/time.go
Normal file
71
pkg/datatype/time.go
Normal file
|
@ -0,0 +1,71 @@
|
|||
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) UnmarshalJSON(data []byte) error {
|
||||
return (*time.Time)(t).UnmarshalJSON(data)
|
||||
}
|
||||
|
||||
func (t Time) MarshalJSON() ([]byte, error) {
|
||||
return time.Time(t).MarshalJSON()
|
||||
}
|
||||
|
||||
func (t Time) String() string {
|
||||
return time.Time(t).String()
|
||||
}
|
||||
|
||||
func (t Time) Time() time.Time {
|
||||
return time.Time(t)
|
||||
}
|
||||
|
||||
// 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/pkg/errors"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/datatype"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
"github.com/c9s/bbgo/pkg/util"
|
||||
)
|
||||
|
@ -59,8 +60,8 @@ func ToGlobalOrder(binanceOrder *binance.Order, isMargin bool) (*types.Order, er
|
|||
OrderID: uint64(binanceOrder.OrderID),
|
||||
Status: toGlobalOrderStatus(binanceOrder.Status),
|
||||
ExecutedQuantity: util.MustParseFloat(binanceOrder.ExecutedQuantity),
|
||||
CreationTime: millisecondTime(binanceOrder.Time),
|
||||
UpdateTime: millisecondTime(binanceOrder.UpdateTime),
|
||||
CreationTime: datatype.Time(millisecondTime(binanceOrder.Time)),
|
||||
UpdateTime: datatype.Time(millisecondTime(binanceOrder.UpdateTime)),
|
||||
IsMargin: isMargin,
|
||||
IsIsolated: binanceOrder.IsIsolated,
|
||||
}, nil
|
||||
|
@ -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
|
||||
|
|
|
@ -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"
|
||||
|
@ -112,7 +113,7 @@ func (e *ExecutionReportEvent) Order() (*types.Order, error) {
|
|||
OrderID: uint64(e.OrderID),
|
||||
Status: toGlobalOrderStatus(binance.OrderStatusType(e.CurrentOrderStatus)),
|
||||
ExecutedQuantity: util.MustParseFloat(e.CumulativeFilledQuantity),
|
||||
CreationTime: orderCreationTime,
|
||||
CreationTime: datatype.Time(orderCreationTime),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
|
|
@ -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"
|
||||
|
@ -166,8 +167,8 @@ func toGlobalOrder(maxOrder max.Order) (*types.Order, error) {
|
|||
OrderID: maxOrder.ID,
|
||||
Status: toGlobalOrderStatus(maxOrder.State, executedVolume, remainingVolume),
|
||||
ExecutedQuantity: executedVolume.Float64(),
|
||||
CreationTime: maxOrder.CreatedAt,
|
||||
UpdateTime: maxOrder.CreatedAt,
|
||||
CreationTime: datatype.Time(maxOrder.CreatedAt),
|
||||
UpdateTime: datatype.Time(maxOrder.CreatedAt),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -228,6 +229,6 @@ func toGlobalOrderUpdate(u max.OrderUpdate) (*types.Order, error) {
|
|||
OrderID: u.ID,
|
||||
Status: toGlobalOrderStatus(u.State, executedVolume, remainingVolume),
|
||||
ExecutedQuantity: executedVolume.Float64(),
|
||||
CreationTime: time.Unix(0, u.CreatedAtMs*int64(time.Millisecond)),
|
||||
CreationTime: datatype.Time(time.Unix(0, u.CreatedAtMs*int64(time.Millisecond))),
|
||||
}, nil
|
||||
}
|
||||
|
|
|
@ -45,7 +45,8 @@ func (s *Server) setupTestDB(c *gin.Context) {
|
|||
|
||||
func (s *Server) setupConfigureDB(c *gin.Context) {
|
||||
payload := struct {
|
||||
DSN string `json:"dsn"`
|
||||
Driver string `json:"driver"`
|
||||
DSN string `json:"dsn"`
|
||||
}{}
|
||||
|
||||
if err := c.BindJSON(&payload); err != nil {
|
||||
|
@ -53,19 +54,25 @@ func (s *Server) setupConfigureDB(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
dsn := payload.DSN
|
||||
if len(dsn) == 0 {
|
||||
if len(payload.DSN) == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing dsn argument"})
|
||||
return
|
||||
}
|
||||
|
||||
driver := "mysql"
|
||||
if err := s.Environ.ConfigureDatabase(c, driver, dsn); err != nil {
|
||||
if payload.Driver == "" {
|
||||
payload.Driver = "mysql"
|
||||
}
|
||||
|
||||
if err := s.Environ.ConfigureDatabase(c, payload.Driver, payload.DSN); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"success": true})
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"driver": payload.Driver,
|
||||
"dsn": payload.DSN,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) setupAddStrategy(c *gin.Context) {
|
||||
|
|
|
@ -34,7 +34,7 @@ func (s *SyncService) SyncOrders(ctx context.Context, exchange types.Exchange, s
|
|||
var lastID uint64 = 0
|
||||
if lastOrder != nil {
|
||||
lastID = lastOrder.OrderID
|
||||
startTime = lastOrder.CreationTime
|
||||
startTime = lastOrder.CreationTime.Time()
|
||||
|
||||
logrus.Infof("found last order, start from lastID = %d since %s", lastID, startTime)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -51,7 +51,7 @@ func (e ExchangeBatchProcessor) BatchQueryClosedOrders(ctx context.Context, symb
|
|||
}
|
||||
|
||||
c <- o
|
||||
startTime = o.CreationTime
|
||||
startTime = o.CreationTime.Time()
|
||||
lastOrderID = o.OrderID
|
||||
orderIDs[o.OrderID] = struct{}{}
|
||||
}
|
||||
|
@ -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 {
|
||||
|
|
|
@ -2,10 +2,10 @@ package types
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/slack-go/slack"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/datatype"
|
||||
"github.com/c9s/bbgo/pkg/util"
|
||||
)
|
||||
|
||||
|
@ -113,14 +113,14 @@ func (o *SubmitOrder) SlackAttachment() slack.Attachment {
|
|||
type Order struct {
|
||||
SubmitOrder
|
||||
|
||||
Exchange string `json:"exchange" db:"exchange"`
|
||||
GID uint64 `json:"gid" db:"gid"`
|
||||
OrderID uint64 `json:"orderID" db:"order_id"` // order id
|
||||
Status OrderStatus `json:"status" db:"status"`
|
||||
ExecutedQuantity float64 `json:"executedQuantity" db:"executed_quantity"`
|
||||
IsWorking bool `json:"isWorking" db:"is_working"`
|
||||
CreationTime time.Time `json:"creationTime" db:"created_at"`
|
||||
UpdateTime time.Time `json:"updateTime" db:"updated_at"`
|
||||
Exchange string `json:"exchange" db:"exchange"`
|
||||
GID uint64 `json:"gid" db:"gid"`
|
||||
OrderID uint64 `json:"orderID" db:"order_id"` // order id
|
||||
Status OrderStatus `json:"status" db:"status"`
|
||||
ExecutedQuantity float64 `json:"executedQuantity" db:"executed_quantity"`
|
||||
IsWorking bool `json:"isWorking" db:"is_working"`
|
||||
CreationTime datatype.Time `json:"creationTime" db:"created_at"`
|
||||
UpdateTime datatype.Time `json:"updateTime" db:"updated_at"`
|
||||
|
||||
IsMargin bool `json:"isMargin" db:"is_margin"`
|
||||
IsIsolated bool `json:"isIsolated" db:"is_isolated"`
|
||||
|
|
|
@ -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"`
|
||||
|
|
Loading…
Reference in New Issue
Block a user