As both a programming exercise as well as a new botty type application to play with, I'm trying to come up with a routine which will watch for me to open a position, then calculate and highlight in the pretty JTable (and even place the bet if I ask it to) the odds and stake which will return a set percentage of the position opening bet as profit.
An example seems applicable here.
Lets say for the sake of argument that I have set my desired profit to be 5% of the opening bet and I open the position with a £3.00 bet at odds of 1.5.
By itself, if my selection wins, that will give me a return of £1.50.
However, my position is still open, meaning I am risking £3.00 for ten times my desired profit. So I want to close it, which I could do by waiting until the odds drop to 1.45 and placing a lay bet on my selection.
I know I'm going through basic stuff here, bear with me - I've been trying to figure this out for half the day and I'm feeling a bit stupid. I'm not sure whether I have thought about it too much or if I'm thinking about it from the wrong angle.
So far the sequence is like this:
Back bet of £3 @ 1.5 = 3 x (1.5 - 1) = £1.50 profit.
Lay bet of £3 @ 1.45 = 3 x (1.45 - 1) = £1.35 liability.
If my selection wins, I have my guaranteed 15p return. But if my selection loses, I get nothing.
I don't want nothing, I want to get my 15p regardless of who wins.
Here is another attempt, with the same position opening back bet of £3.00 @ 1.5.
In order to ensure I get at least my 15p regardless of the outcome, I need to place a lay bet on my original selection which both cancels out my back bet and returns my 15p if my original back bet loses. The solution to that is to make the lay bet size £3.15 - the £3 closes my position and the 15p goes to each of the other runners.
So, we need to lay £3.15 on my selection, but at what odds to ensure that I get my desired profit if my original selection does come in?
Constant rinsing and repeating has told me that laying £3.15 @ 1.38 will give me the answer I'm looking for.
Just to keep the flow, here are the maths:
Back £3.00 @ 1.5 = 3 x (1.5 - 1) = £1.50 profit.
Lay £3.15 @ 1.38 = 3 x (1.4 - 1) = £1.20 liability.
£1.50 - £1.20 = 0.30
Assuming three runners here, runner x, runner y and runner z.
Right now, I should have my 15p return if any of the three selections wins. (Not calculating commission here)
If runner y wins, 15p, if runner z wins, 15p. And if my original selection - runner x - wins, I will get 15p. The reason for this is because while the calculations above say I should get 30p, that is disregarding the fact that the lay bet I placed has actually lost, which means that of the two parts of the lay bet stake (£3.00 and the 0.15p for the other runners) the first part (£3.00) cancels out the back bet and the second part (0.15) goes to the other runners should my selection lose. If my original selection wins though, the lay bet loses and I lose £3.15, which means the final calculation looks like this:
£1.50 - (£1.20 + 0.15) = 0.15.
So, I have the solution, but it is kind of like jeopardy here, in that I'm sort of trying to find part of the question. Is there an equation I can use which will find the odds I am looking for to ensure my desired profit?
It is fairly easy to simply go through the increments/decrements and perform the calculations every time a position is opened, but it isn't terribly efficient. Any ideas?
Sorry about the long winded leadup, but I needed to work through it to make sure I hadn't missed something obvious. Which I probably have.
Alan.
i think, as with the constipated mathematician, you need to work it out with a pencil :-)
I did this a while back, and it went along these lines. Assuming two runners, profit of p1 for runner 1 winning, stake of s1 on runner 1, at odds1 etc, and backing only (cos it's simpler for the example) you can say:
p1 = s1 * (odds1 - 1) - s2 winnings if runner 1 wins
p2 = s2 * (odds2 - 1) - s1
To green up, p1 = p2 ==>
s1 * (odds1 - 1) - s2 = s2 * (odds2 - 1) - s1
s1 * odds1 = s2 * odds2
given you have s1, odds1, odds2 you work out s2.
So it gets a bit more complicated when you want to lay instead. But i think it follows through much the same way.
Or am i teaching you grandmothers to suck eggs here?
I divide the potential profit by the latest price...here is my (C#) code
If I have an opening back position (the size is 'stakeToRecover'), I place a lay bet of size stakeAdjustment:
stakeAdjustment = CalcStake(averageBetBackPrice, currentLayPrice, stakeToRecover);
If I have an opening back position, I place a backbet of size stakeAdjustment:
stakeAdjustment = CalcStake(averageBetLayPrice, currentBackPrice, stakeToRecover);
private static double CalcStake(double price1, double price2, double originalStake)
{
double profit = CalcProfit(price1, price2, originalStake);
double adjustedStake = Math.Round(originalStake + (profit / price2), 2);
if (adjustedStake < MinStake)
{
adjustedStake = MinStake;
}
return adjustedStake;
}
private static double CalcProfit(double price1, double price2, double originalStake)
{
double profit = Math.Round((price1 - price2) * originalStake, 2);
return profit;
}
Does that make sense?! Theres probably a more efficient way of doing it, but this does the job for me.
i think, as with the constipated mathematician, you need to work it out with a pencil :-)
I did this a while back, and it went along these lines. Assuming two runners, profit of p1 for runner 1 winning, stake of s1 on runner 1, at odds1 etc, and backing only (cos it's simpler for the example) you can say:
p1 = s1 * (odds1 - 1) - s2 winnings if runner 1 wins
p2 = s2 * (odds2 - 1) - s1To green up, p1 = p2 ==>
s1 * (odds1 - 1) - s2 = s2 * (odds2 - 1) - s1s1 * odds1 = s2 * odds2
given you have s1, odds1, odds2 you work out s2.
So it gets a bit more complicated when you want to lay instead. But i think it follows through much the same way.
Or am i teaching you grandmothers to suck eggs here?
-denp
No, you aren't teaching me to suck eggs at all - I'm finding it frustrating that I am finding what should be simple maths to be far more complicated than it seems it should be.
What I'm trying to do is essentially green up using a single lay bet. I noticed this happened after accididentally clicking too many times on the betfair website and I really like the idea. Small conservative and extremely safe bets which yield a predictable profit. It doesn't get any better than that, particularly if you have statistics on your side as well.
backing a runner opens a position, laying the same runner at lower odds closes the position at a profit. If the lay bet is calculated properly, you can guarantee the same return regardless of the actual outcome of the event.
That gives us two variables to play with, namely the lay bet size and the odds to place the bet at. Since we have specified what the desired profit is - whether it is 5%, 10%, 50% or 2% is irrelevant, once we have calculated what our percentage is in terms of real numbers, we know what the lay bet size will be. So the size of the lay bet is easy to calculate based on the size of the original stake.
Which leaves us with only one variable, the odds at which to place the lay bet. How do I calculate the odds at which to place the lay bet. I could calculate whether it will do the job every time I get a price update, which is fairly simple.
What I want to do though is calculate it as soon as I get the results of the PlaceBetsReq and watch out for those odds to come in, sort of anticipate the market if that makes any sense.
Thanks for your replies so far guys, I'll give both of them a damn good piece of thinking about.
No matter the result though, I'll post the code in the snippets section when I'm done.
Thanks again,
Alan
OK, if you are looking for a margin of m on an original stake of S at a price P, the formulas you need are:
New Price, P' = (P-m)/(1+m)
Stake to Lay, S' = (1+m)S
So in your example the new price would be :
P' = (1.5 - 0.05) / (1 + 0.05) = 1.45 / 1.05 = 1.3809
And the stake you are laying would be:
S' = (1 + 0.05) x £3.00 = 1.05 x £3.00 = £3.15
HTH
If you know the price you are going to get out at (odds2), then yes it's easy
stake2 = stake1 * odds1 / odds2
We're trying to find the target odds.
First we need to work out the target profit.
If we want to lock in min 5%, then stake2 must be at least stake1 * 1.05
In the example:
stake1 = £3
So stake2 must be at least £3 * 1.05 = £3.15
Now work out the target odds:
odds2 = odds1 * stake1 / stake2
odds2 = 1.5 * 3 / 3.15 = 1.4286
We now need to move to a valid tick
As we backed first, we need to move down to a valid tick - if we had layed first, we would move up, but otherwise all the maths is the same - denp's nice new library code will get you there
So move down to 1.42
So stake2 needs adjusting
stake2 = stake1 * odds1 / odds2
stake2 = 3 * 1.5 / 1.42 = 3.169
So the bet is 3.17 at 1.42, locking in 17p profit, or 5.67%
EDIT: confused - I reckon you can be 26p green if you back at 1.5 and lay at 1.38 - what's happened?
Looks like the quick way to work out the target odds is:
Back first: Odds2 = odds1 / 1.05, and then move down to nearest tick (1.42) - back £3, lay £3.17, 17p green
Lay first: Odds2 = odds1 * 1.05, and then move up to nearest tick (1.58) - lay £3, back £2.85, 15p green
Peteb, I think the confusion arises because your method just breaks even if the horse wins - Inksmithy was specifically after a formula to give an equal green outcome regardless of result.
From both of your examples, I reckon we can come up with some code which works for both back and lay closing bets.
I've been sitting with a pen and a calculator for half the day doing this stuff - I can't use my computer while there is stuff happening at work, but I can get away with a calculator. So paper and a calculator it was for me today.
In the process, I came up with two different prices, 1.38 and 1.4. I didn't get 1.42, however I was interrupted (finished work) before I could finish the calculations and I did find 0.8 coming up using the same example bet, so it would seem I was heading in the same direction as you blokes after all. I didn't get there though and I've been turning the problem over all day.
Since the object of the game is to get out of the market as soon after you have got into it as possible, it seems we can fiddle with one or both of the variables in order to get either exactly the result we are looking for or slightly more.
If we adjust the stake, we a) make slightly more than our target and are out of the market slightly faster, which lessens the risk. If we stick rigorously to our '5% exactly' rule, we could end up with very predictable results at the cost of ever so slightly more risk of the market getting away from us.
My original idea was to give the imaginary user (I say imaginary, but really it will be me and whoever I give the code to) the option to specify either a required percentage of the stake in profit per trade or they could set an actual figure to represent desired profit per trade. What you blokes have supplied is a third mode, which is brilliant. I can have a 'tight' percentage - using the example would be to place the lay bet of 3.15 @ 1.4 to give us a return of exactly 5% of the stake. I had originally come up with 1.38 as the target odds as well, but I found that because of rounding and so forth through the process, 1.4 got the same result and got us out of the market faster.
The other alternative is a loose mode, which means we can change the stake level as the odds change in order to get our target as a minimum, rather than an absolute, which is your solution peteb.
The third option is for the user to set the required profit per trade manually rather than using a percentage, which would seem to be less efficient in terms of escaping the market, but again, has the advantage of predicability. myrddin's calculations cover that one.
Thats brilliant guys, thanks very much.It will take me a couple of days, but when I have the code in place, I'll post it. See if I can incorporate denp's code as well.
I think once I have this code running the way I want it to and before I stir in any special sauce, I'll make a copy of the netbeans project and stick it online somewhere. It will be sad and messy code, but it will work. You know how it goes, write the code, document it later....its a bad habit, but coding is sexier than documenting.
Thanks again.
Alan
[EDIT] hang on a minute, I'm lost again. Its too late tonight, I'll have another look in the morning - the calculations I have done on the formulas you have both provided seem to work though. If the lay bet is larger than the original back bet, the other runners get the difference between the laybet - backbet. So, that bit is easy. if runner b or runner c wins, we have our desired profit. If our original back bet comes in though, we still want our desired profit, which will be (backbet return - laybet liability) - (modulus laybet return - backbet stake) = (in this example, 15p, or desired profit.
Shit this is confusing. I'll give it a good dose of sleeping over and I'll have another go in six hours or so.
Thanks again guys - the truth is out there!
back bet: £3.00 x 1.50 = £4.50 gross
lay bet: £4.50 / 1.45 = £3.10 stake
If selection wins:
back bet = +£1.50
lay bet = -£1.40
net = £1.50 - £1.40 = +£0.10
If selection loses:
back bet = -£3.00
lay bet = +£3.10
net = £3.10 - £3.00 = +£0.10
Arrgh - sorry Peteb, I was working on gross returns, not net - obviously if the horse wins, you make a profit equal to the difference in stakes (which is what we want). I'll extract my head from my arse and try again:
Original bet is a stake of S at price P, and required margin is m
New Price, P' = P/(1+m)
Stake to Lay, S' = (1+m)S
(which is what everyone else was saying anyway)
Obviously, if P' isn't a valid price, go to the next shorter valid price, say P". In this case, the stake to lay will become:
Revised Stake to Lay, S" = SP/P" , rounded to the nearest penny, giving a revised margin of:
Revised margin, m" = (S"-S)/S , or equivalently m" = (P-P")/P"
Sorry to have added confusion with my earlier posts, too many years working with bookmakers' prices:-)
If I could go back to when I was 14/15 years old and meet the kid who was me back then, I would punch the little bastard and tell him to pay attention at school instead of concentrating on being an idiot. I hate being a little slow at this kind of thing.
Thanks again guys, I'll work through this lot today and post the code when I have it.
Alan
myrddin - yes that looks right to me - your maths is much better than mine - I can only think in excel formulae, not maths equations - yours is very neat and clear once I had figured out your notation!
Can I just check though - do we reckon the get-out price is 1.42 for 17p green?? Hope so, as that is obviously going to be easier to match than 1.38!
Yep, that's what I got (eventually)
Original bet is a stake of S at price P, and required margin is m
So S = £3.00 P = 1.5 m = 0.05
New Price, P' = P/(1+m) = 1.5/(1.05) = 1.4285714
P' isn't a valid price, go to the next shorter valid price, so P" = 1.42
Revised Stake to Lay, S" = SP/P" = (£3.00 x 1.5)/1.42 = £3.169014 = £3.17
(rounded to the nearest penny), giving a revised margin of:
Revised margin, m" = (S"-S)/S = (£3.17 - £3.00)/ £3.00 = 0.0567
Outstanding, thank you very much.
Following your calculations on paper was what did the trick myrddin. I think I understand that now. I'll do it a few more times with different sample bets/required margins and drive it into my head.
Thanks again,
Alan
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