Created
November 28, 2019 13:07
-
-
Save StefanBelo/65b31d1c5c372a38323c5a33f0c83292 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.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> | |
/// TriggerStatus | |
/// </summary> | |
type TriggerStatus = | |
| Initialize | |
| WaitToCloseBetPosition | |
| CloseBetPosition | |
/// <summary> | |
/// ClosePositionOnBetEvent | |
/// </summary> | |
type ClosePositionOnBetEvent(market : Market, _selection : Selection, _botName : string, botTriggerParameters : BotTriggerParameters, myBfexplorer : IMyBfexplorer) = | |
let totalProfit = defaultArg (botTriggerParameters.GetParameter<float>("TotalProfit")) 10.0 | |
let mutable status = TriggerStatus.Initialize | |
let mutable markets = nil<Market list> | |
let currentTotalProfit = HistoryValue(0.0) | |
let setMarketsToMonitor (openBetEvent : OpenBetEvent) = | |
markets <- | |
openBetEvent.OpenMarkets | |
|> Seq.filter (fun myMarket -> myMarket.ProfitBalance.HasValue) | |
|> Seq.toList | |
not markets.IsEmpty | |
let setCurrentTotalProfit() = | |
currentTotalProfit.SetValue(markets |> List.sumBy (fun myMarket -> defaultNullableArg myMarket.ProfitBalance 0.0)) | |
let outputMessage message = | |
myBfexplorer.BfexplorerService.OutputMessage(message, market.Id) | |
let reportCurrentTotalProfit() = | |
let marketsProfit = | |
markets | |
|> List.map (fun myMarket -> sprintf "%s: %.2f" myMarket.MarketFullName (defaultNullableArg myMarket.ProfitBalance 0.0)) | |
|> String.concat "\n" | |
outputMessage (sprintf "Total profit: %.2f\n%s" currentTotalProfit.Value marketsProfit) | |
let canClosePositionOnBetEvent() = | |
currentTotalProfit.Value >= totalProfit | |
let startClosePositionOnMarketBot (myMarket : Market) = | |
let botParameters = ClosePositionOnMarketBotParameters() | |
botParameters.Name <- "Close my market bet position" | |
botParameters.ProfitOrLossInPercentage <- true | |
botParameters.CheckingLastPriceTraded <- false | |
botParameters.ClosePositionAtTime <- Nullable(TimeSpan.FromDays -1.0) | |
botParameters.BetMatchingTimeout <- Nullable(TimeSpan.FromSeconds 10.0) | |
myMarket |> addBot (ClosePositionOnMarketBot(myMarket, botParameters, myBfexplorer.BfexplorerService)) | |
let doClosePositionOnBetEvent() = | |
markets |> List.iter startClosePositionOnMarketBot | |
interface IBotTrigger with | |
/// <summary> | |
/// Execute | |
/// </summary> | |
member __.Execute() = | |
match status with | |
| TriggerStatus.Initialize -> | |
let haveOpenBetPosition = | |
match myBfexplorer.OpenBetEvent with | |
| Some openBetEvent -> setMarketsToMonitor openBetEvent | |
| None -> false | |
if haveOpenBetPosition | |
then | |
status <- TriggerStatus.WaitToCloseBetPosition | |
TriggerResult.WaitingForOperation | |
else | |
TriggerResult.EndExecution | |
| TriggerStatus.WaitToCloseBetPosition -> | |
if setCurrentTotalProfit() | |
then | |
reportCurrentTotalProfit() | |
if canClosePositionOnBetEvent() | |
then | |
status <- TriggerStatus.CloseBetPosition | |
TriggerResult.WaitingForOperation | |
| TriggerStatus.CloseBetPosition -> | |
doClosePositionOnBetEvent() | |
TriggerResult.EndExecution | |
/// <summary> | |
/// EndExecution | |
/// </summary> | |
member __.EndExecution() = | |
() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment