Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
Centralized maker only mode (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsaraf authored Mar 14, 2019
1 parent bb9813b commit a9ab034
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 36 deletions.
8 changes: 7 additions & 1 deletion api/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,18 @@ type Constrainable interface {
GetOrderConstraints(pair *model.TradingPair) *model.OrderConstraints
}

// OrderbookFetcher extracts out the method that should go into ExchangeShim for now
type OrderbookFetcher interface {
GetOrderBook(pair *model.TradingPair, maxCount int32) (*model.OrderBook, error)
}

// TradeAPI is the interface we use as a generic API for trading on any crypto exchange
type TradeAPI interface {
GetAssetConverter() *model.AssetConverter

Constrainable

GetOrderBook(pair *model.TradingPair, maxCount int32) (*model.OrderBook, error)
OrderbookFetcher

GetTrades(pair *model.TradingPair, maybeCursor interface{}) (*TradesResult, error)

Expand Down Expand Up @@ -207,4 +212,5 @@ type ExchangeShim interface {
GetBalanceHack(asset horizon.Asset) (*Balance, error)
LoadOffersHack() ([]horizon.Offer, error)
Constrainable
OrderbookFetcher
}
6 changes: 0 additions & 6 deletions cmd/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,6 @@ func init() {
// we want to delete all the offers and exit here since there is something wrong with our setup
deleteAllOffersAndExit(l, botConfig, client, sdex, exchangeShim)
}
if !isTradingSdex && submitMode != api.SubmitModeBoth {
log.Println()
log.Println("cannot run on a non-SDEX exchange with SUBMIT_MODE set to something other than \"both\"")
// we want to delete all the offers and exit here since there is something wrong with our setup
deleteAllOffersAndExit(l, botConfig, client, sdex, exchangeShim)
}
if !isTradingSdex && botConfig.FillTrackerSleepMillis != 0 {
log.Println()
log.Println("cannot run on a non-SDEX exchange with FILL_TRACKER_SLEEP_MILLIS set to something other than 0")
Expand Down
5 changes: 5 additions & 0 deletions plugins/batchedExchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ func (b BatchedExchange) GetOrderConstraints(pair *model.TradingPair) *model.Ord
return b.inner.GetOrderConstraints(pair)
}

// GetOrderBook impl
func (b BatchedExchange) GetOrderBook(pair *model.TradingPair, maxCount int32) (*model.OrderBook, error) {
return b.inner.GetOrderBook(pair, maxCount)
}

// SubmitOps performs any finalization or submission step needed by the exchange
func (b BatchedExchange) SubmitOps(ops []build.TransactionMutator, asyncCallback func(hash string, e error)) error {
var e error
Expand Down
50 changes: 26 additions & 24 deletions plugins/sdexMakerFilter.go → plugins/makerModeFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,30 @@ import (
"github.com/stellar/kelp/support/utils"
)

// MakeSdexMakerModeFilter makes a submit filter based on the passed in submitMode
func MakeSdexMakerModeFilter(submitMode api.SubmitMode, sdex *SDEX, tradingPair *model.TradingPair) SubmitFilter {
type makerModeFilter struct {
tradingPair *model.TradingPair
exchangeShim api.ExchangeShim
sdex *SDEX
}

// MakeFilterMakerMode makes a submit filter based on the passed in submitMode
func MakeFilterMakerMode(submitMode api.SubmitMode, exchangeShim api.ExchangeShim, sdex *SDEX, tradingPair *model.TradingPair) SubmitFilter {
if submitMode == api.SubmitModeMakerOnly {
return &sdexMakerFilter{
tradingPair: tradingPair,
sdex: sdex,
return &makerModeFilter{
tradingPair: tradingPair,
exchangeShim: exchangeShim,
sdex: sdex,
}
}
return nil
}

type sdexMakerFilter struct {
tradingPair *model.TradingPair
sdex *SDEX
}

var _ SubmitFilter = &sdexMakerFilter{}
var _ SubmitFilter = &makerModeFilter{}

func (f *sdexMakerFilter) Apply(ops []build.TransactionMutator, sellingOffers []horizon.Offer, buyingOffers []horizon.Offer) ([]build.TransactionMutator, error) {
ob, e := f.sdex.GetOrderBook(f.tradingPair, math.MaxInt32)
func (f *makerModeFilter) Apply(ops []build.TransactionMutator, sellingOffers []horizon.Offer, buyingOffers []horizon.Offer) ([]build.TransactionMutator, error) {
ob, e := f.exchangeShim.GetOrderBook(f.tradingPair, 50)
if e != nil {
return nil, fmt.Errorf("could not fetch SDEX orderbook: %s", e)
return nil, fmt.Errorf("could not fetch orderbook: %s", e)
}

ops, e = f.filterOps(ops, ob, sellingOffers, buyingOffers)
Expand All @@ -54,8 +56,8 @@ func isNewLevel(lastPrice *model.Number, priceNumber *model.Number, isSell bool)
return false
}

func (f *sdexMakerFilter) collateOffers(traderOffers []horizon.Offer, isSell bool) ([]api.Level, error) {
oc := f.sdex.GetOrderConstraints(f.tradingPair)
func (f *makerModeFilter) collateOffers(traderOffers []horizon.Offer, isSell bool) ([]api.Level, error) {
oc := f.exchangeShim.GetOrderConstraints(f.tradingPair)

levels := []api.Level{}
var lastPrice *model.Number
Expand Down Expand Up @@ -84,7 +86,7 @@ func (f *sdexMakerFilter) collateOffers(traderOffers []horizon.Offer, isSell boo
return levels, nil
}

func (f *sdexMakerFilter) topOrderPriceExcludingTrader(obSide []model.Order, traderOffers []horizon.Offer, isSell bool) (*model.Number, error) {
func (f *makerModeFilter) topOrderPriceExcludingTrader(obSide []model.Order, traderOffers []horizon.Offer, isSell bool) (*model.Number, error) {
traderLevels, e := f.collateOffers(traderOffers, isSell)
if e != nil {
return nil, fmt.Errorf("unable to collate offers: %s", e)
Expand All @@ -107,15 +109,15 @@ func (f *sdexMakerFilter) topOrderPriceExcludingTrader(obSide []model.Order, tra
return nil, nil
}

func (f *sdexMakerFilter) filterOps(
func (f *makerModeFilter) filterOps(
ops []build.TransactionMutator,
ob *model.OrderBook,
sellingOffers []horizon.Offer,
buyingOffers []horizon.Offer,
) ([]build.TransactionMutator, error) {
baseAsset, quoteAsset, e := f.sdex.Assets()
if e != nil {
return nil, fmt.Errorf("could not get sdex assets: %s", e)
return nil, fmt.Errorf("could not get assets: %s", e)
}

topBidPrice, e := f.topOrderPriceExcludingTrader(ob.Bids(), buyingOffers, false)
Expand Down Expand Up @@ -167,11 +169,11 @@ func (f *sdexMakerFilter) filterOps(
}
}
}
log.Printf("dropped %d, transformed %d, kept %d ops in sdexMakerFilter from original %d ops, len(filteredOps) = %d\n", numDropped, numTransformed, numKeep, len(ops), len(filteredOps))
log.Printf("makerModeFilter: dropped %d, transformed %d, kept %d ops from original %d ops, len(filteredOps) = %d\n", numDropped, numTransformed, numKeep, len(ops), len(filteredOps))
return filteredOps, nil
}

func (f *sdexMakerFilter) transformOfferMakerMode(
func (f *makerModeFilter) transformOfferMakerMode(
baseAsset horizon.Asset,
quoteAsset horizon.Asset,
topBidPrice *model.Number,
Expand All @@ -193,10 +195,10 @@ func (f *sdexMakerFilter) transformOfferMakerMode(
if !isSell && topAskPrice != nil {
// invert price when buying
keep = 1/sellPrice < topAskPrice.AsFloat()
log.Printf("sdexMakerFilter: buying, keep = (op price) %.7f < %.7f (topAskPrice): keep = %v", 1/sellPrice, topAskPrice.AsFloat(), keep)
log.Printf("makerModeFilter: buying, keep = (op price) %.7f < %.7f (topAskPrice): keep = %v", 1/sellPrice, topAskPrice.AsFloat(), keep)
} else if isSell && topBidPrice != nil {
keep = sellPrice > topBidPrice.AsFloat()
log.Printf("sdexMakerFilter: selling, keep = (op price) %.7f > %.7f (topBidPrice): keep = %v", sellPrice, topBidPrice.AsFloat(), keep)
log.Printf("makerModeFilter: selling, keep = (op price) %.7f > %.7f (topBidPrice): keep = %v", sellPrice, topBidPrice.AsFloat(), keep)
} else {
price := sellPrice
action := "selling"
Expand All @@ -205,7 +207,7 @@ func (f *sdexMakerFilter) transformOfferMakerMode(
action = " buying"
}
keep = true
log.Printf("sdexMakerFilter: %s, no market (op price = %.7f): keep = %v", action, price, keep)
log.Printf("makerModeFilter: %s, no market (op price = %.7f): keep = %v", action, price, keep)
}

if keep {
Expand Down
6 changes: 3 additions & 3 deletions plugins/orderConstraintsFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type orderConstraintsFilter struct {

var _ SubmitFilter = &orderConstraintsFilter{}

// MakeOrderConstraintsFilter makes a submit filter based on the passed in orderConstraints
func MakeOrderConstraintsFilter(
// MakeFilterOrderConstraints makes a submit filter based on the passed in orderConstraints
func MakeFilterOrderConstraints(
oc *model.OrderConstraints,
baseAsset horizon.Asset,
quoteAsset horizon.Asset,
Expand Down Expand Up @@ -83,7 +83,7 @@ func (f *orderConstraintsFilter) Apply(
}
}

log.Printf("dropped %d, kept %d ops in orderConstraintsFilter from original %d ops, len(filteredOps) = %d\n", numDropped, numKeep, len(ops), len(filteredOps))
log.Printf("orderConstraintsFilter: dropped %d, kept %d ops from original %d ops, len(filteredOps) = %d\n", numDropped, numKeep, len(ops), len(filteredOps))
return filteredOps, nil
}

Expand Down
4 changes: 2 additions & 2 deletions trader/trader.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ func MakeBot(
submitFilters := []plugins.SubmitFilter{}

oc := exchangeShim.GetOrderConstraints(tradingPair)
orderConstraintsFilter := plugins.MakeOrderConstraintsFilter(oc, assetBase, assetQuote)
orderConstraintsFilter := plugins.MakeFilterOrderConstraints(oc, assetBase, assetQuote)
submitFilters = append(submitFilters, orderConstraintsFilter)

sdexSubmitFilter := plugins.MakeSdexMakerModeFilter(submitMode, sdex, tradingPair)
sdexSubmitFilter := plugins.MakeFilterMakerMode(submitMode, exchangeShim, sdex, tradingPair)
if sdexSubmitFilter != nil {
submitFilters = append(submitFilters, sdexSubmitFilter)
}
Expand Down

0 comments on commit a9ab034

Please sign in to comment.