Last active
May 23, 2020 13:26
-
-
Save StefanBelo/529414d438ed3b80e9ddaf22b57ff359 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 | |
type TriggerStatus = | |
| WaitForData | |
| EvaluateAndTrigger | |
/// <summary> | |
/// AverageTradedPriceBotTrigger | |
/// </summary> | |
type AverageTradedPriceBotTrigger(market : Market, selection : Selection, _botName : string, botTriggerParameters : BotTriggerParameters, myBfexplorer : IMyBfexplorer) = | |
let minimalPrice = defaultArg (botTriggerParameters.GetParameter<float>("MinimalPrice")) 1.01 | |
let maximalPrice = defaultArg (botTriggerParameters.GetParameter<float>("MaximalPrice")) 1000.0 | |
let forTime = defaultArg (botTriggerParameters.GetParameter<float>("ForTime")) 60.0 | |
let timeToExecute = | |
let time = DateTime.Now | |
if defaultArg (botTriggerParameters.GetParameter<bool>("WaitForData")) false | |
then | |
time.AddSeconds(forTime) | |
else | |
time | |
let mutable triggerStatus = TriggerStatus.WaitForData | |
let outputMessage message = | |
myBfexplorer.BfexplorerService.OutputMessage(message, market.Id) | |
let getAverageTradedPriceFromTime time = | |
let myTradedPrices = | |
selection.PriceTradedHistory.TradedPrices | |
|> Seq.filter (fun timePriceTraded -> timePriceTraded.Time >= time) | |
|> Seq.toList | |
if myTradedPrices.IsEmpty | |
then | |
None | |
else | |
let averagePriceSize = AveragePriceSize() | |
myTradedPrices | |
|> List.iter (fun timePriceTraded -> averagePriceSize.Add(timePriceTraded.Price, timePriceTraded.Volume)) | |
Some averagePriceSize.Price | |
interface IBotTrigger with | |
/// <summary> | |
/// Execute | |
/// </summary> | |
member __.Execute() = | |
match triggerStatus with | |
| TriggerStatus.WaitForData -> | |
if DateTime.Now >= timeToExecute | |
then | |
triggerStatus <- TriggerStatus.EvaluateAndTrigger | |
TriggerResult.WaitingForOperation | |
| TriggerStatus.EvaluateAndTrigger -> | |
match getAverageTradedPriceFromTime (DateTime.Now.AddSeconds(-forTime)) with | |
| Some averageTradedPrice -> | |
if averageTradedPrice >= minimalPrice && averageTradedPrice <= maximalPrice | |
then | |
sprintf "%s, the average traded price: %.2f" selection.Name averageTradedPrice |> outputMessage | |
TriggerResult.ExecuteActionBot | |
else | |
TriggerResult.EndExecution | |
| None -> TriggerResult.EndExecutionWithMessage "No price traded!" | |
/// <summary> | |
/// EndExecution | |
/// </summary> | |
member __.EndExecution() = | |
() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment