replace Exchange field type with ExchangeName

This commit is contained in:
c9s 2021-05-16 17:02:23 +08:00
parent 0a016cba75
commit e636a5008d
13 changed files with 36 additions and 26 deletions

View File

@ -435,7 +435,7 @@ func (m *SimplePriceMatching) newOrder(o types.SubmitOrder, orderID uint64) type
return types.Order{
OrderID: orderID,
SubmitOrder: o,
Exchange: "backtest",
Exchange: types.ExchangeBacktest,
Status: types.OrderStatusNew,
ExecutedQuantity: 0,
IsWorking: true,

View File

@ -20,9 +20,18 @@ type Position struct {
Quote fixedpoint.Value `json:"quote"`
AverageCost fixedpoint.Value `json:"averageCost"`
ExchangeFeeRates map[types.ExchangeName]fixedpoint.Value `json:"exchangeFeeRates"`
sync.Mutex
}
func (p *Position) SetExchangeFeeRate(ex types.ExchangeName, rate fixedpoint.Value) {
if p.ExchangeFeeRates == nil {
p.ExchangeFeeRates = make(map[types.ExchangeName]fixedpoint.Value)
}
p.ExchangeFeeRates[ex] = rate
}
func (p *Position) SlackAttachment() slack.Attachment {
p.Lock()
averageCost := p.AverageCost

View File

@ -145,7 +145,7 @@ func ToGlobalOrder(binanceOrder *binance.Order, isMargin bool) (*types.Order, er
Price: util.MustParseFloat(binanceOrder.Price),
TimeInForce: string(binanceOrder.TimeInForce),
},
Exchange: types.ExchangeBinance.String(),
Exchange: types.ExchangeBinance,
IsWorking: binanceOrder.IsWorking,
OrderID: uint64(binanceOrder.OrderID),
Status: toGlobalOrderStatus(binanceOrder.Status),

View File

@ -113,7 +113,7 @@ func (e *ExecutionReportEvent) Order() (*types.Order, error) {
orderCreationTime := time.Unix(0, e.OrderCreationTime*int64(time.Millisecond))
return &types.Order{
Exchange: string(types.ExchangeBinance),
Exchange: types.ExchangeBinance,
SubmitOrder: types.SubmitOrder{
Symbol: e.Symbol,
ClientOrderID: e.ClientOrderID,
@ -138,7 +138,7 @@ func (e *ExecutionReportEvent) Trade() (*types.Trade, error) {
tt := time.Unix(0, e.TransactionTime*int64(time.Millisecond))
return &types.Trade{
ID: e.TradeID,
Exchange: string(types.ExchangeBinance),
Exchange: types.ExchangeBinance,
Symbol: e.Symbol,
OrderID: uint64(e.OrderID),
Side: toGlobalSideType(binance.SideType(e.Side)),

View File

@ -43,7 +43,7 @@ func toGlobalOrder(r order) (types.Order, error) {
Price: r.Price,
TimeInForce: "GTC",
},
Exchange: types.ExchangeFTX.String(),
Exchange: types.ExchangeFTX,
IsWorking: r.Status == "open",
OrderID: uint64(r.ID),
Status: "",
@ -114,7 +114,7 @@ func toGlobalTrade(f fill) (types.Trade, error) {
ID: f.TradeId,
GID: 0,
OrderID: f.OrderId,
Exchange: types.ExchangeFTX.String(),
Exchange: types.ExchangeFTX,
Price: f.Price,
Quantity: f.Size,
QuoteQuantity: f.Price * f.Size,

View File

@ -43,7 +43,7 @@ func Test_toGlobalOrderFromOpenOrder(t *testing.T) {
assert.Equal(t, float64(31431), o.Quantity)
assert.Equal(t, 0.306525, o.Price)
assert.Equal(t, "GTC", o.TimeInForce)
assert.Equal(t, types.ExchangeFTX.String(), o.Exchange)
assert.Equal(t, types.ExchangeFTX, o.Exchange)
assert.True(t, o.IsWorking)
assert.Equal(t, uint64(9596912), o.OrderID)
assert.Equal(t, types.OrderStatusPartiallyFilled, o.Status)

View File

@ -602,7 +602,7 @@ func TestExchange_QueryTrades(t *testing.T) {
assert.Equal(t, types.Trade{
ID: 789,
OrderID: 456,
Exchange: types.ExchangeFTX.String(),
Exchange: types.ExchangeFTX,
Price: 672.5,
Quantity: 1.0,
QuoteQuantity: 672.5 * 1.0,

View File

@ -51,7 +51,7 @@ func Test_messageHandler_handleMessage(t *testing.T) {
Price: 2.7185,
TimeInForce: "GTC",
},
Exchange: types.ExchangeFTX.String(),
Exchange: types.ExchangeFTX,
OrderID: 36379,
Status: types.OrderStatusFilled,
ExecutedQuantity: 1.0,
@ -96,7 +96,7 @@ func Test_messageHandler_handleMessage(t *testing.T) {
GID: 0,
ID: 6276431,
OrderID: 323789,
Exchange: types.ExchangeFTX.String(),
Exchange: types.ExchangeFTX,
Price: 2.723,
Quantity: 1.0,
QuoteQuantity: 2.723 * 1.0,

View File

@ -196,7 +196,7 @@ func toGlobalOrder(maxOrder max.Order) (*types.Order, error) {
TimeInForce: "GTC", // MAX only supports GTC
GroupID: maxOrder.GroupID,
},
Exchange: types.ExchangeMax.String(),
Exchange: types.ExchangeMax,
IsWorking: maxOrder.State == "wait",
OrderID: maxOrder.ID,
Status: toGlobalOrderStatus(maxOrder.State, executedVolume, remainingVolume),

View File

@ -217,7 +217,7 @@ func convertWebSocketTrade(t max.TradeUpdate) (*types.Trade, error) {
ID: int64(t.ID),
OrderID: t.OrderID,
Symbol: toGlobalSymbol(t.Market),
Exchange: types.ExchangeMax.String(),
Exchange: types.ExchangeMax,
Price: price,
Quantity: quantity,
Side: side,
@ -253,7 +253,7 @@ func toGlobalOrderUpdate(u max.OrderUpdate) (*types.Order, error) {
TimeInForce: "GTC", // MAX only supports GTC
GroupID: u.GroupID,
},
Exchange: types.ExchangeMax.String(),
Exchange: types.ExchangeMax,
OrderID: u.ID,
Status: toGlobalOrderStatus(u.State, executedVolume, remainingVolume),
ExecutedQuantity: executedVolume.Float64(),

View File

@ -43,6 +43,7 @@ const (
ExchangeMax = ExchangeName("max")
ExchangeBinance = ExchangeName("binance")
ExchangeFTX = ExchangeName("ftx")
ExchangeBacktest = ExchangeName("backtest")
)
func ValidExchangeName(a string) (ExchangeName, error) {

View File

@ -144,7 +144,7 @@ func (o *SubmitOrder) SlackAttachment() slack.Attachment {
type Order struct {
SubmitOrder
Exchange string `json:"exchange" db:"exchange"`
Exchange ExchangeName `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"`
@ -169,13 +169,13 @@ func (o Order) Backup() SubmitOrder {
}
func (o Order) String() string {
return fmt.Sprintf("ORDER %s %s %s %f/%f @ %f -> %s", o.Exchange, o.Symbol, o.Side, o.ExecutedQuantity, o.Quantity, o.Price, o.Status)
return fmt.Sprintf("ORDER %s %s %s %f/%f @ %f -> %s", o.Exchange.String(), o.Symbol, o.Side, o.ExecutedQuantity, o.Quantity, o.Price, o.Status)
}
// PlainText is used for telegram-styled messages
func (o Order) PlainText() string {
return fmt.Sprintf("Order %s %s %s %s @ %s %s/%s -> %s",
o.Exchange,
o.Exchange.String(),
o.Symbol,
o.Type,
o.Side,

View File

@ -49,13 +49,13 @@ type Trade struct {
GID int64 `json:"gid" db:"gid"`
// ID is the source trade ID
ID int64 `json:"id" db:"id"`
OrderID uint64 `json:"orderID" db:"order_id"`
Exchange string `json:"exchange" db:"exchange"`
Price float64 `json:"price" db:"price"`
Quantity float64 `json:"quantity" db:"quantity"`
QuoteQuantity float64 `json:"quoteQuantity" db:"quote_quantity"`
Symbol string `json:"symbol" db:"symbol"`
ID int64 `json:"id" db:"id"`
OrderID uint64 `json:"orderID" db:"order_id"`
Exchange ExchangeName `json:"exchange" db:"exchange"`
Price float64 `json:"price" db:"price"`
Quantity float64 `json:"quantity" db:"quantity"`
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"`
@ -73,7 +73,7 @@ type Trade struct {
func (trade Trade) String() string {
return fmt.Sprintf("TRADE %s %s %s %s @ %s, amount %s",
trade.Exchange,
trade.Exchange.String(),
trade.Symbol,
trade.Side,
util.FormatFloat(trade.Quantity, 4),
@ -84,7 +84,7 @@ func (trade Trade) String() string {
// PlainText is used for telegram-styled messages
func (trade Trade) PlainText() string {
return fmt.Sprintf("Trade %s %s %s %s @ %s, amount %s",
trade.Exchange,
trade.Exchange.String(),
trade.Symbol,
trade.Side,
util.FormatFloat(trade.Quantity, 4),
@ -108,7 +108,7 @@ func (trade Trade) SlackAttachment() slack.Attachment {
Pretext: pretext,
Color: color,
Fields: []slack.AttachmentField{
{Title: "Exchange", Value: trade.Exchange, Short: true},
{Title: "Exchange", Value: trade.Exchange.String(), Short: true},
{Title: "Price", Value: util.FormatFloat(trade.Price, 2), Short: true},
{Title: "Quantity", Value: util.FormatFloat(trade.Quantity, 4), Short: true},
{Title: "QuoteQuantity", Value: util.FormatFloat(trade.QuoteQuantity, 2)},