Skip to content

Instantly share code, notes, and snippets.

@StefanBelo
Created October 20, 2019 13:25
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/6b3d8479177b20de793929755a55966c to your computer and use it in GitHub Desktop.
Save StefanBelo/6b3d8479177b20de793929755a55966c to your computer and use it in GitHub Desktop.
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>
/// MySelectionData
/// </summary>
type MySelectionData(selection : Selection) =
let mutable indexIndicator = -1
let setTradeStatus tradeStatus index =
let priceData = selection.PriceGridData.GetPriceData(index)
priceData.TradeStatus <- tradeStatus
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 this.UpdateData() =
let oddsContext = selection.OddsContext
let price = oddsContext.GetValidOdds(if this.ActualSP = 0.0 then this.NearPrice else this.ActualSP)
let index = oddsContext.GetOddsIndex(price)
if index <> indexIndicator
then
index |> setTradeStatus TradeStatus.High1Level
if indexIndicator <> -1
then
indexIndicator |> setTradeStatus TradeStatus.None
indexIndicator <- index
static member DataKey = "MySelectionData"
static member Set (selection : Selection) =
let mySelectionData = MySelectionData(selection)
selection.SetData(MySelectionData.DataKey, mySelectionData)
mySelectionData
static member UpdateSpPrices (selection : Selection, runner : API.Models.Runner) =
selection.GetData<MySelectionData>(MySelectionData.DataKey)
|> 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
| UpdateData
| WaitForUpdateData
| ReportResults
| ReportError of string
/// <summary>
/// HorseRacingShowStartPrices
/// </summary>
type HorseRacingShowStartPrices(market : Market, _selection : Selection, _botName : string, _botTriggerParameters : BotTriggerParameters, myBfexplorer : IMyBfexplorer) =
let mutable triggerStatus = TriggerStatus.Initialize
let mutable mySelectionDatas = nil<MySelectionData list>
let mutable lastTimeUpdated = DateTime.MinValue
let isHorseRacingMarket() =
market.MarketInfo.BetEventType.Id = 7 && market.MarketDescription.MarketType = "WIN"
let initializeMySelectionDatas() =
market.Selections
|> Seq.filter isActiveSelection
|> Seq.map (fun mySelection ->
mySelection.PriceGridDataEnabled <- true
MySelectionData.Set mySelection
)
|> Seq.toList
let isTimeToUpdateBetfairSpPrices() =
(DateTime.Now - lastTimeUpdated).TotalSeconds >= 2.0
let updateBetfairSpPrices() =
triggerStatus <- TriggerStatus.WaitForUpdateData
async {
let! result = myBfexplorer.BfexplorerService.UpdateMarketData(market, API.Models.PriceProjection.DefaultBetfairSpPrice(), MySelectionData.UpdateSpPrices)
lastTimeUpdated <- DateTime.Now
triggerStatus <-
if result.IsSuccessResult
then
TriggerStatus.ReportResults
else
TriggerStatus.ReportError result.FailureMessage
}
|> Async.Start
let canUpdateBetfairSpPrices() =
if market.IsInPlay
then
not (mySelectionDatas |> List.exists (fun mySelectionData -> mySelectionData.ActualSP <> 0.0))
else
true
interface IBotTrigger with
member __.Execute() =
match triggerStatus with
| TriggerStatus.Initialize ->
if isHorseRacingMarket()
then
mySelectionDatas <- initializeMySelectionDatas()
triggerStatus <- TriggerStatus.UpdateData
TriggerResult.WaitingForOperation
else
TriggerResult.EndExecutionWithMessage "You can execute this bot only on a horse racing market!"
| TriggerStatus.UpdateData ->
if isTimeToUpdateBetfairSpPrices()
then
updateBetfairSpPrices()
TriggerResult.WaitingForOperation
| TriggerStatus.WaitForUpdateData -> TriggerResult.WaitingForOperation
| TriggerStatus.ReportResults ->
mySelectionDatas |> List.iter (fun mySelectionData -> mySelectionData.UpdateData())
if canUpdateBetfairSpPrices()
then
triggerStatus <- TriggerStatus.UpdateData
TriggerResult.WaitingForOperation
else
TriggerResult.EndExecution
| TriggerStatus.ReportError errorMessage -> TriggerResult.EndExecutionWithMessage errorMessage
member __.EndExecution() =
()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment