Skip to content

Instantly share code, notes, and snippets.

@StefanBelo
Created August 19, 2020 17:49
Show Gist options
  • Save StefanBelo/c1853d51e3fc6454bba1fd0dfd2841cf to your computer and use it in GitHub Desktop.
Save StefanBelo/c1853d51e3fc6454bba1fd0dfd2841cf to your computer and use it in GitHub Desktop.
//#I @"C:\Program Files (x86)\BeloSoft\Bfexplorer\"
#I @"D:\Projects\Bfexplorer\Development\Applications\BeloSoft.Bfexplorer.App\bin\Debug\"
#r "DevExpress.Data.v15.1.dll"
#r "DevExpress.Office.v15.1.Core.dll"
#r "DevExpress.Spreadsheet.v15.1.Core.dll"
#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.Threading
open DevExpress.Spreadsheet
open BeloSoft.Data
open BeloSoft.Bfexplorer.Domain
open BeloSoft.Bfexplorer.Trading
open BeloSoft.Bfexplorer.Service
// Comment this line when executing in Bfexplorer Console!
//let bfexplorer : IBfexplorerConsole = nil
let getBetLiability (market : Market) =
market.Bets
|> Seq.sumBy (fun bet ->
if bet.BetType = BetType.Back
then
bet.Size
else
(bet.Price - 1.0) * bet.Size
)
let toSpreadsheet (myStrategyResults : MyStrategyResults) =
let bfexplorerSpreadsheet = bfexplorer.BfexplorerUI.BfexplorerSpreadsheet
if isNotNullObj bfexplorerSpreadsheet
then
let worksheet =
let worksheetName = "My Results"
let workbook = bfexplorerSpreadsheet.Document
if workbook.Worksheets.Contains(worksheetName)
then
workbook.Worksheets.[worksheetName]
else
workbook.Worksheets.Add(worksheetName)
let mutable totalWinningBets = 0
let mutable totalLosingBets = 0
let mutable totalProfit = 0.0
let mutable row = 0
let setValueForColumn column value =
worksheet.[row, column].SetValue(value)
let reportResult (market : Market) =
let settledProfit = defaultNullableArg market.SettledProfit 0.0
let liability = getBetLiability market
market.MarketInfo.StartTime |> setValueForColumn 0
market.MarketFullName |> setValueForColumn 1
settledProfit |> setValueForColumn 2
liability |> setValueForColumn 3
try
bfexplorerSpreadsheet.BeginUpdate()
[ "Time"; "Market"; "Profit"; "Bet Liability" ]
|> List.iteri (fun column text -> text |> setValueForColumn column)
row <- row + 1
myStrategyResults.SettledMarkets
|> Seq.iter (fun market ->
match market.SettledProfit with
| Value settledProfit ->
reportResult market
if settledProfit >= 0.0
then
totalWinningBets <- totalWinningBets + 1
else
totalLosingBets <- totalLosingBets + 1
totalProfit <- totalProfit + settledProfit
row <- row + 1
| Null -> ()
)
row <- row + 1
sprintf "Total profit: %.2f, on bets: %d, won bets/loss bets: %d/%d" totalProfit (totalWinningBets + totalLosingBets) totalWinningBets totalLosingBets
|> setValueForColumn 0
finally
bfexplorerSpreadsheet.EndUpdate()
else
printfn "Open bfexplorer spreadsheet application!"
match MyStrategyOperations.GetMyStrategyResults("My Martingale Test Strategy") with
| Some myStrategyResults ->
async {
do! bfexplorer.BfexplorerService.UiApplication.ExecuteOnUiContext SynchronizationContext.Current <|
fun () -> toSpreadsheet myStrategyResults
}
|> Async.RunSynchronously
| None -> printfn "No results!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment