2020-11-07 08:08:20 +00:00
|
|
|
package backtest
|
|
|
|
|
|
|
|
import (
|
2020-11-09 08:34:35 +00:00
|
|
|
"fmt"
|
2020-11-08 05:07:20 +00:00
|
|
|
"sync"
|
2020-11-07 11:57:36 +00:00
|
|
|
"sync/atomic"
|
2020-11-07 08:08:20 +00:00
|
|
|
"time"
|
|
|
|
|
2022-01-07 18:18:44 +00:00
|
|
|
"github.com/pkg/errors"
|
2022-05-21 18:40:12 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-01-07 18:18:44 +00:00
|
|
|
|
2020-11-07 08:08:20 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2022-06-09 03:46:06 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/util"
|
2020-11-07 08:08:20 +00:00
|
|
|
)
|
|
|
|
|
2020-11-07 11:57:36 +00:00
|
|
|
var orderID uint64 = 1
|
2020-11-08 05:07:20 +00:00
|
|
|
var tradeID uint64 = 1
|
2020-11-07 08:08:20 +00:00
|
|
|
|
2020-11-07 11:57:36 +00:00
|
|
|
func incOrderID() uint64 {
|
|
|
|
return atomic.AddUint64(&orderID, 1)
|
2020-11-07 08:08:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 05:07:20 +00:00
|
|
|
func incTradeID() uint64 {
|
|
|
|
return atomic.AddUint64(&tradeID, 1)
|
|
|
|
}
|
|
|
|
|
2022-06-09 03:46:06 +00:00
|
|
|
var klineMatchingLogger *logrus.Entry = nil
|
|
|
|
|
2022-06-11 19:55:02 +00:00
|
|
|
// FeeToken is used to simulate the exchange platform fee token
|
|
|
|
// This is to ease the back-testing environment for closing positions.
|
|
|
|
const FeeToken = "FEE"
|
2022-06-17 04:01:15 +00:00
|
|
|
|
2022-06-11 19:55:02 +00:00
|
|
|
var useFeeToken = true
|
|
|
|
|
2022-06-09 03:46:06 +00:00
|
|
|
func init() {
|
|
|
|
logger := logrus.New()
|
|
|
|
if v, ok := util.GetEnvVarBool("DEBUG_MATCHING"); ok && v {
|
|
|
|
logger.SetLevel(logrus.DebugLevel)
|
|
|
|
} else {
|
|
|
|
logger.SetLevel(logrus.ErrorLevel)
|
|
|
|
}
|
|
|
|
klineMatchingLogger = logger.WithField("backtest", "klineEngine")
|
2022-06-11 19:55:02 +00:00
|
|
|
|
|
|
|
if v, ok := util.GetEnvVarBool("BACKTEST_USE_FEE_TOKEN"); ok {
|
|
|
|
useFeeToken = v
|
|
|
|
}
|
2022-06-09 03:46:06 +00:00
|
|
|
}
|
2022-05-21 18:40:12 +00:00
|
|
|
|
2020-11-07 11:57:36 +00:00
|
|
|
// SimplePriceMatching implements a simple kline data driven matching engine for backtest
|
2020-11-08 18:58:46 +00:00
|
|
|
//go:generate callbackgen -type SimplePriceMatching
|
2020-11-07 11:57:36 +00:00
|
|
|
type SimplePriceMatching struct {
|
|
|
|
Symbol string
|
2020-11-08 13:52:44 +00:00
|
|
|
Market types.Market
|
2020-11-07 08:08:20 +00:00
|
|
|
|
2022-05-21 18:40:12 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
bidOrders []types.Order
|
|
|
|
askOrders []types.Order
|
2022-06-26 08:13:58 +00:00
|
|
|
closedOrders map[uint64]types.Order
|
2020-11-07 08:08:20 +00:00
|
|
|
|
2022-08-30 12:02:21 +00:00
|
|
|
klineCache map[types.Interval]types.KLine
|
2022-09-01 05:48:33 +00:00
|
|
|
lastPrice fixedpoint.Value
|
|
|
|
lastKLine types.KLine
|
|
|
|
nextKLine *types.KLine
|
|
|
|
currentTime time.Time
|
2020-11-08 04:47:14 +00:00
|
|
|
|
2022-09-01 05:48:33 +00:00
|
|
|
feeModeFunction FeeModeFunction
|
|
|
|
|
|
|
|
account *types.Account
|
2020-11-08 13:52:44 +00:00
|
|
|
|
2020-11-08 18:58:46 +00:00
|
|
|
tradeUpdateCallbacks []func(trade types.Trade)
|
|
|
|
orderUpdateCallbacks []func(order types.Order)
|
2020-11-10 06:18:04 +00:00
|
|
|
balanceUpdateCallbacks []func(balances types.BalanceMap)
|
2020-11-07 08:08:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 05:07:20 +00:00
|
|
|
func (m *SimplePriceMatching) CancelOrder(o types.Order) (types.Order, error) {
|
2020-11-07 11:57:36 +00:00
|
|
|
found := false
|
2020-11-07 08:08:20 +00:00
|
|
|
|
2020-11-07 11:57:36 +00:00
|
|
|
switch o.Side {
|
2020-11-07 08:08:20 +00:00
|
|
|
|
2020-11-07 11:57:36 +00:00
|
|
|
case types.SideTypeBuy:
|
2020-11-08 05:07:20 +00:00
|
|
|
m.mu.Lock()
|
2020-11-07 11:57:36 +00:00
|
|
|
var orders []types.Order
|
|
|
|
for _, order := range m.bidOrders {
|
|
|
|
if o.OrderID == order.OrderID {
|
|
|
|
found = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
orders = append(orders, order)
|
|
|
|
}
|
|
|
|
m.bidOrders = orders
|
2020-11-08 05:07:20 +00:00
|
|
|
m.mu.Unlock()
|
2020-11-07 08:08:20 +00:00
|
|
|
|
2020-11-07 11:57:36 +00:00
|
|
|
case types.SideTypeSell:
|
2020-11-08 05:07:20 +00:00
|
|
|
m.mu.Lock()
|
2020-11-07 11:57:36 +00:00
|
|
|
var orders []types.Order
|
2020-11-10 06:18:04 +00:00
|
|
|
for _, order := range m.askOrders {
|
2020-11-07 11:57:36 +00:00
|
|
|
if o.OrderID == order.OrderID {
|
|
|
|
found = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
orders = append(orders, order)
|
|
|
|
}
|
2020-11-10 06:18:04 +00:00
|
|
|
m.askOrders = orders
|
2020-11-08 05:07:20 +00:00
|
|
|
m.mu.Unlock()
|
2020-11-07 08:08:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 11:57:36 +00:00
|
|
|
if !found {
|
2020-11-09 08:34:35 +00:00
|
|
|
return o, fmt.Errorf("cancel order failed, order %d not found: %+v", o.OrderID, o)
|
2020-11-08 18:58:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch o.Side {
|
|
|
|
case types.SideTypeBuy:
|
2022-09-01 05:48:33 +00:00
|
|
|
if err := m.account.UnlockBalance(m.Market.QuoteCurrency, o.Price.Mul(o.Quantity)); err != nil {
|
2020-11-08 18:58:46 +00:00
|
|
|
return o, err
|
|
|
|
}
|
|
|
|
|
|
|
|
case types.SideTypeSell:
|
2022-09-01 05:48:33 +00:00
|
|
|
if err := m.account.UnlockBalance(m.Market.BaseCurrency, o.Quantity); err != nil {
|
2020-11-08 18:58:46 +00:00
|
|
|
return o, err
|
|
|
|
}
|
2020-11-07 08:08:20 +00:00
|
|
|
}
|
2020-11-08 05:07:20 +00:00
|
|
|
|
|
|
|
o.Status = types.OrderStatusCanceled
|
2020-11-08 18:58:46 +00:00
|
|
|
m.EmitOrderUpdate(o)
|
2022-09-01 05:48:33 +00:00
|
|
|
m.EmitBalanceUpdate(m.account.Balances())
|
2020-11-08 05:07:20 +00:00
|
|
|
return o, nil
|
2020-11-07 08:08:20 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 11:48:14 +00:00
|
|
|
// PlaceOrder returns the created order object, executed trade (if any) and error
|
|
|
|
func (m *SimplePriceMatching) PlaceOrder(o types.SubmitOrder) (*types.Order, *types.Trade, error) {
|
2022-06-28 09:43:51 +00:00
|
|
|
if o.Type == types.OrderTypeMarket {
|
2022-09-01 05:48:33 +00:00
|
|
|
if m.lastPrice.IsZero() {
|
2022-06-28 09:43:51 +00:00
|
|
|
panic("unexpected error: for market order, the last price can not be zero")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 05:48:33 +00:00
|
|
|
isTaker := o.Type == types.OrderTypeMarket || isLimitTakerOrder(o, m.lastPrice)
|
2022-06-28 09:43:51 +00:00
|
|
|
|
2022-01-29 18:41:00 +00:00
|
|
|
// price for checking account balance, default price
|
2020-11-08 13:52:44 +00:00
|
|
|
price := o.Price
|
2022-01-29 18:41:00 +00:00
|
|
|
|
2020-11-08 13:52:44 +00:00
|
|
|
switch o.Type {
|
|
|
|
case types.OrderTypeMarket:
|
2022-09-01 05:48:33 +00:00
|
|
|
price = m.Market.TruncatePrice(m.lastPrice)
|
2022-07-14 11:26:04 +00:00
|
|
|
|
|
|
|
case types.OrderTypeStopMarket:
|
|
|
|
// the actual price might be different.
|
2022-08-18 02:54:06 +00:00
|
|
|
o.StopPrice = m.Market.TruncatePrice(o.StopPrice)
|
2022-07-14 11:26:04 +00:00
|
|
|
price = o.StopPrice
|
|
|
|
|
2022-06-27 11:48:14 +00:00
|
|
|
case types.OrderTypeLimit, types.OrderTypeStopLimit, types.OrderTypeLimitMaker:
|
2022-08-18 02:54:06 +00:00
|
|
|
o.Price = m.Market.TruncatePrice(o.Price)
|
2020-11-08 13:52:44 +00:00
|
|
|
price = o.Price
|
|
|
|
}
|
|
|
|
|
2022-08-17 10:19:13 +00:00
|
|
|
o.Quantity = m.Market.TruncateQuantity(o.Quantity)
|
|
|
|
|
2022-02-03 11:19:56 +00:00
|
|
|
if o.Quantity.Compare(m.Market.MinQuantity) < 0 {
|
|
|
|
return nil, nil, fmt.Errorf("order quantity %s is less than minQuantity %s, order: %+v", o.Quantity.String(), m.Market.MinQuantity.String(), o)
|
2022-01-29 09:44:42 +00:00
|
|
|
}
|
|
|
|
|
2022-08-18 02:54:06 +00:00
|
|
|
quoteQuantity := o.Quantity.Mul(price)
|
2022-05-21 18:40:12 +00:00
|
|
|
if quoteQuantity.Compare(m.Market.MinNotional) < 0 {
|
|
|
|
return nil, nil, fmt.Errorf("order amount %s is less than minNotional %s, order: %+v", quoteQuantity.String(), m.Market.MinNotional.String(), o)
|
2022-01-29 09:44:42 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 13:52:44 +00:00
|
|
|
switch o.Side {
|
|
|
|
case types.SideTypeBuy:
|
2022-09-01 05:48:33 +00:00
|
|
|
if err := m.account.LockBalance(m.Market.QuoteCurrency, quoteQuantity); err != nil {
|
2020-11-08 18:58:46 +00:00
|
|
|
return nil, nil, err
|
2020-11-08 13:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case types.SideTypeSell:
|
2022-09-01 05:48:33 +00:00
|
|
|
if err := m.account.LockBalance(m.Market.BaseCurrency, o.Quantity); err != nil {
|
2020-11-08 18:58:46 +00:00
|
|
|
return nil, nil, err
|
2020-11-08 13:52:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 05:48:33 +00:00
|
|
|
m.EmitBalanceUpdate(m.account.Balances())
|
2020-11-08 19:09:12 +00:00
|
|
|
|
2020-11-10 06:18:04 +00:00
|
|
|
// start from one
|
|
|
|
orderID := incOrderID()
|
2020-11-08 19:09:12 +00:00
|
|
|
order := m.newOrder(o, orderID)
|
|
|
|
|
2022-06-28 09:43:51 +00:00
|
|
|
if isTaker {
|
2022-08-29 05:11:02 +00:00
|
|
|
var price fixedpoint.Value
|
2022-06-28 14:17:39 +00:00
|
|
|
if order.Type == types.OrderTypeMarket {
|
2022-09-01 05:48:33 +00:00
|
|
|
order.Price = m.Market.TruncatePrice(m.lastPrice)
|
2022-08-29 05:11:02 +00:00
|
|
|
price = order.Price
|
2022-08-18 07:09:46 +00:00
|
|
|
} else if order.Type == types.OrderTypeLimit {
|
2022-08-30 12:07:49 +00:00
|
|
|
// if limit order's price is with the range of next kline
|
|
|
|
// we assume it will be traded as a maker trade, and is traded at its original price
|
|
|
|
// TODO: if it is treated as a maker trade, fee should be specially handled
|
|
|
|
// otherwise, set NextKLine.Close(i.e., m.LastPrice) to be the taker traded price
|
2022-09-01 05:48:33 +00:00
|
|
|
if m.nextKLine != nil && m.nextKLine.High.Compare(order.Price) > 0 && order.Side == types.SideTypeBuy {
|
2022-08-29 05:11:02 +00:00
|
|
|
order.AveragePrice = order.Price
|
2022-09-01 05:48:33 +00:00
|
|
|
} else if m.nextKLine != nil && m.nextKLine.Low.Compare(order.Price) < 0 && order.Side == types.SideTypeSell {
|
2022-08-29 05:11:02 +00:00
|
|
|
order.AveragePrice = order.Price
|
|
|
|
} else {
|
2022-09-01 05:48:33 +00:00
|
|
|
order.AveragePrice = m.Market.TruncatePrice(m.lastPrice)
|
2022-08-29 05:11:02 +00:00
|
|
|
}
|
|
|
|
price = order.AveragePrice
|
2022-06-28 14:17:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 12:27:11 +00:00
|
|
|
// emit the order update for Status:New
|
2020-11-08 18:58:46 +00:00
|
|
|
m.EmitOrderUpdate(order)
|
|
|
|
|
2022-06-05 22:12:54 +00:00
|
|
|
// copy the order object to avoid side effect (for different callbacks)
|
|
|
|
var order2 = order
|
|
|
|
|
2020-11-08 18:58:46 +00:00
|
|
|
// emit trade before we publish order
|
2022-08-29 05:11:02 +00:00
|
|
|
trade := m.newTradeFromOrder(&order2, false, price)
|
2020-11-08 19:09:12 +00:00
|
|
|
m.executeTrade(trade)
|
2020-11-08 18:58:46 +00:00
|
|
|
|
2022-08-18 07:09:46 +00:00
|
|
|
// unlock the rest balances for limit taker
|
|
|
|
if order.Type == types.OrderTypeLimit {
|
|
|
|
if order.AveragePrice.IsZero() {
|
|
|
|
return nil, nil, fmt.Errorf("the average price of the given limit taker order can not be zero")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch o.Side {
|
|
|
|
case types.SideTypeBuy:
|
|
|
|
// limit buy taker, the order price is higher than the current best ask price
|
|
|
|
// the executed price is lower than the given price, so we will use less quote currency to buy the base asset.
|
|
|
|
amount := order.Price.Sub(order.AveragePrice).Mul(order.Quantity)
|
|
|
|
if amount.Sign() > 0 {
|
2022-09-01 05:48:33 +00:00
|
|
|
if err := m.account.UnlockBalance(m.Market.QuoteCurrency, amount); err != nil {
|
2022-08-18 07:09:46 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
2022-09-01 05:48:33 +00:00
|
|
|
m.EmitBalanceUpdate(m.account.Balances())
|
2022-08-18 07:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case types.SideTypeSell:
|
|
|
|
// limit sell taker, the order price is lower than the current best bid price
|
|
|
|
// the executed price is higher than the given price, so we will get more quote currency back
|
|
|
|
amount := order.AveragePrice.Sub(order.Price).Mul(order.Quantity)
|
|
|
|
if amount.Sign() > 0 {
|
2022-09-01 05:48:33 +00:00
|
|
|
m.account.AddBalance(m.Market.QuoteCurrency, amount)
|
|
|
|
m.EmitBalanceUpdate(m.account.Balances())
|
2022-08-18 07:09:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-18 07:43:09 +00:00
|
|
|
// update the order status
|
|
|
|
order2.Status = types.OrderStatusFilled
|
|
|
|
order2.ExecutedQuantity = order2.Quantity
|
|
|
|
order2.IsWorking = false
|
|
|
|
m.EmitOrderUpdate(order2)
|
|
|
|
|
2022-06-07 12:27:11 +00:00
|
|
|
// let the exchange emit the "FILLED" order update (we need the closed order)
|
|
|
|
// m.EmitOrderUpdate(order2)
|
2022-06-05 22:12:54 +00:00
|
|
|
return &order2, &trade, nil
|
2020-11-07 08:09:21 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 12:27:11 +00:00
|
|
|
// For limit maker orders (open status)
|
2020-11-07 08:09:21 +00:00
|
|
|
switch o.Side {
|
|
|
|
|
|
|
|
case types.SideTypeBuy:
|
2020-11-08 05:07:20 +00:00
|
|
|
m.mu.Lock()
|
2020-11-07 11:57:36 +00:00
|
|
|
m.bidOrders = append(m.bidOrders, order)
|
2020-11-08 05:07:20 +00:00
|
|
|
m.mu.Unlock()
|
2020-11-07 08:09:21 +00:00
|
|
|
|
|
|
|
case types.SideTypeSell:
|
2020-11-08 05:07:20 +00:00
|
|
|
m.mu.Lock()
|
2020-11-07 11:57:36 +00:00
|
|
|
m.askOrders = append(m.askOrders, order)
|
2020-11-08 05:07:20 +00:00
|
|
|
m.mu.Unlock()
|
2020-11-07 08:09:21 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 12:27:11 +00:00
|
|
|
m.EmitOrderUpdate(order) // emit order New status
|
2020-11-07 11:57:36 +00:00
|
|
|
return &order, nil, nil
|
2020-11-07 08:09:21 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 19:09:12 +00:00
|
|
|
func (m *SimplePriceMatching) executeTrade(trade types.Trade) {
|
|
|
|
var err error
|
2020-11-08 18:58:46 +00:00
|
|
|
// execute trade, update account balances
|
|
|
|
if trade.IsBuyer {
|
2022-09-01 05:48:33 +00:00
|
|
|
err = m.account.UseLockedBalance(m.Market.QuoteCurrency, trade.QuoteQuantity)
|
2022-06-11 19:45:47 +00:00
|
|
|
|
2022-09-01 11:02:18 +00:00
|
|
|
// all-in buy trade, we can only deduct the fee from the quote quantity and re-calculate the base quantity
|
|
|
|
switch trade.FeeCurrency {
|
|
|
|
case m.Market.QuoteCurrency:
|
2022-09-01 11:11:32 +00:00
|
|
|
m.account.AddBalance(m.Market.QuoteCurrency, trade.Fee.Neg())
|
|
|
|
m.account.AddBalance(m.Market.BaseCurrency, trade.Quantity)
|
2022-09-01 11:02:18 +00:00
|
|
|
case m.Market.BaseCurrency:
|
2022-09-01 11:11:32 +00:00
|
|
|
m.account.AddBalance(m.Market.BaseCurrency, trade.Quantity.Sub(trade.Fee))
|
2022-09-05 09:41:12 +00:00
|
|
|
default:
|
|
|
|
m.account.AddBalance(m.Market.BaseCurrency, trade.Quantity)
|
2022-06-11 19:55:02 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 11:11:32 +00:00
|
|
|
} else { // sell trade
|
2022-09-01 05:48:33 +00:00
|
|
|
err = m.account.UseLockedBalance(m.Market.BaseCurrency, trade.Quantity)
|
2022-06-11 19:45:47 +00:00
|
|
|
|
2022-09-01 11:02:18 +00:00
|
|
|
switch trade.FeeCurrency {
|
|
|
|
case m.Market.QuoteCurrency:
|
2022-09-01 11:11:32 +00:00
|
|
|
m.account.AddBalance(m.Market.QuoteCurrency, trade.QuoteQuantity.Sub(trade.Fee))
|
2022-09-01 11:02:18 +00:00
|
|
|
case m.Market.BaseCurrency:
|
2022-09-01 11:11:32 +00:00
|
|
|
m.account.AddBalance(m.Market.BaseCurrency, trade.Fee.Neg())
|
|
|
|
m.account.AddBalance(m.Market.QuoteCurrency, trade.QuoteQuantity)
|
2022-09-05 09:41:12 +00:00
|
|
|
default:
|
|
|
|
m.account.AddBalance(m.Market.QuoteCurrency, trade.QuoteQuantity)
|
2022-06-11 19:55:02 +00:00
|
|
|
}
|
2020-11-08 18:58:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 19:09:12 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrapf(err, "executeTrade exception, wanted to use more than the locked balance"))
|
2020-11-08 18:58:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 19:09:12 +00:00
|
|
|
m.EmitTradeUpdate(trade)
|
2022-09-01 05:48:33 +00:00
|
|
|
m.EmitBalanceUpdate(m.account.Balances())
|
2020-11-08 18:58:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 07:29:01 +00:00
|
|
|
func (m *SimplePriceMatching) getFeeRate(isMaker bool) (feeRate fixedpoint.Value) {
|
2020-11-08 04:47:14 +00:00
|
|
|
// BINANCE uses 0.1% for both maker and taker
|
|
|
|
// MAX uses 0.050% for maker and 0.15% for taker
|
2021-12-05 04:10:45 +00:00
|
|
|
if isMaker {
|
2022-09-01 05:48:33 +00:00
|
|
|
feeRate = m.account.MakerFeeRate
|
2021-12-05 04:10:45 +00:00
|
|
|
} else {
|
2022-09-01 05:48:33 +00:00
|
|
|
feeRate = m.account.TakerFeeRate
|
2020-11-08 04:47:14 +00:00
|
|
|
}
|
2022-06-28 07:29:01 +00:00
|
|
|
return feeRate
|
|
|
|
}
|
|
|
|
|
2022-06-28 09:43:51 +00:00
|
|
|
func (m *SimplePriceMatching) newTradeFromOrder(order *types.Order, isMaker bool, price fixedpoint.Value) types.Trade {
|
2022-06-28 07:29:01 +00:00
|
|
|
// BINANCE uses 0.1% for both maker and taker
|
|
|
|
// MAX uses 0.050% for maker and 0.15% for taker
|
2022-06-28 09:43:51 +00:00
|
|
|
var feeRate = m.getFeeRate(isMaker)
|
2022-08-18 02:54:06 +00:00
|
|
|
var quoteQuantity = order.Quantity.Mul(price)
|
2022-02-03 11:19:56 +00:00
|
|
|
var fee fixedpoint.Value
|
2020-11-08 13:52:44 +00:00
|
|
|
var feeCurrency string
|
|
|
|
|
2022-09-01 05:48:33 +00:00
|
|
|
if m.feeModeFunction != nil {
|
|
|
|
fee, feeCurrency = m.feeModeFunction(order, &m.Market, feeRate)
|
2022-06-11 19:55:02 +00:00
|
|
|
} else {
|
2022-09-01 05:48:33 +00:00
|
|
|
fee, feeCurrency = feeModeFunctionQuote(order, &m.Market, feeRate)
|
2020-11-08 13:52:44 +00:00
|
|
|
}
|
|
|
|
|
2022-05-11 07:04:11 +00:00
|
|
|
// update order time
|
2022-09-01 05:48:33 +00:00
|
|
|
order.UpdateTime = types.Time(m.currentTime)
|
2022-05-11 07:04:11 +00:00
|
|
|
|
2020-11-08 05:07:20 +00:00
|
|
|
var id = incTradeID()
|
2020-11-07 08:09:21 +00:00
|
|
|
return types.Trade{
|
2021-12-23 05:15:27 +00:00
|
|
|
ID: id,
|
2020-11-07 08:09:21 +00:00
|
|
|
OrderID: order.OrderID,
|
2022-06-28 09:43:51 +00:00
|
|
|
Exchange: types.ExchangeBacktest,
|
2022-01-29 17:37:24 +00:00
|
|
|
Price: price,
|
2020-11-07 08:09:21 +00:00
|
|
|
Quantity: order.Quantity,
|
2022-06-11 19:45:47 +00:00
|
|
|
QuoteQuantity: quoteQuantity,
|
2020-11-07 08:09:21 +00:00
|
|
|
Symbol: order.Symbol,
|
|
|
|
Side: order.Side,
|
|
|
|
IsBuyer: order.Side == types.SideTypeBuy,
|
|
|
|
IsMaker: isMaker,
|
2022-09-01 05:48:33 +00:00
|
|
|
Time: types.Time(m.currentTime),
|
2020-11-08 13:52:44 +00:00
|
|
|
Fee: fee,
|
|
|
|
FeeCurrency: feeCurrency,
|
2020-11-07 08:09:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-28 07:29:01 +00:00
|
|
|
// buyToPrice means price go up and the limit sell should be triggered
|
|
|
|
func (m *SimplePriceMatching) buyToPrice(price fixedpoint.Value) (closedOrders []types.Order, trades []types.Trade) {
|
2022-05-21 18:40:12 +00:00
|
|
|
klineMatchingLogger.Debugf("kline buy to price %s", price.String())
|
|
|
|
|
2022-06-27 11:48:14 +00:00
|
|
|
var bidOrders []types.Order
|
|
|
|
for _, o := range m.bidOrders {
|
|
|
|
switch o.Type {
|
2022-06-27 12:49:55 +00:00
|
|
|
|
|
|
|
case types.OrderTypeStopMarket:
|
2022-06-28 06:34:56 +00:00
|
|
|
// the price is still lower than the stop price, we will put the order back to the list
|
|
|
|
if price.Compare(o.StopPrice) < 0 {
|
2022-06-27 12:49:55 +00:00
|
|
|
// not triggering it, put it back
|
|
|
|
bidOrders = append(bidOrders, o)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
o.Type = types.OrderTypeMarket
|
|
|
|
o.ExecutedQuantity = o.Quantity
|
|
|
|
o.Price = price
|
|
|
|
o.Status = types.OrderStatusFilled
|
|
|
|
closedOrders = append(closedOrders, o)
|
|
|
|
|
2022-06-27 11:48:14 +00:00
|
|
|
case types.OrderTypeStopLimit:
|
2022-06-28 06:34:56 +00:00
|
|
|
// the price is still lower than the stop price, we will put the order back to the list
|
|
|
|
if price.Compare(o.StopPrice) < 0 {
|
2022-06-27 11:48:14 +00:00
|
|
|
bidOrders = append(bidOrders, o)
|
|
|
|
break
|
|
|
|
}
|
2020-11-08 19:17:02 +00:00
|
|
|
|
2022-06-27 11:48:14 +00:00
|
|
|
// convert this order to limit order
|
|
|
|
// we use value object here, so it's a copy
|
|
|
|
o.Type = types.OrderTypeLimit
|
|
|
|
|
|
|
|
// is it a taker order?
|
|
|
|
// higher than the current price, then it's a taker order
|
|
|
|
if o.Price.Compare(price) >= 0 {
|
2022-06-28 06:34:56 +00:00
|
|
|
// limit buy taker order, move it to the closed order
|
|
|
|
// we assume that we have no price slippage here, so the latest price will be the executed price
|
2022-08-18 07:26:09 +00:00
|
|
|
o.AveragePrice = price
|
2022-06-27 11:48:14 +00:00
|
|
|
o.ExecutedQuantity = o.Quantity
|
|
|
|
o.Status = types.OrderStatusFilled
|
|
|
|
closedOrders = append(closedOrders, o)
|
|
|
|
} else {
|
|
|
|
// keep it as a maker order
|
|
|
|
bidOrders = append(bidOrders, o)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
bidOrders = append(bidOrders, o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.bidOrders = bidOrders
|
|
|
|
|
|
|
|
var askOrders []types.Order
|
2020-11-07 08:09:21 +00:00
|
|
|
for _, o := range m.askOrders {
|
|
|
|
switch o.Type {
|
|
|
|
|
|
|
|
case types.OrderTypeStopMarket:
|
|
|
|
// should we trigger the order
|
2022-06-28 06:34:56 +00:00
|
|
|
if price.Compare(o.StopPrice) < 0 {
|
2020-11-08 19:17:02 +00:00
|
|
|
// not triggering it, put it back
|
|
|
|
askOrders = append(askOrders, o)
|
|
|
|
break
|
|
|
|
}
|
2020-11-07 08:09:21 +00:00
|
|
|
|
2020-11-08 19:17:02 +00:00
|
|
|
o.Type = types.OrderTypeMarket
|
|
|
|
o.ExecutedQuantity = o.Quantity
|
2022-02-03 11:19:56 +00:00
|
|
|
o.Price = price
|
2020-11-08 19:17:02 +00:00
|
|
|
o.Status = types.OrderStatusFilled
|
|
|
|
closedOrders = append(closedOrders, o)
|
2020-11-08 18:58:46 +00:00
|
|
|
|
2020-11-08 19:17:02 +00:00
|
|
|
case types.OrderTypeStopLimit:
|
|
|
|
// should we trigger the order?
|
2022-06-28 06:34:56 +00:00
|
|
|
if price.Compare(o.StopPrice) < 0 {
|
2020-11-07 08:09:21 +00:00
|
|
|
askOrders = append(askOrders, o)
|
2020-11-08 19:17:02 +00:00
|
|
|
break
|
2020-11-07 08:09:21 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 19:17:02 +00:00
|
|
|
o.Type = types.OrderTypeLimit
|
2020-11-07 08:09:21 +00:00
|
|
|
|
2020-11-08 19:17:02 +00:00
|
|
|
// is it a taker order?
|
2022-06-27 11:48:14 +00:00
|
|
|
// higher than the current price, then it's a taker order
|
2022-06-28 06:34:56 +00:00
|
|
|
if o.Price.Compare(price) <= 0 {
|
|
|
|
// limit sell order as taker, move it to the closed order
|
|
|
|
// we assume that we have no price slippage here, so the latest price will be the executed price
|
|
|
|
// TODO: simulate slippage here
|
2022-08-18 07:26:09 +00:00
|
|
|
o.AveragePrice = price
|
2020-11-08 19:17:02 +00:00
|
|
|
o.ExecutedQuantity = o.Quantity
|
|
|
|
o.Status = types.OrderStatusFilled
|
|
|
|
closedOrders = append(closedOrders, o)
|
2020-11-07 08:09:21 +00:00
|
|
|
} else {
|
2020-11-08 19:17:02 +00:00
|
|
|
// maker order
|
2020-11-07 08:09:21 +00:00
|
|
|
askOrders = append(askOrders, o)
|
|
|
|
}
|
|
|
|
|
2022-01-07 18:18:44 +00:00
|
|
|
case types.OrderTypeLimit, types.OrderTypeLimitMaker:
|
2022-02-03 11:19:56 +00:00
|
|
|
if price.Compare(o.Price) >= 0 {
|
2020-11-07 08:09:21 +00:00
|
|
|
o.ExecutedQuantity = o.Quantity
|
|
|
|
o.Status = types.OrderStatusFilled
|
|
|
|
closedOrders = append(closedOrders, o)
|
|
|
|
} else {
|
|
|
|
askOrders = append(askOrders, o)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
askOrders = append(askOrders, o)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
m.askOrders = askOrders
|
2022-09-01 05:48:33 +00:00
|
|
|
m.lastPrice = price
|
2020-11-07 08:09:21 +00:00
|
|
|
|
2022-05-21 18:40:12 +00:00
|
|
|
for i := range closedOrders {
|
|
|
|
o := closedOrders[i]
|
2022-08-18 07:26:09 +00:00
|
|
|
executedPrice := o.Price
|
|
|
|
if !o.AveragePrice.IsZero() {
|
|
|
|
executedPrice = o.AveragePrice
|
|
|
|
}
|
|
|
|
|
|
|
|
trade := m.newTradeFromOrder(&o, !isTakerOrder(o), executedPrice)
|
2022-03-16 07:01:19 +00:00
|
|
|
m.executeTrade(trade)
|
2022-05-21 18:40:12 +00:00
|
|
|
closedOrders[i] = o
|
2022-03-16 07:01:19 +00:00
|
|
|
|
|
|
|
trades = append(trades, trade)
|
|
|
|
|
|
|
|
m.EmitOrderUpdate(o)
|
2022-06-26 08:13:58 +00:00
|
|
|
|
|
|
|
m.closedOrders[o.OrderID] = o
|
2022-03-16 07:01:19 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 08:09:21 +00:00
|
|
|
return closedOrders, trades
|
|
|
|
}
|
|
|
|
|
2022-06-28 07:29:01 +00:00
|
|
|
// sellToPrice simulates the price trend in down direction.
|
2022-06-28 06:34:56 +00:00
|
|
|
// When price goes down, buy orders should be executed, and the stop orders should be triggered.
|
2022-06-28 07:29:01 +00:00
|
|
|
func (m *SimplePriceMatching) sellToPrice(price fixedpoint.Value) (closedOrders []types.Order, trades []types.Trade) {
|
2022-05-21 18:40:12 +00:00
|
|
|
klineMatchingLogger.Debugf("kline sell to price %s", price.String())
|
|
|
|
|
2022-06-28 06:34:56 +00:00
|
|
|
// in this section we handle --- the price goes lower, and we trigger the stop sell
|
2022-06-27 11:48:14 +00:00
|
|
|
var askOrders []types.Order
|
|
|
|
for _, o := range m.askOrders {
|
|
|
|
switch o.Type {
|
|
|
|
|
2022-06-27 12:49:55 +00:00
|
|
|
case types.OrderTypeStopMarket:
|
|
|
|
// should we trigger the order
|
2022-06-28 06:34:56 +00:00
|
|
|
if price.Compare(o.StopPrice) > 0 {
|
2022-06-27 12:49:55 +00:00
|
|
|
askOrders = append(askOrders, o)
|
2022-06-28 06:34:56 +00:00
|
|
|
break
|
2022-06-27 12:49:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 06:34:56 +00:00
|
|
|
o.Type = types.OrderTypeMarket
|
|
|
|
o.ExecutedQuantity = o.Quantity
|
|
|
|
o.Price = price
|
|
|
|
o.Status = types.OrderStatusFilled
|
|
|
|
closedOrders = append(closedOrders, o)
|
|
|
|
|
2022-06-27 11:48:14 +00:00
|
|
|
case types.OrderTypeStopLimit:
|
|
|
|
// if the price is lower than the stop price
|
|
|
|
// we should trigger the stop sell order
|
2022-06-28 06:34:56 +00:00
|
|
|
if price.Compare(o.StopPrice) > 0 {
|
2022-06-27 11:48:14 +00:00
|
|
|
askOrders = append(askOrders, o)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
o.Type = types.OrderTypeLimit
|
|
|
|
|
2022-06-28 06:34:56 +00:00
|
|
|
// handle TAKER SELL
|
2022-06-27 11:48:14 +00:00
|
|
|
// if the order price is lower than the current price
|
|
|
|
// it's a taker order
|
2022-06-28 06:34:56 +00:00
|
|
|
if o.Price.Compare(price) <= 0 {
|
2022-08-18 07:26:09 +00:00
|
|
|
o.AveragePrice = price
|
2022-06-27 11:48:14 +00:00
|
|
|
o.ExecutedQuantity = o.Quantity
|
|
|
|
o.Status = types.OrderStatusFilled
|
|
|
|
closedOrders = append(closedOrders, o)
|
|
|
|
} else {
|
|
|
|
askOrders = append(askOrders, o)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
askOrders = append(askOrders, o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.askOrders = askOrders
|
|
|
|
|
2020-11-07 08:09:21 +00:00
|
|
|
var bidOrders []types.Order
|
|
|
|
for _, o := range m.bidOrders {
|
|
|
|
switch o.Type {
|
|
|
|
|
|
|
|
case types.OrderTypeStopMarket:
|
2022-06-28 09:43:51 +00:00
|
|
|
// price goes down and if the stop price is still lower than the current price
|
|
|
|
// or the stop price is not touched
|
|
|
|
// then we should skip this order
|
|
|
|
if price.Compare(o.StopPrice) > 0 {
|
2020-11-07 08:09:21 +00:00
|
|
|
bidOrders = append(bidOrders, o)
|
2022-06-28 06:34:56 +00:00
|
|
|
break
|
2020-11-07 08:09:21 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 06:34:56 +00:00
|
|
|
o.Type = types.OrderTypeMarket
|
|
|
|
o.ExecutedQuantity = o.Quantity
|
|
|
|
o.Price = price
|
|
|
|
o.Status = types.OrderStatusFilled
|
|
|
|
closedOrders = append(closedOrders, o)
|
|
|
|
|
2020-11-07 08:09:21 +00:00
|
|
|
case types.OrderTypeStopLimit:
|
2022-06-28 09:43:51 +00:00
|
|
|
// price goes down and if the stop price is still lower than the current price
|
|
|
|
// or the stop price is not touched
|
|
|
|
// then we should skip this order
|
|
|
|
if price.Compare(o.StopPrice) > 0 {
|
2022-06-28 06:34:56 +00:00
|
|
|
bidOrders = append(bidOrders, o)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
o.Type = types.OrderTypeLimit
|
|
|
|
|
2022-08-18 07:26:09 +00:00
|
|
|
// handle TAKER order
|
2022-06-28 06:34:56 +00:00
|
|
|
if o.Price.Compare(price) >= 0 {
|
2022-08-18 07:26:09 +00:00
|
|
|
o.AveragePrice = price
|
2022-06-28 06:34:56 +00:00
|
|
|
o.ExecutedQuantity = o.Quantity
|
|
|
|
o.Status = types.OrderStatusFilled
|
|
|
|
closedOrders = append(closedOrders, o)
|
2020-11-07 08:09:21 +00:00
|
|
|
} else {
|
|
|
|
bidOrders = append(bidOrders, o)
|
|
|
|
}
|
|
|
|
|
2022-01-07 18:18:44 +00:00
|
|
|
case types.OrderTypeLimit, types.OrderTypeLimitMaker:
|
2022-06-28 06:34:56 +00:00
|
|
|
if price.Compare(o.Price) <= 0 {
|
2020-11-07 08:09:21 +00:00
|
|
|
o.ExecutedQuantity = o.Quantity
|
|
|
|
o.Status = types.OrderStatusFilled
|
|
|
|
closedOrders = append(closedOrders, o)
|
|
|
|
} else {
|
|
|
|
bidOrders = append(bidOrders, o)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
bidOrders = append(bidOrders, o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.bidOrders = bidOrders
|
2022-09-01 05:48:33 +00:00
|
|
|
m.lastPrice = price
|
2020-11-07 08:09:21 +00:00
|
|
|
|
2022-05-21 18:40:12 +00:00
|
|
|
for i := range closedOrders {
|
|
|
|
o := closedOrders[i]
|
2022-08-18 07:26:09 +00:00
|
|
|
executedPrice := o.Price
|
|
|
|
if !o.AveragePrice.IsZero() {
|
|
|
|
executedPrice = o.AveragePrice
|
|
|
|
}
|
|
|
|
|
|
|
|
trade := m.newTradeFromOrder(&o, !isTakerOrder(o), executedPrice)
|
2022-03-16 07:01:19 +00:00
|
|
|
m.executeTrade(trade)
|
2022-05-21 18:40:12 +00:00
|
|
|
closedOrders[i] = o
|
2022-03-16 07:01:19 +00:00
|
|
|
|
|
|
|
trades = append(trades, trade)
|
|
|
|
|
|
|
|
m.EmitOrderUpdate(o)
|
2022-06-26 08:13:58 +00:00
|
|
|
|
|
|
|
m.closedOrders[o.OrderID] = o
|
2022-03-16 07:01:19 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 08:09:21 +00:00
|
|
|
return closedOrders, trades
|
|
|
|
}
|
|
|
|
|
2022-06-26 08:13:58 +00:00
|
|
|
func (m *SimplePriceMatching) getOrder(orderID uint64) (types.Order, bool) {
|
|
|
|
if o, ok := m.closedOrders[orderID]; ok {
|
|
|
|
return o, true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range m.bidOrders {
|
|
|
|
if o.OrderID == orderID {
|
|
|
|
return o, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range m.askOrders {
|
|
|
|
if o.OrderID == orderID {
|
|
|
|
return o, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.Order{}, false
|
|
|
|
}
|
|
|
|
|
2020-11-10 06:18:04 +00:00
|
|
|
func (m *SimplePriceMatching) processKLine(kline types.KLine) {
|
2022-09-01 05:48:33 +00:00
|
|
|
m.currentTime = kline.EndTime.Time()
|
2022-07-03 18:38:42 +00:00
|
|
|
|
2022-09-01 05:48:33 +00:00
|
|
|
if m.lastPrice.IsZero() {
|
|
|
|
m.lastPrice = kline.Open
|
2022-05-24 13:55:43 +00:00
|
|
|
} else {
|
2022-09-01 05:48:33 +00:00
|
|
|
if m.lastPrice.Compare(kline.Open) > 0 {
|
2022-06-28 07:29:01 +00:00
|
|
|
m.sellToPrice(kline.Open)
|
2022-05-24 16:48:14 +00:00
|
|
|
} else {
|
2022-06-28 07:29:01 +00:00
|
|
|
m.buyToPrice(kline.Open)
|
2022-05-24 13:55:43 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-08 05:07:20 +00:00
|
|
|
|
2020-12-04 02:18:51 +00:00
|
|
|
switch kline.Direction() {
|
|
|
|
case types.DirectionDown:
|
2022-02-03 11:19:56 +00:00
|
|
|
if kline.High.Compare(kline.Open) >= 0 {
|
2022-06-28 07:29:01 +00:00
|
|
|
m.buyToPrice(kline.High)
|
2020-11-07 11:57:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 18:40:12 +00:00
|
|
|
// if low is lower than close, sell to low first, and then buy up to close
|
|
|
|
if kline.Low.Compare(kline.Close) < 0 {
|
2022-06-28 07:29:01 +00:00
|
|
|
m.sellToPrice(kline.Low)
|
|
|
|
m.buyToPrice(kline.Close)
|
2020-11-10 11:06:20 +00:00
|
|
|
} else {
|
2022-06-28 07:29:01 +00:00
|
|
|
m.sellToPrice(kline.Close)
|
2020-11-08 05:07:20 +00:00
|
|
|
}
|
2020-11-10 06:18:04 +00:00
|
|
|
|
2020-12-04 02:18:51 +00:00
|
|
|
case types.DirectionUp:
|
2022-02-03 11:19:56 +00:00
|
|
|
if kline.Low.Compare(kline.Open) <= 0 {
|
2022-06-28 07:29:01 +00:00
|
|
|
m.sellToPrice(kline.Low)
|
2020-11-08 05:07:20 +00:00
|
|
|
}
|
2020-11-07 11:57:36 +00:00
|
|
|
|
2022-02-03 11:19:56 +00:00
|
|
|
if kline.High.Compare(kline.Close) > 0 {
|
2022-06-28 07:29:01 +00:00
|
|
|
m.buyToPrice(kline.High)
|
|
|
|
m.sellToPrice(kline.Close)
|
2020-11-10 11:06:20 +00:00
|
|
|
} else {
|
2022-06-28 07:29:01 +00:00
|
|
|
m.buyToPrice(kline.Close)
|
2020-11-07 11:57:36 +00:00
|
|
|
}
|
2022-01-29 09:44:42 +00:00
|
|
|
default: // no trade up or down
|
2022-09-01 05:48:33 +00:00
|
|
|
if m.lastPrice.IsZero() {
|
2022-06-28 07:29:01 +00:00
|
|
|
m.buyToPrice(kline.Close)
|
2022-01-21 11:57:55 +00:00
|
|
|
}
|
2020-11-07 08:08:20 +00:00
|
|
|
}
|
2022-08-29 10:46:58 +00:00
|
|
|
|
2022-09-01 05:48:33 +00:00
|
|
|
m.lastKLine = kline
|
2020-11-07 08:08:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 18:58:46 +00:00
|
|
|
func (m *SimplePriceMatching) newOrder(o types.SubmitOrder, orderID uint64) types.Order {
|
2020-11-07 08:08:20 +00:00
|
|
|
return types.Order{
|
2020-11-08 05:07:20 +00:00
|
|
|
OrderID: orderID,
|
2020-11-07 08:08:20 +00:00
|
|
|
SubmitOrder: o,
|
2021-05-16 09:02:23 +00:00
|
|
|
Exchange: types.ExchangeBacktest,
|
2020-11-07 08:08:20 +00:00
|
|
|
Status: types.OrderStatusNew,
|
2022-02-03 11:19:56 +00:00
|
|
|
ExecutedQuantity: fixedpoint.Zero,
|
2020-11-10 06:18:04 +00:00
|
|
|
IsWorking: true,
|
2022-09-01 05:48:33 +00:00
|
|
|
CreationTime: types.Time(m.currentTime),
|
|
|
|
UpdateTime: types.Time(m.currentTime),
|
2022-06-28 07:29:01 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-28 09:43:51 +00:00
|
|
|
|
2022-08-18 07:26:09 +00:00
|
|
|
func isTakerOrder(o types.Order) bool {
|
|
|
|
if o.AveragePrice.IsZero() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
switch o.Side {
|
|
|
|
case types.SideTypeBuy:
|
|
|
|
return o.AveragePrice.Compare(o.Price) < 0
|
|
|
|
|
|
|
|
case types.SideTypeSell:
|
|
|
|
return o.AveragePrice.Compare(o.Price) > 0
|
|
|
|
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-06-28 09:43:51 +00:00
|
|
|
func isLimitTakerOrder(o types.SubmitOrder, currentPrice fixedpoint.Value) bool {
|
|
|
|
if currentPrice.IsZero() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return o.Type == types.OrderTypeLimit && ((o.Side == types.SideTypeBuy && o.Price.Compare(currentPrice) >= 0) ||
|
|
|
|
(o.Side == types.SideTypeSell && o.Price.Compare(currentPrice) <= 0))
|
|
|
|
}
|