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