Betangel API (Python)

The Bet Angel API makes it easy for you to further enhance your betting and trading by integrating your own code into Bet Angel
Post Reply
PeterLe
Posts: 3715
Joined: Wed Apr 15, 2009 3:19 pm

I thought it would be worthwhile starting a new thread to share API code. In this case it is for Python (Maybe the admins will create a new category for the API Code?), one for Python, one for Java etc...
Anyway this is in response to someone who wanted to return the selection ID when inputting the selection name:

Code: Select all

import json
import requests

def post_to_ba(endpoint, raw_json):
    headers = {'Content-Type': 'application/json'}
    response = requests.post(endpoint, data=raw_json, headers=headers)
    return response.text

# Prompt the user for the selection name
selection_name = input("Enter the selection name you require the ID for: ")

# Make the request and store the response in a variable
response = post_to_ba("http://localhost:9000/api/markets/v1.0/getMarkets", '{"dataRequired":["ID","NAME","MARKET_START_TIME","EVENT_ID","EVENT_TYPE_ID","MARKET_TYPE","SELECTION_IDS","SELECTION_NAMES"]}')

# Parse the response text as JSON
response_json = json.loads(response)
print(response_json)

selections = response_json['result']['markets'][0]['selections']

# Search for the selection
for selection in selections:
    if selection['name'] == selection_name:
        selection_id = selection['id']
        print(f"Selection ID for {selection_name}: {selection_id}")
        break
else:
    print(f"{selection_name}, not found in the list of selections.")
    
If you've never used Python before, download pycharm (free) and simply copy the above into it.
You should get the grasp of it fairly quickly
User avatar
ShaunWhite
Posts: 9731
Joined: Sat Sep 03, 2016 3:42 am

29th of Dec and you're already making me feel guilty about not cracking on with work :roll: Python has been on my todo list so long the ink has faded. It's hard working, successful aspirational guys like you that make lazy guys like me feel bad. :D.
User avatar
Derek27
Posts: 23477
Joined: Wed Aug 30, 2017 11:44 am
Location: UK

PeterLe wrote:
Thu Dec 29, 2022 12:41 pm
(Maybe the admins will create a new category for the API Code?), one for Python, one for Java etc...
+1
PeterLe
Posts: 3715
Joined: Wed Apr 15, 2009 3:19 pm

ShaunWhite wrote:
Thu Dec 29, 2022 3:36 pm
29th of Dec and you're already making me feel guilty about not cracking on with work :roll: Python has been on my todo list so long the ink has faded. It's hard working, successful aspirational guys like you that make lazy guys like me feel bad. :D.
It was either that or go visit the mother in law :D
User avatar
ilovepizza82
Posts: 490
Joined: Thu Nov 02, 2017 3:41 pm
Location: Sewers
Contact:

Yea thanks.
I was actually thinking about requesting the new, seperate place for python coders as well
You beat me to it ! :)
PeterLe
Posts: 3715
Joined: Wed Apr 15, 2009 3:19 pm

Correction to the code in the opening post :

Code: Select all

import json
import requests


def post_to_ba(endpoint, raw_json):
    headers = {'Content-Type': 'application/json'}
    response = requests.post(endpoint, data=raw_json, headers=headers)
    return response.text


# Prompt the user for the selection name
selection_name = input("Enter the selection name you require the ID for: ")

# Make the request and store the response in a variable
response = post_to_ba("http://localhost:9000/api/markets/v1.0/getMarkets",
                      '{"dataRequired":["ID","NAME","MARKET_START_TIME","EVENT_ID","EVENT_TYPE_ID","MARKET_TYPE","SELECTION_IDS","SELECTION_NAMES"]}')

# Parse the response text as JSON
response_json = json.loads(response)
print(response_json)

# Initialize an empty list to store the matching selection names and IDs
matching_selections = []

# Search for the selection in ALL markets
for market in response_json['result']['markets']:
    for selection in market['selections']:
        if selection['name'].startswith(selection_name):
            matching_selections.append({'name': selection['name'], 'id': selection['id']})

# Print the name and ID of each matching selection

if matching_selections:
    for selection in matching_selections:
        print(f"Selection name: {selection['name']}, Selection ID: {selection['id']}")
else:
    print(f"No selections found with names starting with {selection_name}.")
    
toptrader
Posts: 77
Joined: Sun Jan 31, 2010 9:58 pm

is there any documentation for BA API?
i.e. to get guardian to remove suspended markets then add all horse racing markets for the day?
(in python ideally)
sniffer66
Posts: 1666
Joined: Thu May 02, 2019 8:37 am

toptrader wrote:
Sun Jan 01, 2023 6:16 pm
is there any documentation for BA API?
i.e. to get guardian to remove suspended markets then add all horse racing markets for the day?
(in python ideally)
https://www.betangel.com/api-guide/

if you look at the python code above it's pretty simple to edit to use the correct endpoints and JSON to add\remove markets

In BA, settings,, BA API tab, you'll see a link to a test page that will give you the endpoints you need
toptrader
Posts: 77
Joined: Sun Jan 31, 2010 9:58 pm

thanks so much.... but I don't have the API tab in my Bet Angel settings to enable this??
i'm running 1.59.2 which is the latest version on the website for download
can you help?
thanks in advance.
Michael5482
Posts: 1218
Joined: Fri Jan 14, 2022 8:11 pm

Spending the last 3 or so months honing my Excel skills I was going to move onto to VBA but with the Bet Angel API addition I think I'll jump straight into Python

Does anyone have any book or online Python learning material they'd recommend

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

toptrader wrote:
Tue Jan 03, 2023 7:55 am
thanks so much.... but I don't have the API tab in my Bet Angel settings to enable this??
i'm running 1.59.2 which is the latest version on the website for download
can you help?
thanks in advance.
Try here:

https://www.betangel.com/api-guide/acti ... 3D&mw=MzIw

Definitely in the latest version
User avatar
Euler
Posts: 24701
Joined: Wed Nov 10, 2010 1:39 pm
Location: Bet Angel HQ

toptrader wrote:
Tue Jan 03, 2023 7:55 am
thanks so much.... but I don't have the API tab in my Bet Angel settings to enable this??
i'm running 1.59.2 which is the latest version on the website for download
can you help?
thanks in advance.
It's about to go into full release, but version 1.60 is available on this thread: -

viewtopic.php?f=46&t=26591
toptrader
Posts: 77
Joined: Sun Jan 31, 2010 9:58 pm

thanks for this.... all up and fully automated now :)
PeterLe
Posts: 3715
Joined: Wed Apr 15, 2009 3:19 pm

toptrader wrote:
Fri Jan 06, 2023 3:17 pm
thanks for this.... all up and fully automated now :)
Good Stuff, (Did you get it to remove suspended markets and add in new one ones for he day?)
i haven't had much free time this week

are you able to post your code for others to use/learn from ?
Thanks
toptrader
Posts: 77
Joined: Sun Jan 31, 2010 9:58 pm

the examples on the API link in the settings in BA pretty much give you all you need to do

Python code below for the BA part of it

I had to use the Betfair API to get a list of new markets for the day and filter the ones i wanted to get their IDs, but then I add them to guardian in the last line of code below ('marketids') and it works a treat!! Amazing. now i just need to get the strategy working even more consistently


def post_to_ba(endpoint, raw_json):
headers = {'Content-Type': 'application/json'}
response = requests.post(endpoint, data=raw_json, headers=headers)
return response.text

# remove markets
apiresponse = post_to_ba("http://localhost:9000/api/guardian/v1.0/removeMarkets", '{"marketsFilter":{"filter":"ALL"},"marketStatus":"ANY"}')
print(f'remove markets: {apiresponse}')

# add new markets
apiresponse = post_to_ba("http://localhost:9000/api/guardian/v1.0/addMarkets", '{"marketIds":' + str(marketids) + '}')
print(f'add markets: {apiresponse}')
Post Reply

Return to “Bet Angel - API”