Skip to content

Instantly share code, notes, and snippets.

@StefanBelo
Created April 17, 2025 13:39
Show Gist options
  • Select an option

  • Save StefanBelo/cb5df0d3f78dc34cb7436c3670faff53 to your computer and use it in GitHub Desktop.

Select an option

Save StefanBelo/cb5df0d3f78dc34cb7436c3670faff53 to your computer and use it in GitHub Desktop.
The FootballNextGoalStrategyBotTrigger.fsx script defines a custom bot trigger for Bfexplorer, specifically designed for football "Next Goal" trading strategies.
// 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 @"E:\Projects\Bfexplorer\Development\Applications\BeloSoft.Bfexplorer.App\bin\Debug\net9.0-windows"
#I @"C:\Program Files\BeloSoft\Bfexplorer\"
#r "BeloSoft.Data.dll"
#r "BeloSoft.Betfair.API.dll"
#r "BeloSoft.Bfexplorer.Domain.dll"
#r "BeloSoft.Bfexplorer.Trading.dll"
#r "BeloSoft.Bfexplorer.Service.Core.dll"
#r "BeloSoft.Bfexplorer.FootballScoreProvider.dll"
//*)
open System
open BeloSoft.Data
open BeloSoft.Bfexplorer.Domain
open BeloSoft.Bfexplorer.Trading
open BeloSoft.Bfexplorer.FootballScoreProvider.Models
/// <summary>
/// TriggerStatus
/// </summary>
type TriggerStatus =
| Initialize
| UpdateMatchScore
| OpenNextGoalMarket of goals : byte
| ExecuteMyStrategy of market : Market
| WaitForUpdateOperation
| EndMyStrategy
/// <summary>
/// FootballNextGoalStrategyBotTrigger
/// </summary>
type FootballNextGoalStrategyBotTrigger (market : Market, _selection : Selection, _botName : string, botTriggerParameters : BotTriggerParameters, myBfexplorer : IMyBfexplorer) =
let matchTimeToExecute = defaultArg (botTriggerParameters.GetParameter<int> ("MatchTimeToExecute")) 79
let executeOnUnderSelection = defaultArg (botTriggerParameters.GetParameter<bool> ("ExecuteOnUnderSelection")) false
let mutable status = TriggerStatus.Initialize
let mutable footballMatch = nil<FootballMatch>
let mutable timeToUpdate = DateTime.MinValue
let bfexplorerService = myBfexplorer.BfexplorerService
let doReport message =
bfexplorerService.OutputMessage (message, market.Id)
let isMatchOddsMarket (market : Market) =
market.MarketInfo.BetEventType.Id = 1 && market.MarketDescription.MarketType = "MATCH_ODDS"
let canUpdateMatchScore () =
DateTime.Now >= timeToUpdate
let setNextUpdateMatchScore () =
status <- TriggerStatus.UpdateMatchScore
timeToUpdate <- DateTime.Now.AddSeconds 15.0
let matchScoreInitialized (result : bool) =
if result
then
setNextUpdateMatchScore ()
doReport (footballMatch.ToString ())
else
status <- TriggerStatus.EndMyStrategy
let getOverUnderMarket (numberOfGoals : byte) =
sprintf "OVER_UNDER_%d5" numberOfGoals
let getMarketTypesToOpen (numberOfGoals : byte) =
[|
getOverUnderMarket (numberOfGoals + 1uy)
|]
let matchScoreUpdated (result : bool) =
setNextUpdateMatchScore ()
if result && footballMatch.IsUpdated
then
if footballMatch.MatchTime >= matchTimeToExecute
then
status <- TriggerStatus.OpenNextGoalMarket (footballMatch.Goals)
doReport (footballMatch.ToString ())
let associatedMarketsOpened (result : DataResult<Market list>) =
status <-
if result.IsSuccessResult
then
TriggerStatus.ExecuteMyStrategy (result.SuccessResult.Head)
else
TriggerStatus.EndMyStrategy
let executeMyStrategy (myMarket : Market) =
if isOpenMarket myMarket
then
let mySelection = myMarket.Selections.[if executeOnUnderSelection then 0 else 1]
TriggerResult.ExecuteActionBotOnMarketSelectionAndContinueToExecute (myMarket, mySelection, false)
else
TriggerResult.WaitingForOperation
interface IBotTrigger with
/// <summary>
/// Execute
/// </summary>
member _this.Execute () =
match status with
| TriggerStatus.Initialize ->
if isMatchOddsMarket market
then
footballMatch <- toFootballMatch market
status <- TriggerStatus.WaitForUpdateOperation
TriggerResult.UpdateFootballMatchScore (footballMatch, matchScoreInitialized)
else
TriggerResult.EndExecutionWithMessage "You can execute this bot strategy only on a football match odds market."
| TriggerStatus.UpdateMatchScore ->
if canUpdateMatchScore ()
then
status <- TriggerStatus.WaitForUpdateOperation
TriggerResult.UpdateFootballMatchScore (footballMatch, matchScoreUpdated)
else
TriggerResult.WaitingForOperation
| TriggerStatus.OpenNextGoalMarket goals ->
status <- TriggerStatus.WaitForUpdateOperation
TriggerResult.OpenAssociatedMarkets (getMarketTypesToOpen goals, associatedMarketsOpened)
| TriggerStatus.ExecuteMyStrategy myMarket ->
executeMyStrategy myMarket
| TriggerStatus.WaitForUpdateOperation -> TriggerResult.WaitingForOperation
| TriggerStatus.EndMyStrategy -> TriggerResult.EndExecution
/// <summary>
/// EndExecution
/// </summary>
member _this.EndExecution () =
()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment