Lefora Free Forum
382 views

[JAVA] parsing GetMarketPricesCompressed

Page 1
posts 1–9 of 9
regular - member
184 posts

Right, I've spent months trying to get a decent parsing routine for GetMarketPricesCompressed and I think I've come up with something now which pretty much does the trick.

In java, if you pass this method the response.getMarketPrices() string, you will be returned a MarketPrices object which has all of the data you would find in a GetMarketPrices request, which means it is fairly easy to access.

It is a standard Java class, just edit the package name and the import names (if the differ from yours) and it should all work fairly easily.

There may still be bugs in this code, if you find any please feel free to point them out to me.

This code is using the fabulous regex written by denp four months ago - made my life a hell of a lot easier, even if I don't completely understand why.

Cheers all,

Alan

__________________
Slashdot. It's like Digg on slow, but sensible.
rookie - member
8 posts

I'm missing some files From this called :

Array of Prices
Array of Runner Prices

Are they not from the free access API ? This is much appreciated b.t.w

regular - member
184 posts

Yes, they are from the BF API. If you have a look, you will see I have imported these objects from the API:

import com.betfair.publicapi.types.exchange.v5.ArrayOfPrice;
import com.betfair.publicapi.types.exchange.v5.ArrayOfRunnerPrices;
import com.betfair.publicapi.types.exchange.v5.BetTypeEnum;
import com.betfair.publicapi.types.exchange.v5.MarketPrices;
import com.betfair.publicapi.types.exchange.v5.Price;
import com.betfair.publicapi.types.exchange.v5.RunnerPrices;

The steps I have taken are roughly as follows:

Create a new object MarketPrices();

Get the market details (id, baserate and so on, the admin stuff).

Create a new object of type ArrayOfRunnerPrices() and a new ArrayOfPrice()

Then for each runner in the market, create a new object of type Price() for each different price offered by the API call. Generally a maximum of three prices. The lay prices get the same treatment.

Add each Price() object to the ArrayOfPrices() and when you have iterated through the runners, add the ArrayOfPrices() to the MarketPrices object and send it on to be processed.

The first couple of times I tried this, I used an object I came up with myself, which I have attached just for shits and giggles (old Australian saying, means just for the hell of it and if you want to laugh at it feel free, I do).

Seriously, I do have a bit of a rueful laugh when I look at this code - the java file all by itself weighs in at 29kb and the object itself can only handle markets with three runners in it. And thats only the start of the problems with it.

I stopped using it because while it fit the bill reasonably well and held all the data I needed it to, it seriously became a pain in the bottom to use.

You shouldn't have too much difficulty using this in other IDEs, although I wrote it in Netbeans. I can't figure out how Eclipse does things with WSDLs, so if you are using eclipse a) you can probably figure it out on your own or b) you need to adjust the import paths.

Once again, if you come up with any impovements on this parser, improve it then post the java file here - I still intend one day to create a library of routines and classes which will allow bfbotters to spend more time concentrating on the tactics and systems they are using than pissing around with parsing strings and working out how to get the API to do something. I want to do it in Java because thats what I use, but most of the stuff will allow itself to be refactored fairly easily to other languages - it is just a matter of working out the logic.

That said though, struggling with the API is incredibly educational, so a little bit of struggle isn't necessarily a bad thing for new coders.

Cheers,

Alan

__________________
Slashdot. It's like Digg on slow, but sensible.
novice - member
37 posts

awwww shucks

regular - member
184 posts
eh?
__________________
Slashdot. It's like Digg on slow, but sensible.
regular - member
184 posts
If anyone has had a chance to have a look at this and has managed to stop NullPointerExceptions from appearing when an empty string is passed into the method which sorts each runner's prices, I would appreciate a look at it - It is driving me insane.
__________________
Slashdot. It's like Digg on slow, but sensible.
regular - member
123 posts

want to post a stack trace?

regular - member
184 posts
I'll see if I can generate one tonight when I get home - currently skiving off at work installing python onto my new Dell Axim X51v (nerdgasm!)

Incredible little machine and the guy who had it before me sold it to me for 30 quid.

Cheers Austin
__________________
Slashdot. It's like Digg on slow, but sensible.
regular - member
184 posts

I think I have it sussed. What was happening was I was splitting the response from GetMarketPricesCompressed into four elements which represented the market details and the three runners (just playing with match odds in football at the moment).

When I passed the runner details string to

private RunnerPrices getRunnerDetails(String rString)
I split that string again, ideally into three elements.

arr[0] had the runner details in it (selection ID and so on)
arr[1] has the back prices
arr[2] has the lay prices.

When I got to that part of the match where there is no money left in the market for a runner (IE, the winning team and all the 1.01s are gone) I was being thrown a NullPointer exception because arr[1] was empty. As far as the array was concerned, it was definitely a string, because the character i used to split the initial rString was there, but there were no characters in it. It probably didn't help that I was using a dodgy if statement too.


if (backP.length > 0) <-------THIS WAS A SILLY THING TO DO

Although I gave it an else statement which made sense, I forgot about the basic rule of equality in Java and didn't take into account how the operators work.

For those who are scratching their heads or those who are new to programming and happen to be reading this, the basic (somewhat simplified) premise is this:

Mathematical operators work by using comparison. To decide whether value a is equal to, greater than or less than value b, there must first be a value b.

The mistake I was making was silly, but an easy one to make I think.

In the code snippet directly above, I was asking the JVM to compare the length of a string with the value 0 and if it was greater than 0, to take the following action.

Problem is, in some cases, I was actually asking the JVM to compare the length of a non-existing string with the value 0.

The reason I was getting the null pointer exception was that the JVM was trying to compare the string, couldn't find it where I said it was and told me so.

The correct test (and the test I have included in the updated version attached) should have been like so:


if (backP != null)

I should also add that I've done a few things in here as well. For a start, I have defined a couple of strings in there which represent market prices with no cash in them. If backP == null, the code will split the nullPrice string instead of just passing it by. This means that while there is no money in the market, I still have values in there which I can work with. I have done the same with the string layP, which is the value in arr[2] I mention above.

I hope you blokes don't mind me rambling like this occasionally - I find putting it down like this reinforces the lesson in my head and it may just help other people with their problems as well.

Here is the corrected version - still messy and still has methods in there I'm not using (yet) but it isn't anything like as awkward as it has been.

__________________
Slashdot. It's like Digg on slow, but sensible.
Page 1
posts 1–9 of 9

This Topic Is Locked To Guest Posts

It's been a while since this topic was active, if you'd like to get it going again, please post as a registered member

join now