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
Collecting data through API
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
EDIT: You've mentioned Guardian so I've assumed you mean via the BA API and not directly to BF
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
That would return: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
I'm using Python but easy enough to port to whatever you are using{
'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}
]
}
}
}
EDIT: You've mentioned Guardian so I've assumed you mean via the BA API and not directly to BF
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
-
Aberdeentrad
- Posts: 21
- Joined: Sat Jan 06, 2024 7:07 pm
Diginet wrote: ↑Tue Mar 10, 2026 8:13 amHi
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.
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
The position in range calc needs some work here is how to do it correctly viewtopic.php?p=225722
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.
