Skip to content

Commit

Permalink
change .cancel(id)'s search to a reverse-order search with early term…
Browse files Browse the repository at this point in the history
…ination on order[cancelCol]
  • Loading branch information
DrPaulBrewer committed Feb 16, 2016
1 parent 6d8a71e commit cec1422
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Emits: trade, trade-cleanup, after-trade

requires: options.idCol, options.qCol

optional: options.cancelCol (shortens search if a previous cancel/replace found)
MarketEngine.prototype.expire(ts)

requires: options.txCol, options.qCol
Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,19 @@ MarketEngine.prototype.cancel = function(id){
var i,l,order,countCancelled=0;
var idCol = this.o.idCol, qCol = this.o.qCol;
var a=this.a, trash=this.trash;
var cancelCol = this.o.cancelCol;
if (!a) return;
for(i=0,l=a.length;i<l;++i){
i = a.length;
while(i-->0){
order = a[i];
if (order && (id===order[idCol])){
countCancelled++;
if (trash)
trash.push(i);
if (qCol)
order[qCol]=0;
if (cancelCol && order[cancelCol])
i = 0; // skip because earlier cancel cancelled others
}
}
return countCancelled;
Expand Down
6 changes: 3 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ describe('MarketEngine', function(){
assert.ok(X.a[4][4]===0);
assert.ok(X.a[5][4]===1);
assert.ok(X.a[6][4]===5);
X.trash.should.eql([2,4]);
X.trash.should.eql([4,2]);
});

it('cancel(4) should still work if .trash does not exist', function(){
Expand Down Expand Up @@ -482,7 +482,7 @@ describe('MarketEngine', function(){
X.push([3000,6000,5,11]);
X.cancel(4);
X.expire(5000);
X.trash.should.eql([2,4,0,2,5]);
X.trash.should.eql([4,2,0,2,5]);
X.emptyTrash().should.eql([0,2,4,5]);
});

Expand All @@ -497,7 +497,7 @@ describe('MarketEngine', function(){
X.push([3000,6000,5,11]);
X.cancel(4);
X.expire(5000);
X.trash.should.eql([2,4,0,2,5]);
X.trash.should.eql([4,2,0,2,5]);
X.emptyTrash().should.eql([0,2,4,5]);
assert.ok(X.a.length===3);
X.a[0].slice(2).should.eql([1200,7000,5,3]);
Expand Down

0 comments on commit cec1422

Please sign in to comment.