Skip to content

Instantly share code, notes, and snippets.

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/e166b9b06eceac59fed687f1ed6ebeef to your computer and use it in GitHub Desktop.
Save StefanBelo/e166b9b06eceac59fed687f1ed6ebeef 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
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
| WaitToUpdateMatchScore
| UpdateMatchScore
| FailedToUpdateMatchScore
| WaitToGetMatchDetails
| OpenCorrectScoreMarket
| ExecuteActionBot
| ReportError of string
/// <summary>
/// FootballSecondHalfKickOffCorrectScoreBotTrigger
/// </summary>
type FootballSecondHalfKickOffCorrectScoreBotTrigger(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 mutable timeToExecute = DateTime.MaxValue
let mutable correctScoreMarket = nil<Market>
let setTimeToExecute() =
timeToExecute <- DateTime.Now.AddSeconds(30.0)
let isSecondHalfKickedOff (updateDetails : UpdateDetailData[]) =
if updateDetails.Length > 0
then
updateDetails |> Array.exists (fun updateDetail -> updateDetail.UpdateType = "SecondHalfKickOff")
else
false
let footballMatchScoreUpdated (result : bool) =
status <-
if result
then
Async.StartWithContinuations(
computation = FootballScoreProvider.GetMatchDetails(footballMatch),
continuation = (fun result ->
status <-
if result.IsSuccessResult
then
matchDetailData <- result.SuccessResult
if isNotNullObj correctScoreMarket
then
TriggerStatus.ExecuteActionBot
else
setTimeToExecute()
if isSecondHalfKickedOff matchDetailData.UpdateDetails
then
TriggerStatus.OpenCorrectScoreMarket
else
TriggerStatus.WaitToUpdateMatchScore
else
TriggerStatus.FailedToUpdateMatchScore
),
exceptionContinuation = (fun _ -> status <- TriggerStatus.FailedToUpdateMatchScore),
cancellationContinuation = (fun _ -> status <- TriggerStatus.FailedToUpdateMatchScore)
)
TriggerStatus.WaitToGetMatchDetails
else
TriggerStatus.FailedToUpdateMatchScore
let initialize() =
if market.MarketInfo.BetEventType.Id = 1 && market.MarketDescription.MarketType = "MATCH_ODDS"
then
footballMatch <- CreateFootballMatch market
TriggerResult.UpdateFootballMatchScore (footballMatch, footballMatchScoreUpdated)
else
TriggerResult.EndExecutionWithMessage "You can execute this bot only on a football market."
let correctScoreMarketOpened (result : DataResult<Market list>) =
status <-
if result.IsSuccessResult
then
correctScoreMarket <- result.SuccessResult.Head
TriggerStatus.UpdateMatchScore
else
TriggerStatus.ReportError result.FailureMessage
let getScoreSelection() =
let score = matchDetailData.Score
let selectionName = sprintf "%s - %s" score.Home.Score score.Away.Score
correctScoreMarket.Selections
|> Seq.tryFind (fun selection -> selection.Name = selectionName)
interface IBotTrigger with
/// <summary>
/// Execute
/// </summary>
member __.Execute() =
match status with
| TriggerStatus.Initialize -> initialize()
| TriggerStatus.WaitToUpdateMatchScore ->
if DateTime.Now >= timeToExecute
then
status <- TriggerStatus.UpdateMatchScore
TriggerResult.WaitingForOperation
| TriggerStatus.UpdateMatchScore -> TriggerResult.UpdateFootballMatchScore (footballMatch, footballMatchScoreUpdated)
| TriggerStatus.FailedToUpdateMatchScore -> TriggerResult.EndExecutionWithMessage "Failed to update the match score."
| TriggerStatus.WaitToGetMatchDetails -> TriggerResult.WaitingForOperation
| TriggerStatus.OpenCorrectScoreMarket -> TriggerResult.OpenAssociatedMarkets ([| "CORRECT_SCORE" |], correctScoreMarketOpened)
| TriggerStatus.ExecuteActionBot ->
match getScoreSelection() with
| Some selection -> TriggerResult.ExecuteActionBotOnMarketSelectionAndContinueToExecute (correctScoreMarket, selection, false)
| None -> TriggerResult.EndExecutionWithMessage "Score selection is not available!"
| 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