Skip to content

Commit

Permalink
profit and loss invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
AlgoTrader committed Sep 4, 2012
1 parent bf82fc6 commit a5fd41f
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 10 deletions.
6 changes: 3 additions & 3 deletions lib/emulator_core.js
Expand Up @@ -192,15 +192,15 @@ Emulator.prototype.handleGetMarketProfitAndLoss = function(req, res, cb) {
currencyCode : 'USD', // get from prices later, for now USD
includesSettledBets : false,
includesBspBets : false,
marketId : req.request.marketId || '0',
marketId : req.request.marketID || '0',
marketName : "Emulated Market " + (req.request.marketId || '0'),
marketStatus : "INVALID",
unit : "N/A"
};
setResponseHeader(req.request, res.response);

// check marketId
var marketId = req.request.marketId;
var marketId = req.request.marketID;
if (!marketId || !self.markets[marketId]) {
// no market in emulator
res.xmlResponseBody = encoder.encode(res.action, res.response);
Expand All @@ -210,7 +210,7 @@ Emulator.prototype.handleGetMarketProfitAndLoss = function(req, res, cb) {

// handle invocation
var market = self.markets[marketId];
market.handleGetCurrentBets(req, res, function(err, res) {
market.handleGetMarketProfitAndLoss(req, res, function(err, res) {
// serialize response
res.xmlResponseBody = encoder.encode(res.action, res.response);
cb(null, res);
Expand Down
25 changes: 20 additions & 5 deletions lib/emulator_encoder.js
Expand Up @@ -370,19 +370,26 @@ function encodeGetMUBets(doc, result) {
// getMarketProfitAndLoss
function encodeGetMarketProfitAndLoss(doc, result) {
// annotations
if(!result.annotations) {
// bets
doc.startElement('annotations').writeAttribute('xsi:null', '1');
doc.endElement();
} else {
doc.startElement('annotations').writeAttribute('xsi:type', 'n2:ArrayOfProfitAndLoss');
for ( var i = 0; i < result.annotations.length; ++i) {
var ann = result.annotations[i];
doc.startElement('n2:ProfitAndLoss').writeAttribute('xsi:type', 'n2:ProfitAndLoss');
// futureIfWin
doc.startElement('futureIfWin').writeAttribute('xsi:type', 'xsd:double');
doc.text(ann.futureIfWin + '');
doc.endElement();
// ifWin
doc.startElement('ifWin').writeAttribute('xsi:type', 'xsd:double');
doc.text(ann.ifWin + '');
doc.text((1*ann.ifWin).toFixed(2));
doc.endElement();
// selectionID
doc.startElement('selectionID').writeAttribute('xsi:type', 'xsd:integer');
doc.text(ann.selectionID);
doc.startElement('selectionId').writeAttribute('xsi:type', 'xsd:integer');
doc.text(ann.selectionId);
doc.endElement();
// selectionName
doc.startElement('selectionName').writeAttribute('xsi:type', 'xsd:string');
Expand All @@ -392,6 +399,14 @@ function encodeGetMarketProfitAndLoss(doc, result) {
doc.startElement('worstCaseIfWin').writeAttribute('xsi:type', 'xsd:double');
doc.text(ann.worstCaseIfWin + '');
doc.endElement();
// ifLoss
doc.startElement('ifLoss').writeAttribute('xsi:type', 'xsd:double');
doc.text((1*ann.ifLoss).toFixed(2));
doc.endElement();

doc.endElement();
}
doc.endElement();
}
// commissionApplied
doc.startElement('commissionApplied').writeAttribute('xsi:type', 'xsd:double');
Expand All @@ -403,11 +418,11 @@ function encodeGetMarketProfitAndLoss(doc, result) {
doc.endElement();
// includesSettledBets
doc.startElement('includesSettledBets').writeAttribute('xsi:type', 'xsd:booleang');
doc.text(result.includesSettledBets);
doc.text(result.includesSettledBets ? "true" : "false");
doc.endElement();
// includesBspBets
doc.startElement('includesBspBets').writeAttribute('xsi:type', 'xsd:booleang');
doc.text(result.includesBspBets);
doc.text(result.includesBspBets ? "true" : "false");
doc.endElement();
// marketId
doc.startElement('marketId').writeAttribute('xsi:type', 'xsd:integer');
Expand Down
41 changes: 40 additions & 1 deletion lib/emulator_market.js
Expand Up @@ -206,7 +206,46 @@ EmulatorMarket.prototype.handleGetMarketProfitAndLoss = function(req, res, cb) {

console.log('EMU: market handleGetMarketProfitAndLoss', req.request);

throw new Error('Not yet supported');
function itemTemplate(player) {
return {
futureIfWin: '0.0',
ifWin: player.ifWin,
selectionId: player.selectionId,
selectionName: 'SelectionName',
worstCaseIfWin: '0.0',
ifLoss: player.ifLoss };
}

for(var i in self.players) {
self.players[i].ifWin=0;
self.players[i].ifLoss=0;
}

// calculate ifWin/ifLoss
for(var i in self.bets) {
var bet = self.bets[i];
var player = self.players[bet.selectionId];
// matched
var selId = bet.selectionId;
var matAvgPrice = bet.averageMatchedPrice();
var matSize = bet.matchedSize();
console.log('@@@',selId, matAvgPrice, matSize);
if(bet.betType==='L') {
player.ifWin -= matSize * (matAvgPrice - 1);
player.ifLoss += 1 * matSize;
} else if(bet.betType==='B') {
player.ifWin += matSize * (matAvgPrice - 1);
player.ifLoss -= 1 * matSize;
}
}

// response
res.response.annotations = [];
for(var i in self.players) {
res.response.annotations.push( itemTemplate(self.players[i]) );
}
res.response.errorCode = "OK";
setTimeout(cb, readonlyDelay, null, res);
}

EmulatorMarket.prototype.handlePlaceBets = function(req, res, cb) {
Expand Down
23 changes: 23 additions & 0 deletions test/common.js
Expand Up @@ -161,3 +161,26 @@ exports.emulatorGetMUBets = function (data, cb)
cb(null, data);
});
}

//GetMarketProfitAndLoss
exports.emulatorGetMarketProfitAndLoss = function (data, cb)
{
var mark = data.market;
console.log('===== Call getMarketProfitAndLoss for marketId="%s" =====', mark.marketId);

var session = exports.session;
var inv = session.getMarketProfitAndLoss(mark.marketId, false);
inv.execute(function(err, res) {
console.log('action:', res.action, 'error:', err, 'duration:', res
.duration() / 1000);
if (err) {
cb(err);
return;
}

console.log( util.inspect(res.result, false, 10) );
console.log("errorCode:", res.result.errorCode);

cb(null, data);
});
}
2 changes: 1 addition & 1 deletion test/emulated_place_update_cancel_bets.js
Expand Up @@ -149,7 +149,7 @@ function cancelBets(data, cb) {

// Run the test
var testSteps = [ common.login, common.getAllMarkets, common.selectMarket,
common.emulatorGetCompleteMarketPrices, placeBets, common.emulatorGetMUBets,
common.emulatorGetCompleteMarketPrices, placeBets, common.emulatorGetMUBets, common.emulatorGetMarketProfitAndLoss,
cancelBets, common.emulatorGetMUBets, common.logout ];
async.waterfall(testSteps, function(err, res) {
if (err)
Expand Down

0 comments on commit a5fd41f

Please sign in to comment.