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

Commit

Permalink
Maker only mode (#101), closes #34
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsaraf authored Feb 5, 2019
1 parent 8afec86 commit 081aa21
Show file tree
Hide file tree
Showing 9 changed files with 368 additions and 4 deletions.
33 changes: 33 additions & 0 deletions api/submitMode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package api

import (
"fmt"
)

// SubmitMode is the type of mode to be used when submitting orders to the trader bot
type SubmitMode uint8

// constants for the SubmitMode
const (
SubmitModeMakerOnly SubmitMode = iota
SubmitModeBoth
)

// ParseSubmitMode converts a string to the SubmitMode constant
func ParseSubmitMode(submitMode string) (SubmitMode, error) {
if submitMode == "maker_only" {
return SubmitModeMakerOnly, nil
} else if submitMode == "both" || submitMode == "" {
return SubmitModeBoth, nil
}

return SubmitModeBoth, fmt.Errorf("unable to parse submit mode: %s", submitMode)
}

func (s *SubmitMode) String() string {
if *s == SubmitModeMakerOnly {
return "maker_only"
}

return "both"
}
10 changes: 10 additions & 0 deletions cmd/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"time"

"github.com/interstellar/kelp/api"
"github.com/interstellar/kelp/model"
"github.com/interstellar/kelp/plugins"
"github.com/interstellar/kelp/support/logger"
Expand Down Expand Up @@ -181,6 +182,13 @@ func init() {
deleteAllOffersAndExit(l, botConfig, client, sdex)
}

submitMode, e := api.ParseSubmitMode(botConfig.SubmitMode)
if e != nil {
log.Println()
log.Println(e)
// we want to delete all the offers and exit here since there is something wrong with our setup
deleteAllOffersAndExit(l, botConfig, client, sdex)
}
timeController := plugins.MakeIntervalTimeController(
time.Duration(botConfig.TickIntervalSeconds)*time.Second,
botConfig.MaxTickDelayMillis,
Expand All @@ -189,11 +197,13 @@ func init() {
client,
botConfig.AssetBase(),
botConfig.AssetQuote(),
tradingPair,
botConfig.TradingAccount(),
sdex,
strat,
timeController,
botConfig.DeleteCyclesThreshold,
submitMode,
threadTracker,
fixedIterations,
dataKey,
Expand Down
3 changes: 3 additions & 0 deletions examples/configs/trader/sample_trader.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ TICK_INTERVAL_SECONDS=300
# randomized interval delay in millis
MAX_TICK_DELAY_MILLIS=0

# the mode to use when submitting - maker_only, both (default)
SUBMIT_MODE="both"

# how many continuous errors in each update cycle can the bot accept before it will delete all offers to protect its exposure.
# this number has to be exceeded for all the offers to be deleted and any error will be counted only once per update cycle.
# any time the bot completes a full run successfully this counter will be reset.
Expand Down
16 changes: 16 additions & 0 deletions model/orderbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,22 @@ func (o OrderBook) Bids() []Order {
return o.bids
}

// TopAsk returns the best ask in an orderbook
func (o OrderBook) TopAsk() *Order {
if len(o.Asks()) > 0 {
return &o.Asks()[0]
}
return nil
}

// TopBid returns the best bid in an orderbook
func (o OrderBook) TopBid() *Order {
if len(o.Bids()) > 0 {
return &o.Bids()[0]
}
return nil
}

// MakeOrderBook creates a new OrderBook from the asks and the bids
func MakeOrderBook(pair *TradingPair, asks []Order, bids []Order) *OrderBook {
return &OrderBook{
Expand Down
9 changes: 5 additions & 4 deletions plugins/sdex.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,8 @@ func (sdex *SDEX) _liabilities(asset horizon.Asset, otherAsset horizon.Asset) (*
return &liabilities, &pairLiabilities, nil
}

func (sdex *SDEX) pair2Assets() (baseAsset horizon.Asset, quoteAsset horizon.Asset, e error) {
// Assets returns the base and quote asset used by sdex
func (sdex *SDEX) Assets() (baseAsset horizon.Asset, quoteAsset horizon.Asset, e error) {
var ok bool
baseAsset, ok = sdex.assetMap[sdex.pair.Base]
if !ok {
Expand All @@ -652,7 +653,7 @@ func (sdex *SDEX) GetTradeHistory(pair model.TradingPair, maybeCursorStart inter
return nil, fmt.Errorf("passed in pair (%s) did not match sdex.pair (%s)", pair.String(), sdex.pair.String())
}

baseAsset, quoteAsset, e := sdex.pair2Assets()
baseAsset, quoteAsset, e := sdex.Assets()
if e != nil {
return nil, fmt.Errorf("error while converting pair to base and quote asset: %s", e)
}
Expand Down Expand Up @@ -796,7 +797,7 @@ func (sdex *SDEX) tradesPage2TradeHistoryResult(baseAsset horizon.Asset, quoteAs

// GetLatestTradeCursor impl.
func (sdex *SDEX) GetLatestTradeCursor() (interface{}, error) {
baseAsset, quoteAsset, e := sdex.pair2Assets()
baseAsset, quoteAsset, e := sdex.Assets()
if e != nil {
return nil, fmt.Errorf("error while convertig pair to base and quote asset: %s", e)
}
Expand All @@ -821,7 +822,7 @@ func (sdex *SDEX) GetOrderBook(pair *model.TradingPair, maxCount int32) (*model.
return nil, fmt.Errorf("unregistered trading pair (%s) cannot be converted to horizon.Assets, instance's pair: %s", pair.String(), sdex.pair.String())
}

baseAsset, quoteAsset, e := sdex.pair2Assets()
baseAsset, quoteAsset, e := sdex.Assets()
if e != nil {
return nil, fmt.Errorf("cannot get SDEX orderbook: %s", e)
}
Expand Down
Loading

0 comments on commit 081aa21

Please sign in to comment.