Created
June 5, 2020 14:21
-
-
Save StefanBelo/7b1b1dd483af4380d6498bf29f1130d2 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 System.Collections.Generic | |
open System.IO | |
open BeloSoft.Data | |
open BeloSoft.Bfexplorer.Domain | |
open BeloSoft.Bfexplorer.Trading | |
// Cached data | |
let mySelectionsByDate = Dictionary<DateTime, string list>() | |
let loadCsvData (filePathName : string) = async { | |
try | |
use reader = new StreamReader(filePathName) | |
let mutable selections = list.Empty | |
while not reader.EndOfStream do | |
let line = reader.ReadLine() | |
if not (String.IsNullOrEmpty line) | |
then | |
selections <- line :: selections | |
mySelectionsByDate.Add(DateTime.Today, selections) | |
return Result.Success | |
with | |
| ex -> return Result.Failure ex.Message | |
} | |
let haveTodaysSelections() = | |
mySelectionsByDate.ContainsKey(DateTime.Today) | |
let getMySelections (market : Market) = maybe { | |
let status, mySelectionNames = mySelectionsByDate.TryGetValue DateTime.Today | |
if status | |
then | |
let mySelections = | |
market.Selections | |
|> Seq.filter isActiveSelection | |
|> Seq.choose (fun selection -> | |
let myName = selection.Name | |
match mySelectionNames |> List.tryFind (fun name -> name = myName) with | |
| Some _ -> Some selection | |
| None -> None | |
) | |
|> Seq.toList | |
if not mySelections.IsEmpty | |
then | |
return mySelections | |
} | |
/// <summary> | |
/// TriggerStatus | |
/// </summary> | |
type TriggerStatus = | |
| LoadMySelections | |
| WaitForLoadingMySelections | |
| FailedToLoadData of string | |
| TriggerMyBot | |
/// <summary> | |
/// MySelectionsBotTrigger | |
/// </summary> | |
type MySelectionsBotTrigger(market : Market, _selection : Selection, _botName : string, botTriggerParameters : BotTriggerParameters, _myBfexplorer : IMyBfexplorer) = | |
let mutable triggerStatus = TriggerStatus.LoadMySelections | |
let setFailedToLoadData errorMessage = | |
triggerStatus <- TriggerStatus.FailedToLoadData errorMessage | |
interface IBotTrigger with | |
member _this.Execute() = | |
match triggerStatus with | |
| TriggerStatus.LoadMySelections -> | |
if haveTodaysSelections() | |
then | |
triggerStatus <- TriggerStatus.TriggerMyBot | |
TriggerResult.WaitingForOperation | |
else | |
let filePathName = defaultArg (botTriggerParameters.GetParameter<string>("CsvFile")) String.Empty | |
if String.IsNullOrEmpty filePathName | |
then | |
TriggerResult.EndExecutionWithMessage "Set the CsvFile parameter!" | |
else | |
triggerStatus <- TriggerStatus.WaitForLoadingMySelections | |
Async.StartWithContinuations( | |
computation = loadCsvData filePathName, | |
continuation = (fun result -> | |
if result.IsSuccessResult | |
then | |
triggerStatus <- TriggerStatus.TriggerMyBot | |
else | |
setFailedToLoadData result.FailureMessage | |
), | |
exceptionContinuation = (fun ex -> setFailedToLoadData ex.Message), | |
cancellationContinuation = (fun ex -> setFailedToLoadData ex.Message) | |
) | |
TriggerResult.WaitingForOperation | |
| TriggerStatus.WaitForLoadingMySelections -> TriggerResult.WaitingForOperation | |
| TriggerStatus.FailedToLoadData errorMessage -> TriggerResult.EndExecutionWithMessage errorMessage | |
| TriggerStatus.TriggerMyBot -> | |
match getMySelections market with | |
| Some selections -> TriggerResult.ExecuteActionBotOnSelectionsAndContinueToExecute (selections, false) | |
| None -> TriggerResult.EndExecution | |
member _this.EndExecution() = | |
() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment