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

Commit

Permalink
Non-native operational buffer to prevent op_underfunded errors (#64), f…
Browse files Browse the repository at this point in the history
…ixes #63
  • Loading branch information
nikhilsaraf authored Nov 26, 2018
1 parent db5615e commit e6bebee
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 26 deletions.
1 change: 1 addition & 0 deletions cmd/terminate.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func init() {
utils.ParseNetwork(configFile.HorizonURL),
multithreading.MakeThreadTracker(),
-1, // not needed here
-1, // not needed here
false,
)
terminator := terminator.MakeTerminator(client, sdex, *configFile.TradingAccount, configFile.TickIntervalSeconds, configFile.AllowInactiveMinutes)
Expand Down
28 changes: 22 additions & 6 deletions cmd/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,34 @@ func init() {
stratConfigPath := tradeCmd.Flags().StringP("stratConf", "f", "", "strategy config file path")
// long-only flags
operationalBuffer := tradeCmd.Flags().Float64("operationalBuffer", 20, "buffer of native XLM to maintain beyond minimum account balance requirement")
operationalBufferNonNativePct := tradeCmd.Flags().Float64("operationalBufferNonNativePct", 0.001, "buffer of non-native assets to maintain as a percentage (0.001 = 0.1%)")
simMode := tradeCmd.Flags().Bool("sim", false, "simulate the bot's actions without placing any trades")
logPrefix := tradeCmd.Flags().StringP("log", "l", "", "log to a file (and stdout) with this prefix for the filename")
fixedIterations := tradeCmd.Flags().Uint64("iter", 0, "only run the bot for the first N iterations (defaults value 0 runs unboundedly)")

requiredFlag("botConf")
requiredFlag("strategy")
hiddenFlag("operationalBuffer")
hiddenFlag("operationalBufferNonNativePct")
tradeCmd.Flags().SortFlags = false

validateCliParams := func() {
if *operationalBuffer < 0 {
panic(fmt.Sprintf("invalid operationalBuffer argument, must be non-negative: %f", *operationalBuffer))
}

if *operationalBufferNonNativePct < 0 || *operationalBufferNonNativePct > 1 {
panic(fmt.Sprintf("invalid operationalBufferNonNativePct argument, must be between 0 and 1 inclusive: %f", *operationalBufferNonNativePct))
}

if *fixedIterations == 0 {
fixedIterations = nil
log.Printf("will run unbounded iterations\n")
} else {
log.Printf("will run only %d update iterations\n", *fixedIterations)
}
}

tradeCmd.Run = func(ccmd *cobra.Command, args []string) {
var botConfig trader.BotConfig
e := config.Read(*botConfigPath, &botConfig)
Expand Down Expand Up @@ -99,12 +118,8 @@ func init() {
}
log.Println(startupMessage)

if *fixedIterations == 0 {
fixedIterations = nil
log.Printf("will run unbounded iterations\n")
} else {
log.Printf("will run only %d update iterations\n", *fixedIterations)
}
// now that we've got the basic messages logged, validate the cli params
validateCliParams()

// only log botConfig file here so it can be included in the log file
utils.LogConfig(botConfig)
Expand All @@ -131,6 +146,7 @@ func init() {
utils.ParseNetwork(botConfig.HorizonURL),
threadTracker,
*operationalBuffer,
*operationalBufferNonNativePct,
*simMode,
)

Expand Down
43 changes: 23 additions & 20 deletions plugins/sdex.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ const maxLumenTrust = math.MaxFloat64

// SDEX helps with building and submitting transactions to the Stellar network
type SDEX struct {
API *horizon.Client
SourceAccount string
TradingAccount string
SourceSeed string
TradingSeed string
Network build.Network
threadTracker *multithreading.ThreadTracker
operationalBuffer float64
simMode bool
API *horizon.Client
SourceAccount string
TradingAccount string
SourceSeed string
TradingSeed string
Network build.Network
threadTracker *multithreading.ThreadTracker
operationalBuffer float64
operationalBufferNonNativePct float64
simMode bool

// uninitialized
seqNum uint64
Expand Down Expand Up @@ -63,18 +64,20 @@ func MakeSDEX(
network build.Network,
threadTracker *multithreading.ThreadTracker,
operationalBuffer float64,
operationalBufferNonNativePct float64,
simMode bool,
) *SDEX {
sdex := &SDEX{
API: api,
SourceSeed: sourceSeed,
TradingSeed: tradingSeed,
SourceAccount: sourceAccount,
TradingAccount: tradingAccount,
Network: network,
threadTracker: threadTracker,
operationalBuffer: operationalBuffer,
simMode: simMode,
API: api,
SourceSeed: sourceSeed,
TradingSeed: tradingSeed,
SourceAccount: sourceAccount,
TradingAccount: tradingAccount,
Network: network,
threadTracker: threadTracker,
operationalBuffer: operationalBuffer,
operationalBufferNonNativePct: operationalBufferNonNativePct,
simMode: simMode,
}

log.Printf("Using network passphrase: %s\n", sdex.Network.Passphrase)
Expand Down Expand Up @@ -179,7 +182,7 @@ func (sdex *SDEX) assetBalance(asset horizon.Asset) (float64, float64, float64,
return b, t, r, e
}

// assetBalance returns asset balance, asset trust limit, reserve balance (zero for non-XLM), error
// assetBalance returns asset balance, asset trust limit, reserve balance, error
func (sdex *SDEX) _assetBalance(asset horizon.Asset) (float64, float64, float64, error) {
account, err := sdex.API.LoadAccount(sdex.TradingAccount)
if err != nil {
Expand All @@ -200,7 +203,7 @@ func (sdex *SDEX) _assetBalance(asset horizon.Asset) (float64, float64, float64,
if e != nil {
return -1, -1, -1, fmt.Errorf("error: cannot parse trust limit: %s", e)
}
return b, t, 0, nil
return b, t, b * sdex.operationalBufferNonNativePct, nil
}
}
return -1, -1, -1, errors.New("could not find a balance for the asset passed in")
Expand Down

0 comments on commit e6bebee

Please sign in to comment.