Skip to content

Instantly share code, notes, and snippets.

@StefanBelo
Created September 17, 2018 12:32
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/9703de04765e54ca47cdfe2397bd664d to your computer and use it in GitHub Desktop.
Save StefanBelo/9703de04765e54ca47cdfe2397bd664d 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 BeloSoft.Data
open BeloSoft.Bfexplorer.Domain
open BeloSoft.Bfexplorer.Trading
let getTradedRange minimalTradedVolume (selection : Selection) =
let tradedPriceDatas =
selection.PriceGridData.Data
|> Array.filter (fun priceData -> priceData.TotalMatched >= minimalTradedVolume)
|> Array.map (fun priceData -> priceData.Price)
if tradedPriceDatas.Length > 1
then
let maximalPriceTraded = tradedPriceDatas |> Array.head
let minimalPriceTraded = tradedPriceDatas |> Array.last
Some (minimalPriceTraded, maximalPriceTraded)
else
None
/// <summary>
/// TriggerStatus
/// </summary>
type TriggerStatus =
| Initialize
| PlaceBets
| WaitForResult
/// <summary>
/// PlaceBetsAtBottomOfTradedRangeBot
/// </summary>
type PlaceBetsAtBottomOfTradedRangeBot(market : Market, _selection : Selection, _botName : string, botTriggerParameters : BotTriggerParameters, myBfexplorer : IMyBfexplorer) =
let mutable status = TriggerStatus.Initialize
let mutable betsPlaced = false
let outputMessage message =
myBfexplorer.BfexplorerService.OutputMessage(message, market.Id)
let activeSelections() =
market.Selections |> Seq.filter isActiveSelection
let getMySelections minimalTradedVolume minDifferenceFromBottom =
activeSelections()
|> Seq.choose (fun selection ->
match selection |> getTradedRange minimalTradedVolume with
| Some (minimalPriceTraded, maximalPriceTraded) ->
let oddsDifference = selection.OddsContext.GetOddsDifference(minimalPriceTraded, selection.LastPriceTraded)
if oddsDifference <= minDifferenceFromBottom
then
Some (selection, minimalPriceTraded, maximalPriceTraded)
else
None
| None -> None
)
|> Seq.toList
let reportResults (mySelections : ((Selection * float * float) list)) =
if mySelections.IsEmpty
then
"No selections"
else
mySelections
|> List.map (fun (selection, minimalPriceTraded, maximalPriceTraded) -> sprintf "%s: %.2f, %.2f" selection.Name minimalPriceTraded maximalPriceTraded)
|> String.concat "\n"
|> outputMessage
let onBetsPlaced (_status : bool) =
betsPlaced <- true
interface IBotTrigger with
/// <summary>
/// Execute
/// </summary>
member __.Execute() =
match status with
| TriggerStatus.Initialize ->
status <- TriggerStatus.PlaceBets
activeSelections() |> Seq.iter (fun selection -> selection.PriceGridDataEnabled <- true)
TriggerResult.WaitingForOperation
| TriggerStatus.PlaceBets ->
let minimalTradedVolume = defaultArg (botTriggerParameters.GetParameter<float>("MinimalTradedVolume")) 5.0
let minimalDifferenceFromBottom = defaultArg (botTriggerParameters.GetParameter<int>("MinimalDifferenceFromBottom")) 3
let mySelections = getMySelections minimalTradedVolume minimalDifferenceFromBottom
reportResults mySelections
if mySelections.IsEmpty
then
TriggerResult.EndExecution
else
let minimalOdds = defaultArg (botTriggerParameters.GetParameter<float>("MinimalOdds")) 1.5
let maximalOdds = defaultArg (botTriggerParameters.GetParameter<float>("MaximalOdds")) 8.0
let betType = defaultArg (botTriggerParameters.GetEnumParameter<BetType>("BetType")) BetType.Lay
let stake = defaultArg (botTriggerParameters.GetParameter<float>("Stake")) 2.0
let bets =
mySelections
|> List.choose (fun (selection, _, _) ->
if selection.LastPriceTraded >= minimalOdds && selection.LastPriceTraded <= maximalOdds
then
Some (SelectionBetOrder.Create(selection, betType, selection.GetBestPrice(betType), stake))
else
None
)
if bets.IsEmpty
then
TriggerResult.EndExecution
else
status <- TriggerStatus.WaitForResult
TriggerResult.PlaceBets (bets, PersistenceType.Lapse, onBetsPlaced)
| TriggerStatus.WaitForResult ->
if betsPlaced
then
TriggerResult.EndExecution
else
TriggerResult.WaitingForOperation
/// <summary>
/// EndExecution
/// </summary>
member __.EndExecution() =
()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment