Skip to content

Instantly share code, notes, and snippets.

@StefanBelo
Created June 25, 2020 16:17
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/cf0a6b442c19f83cc17d22238c1729c8 to your computer and use it in GitHub Desktop.
Save StefanBelo/cf0a6b442c19f83cc17d22238c1729c8 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 BeloSoft.Data
open BeloSoft.Betfair
open BeloSoft.Bfexplorer.Domain
open BeloSoft.Bfexplorer.Trading
/// <summary>
/// SelectionData
/// </summary>
type SelectionData(selection : Selection) =
let mutable factor = 0.0
member __.Selection
with get() = selection
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
member __.Factor
with get() = factor
member this.CalculateFactor() =
factor <- (toPriceProbability this.FarPrice) - (toPriceProbability this.NearPrice)
static member DataKey = "SelectionData"
static member Set (selection : Selection) =
selection.SetData(SelectionData.DataKey, SelectionData(selection))
static member Get (selection : Selection) =
selection.GetData<SelectionData>(SelectionData.DataKey)
[<AutoOpen>]
module Helpers =
let updateSelectionSpPrice (selection : Selection, runner : API.Models.Runner) =
SelectionData.Get selection
|> Option.iter (fun selectionData ->
let spPrices = runner.sp
let spPrice = spPrices.actualSP
if spPrice = 0.0
then
selectionData.NearPrice <- spPrices.nearPrice
selectionData.FarPrice <- spPrices.farPrice
else
selectionData.ActualSP <- spPrice
selectionData.CalculateFactor()
)
/// <summary>
/// TriggerStatus
/// </summary>
type TriggerStatus =
| Initialize
| WaitForUpdateSpPrices
| TriggerMyActionBot
| ReportError of string
/// <summary>
/// HorseRacingFarPriceBotTrigger
/// </summary>
type HorseRacingFarPriceBotTrigger(market : Market, _selection : Selection, _botName : string, _botTriggerParameters : BotTriggerParameters, myBfexplorer : IMyBfexplorer) =
let mutable status = TriggerStatus.Initialize
let isHorseRacingMarket() =
market.MarketInfo.BetEventType.Id = 7 && market.MarketDescription.MarketType = "WIN"
let createBetfairSpPrices() =
market.Selections
|> Seq.filter isActiveSelection
|> Seq.iter SelectionData.Set
let updateBetfairSpPrices() =
async {
let! result = myBfexplorer.BfexplorerService.UpdateMarketData(market, API.Models.PriceProjection.DefaultBetfairSpPrice(), updateSelectionSpPrice)
status <-
if result.IsSuccessResult
then
TriggerStatus.TriggerMyActionBot
else
TriggerStatus.ReportError result.FailureMessage
}
|> Async.Start
let outputMessage message =
myBfexplorer.BfexplorerService.OutputMessage(message, market.Id)
let reportData (selectionDatas : SelectionData list) =
selectionDatas
|> List.map (fun selectionData ->
let selection = selectionData.Selection
sprintf "%s: %.2f - %.2f | %.3f" selection.Name selection.LastPriceTraded selectionData.FarPrice selectionData.Factor
)
|> String.concat "\n"
|> outputMessage
let getMySelection() = maybe {
let selectionDatas =
market.Selections
|> Seq.choose SelectionData.Get
|> Seq.sortByDescending (fun selectionData -> selectionData.Factor)
|> Seq.toList
reportData selectionDatas
return selectionDatas.[0].Selection
}
interface IBotTrigger with
/// <summary>
/// Execute
/// </summary>
member __.Execute() =
match status with
| TriggerStatus.Initialize ->
if isHorseRacingMarket()
then
status <- TriggerStatus.WaitForUpdateSpPrices
createBetfairSpPrices()
updateBetfairSpPrices()
TriggerResult.WaitingForOperation
else
TriggerResult.EndExecutionWithMessage "You can run this bot on a horse racing market only!"
| TriggerStatus.WaitForUpdateSpPrices -> TriggerResult.WaitingForOperation
| TriggerStatus.TriggerMyActionBot ->
match getMySelection() with
| Some mySelection -> TriggerResult.ExecuteActionBotOnSelection mySelection
| None -> TriggerResult.EndExecution
| 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