Skip to content

Instantly share code, notes, and snippets.

@StefanBelo
Last active February 26, 2021 07:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StefanBelo/e49bdc9fdd60c580a212b688190dd33c to your computer and use it in GitHub Desktop.
Save StefanBelo/e49bdc9fdd60c580a212b688190dd33c 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 System
open BeloSoft.Data
open BeloSoft.Bfexplorer.Domain
open BeloSoft.Bfexplorer.Trading
open BeloSoft.Bfexplorer.Service
/// <summary>
/// createBotParameters
/// </summary>
/// <param name="betType"></param>
/// <param name="stake"></param>
let createBotParameters (betType : BetType, stake : float) =
let botParameters = PlaceBetBotParameters()
botParameters.Name <- sprintf "My %A bot" betType
botParameters.BetType <- betType
botParameters.Stake <- stake
botParameters.MaximumOdds <- 10.0
botParameters.MinimumOdds <- 1.5
botParameters.PriceImprovement <- 1y
botParameters.AllowPlacingBetInPlay <- false
botParameters.ChaseOddsTimeout <- Nullable(TimeSpan.FromSeconds 15.0)
botParameters
/// <summary>
/// isBotRunning
/// </summary>
/// <param name="bot"></param>
let isBotRunning (bot : Bot) =
bot.Status <> BotStatus.ExecutionEnded
/// <summary>
/// MyTraderBot
/// </summary>
type MyTraderBot(selection : Selection, stake : float) =
let backBotParameters = createBotParameters (BetType.Back, stake)
let layBotParameters = createBotParameters (BetType.Lay, stake)
let mutable backBot = nil<PlaceBetBot>
let mutable layBot = nil<PlaceBetBot>
member __.IsBotRunning
with get() = isBotRunning backBot || isBotRunning layBot
member __.Start(market : Market, bfexplorerService : IBfexplorerService) =
backBot <- PlaceBetBot(market, selection, backBotParameters, bfexplorerService)
layBot <- PlaceBetBot(market, selection, layBotParameters, bfexplorerService)
market |> addBot backBot
market |> addBot layBot
member __.Update() =
let betPosition = selection.BetPosition
if betPosition.IsValid
then
let oddsContext = selection.OddsContext
let backPosition = betPosition.BackPosition
let layPosition = betPosition.LayPosition
if backPosition.IsValid
then
layBotParameters.MaximumOdds <- oddsContext.GetNextOdds(backPosition.Price, 2, false)
layBotParameters.MinimumOdds <- OddsData.MinimalOdds
if layPosition.IsValid
then
backBotParameters.MaximumOdds <- OddsData.MaximalOdds
backBotParameters.MinimumOdds <- oddsContext.GetNextOdds(layPosition.Price, 2, true)
/// <summary>
/// createMyBots
/// </summary>
/// <param name="market"></param>
/// <param name="botTriggerParameters"></param>
/// <param name="bfexplorerService"></param>
let createMyBots (market : Market, botTriggerParameters : BotTriggerParameters) (bfexplorerService : IBfexplorerService) =
let botParameters = ClosePositionOnMarketBotParameters()
let stake = defaultArg (botTriggerParameters.GetParameter<float>("Stake")) 5.0
let profit = defaultArg (botTriggerParameters.GetParameter<float>("Profit")) 1.0
let profitOrLossInPercentage = defaultArg (botTriggerParameters.GetParameter<bool>("ProfitOrLossInPercentage")) false
botParameters.Name <- "Close my market bet position"
botParameters.Profit <- profit
botParameters.Loss <- 0.0
botParameters.ProfitOrLossInPercentage <- profitOrLossInPercentage
botParameters.CheckingLastPriceTraded <- false
botParameters.BetMatchingTimeout <- Nullable(TimeSpan.FromSeconds 1.0)
botTriggerParameters.GetParameter<float>("ClosePositionAtTime")
|> Option.iter (fun closePositionAtTime -> botParameters.ClosePositionAtTime <- Nullable(TimeSpan.FromSeconds closePositionAtTime))
market |> addBot (ClosePositionOnMarketBot(market, botParameters, bfexplorerService))
market.Selections
|> Seq.filter isActiveSelection
|> Seq.map (fun selection ->
let bot = MyTraderBot(selection, stake)
bot.Start(market, bfexplorerService)
bot
)
|> Seq.toList
/// <summary>
/// TriggerStatus
/// </summary>
type TriggerStatus =
| Initialize
| UpdateBotParameters
/// <summary>
/// ScalpingTradingStrategyBot
/// </summary>
type ScalpingTradingStrategyBot(market : Market, _selection : Selection, _botName : string, botTriggerParameters : BotTriggerParameters, myBfexplorer : IMyBfexplorer) =
let mutable status = TriggerStatus.Initialize
let mutable myBots = nil<MyTraderBot list>
interface IBotTrigger with
member __.Execute() =
match status with
| TriggerStatus.Initialize ->
status <- TriggerStatus.UpdateBotParameters
myBots <- myBfexplorer.BfexplorerService |> createMyBots (market, botTriggerParameters)
| TriggerStatus.UpdateBotParameters ->
myBots
|> List.iter (fun bot ->
if bot.IsBotRunning
then
bot.Update()
)
TriggerResult.WaitingForOperation
member __.EndExecution() =
()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment