## Pages

### Greening algorithm for Betfair bots

I have seen many complex and convoluted "greening up" algorithms discussed on the forums. Here is the logic behind my greening up algorithm to help you along. Of course, this algorithm is for bot traders writing their own software. Manual traders will use the greening up function provided by their third party software.

Some algorithms that I know of for greening up in horse race trading utilise all the horses in a race, regardless of whether or not they were traded. This is not necessary. You need only balance the horses that you have traded.

The process is simply a matter of greening up each horse that you have traded individually. To do this we keep a track of all the trades we have made and calculate the average back and lay price for each traded horse and the volume traded. This gives us three figures; the average price backed, the average priced layed and profit/loss.

profit = (sum back prices * back volume) - (sum lay prices * lay volume)

The resulting value is positive for a profit and negative for a loss.

For example, if we back a horse at average odds of 3.5 with £100 volume then we will need to lay the horse for £102.94 if the odds are at 3.4 (see following image - click to enlarge) so that we distribute the £10 profit for that horse across the entire field.

The equation for reversing a trade and greening at the same time is given by

lay amount = backed amount +  (back profit / lay odds)

or

back amount = layed amount - (lay profit / back odds)

If you backed first then you use the lay amount equation to green up and if you layed first then you use the back amount equation to green up.

We have two choices with regards to greening a horse; we can either green as we go or leave it to the end. Manual traders nearly always green up at the end because it makes trading easier. A manual trader will back and lay in £100 sums because there are no calculations to make until the end. Also, if they are using third party software then the software will green the position for them when required. For bot programmers I would recommend greening up as you go. The advantage is that if a position turns after you have closed the trade then you will get a better price to green with and make slightly more profit.

We shall now add two more horses traded in the same race; one with a positive profit and one with a negative profit. Again we shall green them individually (see following images - click to enlarge).

The runner above was a losing trade (£100 backed and layed for a £10 liability) and so we spread the risk amongst all the runners with the following calculation,

balance a loss = (loss / back odds)

The value given is then used to place another back trade and thus spread the loss across the field.

Our third runner traded is another winning trade of £20. Here we have backed the horse for £100 and layed off for £107.69 and distribute the profit across the field.

Even though all of these trades are greened up individually, when combined they give us a balanced book (see following image - click to enlarge). Note that not all horses give the same profit. This is due to rounding and the fact that the smallest amount you can bet is 1p. I advise you to round your bet amount figures to the nearest 2nd decimal place as the API does not accept betting with fractions of a penny.

Using the Betfair API it is a simple task to add each new completed bet to a running total for each runner that gives a volume weighted average price, total volume traded and profit. With these figures it is easy to calcuate the exact size of bet for the closing leg of your trade. You can also carry out "what if" scenarios and have pre-sized trigger bets to build volume with a desired profit. Of course, you can mess around with the equations, if you so wish, to place all the profit on one horse and break-even on all the others or have a small amount of risk on one or more horses. However, unless you have inside information then market efficiency suggests that you have a balanced book.