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
