Collecting data through API

Post Reply
Diginet
Posts: 112
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: 1873
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: 112
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.
Post Reply

Return to “Betfair Exchange API”