Skip to content

Instantly share code, notes, and snippets.

@StefanBelo
Created November 6, 2019 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StefanBelo/8c7d31771a8ada58df02c369e20ba918 to your computer and use it in GitHub Desktop.
Save StefanBelo/8c7d31771a8ada58df02c369e20ba918 to your computer and use it in GitHub Desktop.
// Bfexplorer cannot be held responsible for any losses or damages incurred during the use of this betfair bot.
// It is up to you to determine the level of risk you wish to trade under.
// Do not gamble with money you cannot afford to lose.
module TestBotTrigger
open System.Threading.Tasks
open BeloSoft.Data
open BeloSoft.Bfexplorer.Domain
open BeloSoft.Bfexplorer.Trading
open BeloSoft.AtpWorldTourProvider
open BeloSoft.AtpWorldTourProvider.Models
/// <summary>
/// toSome
/// </summary>
/// <param name="dataResult"></param>
let toSome (dataResult : DataResult<_>) =
if dataResult.IsSuccessResult
then
Some dataResult.SuccessResult
else
None
/// <summary>
/// playerBioToString
/// </summary>
/// <param name="dataResult"></param>
let playerBioToString (playerBio : PlayerBio) =
sprintf "Ranking: %d, Age: %d, Plays: %s" playerBio.Ranking playerBio.Age playerBio.Plays
/// <summary>
/// PlayerData
/// </summary>
type PlayerData =
{
Selection : Selection
PlayerBio : PlayerBio option
}
override this.ToString() =
match this.PlayerBio with
| Some playerBio -> sprintf "%s\n%s" this.Selection.Name (playerBioToString playerBio)
| None -> sprintf "%s | No data" this.Selection.Name
static member Create selection (dataResult : DataResult<PlayerBio>) =
{
Selection = selection
PlayerBio = toSome dataResult
}
/// <summary>
/// TriggerStatus
/// </summary>
type TriggerStatus =
| Initialize
| LoadTennisData
| WaitForTennisDataLoaded
| ReportResult of PlayerData * PlayerData
| ReportError of string
/// <summary>
/// TennisAtpDataBotTrigger
/// </summary>
type TennisAtpDataBotTrigger(market : Market, _selection : Selection, _botName : string, _botTriggerParameters : BotTriggerParameters, _myBfexplorer : IMyBfexplorer) =
let mutable triggerStatus = TriggerStatus.Initialize
let isTennisMatch() =
market.MarketInfo.BetEventType.Id = 2 && market.MarketDescription.MarketType = "MATCH_ODDS"
let reportError errorMessage =
triggerStatus <- TriggerStatus.ReportError errorMessage
let loadAtpData() =
Async.StartWithContinuations(
computation = async {
let tasks = market.Selections |> Seq.map (fun mySelection -> AtpWorldTourProvider.GetPlayerDetails(mySelection.Name) |> Async.StartAsTask)
let! results = Async.AwaitTask(Task.WhenAll(tasks))
return
if results |> Array.exists (fun result -> result.IsSuccessResult)
then
DataResult.Success results
else
DataResult.Failure "Failed to load ATP players data!"
},
continuation = (fun result ->
if result.IsSuccessResult
then
let selections = market.Selections
let dataResults = result.SuccessResult
triggerStatus <- TriggerStatus.ReportResult (PlayerData.Create selections.[0] dataResults.[0], PlayerData.Create selections.[1] dataResults.[1])
else
reportError result.FailureMessage
),
exceptionContinuation = (fun ex -> reportError ex.Message),
cancellationContinuation = (fun ex -> reportError ex.Message)
)
interface IBotTrigger with
/// <summary>
/// Execute
/// </summary>
member _.Execute() =
match triggerStatus with
| TriggerStatus.Initialize ->
if isTennisMatch()
then
triggerStatus <- TriggerStatus.LoadTennisData
TriggerResult.WaitingForOperation
else
TriggerResult.EndExecutionWithMessage "You can run this bot on a tennis match market only!"
| TriggerStatus.LoadTennisData ->
loadAtpData()
TriggerResult.WaitingForOperation
| TriggerStatus.WaitForTennisDataLoaded -> TriggerResult.WaitingForOperation
| TriggerStatus.ReportResult (playerOneData, playerTwoData) ->
let message = [ playerOneData; playerTwoData ] |> List.map (fun playerData -> sprintf "%s" (playerData.ToString())) |> String.concat "\n\n"
TriggerResult.EndExecutionWithMessage (sprintf "\n\n%s" message)
| TriggerStatus.ReportError errorMessage -> TriggerResult.EndExecutionWithMessage errorMessage
/// <summary>
/// EndExecution
/// </summary>
member _.EndExecution() =
()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment