Football scores to Guardian programatically - Script & Sample BAF

Football, Soccer - whatever you call it. It is the beautiful game.
Post Reply
sniffer66
Posts: 848
Joined: Thu May 02, 2019 8:37 am

For anyone using the new beta you'll be aware that it is now possible to pass stored values to Guardian using a local CSV file. I don't trade football automated but I know getting accurate scoring into Guardian is problematic and involves workarounds based on price moves in various markets etc

I was reading a few threads this morning and had a quick play with my script that pulls tennis in play data to a CSV. A few edits and I was able to grab the current teams\scores from the Sofascore API and save a to a CSV to import regularly into Guardian (every 30s)

Posting up here and hopefully this may help a few of you

Script:

This runs in a language called Auto-It. It's a fork of AutoHotKey and is pretty powerful.
To run the script you will need to download the program and Scite Editor from here:

https://www.autoitscript.com/site/autoit/downloads/

Once installed, paste the code below into the Scite Editor and save to a local folder as Football.au3 (or whatever name you wish)

Then, in Scite do "Tools - Go" and the script will start

The script will run in a 30s timed loop, pulling down the team names and current scores for all currently LIVE games in Sofascore. It will then save those results to a file called "score.csv"

I've attached a sample baf file that is then configured to pull the data from the CSV into Guardian on a timed 5s cycle. It will match the wildcarded team name to each selection name and create a stored value for each Selection called "Score"
I've left the log updates in place for the SV score values but you may want to turn those off when you are confident, as they will fill your log up
Just browse in the baf parameters tab to the folder you saved the script in and select score.csv

Once you have the SV's in Guardian you can use them in rules in any way you like i.e S1 Score + S2 Score > 0 (first time) is 1 goal scored, hardcoded score of 2-1, 1-3 etc etc Using a signal on either SV changing in x seconds to wait for the market to settle post goal. Lots of possibilities....

NOTES:
Be careful adjusting the speed of the script loop ( line 54). Lower than 10s and you risk your IP being blocked by SofaScore for too many accesses per minute.
Its possible to compile the script as an exe in Scite so you can just double click it whenever you have footy automation running. Scite - Tools - Compile. Just go with the defaults. Remmber to kill (in the Task bar) when trading is over.
There is a tiny CPU overhead for the running script and shouldn't cause any performance issues, runs in a split second every 30s

****************
Because of the way this is designed, there may be the odd file lockout where both Guardian and the script are trying to read/write the CSV file at the same time. BetAngel Support have confirmed (further down the thread) that in this case Guardian will only read and use the entire file when unlocked. There is no chance of partial reads of the data and a problem with the SV's. In this case, the SV's will just update when the file is unlocked again i.e the script has finished writing. If this happens you will see a corresponding log entry in Guardian
*******

Using the same API query process and a little work it's possible to pull down all the in play stats available on SofaScore and pass those as SV's in the same CSV. Which would make Guardian footbal automation pretty powerful ;)

Shout if you need any help
Stu

Code: Select all


#include <Inet.au3>
#include <json.au3>
#include <Array.au3>
#include <String.au3>
#include <File.au3>

$TitleString = "Team|Score"

;File to connect Guardian to. Will save in same location as script
$File = "score.csv"


While 1

	$URL = "https://api.sofascore.com/api/v1/sport/football/events/live"
	$data = _INetGetSource($URL)
	$object = json_decode($data)

	Local $Count = Json_Get($object, '.events')

	FileDelete($File)
	FileOpen($File)

	$z = 0

	For $i = 0 To UBound($Count) - 1

		;Writing "1" to first line of the CSV. Required by Guardian
		If $z = 0 Then
			FileWriteLine($File, "1")
		EndIf
		$z = 1


		$HomeString = "*,*," & "*" & Json_Get($object, '.events' & '[' & $i & '].homeTeam.name') & "*,*,"
		$AwayString = "*,*," & "*" & Json_Get($object, '.events' & '[' & $i & '].awayTeam.name') & "*,*,"

		$HomeString = $HomeString & "S,Score," & Json_Get($object, '.events' & '[' & $i & '].homeScore.current')
		$AwayString = $AwayString & "S,Score," & Json_Get($object, '.events' & '[' & $i & '].awayScore.current')

		;MsgBox($MB_SYSTEMMODAL, "Score", $HomeString & @CRLF & $AwayString)
		FileWriteLine($File,$HomeString)
		FileWriteLine($File,$AwayString)


	Next

	FileClose($File)

	;Wait time between score refresh in milliseconds
	;#######DO NOT SET SHORTER THAN 10S AS YOUR IP MAY BE BLOCKED#####

	Sleep(30000) ;30 seconds


WEnd


Sample BAF File
Football_Scores.baf
You do not have the required permissions to view the files attached to this post.
Last edited by sniffer66 on Fri Jan 29, 2021 12:12 pm, edited 2 times in total.
User avatar
jimibt
Posts: 3164
Joined: Mon Nov 30, 2015 6:42 pm
Location: South West Scotland

Nice one Stu -this will be a very useful jump-on point for folks using the csv->sv functionality... i can see this being easily adapted for use on the greys using sportinglife!!

thanks!!
sniffer66
Posts: 848
Joined: Thu May 02, 2019 8:37 am

jimibt wrote:
Fri Jan 29, 2021 9:46 am
Nice one Stu -this will be a very useful jump-on point for folks using the csv->sv functionality... i can see this being easily adapted for use on the greys using sportinglife!!

thanks!!
Cheers Jim, Will keep me/us busy. Opens up so many possibilities....
User avatar
Euler
Posts: 21556
Joined: Wed Nov 10, 2010 1:39 pm
Location: Bet Angel HQ
Contact:

Very good, thanks for posting.
User avatar
goat68
Posts: 1434
Joined: Tue Jun 30, 2020 3:53 pm
Location: Hampshire, UK

Nice idea.

There's possibly a slight issue depending on how BA Guardian Windows File locking is implemented:
1) Race condition between the external script "deleting" the file and re-creating it and Guardian rule set reading a non-existant or partial file
2) DeleteFile may fail if Guardian currently has a FileLock, depending on how they have implemented it

Ideally for this to be repeatable some sort of Windows File Locking contract needs using. So not relying on deleting/recreating the file, but locking the file for update and Guardian doing something similar.

I'm sure it will work most of the time, but you may get intermittent glitches where Guardian doesn't find the file, or gets an error reading it.
sniffer66
Posts: 848
Joined: Thu May 02, 2019 8:37 am

goat68 wrote:
Fri Jan 29, 2021 10:39 am
Nice idea.

There's possibly a slight issue depending on how BA Guardian Windows File locking is implemented:
1) Race condition between the external script "deleting" the file and re-creating it and Guardian rule set reading a non-existant or partial file
2) DeleteFile may fail if Guardian currently has a FileLock, depending on how they have implemented it

Ideally for this to be repeatable some sort of Windows File Locking contract needs using. So not relying on deleting/recreating the file, but locking the file for update and Guardian doing something similar.

I'm sure it will work most of the time, but you may get intermittent glitches where Guardian doesn't find the file, or gets an error reading it.
Agreed.

With tennis, I create a temp file with all lines in and do a quick overwrite of the main csv in one hit. Seeing as score updates arent required as fast on footy I didnt think necessary. I get the odd lockout but it runs fine on the next refresh.
Can update with the above if you think it worth it.....
Bet Angel
Bet Angel
Bet Angel
Posts: 3856
Joined: Tue Apr 14, 2009 3:47 pm

goat68 wrote:
Fri Jan 29, 2021 10:39 am
Nice idea.

There's possibly a slight issue depending on how BA Guardian Windows File locking is implemented:
1) Race condition between the external script "deleting" the file and re-creating it and Guardian rule set reading a non-existant or partial file
2) DeleteFile may fail if Guardian currently has a FileLock, depending on how they have implemented it

Ideally for this to be repeatable some sort of Windows File Locking contract needs using. So not relying on deleting/recreating the file, but locking the file for update and Guardian doing something similar.

I'm sure it will work most of the time, but you may get intermittent glitches where Guardian doesn't find the file, or gets an error reading it.
From Guardian's point of view, it reads the entire file into memory before processing, so if it was locked out it would just fail to update any of the stored values for that cycle; so no risk in just some of the stored values being updated. You may get a note of the error written to the log, but it's not going to cause any harm.
sniffer66
Posts: 848
Joined: Thu May 02, 2019 8:37 am

Bet Angel wrote:
Fri Jan 29, 2021 10:50 am
goat68 wrote:
Fri Jan 29, 2021 10:39 am
Nice idea.

There's possibly a slight issue depending on how BA Guardian Windows File locking is implemented:
1) Race condition between the external script "deleting" the file and re-creating it and Guardian rule set reading a non-existant or partial file
2) DeleteFile may fail if Guardian currently has a FileLock, depending on how they have implemented it

Ideally for this to be repeatable some sort of Windows File Locking contract needs using. So not relying on deleting/recreating the file, but locking the file for update and Guardian doing something similar.

I'm sure it will work most of the time, but you may get intermittent glitches where Guardian doesn't find the file, or gets an error reading it.
From Guardian's point of view, it reads the entire file into memory before processing, so if it was locked out it would just fail to update any of the stored values for that cycle; so no risk in just some of the stored values being updated. You may get a note of the error written to the log, but it's not going to cause any harm.
Yes, thats exactly what I'm seeing in tennis. The odd lockout log entry
sniffer66
Posts: 848
Joined: Thu May 02, 2019 8:37 am

sniffer66 wrote:
Fri Jan 29, 2021 10:57 am
Bet Angel wrote:
Fri Jan 29, 2021 10:50 am
goat68 wrote:
Fri Jan 29, 2021 10:39 am
Nice idea.

There's possibly a slight issue depending on how BA Guardian Windows File locking is implemented:
1) Race condition between the external script "deleting" the file and re-creating it and Guardian rule set reading a non-existant or partial file
2) DeleteFile may fail if Guardian currently has a FileLock, depending on how they have implemented it

Ideally for this to be repeatable some sort of Windows File Locking contract needs using. So not relying on deleting/recreating the file, but locking the file for update and Guardian doing something similar.

I'm sure it will work most of the time, but you may get intermittent glitches where Guardian doesn't find the file, or gets an error reading it.
From Guardian's point of view, it reads the entire file into memory before processing, so if it was locked out it would just fail to update any of the stored values for that cycle; so no risk in just some of the stored values being updated. You may get a note of the error written to the log, but it's not going to cause any harm.
Yes, thats exactly what I'm seeing in tennis. The odd lockout log entry. But I'm rereshing in Guardian a hell of a lot faster than for footy and data is getting in quickly enough
User avatar
goat68
Posts: 1434
Joined: Tue Jun 30, 2020 3:53 pm
Location: Hampshire, UK

Bet Angel wrote:
Fri Jan 29, 2021 10:50 am
goat68 wrote:
Fri Jan 29, 2021 10:39 am
Nice idea.

There's possibly a slight issue depending on how BA Guardian Windows File locking is implemented:
1) Race condition between the external script "deleting" the file and re-creating it and Guardian rule set reading a non-existant or partial file
2) DeleteFile may fail if Guardian currently has a FileLock, depending on how they have implemented it

Ideally for this to be repeatable some sort of Windows File Locking contract needs using. So not relying on deleting/recreating the file, but locking the file for update and Guardian doing something similar.

I'm sure it will work most of the time, but you may get intermittent glitches where Guardian doesn't find the file, or gets an error reading it.
From Guardian's point of view, it reads the entire file into memory before processing, so if it was locked out it would just fail to update any of the stored values for that cycle; so no risk in just some of the stored values being updated. You may get a note of the error written to the log, but it's not going to cause any harm.
That's good to know. So with the current script above, the risk is Guardian attempts to read the file when it is Deleted/Re-created, but presumably Guardian will just not update the current SV values, it's not going to reset or clear them is it?
Post Reply

Return to “Football trading”