This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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.Collections.Generic | |
open BeloSoft.Data | |
open BeloSoft.Bfexplorer.Domain | |
open BeloSoft.Bfexplorer.Trading | |
open BeloSoft.Bfexplorer.FootballScoreProvider.Models | |
/// <summary> | |
/// TriggerStatus | |
/// </summary> | |
type TriggerStatus = | |
| Initialize | |
| WaitToUpdateMatchScore | |
| UpdateMatchScore | |
| OpenCorrectScoreMarket | |
| CloseBetPosition | |
| ReportError of string | |
/// <summary> | |
/// FootballCloseFavouriteBetPositionBotTrigger | |
/// </summary> | |
type FootballCloseFavouriteBetPositionBotTrigger(market : Market, _selection : Selection, _botName : string, _botTriggerParameters : BotTriggerParameters, myBfexplorer : IMyBfexplorer) = | |
let mutable status = TriggerStatus.Initialize | |
let mutable footballMatch = nil<FootballMatch> | |
let mutable footballMatchResourceLocker = nil<ResourceLocker> | |
let mutable scoreDifference = 0y | |
let mutable favourite = nil<Selection> | |
let mutable correctScoreMarket = nil<Market> | |
let mutable executeOnMarketSelections = nil<Queue<MarketSelection>> | |
let canUpdateMatchData() = | |
market.IsInPlay && not footballMatchResourceLocker.IsLocked | |
let getNumberOfScoredGoals() = | |
footballMatch.HomeScore + footballMatch.AwayScore | |
let isAllowedScore() = | |
getNumberOfScoredGoals() <= 2uy | |
let isFavouriteFirstGoalOrDraw() = | |
if getNumberOfScoredGoals() = 2uy | |
then | |
Some false | |
else | |
let favouriteScoreDifference = | |
let selections = market.Selections | |
let homeTeam = selections.[0] | |
if favourite.Id = homeTeam.Id | |
then | |
1y | |
else | |
-1y | |
if favouriteScoreDifference = scoreDifference | |
then | |
Some true | |
else | |
None | |
let notValidScore() = | |
TriggerStatus.ReportError "Not valid score to execute my strategy!" | |
let getMarketSelectionsToClose() = | |
[ | |
{ MarketSelection.Market = market; MarketSelection.Selection = favourite } | |
{ MarketSelection.Market = correctScoreMarket; MarketSelection.Selection = correctScoreMarket.Selections.[5] (* 1 - 1 *) } | |
] | |
let footballMatchScoreUpdated (result : bool) = | |
if isNullObj correctScoreMarket | |
then | |
status <- TriggerStatus.WaitToUpdateMatchScore | |
if result && footballMatch.IsUpdated | |
then | |
if scoreDifference <> footballMatch.ScoreDifference | |
then | |
scoreDifference <- footballMatch.ScoreDifference | |
if isAllowedScore() | |
then | |
match isFavouriteFirstGoalOrDraw() with | |
| Some _isFavouriteFirstGoal -> | |
status <- TriggerStatus.OpenCorrectScoreMarket | |
| None -> () | |
else | |
status <- notValidScore() | |
else | |
status <- | |
if isAllowedScore() | |
then | |
scoreDifference <- footballMatch.ScoreDifference | |
executeOnMarketSelections <- Queue(getMarketSelectionsToClose()) | |
TriggerStatus.CloseBetPosition | |
else | |
notValidScore() | |
let initialize() = | |
if market.MarketInfo.BetEventType.Id = 1 && market.MarketDescription.MarketType = "MATCH_ODDS" | |
then | |
footballMatch <- CreateFootballMatch market | |
footballMatchResourceLocker <- market |> CreateFootballMatchResourceLocker 20.0 | |
favourite <- | |
let selections = market.Selections | |
let homeTeam = selections.[0] | |
let awayTeam = selections.[1] | |
if homeTeam.LastPriceTraded < awayTeam.LastPriceTraded | |
then | |
homeTeam | |
else | |
awayTeam | |
footballMatchResourceLocker.Lock() | |
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 | |
interface IBotTrigger with | |
/// <summary> | |
/// Execute | |
/// </summary> | |
member __.Execute() = | |
match status with | |
| TriggerStatus.Initialize -> initialize() | |
| TriggerStatus.WaitToUpdateMatchScore -> | |
if canUpdateMatchData() | |
then | |
status <- TriggerStatus.UpdateMatchScore | |
TriggerResult.WaitingForOperation | |
| TriggerStatus.UpdateMatchScore -> | |
footballMatchResourceLocker.Lock() | |
TriggerResult.UpdateFootballMatchScore (footballMatch, footballMatchScoreUpdated) | |
| TriggerStatus.OpenCorrectScoreMarket -> TriggerResult.OpenAssociatedMarkets ([| "CORRECT_SCORE" |], correctScoreMarketOpened) | |
| TriggerStatus.CloseBetPosition -> | |
let marketSelection = executeOnMarketSelections.Dequeue() | |
TriggerResult.ExecuteActionBotOnMarketSelectionAndContinueToExecute (marketSelection.Market, marketSelection.Selection, executeOnMarketSelections.Count > 0) | |
| 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