Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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.Models
/// <summary>
/// TriggerStatus
/// </summary>
type TriggerStatus =
| Initialize
| WaitToUpdateMatchScore
| UpdateMatchScore
| ExecuteActionBotOnSelections of Selection list
| ReportError of string
| EndExecution
/// <summary>
/// FootballUnrealScoreStrategyBotTrigger
/// </summary>
type FootballUnrealScoreStrategyBotTrigger(market : Market, _selection : Selection, botName : string, _botTriggerParameters : BotTriggerParameters, myBfexplorer : IMyBfexplorer) =
let myActionBotName, canExecuteMyActionBot =
match MyBots.GetBot(botName) with
| Some myBot ->
let myActionBotName = (myBot.BotParameters :?> ExecuteTriggerBotParameters).BotName
myActionBotName, not (String.IsNullOrEmpty myActionBotName)
| None -> String.Empty, false
let mutable status = TriggerStatus.Initialize
let mutable footballMatch = nil<FootballMatch>
let mutable footballMatchResourceLocker = nil<ResourceLocker>
let mutable score = "0 - 0"
let outputMessage message =
myBfexplorer.BfexplorerService.OutputMessage(message, market.Id)
let isFootballMatch() =
market.MarketInfo.BetEventType.Id = 1
let matchOddsMarketOpened (result : DataResult<Market list>) =
status <-
if result.IsSuccessResult
then
footballMatch <- CreateFootballMatch result.SuccessResult.Head
footballMatchResourceLocker <- market |> CreateFootballMatchResourceLocker 20.0
TriggerStatus.WaitToUpdateMatchScore
else
TriggerStatus.ReportError result.FailureMessage
let canUpdateMatchData() =
market.IsInPlay && not footballMatchResourceLocker.IsLocked
let toScores (selection : Selection) =
let scoreParts = selection.Name.Split('-')
if scoreParts.Length = 2
then
let scores = scoreParts |> Array.map int
Some (scores.[0], scores.[1])
else
None
let getUnrealScoreSelections() =
let unrealHomeScore = int footballMatch.HomeScore - 1
let unrealAwayScore = int footballMatch.AwayScore - 1
market.Selections
|> Seq.filter (fun selection ->
match toScores selection with
| Some (homeScore, awayScore) -> homeScore <= unrealHomeScore || awayScore <= unrealAwayScore
| None -> false
)
|> Seq.toList
let canExecuteMyActionBotOnSelection (selection : Selection) =
let selectionId = selection.Id
market.RunningBots
|> Seq.filter (fun bot -> (isNotNullObj bot.RunningOnSelection) && bot.RunningOnSelection.Id = selectionId)
|> Seq.exists (fun bot -> bot.Name = myActionBotName)
|> not
let getSelectionsToExecuteActionBotOnUnrealScores() =
let selections = getUnrealScoreSelections()
selections
|> List.map (fun selection -> selection.Name)
|> String.concat "\n"
|> (sprintf "%A\nUnreal scores:\n%s" footballMatch)
|> outputMessage
if canExecuteMyActionBot
then
selections |> List.filter canExecuteMyActionBotOnSelection
else
list.Empty
let footballMatchScoreUpdated (result : bool) =
if result
then
status <- TriggerStatus.WaitToUpdateMatchScore
if footballMatch.IsUpdated
then
outputMessage (footballMatch.ToString())
let currentScore = footballMatch.Score
if score <> currentScore
then
score <- currentScore
let mySelections = getSelectionsToExecuteActionBotOnUnrealScores()
if not mySelections.IsEmpty
then
status <- TriggerStatus.ExecuteActionBotOnSelections mySelections
else
status <- TriggerStatus.ReportError "Failed to update the match score!"
interface IBotTrigger with
/// <summary>
/// Execute
/// </summary>
member _this.Execute() =
match status with
| TriggerStatus.Initialize ->
if isFootballMatch()
then
status <- TriggerStatus.WaitToUpdateMatchScore
TriggerResult.OpenAssociatedMarkets ([| "MATCH_ODDS" |], matchOddsMarketOpened)
else
TriggerResult.EndExecutionWithMessage "You can execute this bot only on a football market."
| TriggerStatus.WaitToUpdateMatchScore ->
if canUpdateMatchData()
then
status <- TriggerStatus.UpdateMatchScore
TriggerResult.WaitingForOperation
| TriggerStatus.UpdateMatchScore ->
footballMatchResourceLocker.Lock()
TriggerResult.UpdateFootballMatchScore (footballMatch, footballMatchScoreUpdated)
| TriggerStatus.ExecuteActionBotOnSelections mySelections ->
status <- TriggerStatus.WaitToUpdateMatchScore
TriggerResult.ExecuteActionBotOnSelectionsAndContinueToExecute (mySelections, true)
| TriggerStatus.ReportError errorMessage -> TriggerResult.EndExecutionWithMessage errorMessage
| TriggerStatus.EndExecution -> 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
You can’t perform that action at this time.