Skip to content

Instantly share code, notes, and snippets.

@StefanBelo
Created December 3, 2019 11:55
Show Gist options
  • Save StefanBelo/5ca42bee0a4d1f8927c82bb845d1ecf6 to your computer and use it in GitHub Desktop.
Save StefanBelo/5ca42bee0a4d1f8927c82bb845d1ecf6 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 BfexplorerBot
//(*
#I @"C:\Program Files (x86)\BeloSoft\Bfexplorer\"
#r "BeloSoft.Data.dll"
#r "BeloSoft.Betfair.API.dll"
#r "BeloSoft.Bfexplorer.Domain.dll"
#r "BeloSoft.Bfexplorer.FootballScoreProvider.dll"
#r "BeloSoft.Bfexplorer.Trading.dll"
#r "BeloSoft.Bfexplorer.Service.Core.dll"
//*)
open System.Text
open BeloSoft.Data
open BeloSoft.Bfexplorer.Domain
open BeloSoft.Bfexplorer.Trading
open BeloSoft.Bfexplorer.FootballScoreProvider
open BeloSoft.Bfexplorer.FootballScoreProvider.API.Models
open BeloSoft.Bfexplorer.FootballScoreProvider.Models
/// <summary>
/// TriggerStatus
/// </summary>
type TriggerStatus =
| Initialize
| UpdateMatchScore
| FailedToUpdateMatchScore
| WaitToGetMatchDetails
| ReportMatchDetails
/// <summary>
/// FootballReportCardsAndGoalsBotTrigger
/// </summary>
type FootballReportCardsAndGoalsBotTrigger(market : Market, _selection : Selection, _botName : string, _botTriggerParameters : BotTriggerParameters, myBfexplorer : IMyBfexplorer) =
let mutable status = TriggerStatus.Initialize
let mutable footballMatch = nil<FootballMatch>
let mutable matchDetailData = nil<MatchDetailData>
let outputMessage message =
myBfexplorer.BfexplorerService.OutputMessage(message, market.Id)
let getMatchDetails() =
let footballScoreProvider = FootballScoreProvider.GetInstance(myBfexplorer.BfexplorerService)
Async.StartWithContinuations(
computation = footballScoreProvider.GetMatchDetails(footballMatch),
continuation = (fun result ->
status <-
if result.IsSuccessResult
then
matchDetailData <- result.SuccessResult
TriggerStatus.ReportMatchDetails
else
TriggerStatus.FailedToUpdateMatchScore
),
exceptionContinuation = (fun _ -> status <- TriggerStatus.FailedToUpdateMatchScore),
cancellationContinuation = (fun _ -> status <- TriggerStatus.FailedToUpdateMatchScore)
)
let footballMatchScoreUpdated (result : bool) =
status <-
if result
then
getMatchDetails()
TriggerStatus.WaitToGetMatchDetails
else
TriggerStatus.FailedToUpdateMatchScore
let initialize() =
if market.MarketInfo.BetEventType.Id = 1 && market.MarketDescription.MarketType = "MATCH_ODDS"
then
footballMatch <- toFootballMatch market
TriggerResult.UpdateFootballMatchScore (footballMatch, footballMatchScoreUpdated)
else
TriggerResult.EndExecutionWithMessage "You can execute this bot only on a football market."
let getGoals (updateDetails : UpdateDetailData[]) =
if updateDetails.Length > 0
then
updateDetails |> Array.map (fun updateDetail -> sprintf "%d' %s" updateDetail.MatchTime updateDetail.Team) |> String.concat ", "
else
"no goals scored"
let getCards team (updateDetails : UpdateDetailData[]) =
if updateDetails.Length > 0
then
updateDetails
|> Array.filter (fun updateDetail -> updateDetail.Team = team && updateDetail.UpdateType.Contains("Card"))
|> Array.map (fun updateDetail -> sprintf "%d' %s" updateDetail.MatchTime updateDetail.UpdateType) |> String.concat ", "
else
"no cards"
let reportMatchDetails() =
let sb = StringBuilder()
sb.AppendLine(footballMatch.ToString()) |> ignore
let goals = matchDetailData.UpdateDetails |> Array.filter (fun updateDetail -> updateDetail.UpdateType = "Goal")
if goals.Length > 0
then
let firstHalfGoals, secondHalfGoals = goals |> Array.partition (fun updateDetail -> updateDetail.MatchTime <= 45)
sb
.AppendLine(sprintf "The first half goals: %s" (getGoals firstHalfGoals))
.AppendLine(sprintf "The second half goals: %s" (getGoals secondHalfGoals))
else
sb
.AppendLine("No goals scored.")
|> ignore
let message =
sb
.AppendLine(sprintf "Home team cards: %s" (matchDetailData.UpdateDetails |> getCards "home"))
.AppendLine(sprintf "Away team cards: %s" (matchDetailData.UpdateDetails |> getCards "away"))
.ToString()
outputMessage message
TriggerResult.EndExecution
interface IBotTrigger with
/// <summary>
/// Execute
/// </summary>
member _this.Execute() =
match status with
| TriggerStatus.Initialize -> initialize()
| TriggerStatus.UpdateMatchScore -> UpdateFootballMatchScore (footballMatch, footballMatchScoreUpdated)
| TriggerStatus.FailedToUpdateMatchScore -> EndExecutionWithMessage "Failed to update the match score."
| TriggerStatus.WaitToGetMatchDetails -> WaitingForOperation
| TriggerStatus.ReportMatchDetails -> reportMatchDetails()
/// <summary>
/// EndExecution
/// </summary>
member _this.EndExecution() =
()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment