SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is there a problem with exchange.js

    I am in the middle of page 38, where I run "node nockmarket". I stopped the trading and took a look at the display feed and it appears that the exchange is not functioning properly.

    First, here is the output:

    Code:
    order { type: 'buys', price: 33, volume: 107 }
    
    33, 107
    
    
    
    order { type: 'buys', price: 37, volume: 106 }
    
    37, 106
    33, 107
    
    
    
    order { type: 'sells', price: 33, volume: 91 }
    
    37, 15
    33, 107
    
    
    TRADE 91 @ 37
    
    order { type: 'sells', price: 34, volume: 103 }
    
    33, 19
    
    
    TRADE 15 @ 37
    TRADE 88 @ 33
    
    order { type: 'sells', price: 37, volume: 105 }
    
            | 37, 105
    33, 19
    
    
    
    order { type: 'sells', price: 32, volume: 87 }
    
            | 37, 105
            | 32, 68
    
    
    TRADE 19 @ 33
    
    order { type: 'buys', price: 35, volume: 97 }
    
            | 37, 76
    
    
    TRADE 68 @ 32
    TRADE 29 @ 37
    
    order { type: 'buys', price: 38, volume: 119 }
    
    38, 43
    
    
    TRADE 76 @ 37
    
    order { type: 'sells', price: 36, volume: 115 }
    
            | 36, 72
    
    
    TRADE 43 @ 38
    Look at the third order. It is to sell 103 shares at 34. But the trade is 15 @ 37 and 88 @ 33! Why is it trading 88 shares at 33. The bid is 33 and the ask is 34, so there should be no trade at 33.

    Then look at the seventh trade. It is to buy 97 shares at 35. But the resulting trade is 68 @ 32 and 29 @ 37! If the bid is 35 and the ask is 37, again, there should be no sale at 37!

    I have compared my code to the code in the repository and a diff shows there is no difference in my code from the repo.

    Has anybody taken the time to confirm that the trading engine is working as it should be?

    I am about to move on to the tests. I wonder if there is no test testing for this condition and thus things look fine.

  2. #2
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tinjaw View Post
    Look at the third order.
    That should have been *fourth* order.

  3. #3
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Written Test

    I have modified exchange.test.js to test for the case I am seeing. And it fails. Can somebody confirm that it fails for them too?

    I will begin looking through the code to find where the logic error is in my/the code.

    exchange.test.js
    Code:
    suite('exchange', function() {
      test('buy should add a BUY nockmarket order', function(done) {
        exchangeData = exchange.buy(33, 107, exchangeData);
        exchangeData.buys.volumes[33].should.eql(107);
        done();
      });
      
      test('buy should add a BUY nockmarket order', function(done) {
        exchangeData = exchange.buy(37, 106, exchangeData);
        exchangeData.buys.volumes[37].should.eql(106);
        done();
      });
      
      test('sell should produce trades', function(done) {
        exchangeData = exchange.sell(33, 91, exchangeData);
        exchangeData.trades[0].price.should.eql(37);
        exchangeData.trades[0].volume.should.eql(91);
        exchangeData.buys.volumes[37].should.eql(15);
        exchangeData.buys.volumes[33].should.eql(107);
        done();
      });
    
      test('sell should produce trades', function(done) {
        exchangeData = exchange.sell(34, 103, exchangeData);
        exchangeData.trades[0].price.should.eql(37);
        exchangeData.trades[0].volume.should.eql(15);
        exchangeData.buys.volumes[33].should.eql(107);
        exchangeData.sells.volumes[34].should.eql(88);
        done();
      });
    });

  4. #4
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looking at order() in exchange.js, I believe I found the issue. The problem is the while loop isn't checking isTrade(). Although order() checks isTrade(), it does so only once -- before the while loop.

    This means that once it sells shares, if there are any left to sell it just sells at the next bid in the BUY book. It doesn't check isTrade() to determine if the prices are proper for the trade.

  5. #5
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    patch

    It appears that if you change the while loop to:

    Code:
    while (remainingVolume > 0 && Object.keys(oppBook.volumes).length > 0 && isTrade()) {
    then the behavior is as I expected -- and my test passes.

    Can somebody please confirm the stuff I have posted in this thread?

    Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •