Skip to content

Instantly share code, notes, and snippets.

@StefanBelo
Created October 5, 2018 16:05
Show Gist options
  • Save StefanBelo/a8da1bd1162df9458fdc69f60b9f1fa9 to your computer and use it in GitHub Desktop.
Save StefanBelo/a8da1bd1162df9458fdc69f60b9f1fa9 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.Trading.dll"
#r "BeloSoft.Bfexplorer.Service.Core.dll"
open System
open BeloSoft.Data
open BeloSoft.Betfair
open BeloSoft.Bfexplorer.Domain
open BeloSoft.Bfexplorer.Trading
/// <summary>
/// BetfairSpPrices
/// </summary>
type BetfairSpPrices() =
member val NearPrice : float = 0.0 with get, set
member val FarPrice : float = 0.0 with get, set
member val ActualSP : float = 0.0 with get, set
static member DataKey = "bsp"
static member Set (selection : Selection) =
selection.SetData(BetfairSpPrices.DataKey, BetfairSpPrices())
static member Get (selection : Selection) =
selection.GetData<BetfairSpPrices>(BetfairSpPrices.DataKey)
static member GetProbabilityDifference (selection : Selection) =
match BetfairSpPrices.Get selection with
| Some betfairSpPrices ->
let winProbability = toPriceProbability selection.LastPriceTraded
let spProbability = toPriceProbability betfairSpPrices.ActualSP
((winProbability - spProbability) / winProbability) * 100.0
| None -> 0.0
/// <summary>
/// updateSelectionSpPrices
/// </summary>
/// <param name="selection"></param>
/// <param name="runner"></param>
let updateSelectionSpPrices (selection : Selection, runner : API.Models.Runner) =
BetfairSpPrices.Get selection
|> Option.iter (fun betfairSpPrices ->
let spPrices = runner.sp
betfairSpPrices.NearPrice <- spPrices.nearPrice
betfairSpPrices.FarPrice <- spPrices.farPrice
betfairSpPrices.ActualSP <- spPrices.actualSP
)
/// <summary>
/// TriggerStatus
/// </summary>
type TriggerStatus =
| Initialize
| WaitForInPlay
| WaitForUpdateSpPrices
| ReportResults
| TriggerActionBot
| ReportError of string
/// <summary>
/// HorseRacingFavouriteSpBotTrigger
/// </summary>
type HorseRacingFavouriteSpBotTrigger(market : Market, _selection : Selection, _botName : string, botTriggerParameters : BotTriggerParameters, myBfexplorer : IMyBfexplorer) =
let oddsDifference = defaultArg (botTriggerParameters.GetParameter<float>("OddsDifference")) 50.0
let time = defaultArg (botTriggerParameters.GetParameter<float>("Time")) 4.0
let mutable status = TriggerStatus.Initialize
let mutable myFavourite = nil<Selection>
let mutable fromTime = DateTime.MinValue
let outputMessage message =
myBfexplorer.BfexplorerService.OutputMessage(message, market.Id)
let isHorseRacingMarket() =
market.MarketInfo.BetEventType.Id = 7 && market.MarketDescription.MarketType = "WIN"
let loadSpPrices() =
async {
let! result = myBfexplorer.BfexplorerService.UpdateMarketData(market, API.Models.PriceProjection.DefaultBetfairSpPrice(), updateSelectionSpPrices)
status <-
if result.IsSuccessResult
then
TriggerStatus.ReportResults
else
TriggerStatus.ReportError result.FailureMessage
}
|> Async.Start
let showHorsesData() =
let selectionDataToString (selection : Selection) = maybe {
let! metaData = selection.MetaData
let! betfairSpPrices = BetfairSpPrices.Get selection
return sprintf "%s: %.2f | %.2f | %.2f" selection.Name selection.LastPriceTraded metaData.ForecastPrice betfairSpPrices.ActualSP
}
market.Selections
|> Seq.filter isNotRemovedSelection
|> Seq.choose selectionDataToString
|> String.concat "\n"
|> outputMessage
let getFavourite() =
market.Selections
|> Seq.filter isActiveSelection
|> Seq.sortBy (fun mySelection -> mySelection.LastPriceTraded)
|> Seq.head
let canExecuteActionBot() =
if (DateTime.Now - fromTime).TotalSeconds >= time
then
BetfairSpPrices.GetProbabilityDifference(myFavourite) >= oddsDifference
else
false
interface IBotTrigger with
/// <summary>
/// Execute
/// </summary>
member __.Execute() =
match status with
| TriggerStatus.Initialize ->
if isHorseRacingMarket()
then
status <- TriggerStatus.WaitForInPlay
market.Selections |> Seq.iter BetfairSpPrices.Set
TriggerResult.WaitingForOperation
else
TriggerResult.EndExecutionWithMessage "You can run this bot on a horse racing market only!"
| TriggerStatus.WaitForInPlay ->
if market.IsInPlay
then
status <- TriggerStatus.WaitForUpdateSpPrices
loadSpPrices()
TriggerResult.WaitingForOperation
| TriggerStatus.WaitForUpdateSpPrices -> TriggerResult.WaitingForOperation
| TriggerStatus.ReportResults ->
status <- TriggerStatus.TriggerActionBot
showHorsesData()
myFavourite <- getFavourite()
fromTime <- DateTime.Now
TriggerResult.WaitingForOperation
| TriggerStatus.TriggerActionBot ->
let favourite = getFavourite()
if favourite.Id = myFavourite.Id
then
if canExecuteActionBot()
then
TriggerResult.ExecuteActionBotOnSelection myFavourite
else
TriggerResult.WaitingForOperation
else
myFavourite <- favourite
fromTime <- DateTime.Now
TriggerResult.WaitingForOperation
| 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