I have one market where I have more than 1000 orders so I need to ListCurrentOrders more than once to find my position. I have some code that works but after an hour or so of doing this once every two seconds it always crashes with
SyntaxError: Unexpected token { // the token varies each time.
where I am trying to parse the resultant string. I am wondering if I even have the right approach to reading more than 1000 orders where Betfair’s limit is 1000 orders at a time so I am going to describe my method and ask for suggestions.
I am going to listCurrentOrders and if order length = 1000 looping round and doing it again adding the second string from a starting length of 1000 to the string generated first time round.
I am using Javascript and node with my code based on the sample code provided. This reads the current orders in chunks which it adds to a string called str. When there is no more data it parses the string to identify the fields in it putting the parsed string into a variable called response.
To make my loop work I had to make str globally declared rather than locally declared. Because Javascript is asynchronous this meant it would start the next market before the last market had finished and I ended up with more than one market in the same string. I learned that way why str was declared locally. So I gave my string name for listCurrentOrders only a different name with global declaration but a counter which changed for each market so each market had its own global string variable.
Global declaration
var str = [];
The trouble with looping round and adding a second string to the first string is that it has footer and header data which needs to be stripped out of the middle of the resultant string so that it can be parsed. I do this with the following line of code.
str[counter1] = str[counter1].replace(/\],"moreAvailable":true\},"id"\:1\}\{"jsonrpc"\:"2\. 0","result":\{"currentOrders"\:\[/,",");
This then works for a thousand or so iterations and then crashes at this point in the code:
var response = JSON.parse(str[counter1]);
The full code is not all that much and is set out below. It never crashes with markets with less than 1000 orders. I would like to know if there is a completely different way of doing this that is generally recognised as being better as I have just worked this way out from scratch. If I am going about it the right way can anyone see a problem with my detailed approach or know from experience what the reason for the crashes might be.
function checkOrders(options, marketId, selection,counter1,counter2) {
// processed for each market in turn
var recordCount = 1000;
console.log("MarketId = "+marketId);
orderlength = (orderlength === recordCount ? recordCount : 0);
str[counter1] = (orderlength === recordCount ? str[counter1] :'');
var requestFilters = '{"marketIds":["'+ marketId+'"],"fromRecord":"'+orderlength+'","recordCount":"'+r ecordCount+'"}';
var jsonRequest = constructJsonRpcRequest('listCurrentOrders', requestFilters );
var req = https.request(options,function (res){
res.setEncoding(DEFAULT_ENCODING);
res.on('data', function (chunk) {
str[counter1] += chunk;
});
res.on('end', function (chunk) {
// delete joiner parts of string here
str[counter1] = str[counter1].replace(/\],"moreAvailable":true\},"id"\:1\}\{"jsonrpc"\:"2\. 0","result":\{"currentOrders"\:\[/,",");
// program crashes here
var response = JSON.parse(str[counter1]);
handleError(response);
console.log("order response length = "+Object.keys(response.result.currentOrders).lengt h);
orderlength = Object.keys(response.result.currentOrders).length;
// start again if orderlength = 1000 or multiple thereof
if (orderlength % recordCount === 0) {
checkOrders(options, marketId, selection,counter1,counter2);
}
There are 1050 orders in the market so when there is a remainder from dividing orderlength by 1000 it just gets on with the rest of the program.
SyntaxError: Unexpected token { // the token varies each time.
where I am trying to parse the resultant string. I am wondering if I even have the right approach to reading more than 1000 orders where Betfair’s limit is 1000 orders at a time so I am going to describe my method and ask for suggestions.
I am going to listCurrentOrders and if order length = 1000 looping round and doing it again adding the second string from a starting length of 1000 to the string generated first time round.
I am using Javascript and node with my code based on the sample code provided. This reads the current orders in chunks which it adds to a string called str. When there is no more data it parses the string to identify the fields in it putting the parsed string into a variable called response.
To make my loop work I had to make str globally declared rather than locally declared. Because Javascript is asynchronous this meant it would start the next market before the last market had finished and I ended up with more than one market in the same string. I learned that way why str was declared locally. So I gave my string name for listCurrentOrders only a different name with global declaration but a counter which changed for each market so each market had its own global string variable.
Global declaration
var str = [];
The trouble with looping round and adding a second string to the first string is that it has footer and header data which needs to be stripped out of the middle of the resultant string so that it can be parsed. I do this with the following line of code.
str[counter1] = str[counter1].replace(/\],"moreAvailable":true\},"id"\:1\}\{"jsonrpc"\:"2\. 0","result":\{"currentOrders"\:\[/,",");
This then works for a thousand or so iterations and then crashes at this point in the code:
var response = JSON.parse(str[counter1]);
The full code is not all that much and is set out below. It never crashes with markets with less than 1000 orders. I would like to know if there is a completely different way of doing this that is generally recognised as being better as I have just worked this way out from scratch. If I am going about it the right way can anyone see a problem with my detailed approach or know from experience what the reason for the crashes might be.
function checkOrders(options, marketId, selection,counter1,counter2) {
// processed for each market in turn
var recordCount = 1000;
console.log("MarketId = "+marketId);
orderlength = (orderlength === recordCount ? recordCount : 0);
str[counter1] = (orderlength === recordCount ? str[counter1] :'');
var requestFilters = '{"marketIds":["'+ marketId+'"],"fromRecord":"'+orderlength+'","recordCount":"'+r ecordCount+'"}';
var jsonRequest = constructJsonRpcRequest('listCurrentOrders', requestFilters );
var req = https.request(options,function (res){
res.setEncoding(DEFAULT_ENCODING);
res.on('data', function (chunk) {
str[counter1] += chunk;
});
res.on('end', function (chunk) {
// delete joiner parts of string here
str[counter1] = str[counter1].replace(/\],"moreAvailable":true\},"id"\:1\}\{"jsonrpc"\:"2\. 0","result":\{"currentOrders"\:\[/,",");
// program crashes here
var response = JSON.parse(str[counter1]);
handleError(response);
console.log("order response length = "+Object.keys(response.result.currentOrders).lengt h);
orderlength = Object.keys(response.result.currentOrders).length;
// start again if orderlength = 1000 or multiple thereof
if (orderlength % recordCount === 0) {
checkOrders(options, marketId, selection,counter1,counter2);
}
There are 1050 orders in the market so when there is a remainder from dividing orderlength by 1000 it just gets on with the rest of the program.


Comment