Created
October 26, 2019 11:39
-
-
Save StefanBelo/fe96458d5e1acdc8e426806b90b6a179 to your computer and use it in GitHub Desktop.
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.Trading.dll" | |
#r "BeloSoft.Bfexplorer.Service.Core.dll" | |
//*) | |
open System | |
open BeloSoft.Data | |
open BeloSoft.Bfexplorer.Domain | |
open BeloSoft.Bfexplorer.Trading | |
let toBotStatus (bot : Bot) = | |
if isNullObj bot.RunningOnSelection | |
then | |
sprintf "%s" bot.Name | |
else | |
sprintf "%s on %s" bot.Name bot.RunningOnSelection.Name | |
let toBetStatus (bet : Bet) = | |
sprintf "%s: %A, %.2f @ %.2f, %A" bet.Selection.Name bet.BetType bet.Size bet.Price bet.OrderStatus | |
let toBetPosition (selection : Selection) = | |
sprintf "%s: %A" selection.Name selection.BetPosition | |
/// <summary> | |
/// TriggerStatus | |
/// </summary> | |
type TriggerStatus = | |
| ExecuteActionBot = 1 | |
| ReportAllBots = 2 | |
| ReportBetStatus = 3 | |
| ReportBetPosition = 4 | |
| CancelBets = 5 | |
| WaitToCancelBets = 6 | |
| StopMySelectionActionBot = 7 | |
| EndBotExecution = 8 | |
/// <summary> | |
/// BetStatusBetCancellingBotOrchestrationBotTrigger | |
/// </summary> | |
type BetStatusBetCancellingBotOrchestrationBotTrigger(market : Market, selection : Selection, _botName : string, _botTriggerParameters : BotTriggerParameters, myBfexplorer : IMyBfexplorer) = | |
let mutable status = TriggerStatus.ExecuteActionBot | |
let mutable timeToExecute = DateTime.MaxValue | |
let goToNextTriggerStatus() = | |
enum<TriggerStatus> (int status + 1) | |
let setTimeToExecute inSeconds = | |
timeToExecute <- DateTime.Now.AddSeconds(inSeconds) | |
let isTimeToExecute() = | |
DateTime.Now >= timeToExecute | |
let outputMessage message = | |
myBfexplorer.BfexplorerService.OutputMessage(message, market.Id) | |
let report title (lines : string seq) = | |
lines |> String.concat "\n" |> sprintf "%s:\n%s" title |> outputMessage | |
let reportAllBots() = | |
market.RunningBots |> Seq.map (fun bot -> toBotStatus bot) |> report "Running Bots" | |
let reportAllBets() = | |
market.Bets |> Seq.map toBetStatus |> report "Bets" | |
let reportAllBetPositions() = | |
market.Selections |> Seq.filter isActiveSelection |> Seq.map toBetPosition |> report "Bet Positions" | |
let onBetsCancelled (_status : bool) = | |
setTimeToExecute 5.0 | |
status <- goToNextTriggerStatus() | |
let getMySelectionActionBot() = | |
let selectionId = selection.Id | |
market.RunningBots | |
|> Seq.tryFind (fun bot -> | |
if bot :? ExecuteTriggerBot | |
then | |
false | |
else | |
if isNotNullObj bot.RunningOnSelection | |
then | |
bot.RunningOnSelection.Id = selectionId | |
else | |
false | |
) | |
interface IBotTrigger with | |
/// <summary> | |
/// Execute | |
/// </summary> | |
member __.Execute() = | |
let triggerResult, goToNext = | |
match status with | |
| TriggerStatus.ExecuteActionBot -> | |
TriggerResult.ExecuteActionBotOnSelectionsAndContinueToExecute ([ selection ], true), true | |
| TriggerStatus.ReportAllBots -> | |
reportAllBots() | |
TriggerResult.WaitingForOperation, true | |
| TriggerStatus.ReportBetStatus -> | |
let goToNext = | |
if market.Bets.HaveBets | |
then | |
reportAllBets() | |
true | |
else | |
false | |
TriggerResult.WaitingForOperation, goToNext | |
| TriggerStatus.ReportBetPosition -> | |
reportAllBetPositions() | |
setTimeToExecute 5.0 | |
TriggerResult.WaitingForOperation, true | |
| TriggerStatus.CancelBets -> | |
if isTimeToExecute() | |
then | |
setTimeToExecute 5.0 | |
let bets = getUnmatchedBets selection.Bets | |
if bets.IsEmpty | |
then | |
TriggerResult.WaitingForOperation, true | |
else | |
TriggerResult.CancelBets (Some bets, onBetsCancelled), true | |
else | |
TriggerResult.WaitingForOperation, false | |
| TriggerStatus.StopMySelectionActionBot -> | |
if isTimeToExecute() | |
then | |
getMySelectionActionBot() | |
|> Option.iter (fun bot -> | |
bot.EndExecution() | |
outputMessage (sprintf "My bot: %s has been stopped." (toBotStatus bot)) | |
) | |
TriggerResult.WaitingForOperation, true | |
else | |
TriggerResult.WaitingForOperation, false | |
| TriggerStatus.EndBotExecution -> | |
reportAllBots() | |
TriggerResult.EndExecution, false | |
| _ -> TriggerResult.WaitingForOperation, false | |
if goToNext | |
then | |
status <- goToNextTriggerStatus() | |
triggerResult | |
/// <summary> | |
/// EndExecution | |
/// </summary> | |
member __.EndExecution() = | |
() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment