Created
September 9, 2021 06:37
-
-
Save StefanBelo/84b00b990de2839c56b48bcfe1d6cb18 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 SaveMarketGraphs | |
| #I @"C:\Program Files (x86)\BeloSoft\Bfexplorer\" | |
| #r "BeloSoft.Betfair.API.dll" | |
| #r "BeloSoft.Data.dll" | |
| #r "BeloSoft.Bfexplorer.Domain.dll" | |
| #r "BeloSoft.Bfexplorer.Trading.dll" | |
| #r "BeloSoft.Bfexplorer.Service.Core.dll" | |
| open System | |
| open System.IO | |
| open System.Net | |
| open System.Text.RegularExpressions | |
| open System.Threading | |
| open BeloSoft.Data | |
| open BeloSoft.Bfexplorer.Domain | |
| open BeloSoft.Bfexplorer.Trading | |
| let toCorrectName (text : string) = | |
| Regex.Replace(text, "[^A-Za-z0-9_. ]+", "") | |
| let createDirectory (directory : string) = | |
| if not (Directory.Exists(directory)) | |
| then | |
| Directory.CreateDirectory(directory) |> ignore | |
| let createMarketDirectory (rootDirectory : string) (market : Market) = | |
| let marketInfo = market.MarketInfo | |
| let todayDirectory = Path.Combine(rootDirectory, DateTime.Now.ToString("yyyyMMdd")) | |
| createDirectory todayDirectory | |
| let eventTypeDirectory = Path.Combine(todayDirectory, marketInfo.EventTypeName) | |
| createDirectory eventTypeDirectory | |
| let eventDirectory = Path.Combine(eventTypeDirectory, toCorrectName marketInfo.EventName) | |
| createDirectory eventDirectory | |
| let marketName = | |
| match marketInfo.BetEventType.Id with | |
| | 7 (* Horse Racing *) | 4339 (* Greyhound Racing *) -> sprintf "%s %s" (marketInfo.StartTime.ToString("HHmm")) marketInfo.MarketName | |
| | _ -> marketInfo.MarketName | |
| let marketDirectory = Path.Combine(eventDirectory, toCorrectName marketName) | |
| createDirectory marketDirectory | |
| marketDirectory | |
| /// <summary> | |
| /// SaveMarketCharts | |
| /// </summary> | |
| type SaveMarketCharts(market : Market, _selection : Selection, _botName : string, botTriggerParameters : BotTriggerParameters, myBfexplorer : IMyBfexplorer) = | |
| let webClient = new WebClient() | |
| let selectionGraphUrl = | |
| let marketId = market.Id.Split('.').[1] | |
| sprintf @"https://xtsd.betfair.com/LoadRunnerInfoChartAction/?marketId=%s&selectionId=" marketId | |
| let saveSelectionGraph (directory : string) (selection : Selection) = | |
| let graphUrl = sprintf "%s%d" selectionGraphUrl selection.Identity.Id | |
| let filePathName = Path.Combine(directory, sprintf "%s.jpg" (toCorrectName selection.Name)) | |
| webClient.DownloadFile(graphUrl, filePathName) | |
| let outputMessage message = | |
| let bfexplorerService = myBfexplorer.BfexplorerService | |
| bfexplorerService.UiApplication.ExecuteOnUiContext SynchronizationContext.Current <| | |
| fun () -> bfexplorerService.OutputMessage(message, market.Id) | |
| let doSaveCharts (selections : Selection list) = | |
| async { | |
| let rootDirectory = defaultArg (botTriggerParameters.GetParameter<string>("Folder")) (Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Betfair Graphs")) | |
| try | |
| let directory = market |> createMarketDirectory rootDirectory | |
| selections |> List.iter (saveSelectionGraph directory) | |
| with | |
| | ex -> do! outputMessage (sprintf "Failed to save data: %s" (toFailureMessage ex)) | |
| } | |
| |> Async.Start | |
| let waitForMarketClosing = defaultArg (botTriggerParameters.GetParameter<bool>("WaitForMarketClosing")) false | |
| interface IBotTrigger with | |
| /// <summary> | |
| /// Execute | |
| /// </summary> | |
| member _this.Execute() = | |
| if waitForMarketClosing | |
| then | |
| TriggerResult.WaitingForOperation | |
| else | |
| TriggerResult.EndExecution | |
| /// <summary> | |
| /// EndExecution | |
| /// </summary> | |
| member _this.EndExecution() = | |
| let winnerOnly = waitForMarketClosing && (defaultArg (botTriggerParameters.GetParameter<bool>("WinnerOnly")) false) | |
| let selectionsToSave = | |
| if winnerOnly | |
| then | |
| market.Selections | |
| |> Seq.filter isWinnerSelection | |
| else | |
| market.Selections | |
| |> Seq.filter isNotRemovedSelection | |
| doSaveCharts (selectionsToSave |> Seq.toList) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment