Lefora Free Forum
175 views

Another maths question.

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

Alright, for some reason this problem is giving me no end of difficulty.

I'm writing a trading interface, with similar functionality to the standard betfair web interface, except betting will just have single click functionality. At the moment opening and closing positions is a manual operation, but when I have it sorted, it will be simply a matter of opening the position and the bot will monitor the market and close it at a profit level to be decided by the user (at the moment, the user is me).

Right now, I have the application making simulated bets so I can fine tune the interface. The idea really is to set the TableCellRenderer to give the user visual clues as to where he stands regarding profits and losses. Not that big a deal now I have worked out how to do that, but there are a couple of other things bothering me.

The one getting me at the moment is the calculations involved in setting profit and loss on each runner after a bet has been made. On the betfair interface, they are the red or green numbers underneath the runners name in the market table. This should be an incredibly easy calculation, but for some reason, I can't get it right. Here is a picture to demonstrate:

This is such a weird editor, I don't have this pic on a server at the moment and the upload thing only seems to work when you are editing the post after it has been posted first. If you read it before I manage to fix it, wait a couple of minutes and reload, I'll have it posted shortly. Anyway, on with the story.

You can see from the screenshot that initially, the values in the 'Status' column of the table are correct, except commission hasn't been subtracted from the value in the table.

Now for the second screenie.

As you can see here, something has gone sadly wrong. I do think I have managed to make this more complicated than it should be, but I can't for the life of me figure out what has gone wrong.

Here is the code which sets that column of the table after each bet:

private void setStatus(PlaceBets theBet)
{
PlaceBets bet = theBet;
DefaultTableModel mod = (DefaultTableModel) TradingForm.tblMarket.getModel();
DefaultTableModel bMod = (DefaultTableModel) TradingForm.tblBets.getModel();
Double price = bet.getPrice();
Double size = bet.getSize();
Double liable = 0.00;
Double totReturn = 0.00;
if (bet.getBetType().value().equalsIgnoreCase("B"))
{
totReturn = size * (price - 1);
liable = 0.00 - size;
}
else
{
totReturn = size;
liable = 0.00 - (size * (price - 1));
}
int selID = bet.getSelectionId();

DecimalFormat df = new DecimalFormat("##00.00");
for (int i = 0; i < mod.getRowCount(); i++)
{
if (mod.getValueAt(i, 8) == null)
{
mod.setValueAt(0.00, i, 8);
}
}
for (int i = 0; i < mod.getRowCount(); i++)
{
int curSelID = Integer.parseInt(String.valueOf(mod.getValueAt(i, 1)));
double curVal = Double.parseDouble(String.valueOf(mod.getValueAt(i, 8)));
if (curSelID == selID)
{
double newVal = (curVal + totReturn);
mod.setValueAt(newVal, i, 8);
}
else
{
double newVal = (curVal + liable);
mod.setValueAt(newVal, i, 8);
}
}
}

That code is me trying to deconstruct what is happening throughout the process. I know what has happened in the end, but it seems that no matter what I do, I still end up with crazy values in the column I need the data in. The funny thing is, I know how to get the answer I'm looking for, but for some reason I can't get it right in that table.

Anyone have any ideas? I'm going to keep turning this over in my head, but it has me bamboozled, seriously.

Cheers,

Alan

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

Dunno what your specific problem here is but why are you even doing it like this?
You really don't want to be using the tableModel to store some 'current value' which you then update as bets come in.
Do your Pnl calculation elsewhere. Why not have a method somewhere which takes a Set of 'Bets' and works out the pnl for each selection.
You can then use that to calc pnl based on current bets.

superstar - member
230 posts
Probably not of much use, but if you scrape the website, the red/green P&L figures are included in the market data if you are logged in...
__________________
regular - member
184 posts
Cheers guys.

I probably am doing it arse about, but I had the idea of having a TableModelListener watching that column and taking action when the calculations show that a profitable closing bet can be made.

Also, if the value is stored in the table, the triggering action can come from the TableCellRenderer. Again, probably a silly idea, but it made sense to me.

The idea of having a Set of bets is a good one though, it shouldn't be hard to make that calculation using the set of bets and simply store the pnl value in the table.

I'm printing it off now so I can sit with a pencil and paper and work out exactly what I should be doing. I find that way is laborious, but it can help sometimes.

I have a feeling that I'm going to have to rewrite the entire class and possibly the code which is processed when the cell is clicked. If I create the PlaceBets object directly at the source and pass that object to a MakeBet class, it would probably be a lot more efficient. I think I will end up doing that actually - just looking over the code as I write this and the whole damn thing is a mess. Two constructors, umpteen variables and code going off in all directions.

Hmm, its pencil time.

Thanks guys.

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

Hi Alan,

There's some example code on this thread which might save some time with the pencil and paper:
http://diybetfairbots.lefora.com/2008/06/11/exposure-calculation/page1/
(I've since changed my approach to work straight off a list of bets, and to work out PnL, possible green-up bets, and exposure across all selections in the market. I can't acces it from here though...)

A couple of pointers anyway (based on my code sample):
Your Ifwin Pnl (i.e. what you are trying to display) for a selection is:
IfWin = sum of netStakes across all other selections - netProfit on this selection
and your worst case for the whole market is the minimum of these.

You can also work out netPosition = NetProfit - netStakes
The possible green-up bet for that selection is now:
greenup stake = netPosition / available odds.
(if NetPosition is +ve, you will lay out, and if -ve you will back out)
To work out the effect of these green-up bets, you need to update netStake and NetProfit for the selection accordingly, and then repeat the IfWin calculation for all selections, and take the minimum again.

Exposure is a bit more fiddly - you need to work out 2 possible IfWins for each selection - one including all unmatched backs on this selection and all unmatched lays on other selections getting matched, and the other including all unmatched lays on this selection and all unmatched backs on other selections getting matched. You exposure is then the minimum of all these IfWins.

Cheers,

Pete.

regular - member
123 posts

Hi,

As birchy says, your pnl is available if you scrape and also from the API so in theory you don't need to calculate it yourself. But of course if you want a simulation mode, or if you want to calculated pnl based on 'potential' bets you have to calc it yourself.

Alan, yes 'arse about' is a good description ... ;)
Seriously though ... try taking you 'business model' and logic up a level away from all the TableModel/Renderer code.
Yes, "it shouldn't be hard to make that calculation using the set of bets", but "simply store the pnl value in the table" ... no. Store the values in your business objects and have the TableModel fetch them from there. Your 'triggers' can then be based on the core business model data and not tied into the specifics of your GUI.
etc.

regular - member
184 posts
Peteb, thanks very much for that, those calcs will come in handy. Already made things a fair bit clearer.

austinpodhorzer, again, you have set me straight. I think I am fairly prone to letting the two parts of the application merge in places, which sometimes makes it incredibly difficult to debug even the code I have written myself. You are absolutely right, I need to bring the business logic out of the interface/gui levels and keep it that way.

Thats going to be a wrench, but a necessary one I think. A lot of code revision needs to happen in that case. Still I'm doing it as much for the coding as for playing with the betfair API.

Birchy, I'm afraid the one thing which makes my blood run cold is the idea scraping the betfair website - I know it can be done and I know there are a few tools to make it easier, but if someone came up to me and said "Do it. Now." I think I would just burst into tears - I've only just managed to get my head around the API, I couldn't bear the thought of learning how the website works! Thanks for the info though - if absolutely necessary I'll figure out enough to get some of that info in. Possibly as a means of speeding up the price calls. Hmm, interesting idea that. I have plenty of stuff to do though, so it will have to wait.

Thanks again guys.

Alan
__________________
Slashdot. It's like Digg on slow, but sensible.
regular - member
123 posts

Been thinking a bit about this pnl calc.
What's the fastest way to do it?
Let's say in a market of n runners and you have a set of m bets, the obvious way of doing it would be O(n*m).
I'm sure this could be brought down to O(n+m) though.

regular - member
134 posts

Hi, I've seen these little performance related equations mentioned before, I understand they are to do with complexity/number of iterations or something, do they have a name so that I can go google away my ignorance please?

regular - member
134 posts

sorry forget that, I found it, http://en.wikipedia.org/wiki/Big_O_notation

Page 1
posts 1–10 of 10

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