Collecting data through API

Post Reply
Diginet
Posts: 119
Joined: Tue Jul 09, 2013 1:43 pm

Hi
How can I save my data through the api. I have successfully connected to the api and into a sql. It seems like it only sees data when markets are in guardian. The problem I have is the fields keep coming back empty. If a started basic with just last traded price how does it need to be captured in guardian for the api call to pull in into sql. ?

Thanks
sniffer66
Posts: 1879
Joined: Thu May 02, 2019 8:37 am

You only need the markets loaded in Guardian (which you can do via the API anyway)

To get Top3 back, Top 3 lay and LTP for a runner you'd use something like this:
You can extend that for Volume etc if you like

def get_market_price_ladders(self):
"""
Pulls top 3 back prices, top 3 lay prices, and LTP for all markets.
Returns structured dict:
{market_id: {selection_id: {...}}}
"""

url = f"{self.base_url}markets/v1.0/getMarketPrices"

payload = {
"dataRequired": ["EX_ALL_OFFERS", "LAST_TRADED", "STATUS"]
}

r = requests.post(url, headers=self.headers, json=payload, timeout=20)
r.raise_for_status()

data = r.json()

results = {}

for market in data.get("result", {}).get("markets", []):

market_id = str(market.get("id") or market.get("marketId"))
results[market_id] = {}

for sel in market.get("selections", []):

sid = int(sel.get("id"))

back_prices = []
lay_prices = []

# top 3 back
for offer in (sel.get("availableToBack") or [])[:3]:
back_prices.append({
"price": offer.get("price"),
"size": offer.get("size")
})

# top 3 lay
for offer in (sel.get("availableToLay") or [])[:3]:
lay_prices.append({
"price": offer.get("price"),
"size": offer.get("size")
})

ltp = sel.get("lastTradedPrice") or sel.get("ltp")

results[market_id][sid] = {
"ltp": ltp,
"back": back_prices,
"lay": lay_prices
}

return results
That would return:
{
'1.23456789': {
123456: {
'ltp': 5.2,
'back': [
{'price': 5.2, 'size': 120},
{'price': 5.1, 'size': 340},
{'price': 5.0, 'size': 210}
],
'lay': [
{'price': 5.3, 'size': 150},
{'price': 5.4, 'size': 90},
{'price': 5.5, 'size': 75}
]
}
}
}
I'm using Python but easy enough to port to whatever you are using

EDIT: You've mentioned Guardian so I've assumed you mean via the BA API and not directly to BF
Diginet
Posts: 119
Joined: Tue Jul 09, 2013 1:43 pm

Thank you yes through BA. I have setup the BF API it’s a delayed key. The data is pretty useless for anything meaningful from what I can make out. Thanks you for a quick reply.
Diginet
Posts: 119
Joined: Tue Jul 09, 2013 1:43 pm

Hi

How would you do this

You only need the markets loaded in Guardian (which you can do via the API anyway)

Everything I can find seems to be uploading the markets manually.

Cheers
sniffer66
Posts: 1879
Joined: Thu May 02, 2019 8:37 am


def apply_coupon(self, coupon_name="Todays_Horses"):
"""
Loads a Guardian coupon in Bet Angel.

Parameters
----------
coupon_name : str
Name of the coupon saved in Bet Angel Guardian.
"""

url = f"{self.base_url}guardian/v1.0/applyCoupon"

payload = {
"couponName": coupon_name,
"clearOption": "CLEAR_GUARDIAN_AND_WATCH_LIST",
"watchListNumber": 1
}

try:
r = requests.post(url, headers=self.headers, json=payload)
r.raise_for_status()
print("Coupon applied:", r.json())

except requests.RequestException as e:
print("Error applying coupon:", e)

Create a coupon in Guardian, then load the coupon via the API
Diginet
Posts: 119
Joined: Tue Jul 09, 2013 1:43 pm

You are a genius :) one final question my SQL keeps coming back null this is the problem I had yesterday. How do I get the api to pull the data
You do not have the required permissions to view the files attached to this post.
Aberdeentrad
Posts: 21
Joined: Sat Jan 06, 2024 7:07 pm

Diginet wrote:
Tue Mar 10, 2026 8:13 am
Hi
How can I save my data through the api. I have successfully connected to the api and into a sql. It seems like it only sees data when markets are in guardian. The problem I have is the fields keep coming back empty. If a started basic with just last traded price how does it need to be captured in guardian for the api call to pull in into sql. ?

Thanks

Thanks for starting this topic, has opened my eyes to capturing a lot more data than I have been previously.

Next step is to work out what to do with it, but a lot better than paying Betfair for it.
Diginet
Posts: 119
Joined: Tue Jul 09, 2013 1:43 pm

Its taken me a while of trial and error. I would suggest really working on an automation rule with guardian to export a csv file then manipulate the data into a sql. Early days but there are some promising patterns emerging. It started to seem a little complicated with the API when you can do it through automation. Anyways to give a little back to the community here is what I have created so far. Feel free to use amend as you wish. Tip you will need to change the exports file name parameter to your PC folder
V4 Last Traded Price.baf
The position in range calc needs some work here is how to do it correctly viewtopic.php?p=225722
You do not have the required permissions to view the files attached to this post.
Post Reply

Return to “Betfair Exchange API”