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
#I @"D:\Projects\Bfexplorer\Development\packages\FSharp.Plotly.1.1.21\lib\net47\" | |
#I @"D:\Projects\Bfexplorer\Development\Applications\BeloSoft.Bfexplorer.App\bin\Debug\" | |
#r "FSharp.Plotly.dll" | |
#r "BeloSoft.Data.dll" | |
#r "BeloSoft.Bfexplorer.Domain.dll" | |
#r "BeloSoft.Bfexplorer.Service.Core.dll" | |
#r "BeloSoft.Bfexplorer.Trading.dll" | |
#r "BeloSoft.Bfexplorer.MyHorseRacingTrading.Bot.dll" | |
open System | |
open FSharp.Plotly | |
open BeloSoft.Data | |
open BeloSoft.Bfexplorer.Domain | |
open BeloSoft.Bfexplorer.Service | |
open BeloSoft.Bfexplorer.MyHorseRacingTrading | |
/// <summary> | |
/// HorseChartData | |
/// </summary> | |
type HorseChartData = | |
{ | |
Name : string | |
X : float list | |
Y : float list | |
Label : string list | |
IsFrontRunner : bool | |
IsWinner : bool | |
} | |
(* | |
Helpers | |
*) | |
let mapHorsePathToVictory mapFun (horsePathToVictory : HorsePathToVictory) = | |
horsePathToVictory.PositionChanges |> List.map mapFun | |
let horsePathToVictoryToX (raceStartTime : DateTime) (horsePathToVictory : HorsePathToVictory) = | |
horsePathToVictory |> mapHorsePathToVictory (fun horsePositionChange -> (horsePositionChange.Time - raceStartTime).TotalSeconds) | |
let horsePathToVictoryToYProbability (horsePathToVictory : HorsePathToVictory) = | |
horsePathToVictory |> mapHorsePathToVictory (fun horsePositionChange -> toPriceProbability horsePositionChange.Price) | |
let horsePathToVictoryToY (horsePathToVictory : HorsePathToVictory) = | |
horsePathToVictory |> mapHorsePathToVictory (fun horsePositionChange -> horsePositionChange.Price) | |
let horsePathToVictoryToLabel (horsePathToVictory : HorsePathToVictory) = | |
horsePathToVictory |> mapHorsePathToVictory (fun horsePositionChange -> sprintf "%d: %.2f" horsePositionChange.Position horsePositionChange.Price) | |
let isFrontRunner (horsePathToVictory : HorsePathToVictory) = | |
horsePathToVictory.PositionChanges | |
|> List.exists (fun horsePositionChange -> horsePositionChange.Position = 1uy) | |
let getHorseChartData (raceStartTime : DateTime) (selection : Selection) = | |
match HorsePathToVictory.Get selection with | |
| Some horsePathToVictory -> | |
Some | |
{ | |
HorseChartData.Name = selection.Name | |
HorseChartData.X = horsePathToVictory |> horsePathToVictoryToX raceStartTime | |
HorseChartData.Y = horsePathToVictory |> horsePathToVictoryToYProbability | |
HorseChartData.Label = horsePathToVictory |> horsePathToVictoryToLabel | |
HorseChartData.IsFrontRunner = horsePathToVictory |> isFrontRunner | |
HorseChartData.IsWinner = isWinnerSelection selection | |
} | |
| None -> None | |
let getSelections (market : Market) = | |
market.Selections | |
|> Seq.filter isNotRemovedSelection | |
|> Seq.sortBy (fun selection -> selection.LastPriceTraded) | |
|> Seq.toList | |
let getWinner (market : Market) = | |
market.Selections |> Seq.tryFind isWinnerSelection | |
let mapTradedAndOfferedPricesDatas mapFun (tradedAndOfferedPricesDatas : TradedAndOfferedPricesData seq) = | |
tradedAndOfferedPricesDatas | |
|> Seq.map mapFun | |
|> Seq.toList | |
let getHorsePricesChartData (raceStartTime : DateTime) (selection : Selection) = maybe { | |
let! selectionTradedAndOfferedPricesData = SelectionTradedAndOfferedPricesData.Get selection | |
let! horsePathToVictory = HorsePathToVictory.Get selection | |
let tradedAndOfferedPricesDatas = selectionTradedAndOfferedPricesData.TradedAndOfferedPricesDatas | |
let x = | |
tradedAndOfferedPricesDatas | |
|> mapTradedAndOfferedPricesDatas (fun tradedAndOfferedPricesData -> (tradedAndOfferedPricesData.Time - raceStartTime).TotalSeconds) | |
let yPrice = | |
tradedAndOfferedPricesDatas | |
|> mapTradedAndOfferedPricesDatas (fun tradedAndOfferedPricesData -> tradedAndOfferedPricesData.Price) | |
let yBackPrice = | |
tradedAndOfferedPricesDatas | |
|> mapTradedAndOfferedPricesDatas (fun tradedAndOfferedPricesData -> tradedAndOfferedPricesData.BackPrice) | |
let yLayPrice = | |
tradedAndOfferedPricesDatas | |
|> mapTradedAndOfferedPricesDatas (fun tradedAndOfferedPricesData -> tradedAndOfferedPricesData.LayPrice) | |
return | |
[ | |
Chart.Line(x, yPrice, Name = "Price") | |
Chart.Line(x, yBackPrice, Name = "Back Price") | |
Chart.Line(x, yLayPrice, Name = "Lay Price") | |
Chart.Point(horsePathToVictory |> horsePathToVictoryToX raceStartTime, horsePathToVictory |> horsePathToVictoryToY, | |
Labels = (horsePathToVictory |> horsePathToVictoryToLabel), Name = "Position") | |
] | |
} | |
(* | |
Execute | |
*) | |
let bfexplorer = nil<IBfexplorerConsole> | |
let market = bfexplorer.ActiveMarket | |
// Path to victory | |
let raceStartTime = defaultArg (market.GetData<DateTime>("RaceStartTime")) market.MarketInfo.StartTime | |
let selections = market |> getSelections | |
let allHorsesChartData = | |
selections | |
|> List.choose (fun selection -> selection |> getHorseChartData raceStartTime) | |
allHorsesChartData | |
|> List.filter (fun horseChartData -> horseChartData.IsFrontRunner) | |
|> List.map (fun horseChartData -> Chart.Point(horseChartData.X, horseChartData.Y, Labels = horseChartData.Label, Name = horseChartData.Name)) | |
|> Chart.Combine | |
|> Chart.withTitle (market.ToString()) | |
|> Chart.Show | |
// Traded and offered | |
let aWinnerCharts = maybe { | |
let! winner = market |> getWinner | |
let! charts = winner |> getHorsePricesChartData raceStartTime | |
return winner, charts | |
} | |
match aWinnerCharts with | |
| Some (winner, charts) -> | |
charts | |
|> Chart.Combine | |
|> Chart.withTitle (sprintf "%A - %s" market winner.Name) | |
|> Chart.Show | |
| None -> printfn "No winner!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment