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.Trading.dll"
#r "BeloSoft.Bfexplorer.Service.Core.dll"
open System
open BeloSoft.Data
open BeloSoft.Bfexplorer.Domain
open BeloSoft.Bfexplorer.Trading
/// <summary>
/// VolumeDifference
/// </summary>
type VolumeDifference(initialVolume : float) =
let mutable volumeDifference = 0.0
member _this.Difference
with get() = volumeDifference
member _this.SetDifference(volume : float) =
volumeDifference <- volume - initialVolume
static member DataKey = "VolumeDifference"
/// <summary>
/// TriggerStatus
/// </summary>
type TriggerStatus =
| Initialize
| WaitToExecute
| EvaluateAndTrigger
/// <summary>
/// VolumeDifferenceBotTrigger
/// </summary>
type VolumeDifferenceBotTrigger(market : Market, _selection : Selection, _botName : string, botTriggerParameters : BotTriggerParameters, myBfexplorer : IMyBfexplorer) =
let timeToExecute =
let time = defaultArg (botTriggerParameters.GetParameter<float>("Time")) -10.0
market.MarketInfo.StartTime.AddSeconds(time)
let mutable triggerStatus = TriggerStatus.Initialize
let isTimeToExecute() =
DateTime.Now >= timeToExecute
let outputMessage message =
myBfexplorer.BfexplorerService.OutputMessage(message, market.Id)
let forActiveSelections (doFun : Selection -> unit) =
market.Selections
|> Seq.iter (fun mySelection ->
if isActiveSelection mySelection
then
doFun mySelection
)
let getMySelectionsData() =
market.Selections
|> Seq.choose (fun selection ->
match selection.GetData<VolumeDifference>(VolumeDifference.DataKey) with
| Some volumeDifference -> Some (selection, volumeDifference.Difference)
| None -> None
)
|> Seq.sortByDescending (fun (_selection, difference) -> difference)
|> Seq.toList
let report (mySelectionsData : (Selection * float) list) =
mySelectionsData
|> List.map (fun (selection, difference) -> sprintf "%s : %.2f | %.2f" selection.Name selection.LastPriceTraded difference)
|> String.concat "\n"
|> outputMessage
interface IBotTrigger with
/// <summary>
/// Execute
/// </summary>
member __.Execute() =
match triggerStatus with
| TriggerStatus.Initialize ->
triggerStatus <- TriggerStatus.WaitToExecute
forActiveSelections (fun selection ->
selection.SetData(VolumeDifference.DataKey, VolumeDifference(selection.TotalMatched))
)
TriggerResult.WaitingForOperation
| TriggerStatus.WaitToExecute ->
if isTimeToExecute()
then
triggerStatus <- TriggerStatus.EvaluateAndTrigger
forActiveSelections (fun selection ->
selection.GetData<VolumeDifference>(VolumeDifference.DataKey)
|> Option.iter (fun volumeDifference -> volumeDifference.SetDifference(selection.TotalMatched))
)
TriggerResult.WaitingForOperation
| TriggerStatus.EvaluateAndTrigger ->
let mySelectionsData = getMySelectionsData()
report mySelectionsData
TriggerResult.ExecuteActionBotOnSelection (fst mySelectionsData.Head)
/// <summary>
/// EndExecution
/// </summary>
member __.EndExecution() =
()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment