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

Commit

Permalink
randomized delay via the MAX_TICK_DELAY_MILLIS config param (#89), cl…
Browse files Browse the repository at this point in the history
…oses #68
  • Loading branch information
nikhilsaraf authored Jan 11, 2019
1 parent de9b9ca commit 4b74aff
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
5 changes: 4 additions & 1 deletion cmd/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ func init() {
deleteAllOffersAndExit(botConfig, client, sdex)
}

timeController := plugins.MakeIntervalTimeController(time.Duration(botConfig.TickIntervalSeconds) * time.Second)
timeController := plugins.MakeIntervalTimeController(
time.Duration(botConfig.TickIntervalSeconds)*time.Second,
botConfig.MaxTickDelayMillis,
)
bot := trader.MakeBot(
client,
botConfig.AssetBase(),
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 @@ -17,6 +17,9 @@ ISSUER_B="GBMMZMK2DC4FFP4CAI6KCVNCQ7WLO5A7DQU7EC7WGHRDQBZB763X4OQI"

# how often you want the bot to run
TICK_INTERVAL_SECONDS=300
# randomized interval delay in millis
MAX_TICK_DELAY_MILLIS=0

# 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
27 changes: 23 additions & 4 deletions plugins/intervalTimeController.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@ package plugins

import (
"log"
"math/rand"
"time"

"github.com/interstellar/kelp/api"
)

// IntervalTimeController provides a standard time interval
type IntervalTimeController struct {
tickInterval time.Duration
tickInterval time.Duration
maxTickDelayMillis int64
randGen *rand.Rand
}

// MakeIntervalTimeController is a factory method
func MakeIntervalTimeController(tickInterval time.Duration) api.TimeController {
return &IntervalTimeController{tickInterval: tickInterval}
func MakeIntervalTimeController(tickInterval time.Duration, maxTickDelayMillis int64) api.TimeController {
randGen := rand.New(rand.NewSource(time.Now().UnixNano()))
return &IntervalTimeController{
tickInterval: tickInterval,
maxTickDelayMillis: maxTickDelayMillis,
randGen: randGen,
}
}

var _ api.TimeController = &IntervalTimeController{}
Expand All @@ -31,5 +39,16 @@ func (t *IntervalTimeController) ShouldUpdate(lastUpdateTime time.Time, currentU
func (t *IntervalTimeController) SleepTime(lastUpdateTime time.Time, currentUpdateTime time.Time) time.Duration {
// use time till now as opposed to currentUpdateTime because we want the start of the clock cycle to be synchronized
elapsedSinceUpdate := time.Since(lastUpdateTime)
return time.Duration(t.tickInterval.Nanoseconds() - elapsedSinceUpdate.Nanoseconds())
fixedDurationCatchup := time.Duration(t.tickInterval.Nanoseconds() - elapsedSinceUpdate.Nanoseconds())
randomizedDelayMillis := t.makeRandomDelay()

// if fixedDurationCatchup < 0 then we already have a built-in randomized delay because of the variable processing time consumed
return fixedDurationCatchup + randomizedDelayMillis
}

func (t *IntervalTimeController) makeRandomDelay() time.Duration {
if t.maxTickDelayMillis > 0 {
return time.Duration(t.randGen.Int63n(t.maxTickDelayMillis)) * time.Millisecond
}
return time.Duration(0) * time.Millisecond
}
1 change: 1 addition & 0 deletions trader/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type BotConfig struct {
AssetCodeB string `valid:"-" toml:"ASSET_CODE_B"`
IssuerB string `valid:"-" toml:"ISSUER_B"`
TickIntervalSeconds int32 `valid:"-" toml:"TICK_INTERVAL_SECONDS"`
MaxTickDelayMillis int64 `valid:"-" toml:"MAX_TICK_DELAY_MILLIS"`
DeleteCyclesThreshold int64 `valid:"-" toml:"DELETE_CYCLES_THRESHOLD"`
FillTrackerSleepMillis uint32 `valid:"-" toml:"FILL_TRACKER_SLEEP_MILLIS"`
HorizonURL string `valid:"-" toml:"HORIZON_URL"`
Expand Down

0 comments on commit 4b74aff

Please sign in to comment.