Skip to content

Commit

Permalink
ugly getCurrentBets in emulator, shit
Browse files Browse the repository at this point in the history
  • Loading branch information
AlgoTrader committed Sep 6, 2012
1 parent 009962b commit 5742fa1
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 8 deletions.
151 changes: 150 additions & 1 deletion lib/emulator_encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ function encodeResponse(doc, action, input) {
case 'getMUBets':
encodeGetMUBets(doc, input);
break;
case 'getCurrentBets':
encodeGetCurrentBets(doc, input);
break;
case 'getMarketProfitAndLoss':
encodeGetMarketProfitAndLoss(doc, input);
break;
Expand Down Expand Up @@ -370,6 +373,153 @@ function encodeGetMUBets(doc, result) {
}
}

// getCurrentBets
function encodeGetCurrentBets(doc, result) {
// betResults
if (!result.bets) {
// bets
doc.startElement('bets').writeAttribute('xsi:null', '1');
doc.endElement();
// totalRecordCount
doc.startElement('totalRecordCount').writeAttribute('xsi:type', 'xsd:integer');
doc.text('0');
doc.endElement();
} else {
// bets
doc.startElement('bets').writeAttribute('xsi:type', 'n2:ArrayOfCurrentBet ');
for ( var i = 0; i < result.bets.length; ++i) {
var item = result.bets[i];
doc.startElement('n2:CurrentBet').writeAttribute('xsi:type', 'n2:CurrentBet');
// asianLineId
doc.startElement('asianLineId').writeAttribute('xsi:type', 'xsd:integer');
doc.text(item.asianLineId + '');
doc.endElement();
// avgPrice
doc.startElement('avgPrice').writeAttribute('xsi:type', 'xsd:double');
doc.text(item.avgPrice + '');
doc.endElement();
// betId
doc.startElement('betId').writeAttribute('xsi:type', 'xsd:long');
doc.text(item.betId + '');
doc.endElement();
// betStatus
doc.startElement('betStatus').writeAttribute('xsi:type', 'n2:BetStatusEnum');
doc.text(item.betStatus + '');
doc.endElement();
// betType
doc.startElement('betType').writeAttribute('xsi:type', 'n2:BetTypeEnum');
doc.text(item.betType + '');
doc.endElement();
// betCategoryType
doc.startElement('betCategoryType').writeAttribute('xsi:type', 'n2:BetCategoryTypeEnum');
doc.text(item.betCategoryType + '');
doc.endElement();
// betPersistenceType
doc.startElement('betPersistenceType').writeAttribute('xsi:type', 'n2:BetPersistanceTypeEnum');
doc.text(item.betPersistenceType + '');
doc.endElement();
// cancelledDate
doc.startElement('cancelledDate').writeAttribute('xsi:type', 'xsd:dateTime');
doc.text(item.cancelledDate.toISOString());
doc.endElement();
// lapsedDate
doc.startElement('lapsedDate').writeAttribute('xsi:type', 'xsd:dateTime');
doc.text(item.lapsedDate.toISOString());
doc.endElement();
// marketId
doc.startElement('marketId').writeAttribute('xsi:type', 'xsd:integer');
doc.text(item.marketId+'');
doc.endElement();
// marketName
doc.startElement('marketName').writeAttribute('xsi:type', 'xsd:string');
doc.text(item.marketName+'');
doc.endElement();
// fullMarketName
doc.startElement('fullMarketName').writeAttribute('xsi:type', 'xsd:string');
doc.text(item.fullMarketName+'');
doc.endElement();
// marketType
doc.startElement('marketType').writeAttribute('xsi:type', 'n2:MarketTypeEnum');
doc.text(item.marketType+'');
doc.endElement();
// marketType
doc.startElement('marketType').writeAttribute('xsi:type', 'n2:MarketTypeEnum');
doc.text(item.marketType+'');
doc.endElement();
// matchedDate
doc.startElement('matchedDate').writeAttribute('xsi:type', 'xsd:dateTime');
doc.text(item.matchedDate.toISOString());
doc.endElement();
// matchedSize
doc.startElement('matchedSize').writeAttribute('xsi:type', 'xsd:double');
doc.text((item.matchedSize*1).toFixed(2));
doc.endElement();
// matches
doc.startElement('matches').writeAttribute('xsi:null', '1');
doc.endElement();
// placedDate
doc.startElement('placedDate').writeAttribute('xsi:type', 'xsd:dateTime');
doc.text(item.placedDate.toISOString());
doc.endElement();
// bspLiability
doc.startElement('bspLiability').writeAttribute('xsi:type', 'xsd:double');
doc.text((item.bspLiability*1).toFixed(2));
doc.endElement();
// price
doc.startElement('price').writeAttribute('xsi:type', 'xsd:double');
doc.text((item.price*1).toFixed(2));
doc.endElement();
// profitAndLoss
doc.startElement('profitAndLoss').writeAttribute('xsi:type', 'xsd:double');
doc.text((item.profitAndLoss*1).toFixed(2));
doc.endElement();
// selectionId
doc.startElement('selectionId').writeAttribute('xsi:type', 'xsd:integer');
doc.text(item.selectionId+'');
doc.endElement();
// selectionName
doc.startElement('selectionName').writeAttribute('xsi:type', 'xsd:string');
doc.text(item.selectionName+'');
doc.endElement();
// settledDate
doc.startElement('settledDate').writeAttribute('xsi:type', 'xsd:dateTime');
doc.text(item.settledDate.toISOString());
doc.endElement();
// remainingSize
doc.startElement('remainingSize').writeAttribute('xsi:type', 'xsd:double');
doc.text((item.remainingSize*1).toFixed(2));
doc.endElement();
// requestedSize
doc.startElement('requestedSize').writeAttribute('xsi:type', 'xsd:double');
doc.text((item.requestedSize*1).toFixed(2));
doc.endElement();
// voidedDate
doc.startElement('voidedDate').writeAttribute('xsi:type', 'xsd:dateTime');
doc.text(item.voidedDate.toISOString());
doc.endElement();
// executedBy
doc.startElement('executedBy').writeAttribute('xsi:type', 'xsd:string');
doc.text(item.executedBy);
doc.endElement();
// handicap
doc.startElement('handicap').writeAttribute('xsi:type', 'xsd:double');
doc.text((item.handicap*1).toFixed(2));
doc.endElement();
// marketTypeVariant
doc.startElement('marketTypeVariant').writeAttribute('xsi:type', 'n2:NarketTypeVariantEnum');
doc.text(item.marketTypeVariant);
doc.endElement();

doc.endElement();
}
doc.endElement();
// totalRecordCount
doc.startElement('totalRecordCount').writeAttribute('xsi:type', 'xsd:integer');
doc.text(result.bets.length + '');
doc.endElement();
}
}

// getMarketProfitAndLoss
function encodeGetMarketProfitAndLoss(doc, result) {
// annotations
Expand Down Expand Up @@ -445,5 +595,4 @@ function encodeGetMarketProfitAndLoss(doc, result) {
doc.startElement('unit').writeAttribute('xsi:type', 'xsd:string');
doc.text(result.unit || 'N/A');
doc.endElement();

}
72 changes: 66 additions & 6 deletions lib/emulator_market.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ EmulatorMarket.prototype.onGetCompleteMarketPricesCompressed = function(result)
price : null,
amount : null
};
// var prices = {};

for ( var index in selectionPrices) {
var price = 1 * selectionPrices[index].price;
var backAmount = 1 * selectionPrices[index].backAmount;
Expand All @@ -93,10 +93,6 @@ EmulatorMarket.prototype.onGetCompleteMarketPricesCompressed = function(result)
bestLay.price = price;
bestLay.amount = layAmount;
}
// prices[price] = {};
// prices[price].price = price;
// prices[price].backAmount = price;
// prices[price].price = price;
}

self.players[selectionId] = self.players[selectionId] || {};
Expand Down Expand Up @@ -201,8 +197,72 @@ EmulatorMarket.prototype.handleGetCurrentBets = function(req, res, cb) {
var self = this;

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

function itemTemplate(bet) {
var date = new Date(0,0,1);
return {
asianLineId: bet.asianLineId,
avgPrice: bet.averageMatchedPrice(),
betId: bet.betId,
betStatus: bet.betStatus,
betType: bet.betType,
betCategoryType: "E",
betPersistenceType: bet.betPersistenceType,
cancelledDate: date,
lapsedDate: date,
marketId: bet.marketId,
marketName: "N/A",
fullMarketName: "N/A",
marketType: "O",
matchedDate: date,
matchedSize: bet.matchedSize(),
matches: null,
placedDate: bet.placedDate,
bspLiability: bet.bspLiability,
price: bet.price,
profitAndLoss: 0,
selectionId: bet.selectionId,
selectionName: "N/A",
settledDate: date,
remainingSize: bet.unmatchedSize(),
requestedSize: bet.unmatchedSize(),
voidedDate: date,
executedBy: "UNKNOWN",
handicap: 0,
marketTypeVariant: "D"
}
}

// generate MU bets list
var statusToOutput = req.request.betStatus;
var bets = [];
for ( var i in self.bets) {
var bet = self.bets[i];
if (statusToOutput == "U" ) {
// unmatched
var unmatched = itemTemplate(bet);
unmatched.betStatus = "U";
unmatched.price = bet.price;
unmatched.size = bet.unmatchedSize();
if (!bet.cancelled && !bet.isMatched())
bets.push(unmatched);
} else if (statusToOutput == "M" ) {
// matched
var matched = itemTemplate(bet);
matched.betStatus = "M";
matched.price = bet.averageMatchedPrice();
matched.size = bet.matchedSize();
if(bet.matchedSize()>0)
bets.push(matched);
}
}

throw new Error('Not yet supported');
// done
if (bets.length > 0) {
res.response.errorCode = "OK";
res.response.bets = bets;
}
setTimeout(cb, readonlyDelay, null, res);
}

EmulatorMarket.prototype.handleGetMarketProfitAndLoss = function(req, res, cb) {
Expand Down
30 changes: 30 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,36 @@ exports.emulatorGetMUBets = function (data, cb)
});
}

//Emulator helper
//GetCurrentBets
exports.emulatorGetCurrentBets = function (data, cb)
{
var mark = data.market;
console.log('===== Call getCurrentBets for marketId="%s" =====', mark.marketId);

var session = exports.session;
var inv = session.getCurrentBets("M", true, "PLACED_DATE", 200, 0, false, {marketId:mark.marketId});
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,
"recCount", res.result.totalRecordCount );

for(var record in res.result.bets) {
var bet = res.result.bets[record];
console.log( "\tbetId=%s betStatus=%s size=%s price=%s", bet.betId, bet.betStatus, bet.size, bet.price);
}

cb(null, data);
});
}

//GetMarketProfitAndLoss
exports.emulatorGetMarketProfitAndLoss = function (data, cb)
{
Expand Down
2 changes: 1 addition & 1 deletion test/emulated_place_update_cancel_bets.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function cancelBets(data, cb) {

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

0 comments on commit 5742fa1

Please sign in to comment.