Skip to content

Commit

Permalink
add scenarios, tests for order-triggers-order and debug
Browse files Browse the repository at this point in the history
  • Loading branch information
DrPaulBrewer committed Feb 22, 2016
1 parent 8f0b128 commit 09454c2
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 5 deletions.
8 changes: 5 additions & 3 deletions index.js
Expand Up @@ -92,14 +92,14 @@ var Market = function(options){
});
}
});
this.on('trade', this.tradeTrigger);
this.on('trade-cleanup', function(tradespec){
var matches;
while (Math.max.apply(Math,(matches = this.stopsMatch(tradespec)))>0){
this.emit('stops', tradespec.t, matches);
}
this.tradeTrigger(tradespec);
this.cleanup();
});
this.on('trade-cleanup', this.cleanup);
this.on('stops', this.stopsTrigger);
this.clear();
};
Expand Down Expand Up @@ -178,7 +178,9 @@ Market.prototype.triggerOrderToInbox = function(j,q,t){
if ((myorder[trigSliceBegin]>0) ||
(myorder[trigSliceBegin+1]>0) ||
(myorder[trigSliceBegin+2]>0) ||
(myorder[trigSliceBegin+3]>0)){
(myorder[trigSliceBegin+3]>0) ||
(myorder[trigSliceBegin+4]>0) ||
(myorder[trigSliceBegin+5]>0)){
trigorder = [];
for(ii=0,ll=trigSliceEnd-2;ii<ll;++ii)
trigorder[ii] = 0;
Expand Down
66 changes: 66 additions & 0 deletions test/index.js
Expand Up @@ -681,5 +681,71 @@ describe('Market', function(){
assert.ok(AM.book.sellStop.idx.length === 0);
});
});

describe('buy triggers another buy', function(){
var scenario = [
orders.id2_sell_1_at_95,
orders.id2_sell_1_at_105,
orders.id2_sell_1_at_115,
orders.id4_buy_1_at_110_triggers_buy_1_at_120
];
var AM = new Market({});
var trades=[], stops = [];
AM.on('trade', function(tradespec){ trades.push(tradespec) });
AM.on('stops', function(t, matches){ stops.push(matches) });
process(AM, scenario);
it('should have empty inbox', function(){
assert.ok(AM.inbox.length === 0);
});
it('should not execute any stops', function(){
assert.ok(stops.length === 0);
});
it('should generate 2 trades', function(){
assert.equal(trades.length, 2);
});
it('should generate trade prices of 95 and 105', function(){
assert.equal(trades[0].prices[0], 95);
assert.equal(trades[1].prices[0], 105);
});
it('should have empty buy book', function(){
assert.ok(AM.book.buy.idx.length === 0);
});
it('should have sell book with one order at 115', function(){
assert.ok(AM.book.sell.idx.length === 1);
assert.ok(AM.book.sell.val(0) === 115);
});
});

describe('buy triggers OCO bracket sell-to-close', function(){
var scenario = [
orders.id2_sell_1_at_95,
orders.id2_sell_1_at_105,
orders.id2_sell_1_at_115,
orders.id4_buy_1_at_110_triggers_sell_1_at_120_stoplimit_100
];
var AM = new Market({});
var trades=[], stops = [];
AM.on('trade', function(tradespec){ trades.push(tradespec) });
AM.on('stops', function(t, matches){ stops.push(matches) });
process(AM, scenario);
it('should have empty inbox', function(){
assert.ok(AM.inbox.length === 0);
});
it('should not execute any stops', function(){
assert.ok(stops.length === 0);
});
it('should generate 1 trade', function(){
assert.equal(trades.length, 1);
});
it('should have empty buy book', function(){
assert.equal(AM.book.buy.idx.length, 0);
});
it('should have 3 orders in sell book', function(){
assert.equal(AM.book.sell.idx.length, 3);
});
it('should have 120 OCO stoplimit 100 as last sell book order', function(){
omit2(AM.book.sell.idxdata(2)).should.deepEqual(orders.id4_sell_1_at_120_OCO_100);
});
});

});
57 changes: 55 additions & 2 deletions test/orders.js
Expand Up @@ -150,7 +150,7 @@ module.exports = {
id3_buystop_1_at_112: [
0, // t
0, // tx
2, // usernum
3, // usernum
0, // cancelreplace
1, // quantity
0, // buy price
Expand Down Expand Up @@ -235,8 +235,61 @@ module.exports = {
0, // trgger sell price
0,0, // trigger buy stop
0,0 // trigger sell stop
]
],

id4_buy_1_at_110_triggers_buy_1_at_120: [
0, // t
0, // tx
4, // usernum
1, // cancelreplace
1, // quantity
110, // buy price
0, // sell price
0, // buy stop
0, // limit
0, // sell stop
0, // limit
120, // trigger buy price
0, // trgger sell price
0,0, // trigger buy stop
0,0 // trigger sell stop
],

id4_buy_1_at_110_triggers_sell_1_at_120_stoplimit_100: [
0, // t
0, // tx
4, // usernum
0, // cancelreplace
1, // quantity
110, // buy price
0, // sell price
0, // buy stop
0, // limit
0, // sell stop
0, // limit
0, // trigger buy price
120, // trgger sell price
0,0, // trigger buy stop
100,100 // trigger sell stop
],

id4_sell_1_at_120_OCO_100: [
0, // t
0, // tx
4, // usernum
0, // cancelreplace
1, // quantity
0, // buy price
120, // sell price
0, // buy stop
0, // limit
100, // sell stop
100, // limit
0, // trigger buy price
0, // trgger sell price
0,0, // trigger buy stop
0,0 // trigger sell stop
]

}

Expand Down

0 comments on commit 09454c2

Please sign in to comment.