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

Commit

Permalink
guarantee fixed number of successful update() runs with --iter param, c…
Browse files Browse the repository at this point in the history
…loses #298
  • Loading branch information
nikhilsaraf committed Sep 23, 2019
1 parent f765ae0 commit 4845a62
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions trader/trader.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ func (t *Trader) Start() {
for {
currentUpdateTime := time.Now()
if lastUpdateTime.IsZero() || t.timeController.ShouldUpdate(lastUpdateTime, currentUpdateTime) {
t.update()
if t.fixedIterations != nil {
success := t.update()
if t.fixedIterations != nil && success {
*t.fixedIterations = *t.fixedIterations - 1
if *t.fixedIterations <= 0 {
log.Printf("finished requested number of iterations, waiting for all threads to finish...\n")
Expand Down Expand Up @@ -160,7 +160,8 @@ func (t *Trader) deleteAllOffers() {
}

// time to update the order book and possibly readjust the offers
func (t *Trader) update() {
// returns true if the update was successful, otherwise false
func (t *Trader) update() bool {
var e error
t.load()
t.loadExistingOffers()
Expand All @@ -181,15 +182,15 @@ func (t *Trader) update() {
if e != nil {
log.Println(e)
t.deleteAllOffers()
return
return false
}

// strategy has a chance to set any state it needs
e = t.strategy.PreUpdate(t.maxAssetA, t.maxAssetB, t.trustAssetA, t.trustAssetB)
if e != nil {
log.Println(e)
t.deleteAllOffers()
return
return false
}

// delete excess offers
Expand All @@ -201,7 +202,7 @@ func (t *Trader) update() {
if e != nil {
log.Println(e)
t.deleteAllOffers()
return
return false
}
}

Expand All @@ -215,7 +216,7 @@ func (t *Trader) update() {
if e != nil {
log.Println(e)
t.deleteAllOffers()
return
return false
}

opsOld, e := t.strategy.UpdateWithOps(t.buyingAOffers, t.sellingAOffers)
Expand All @@ -226,7 +227,7 @@ func (t *Trader) update() {
log.Printf("liabilities (force recomputed) after encountering an error after a call to UpdateWithOps\n")
t.sdex.IEIF().RecomputeAndLogCachedLiabilities(t.assetBase, t.assetQuote)
t.deleteAllOffers()
return
return false
}

ops := api.ConvertTM2Operation(opsOld)
Expand All @@ -235,7 +236,7 @@ func (t *Trader) update() {
if e != nil {
log.Printf("error in filter index %d: %s\n", i, e)
t.deleteAllOffers()
return
return false
}
}

Expand All @@ -245,19 +246,20 @@ func (t *Trader) update() {
if e != nil {
log.Println(e)
t.deleteAllOffers()
return
return false
}
}

e = t.strategy.PostUpdate()
if e != nil {
log.Println(e)
t.deleteAllOffers()
return
return false
}

// reset deleteCycles on every successful run
t.deleteCycles = 0
return true
}

func (t *Trader) load() {
Expand Down

0 comments on commit 4845a62

Please sign in to comment.