Last active
August 12, 2018 13:33
-
-
Save StefanBelo/e20d514c572d21c3adb955b7b90935c2 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
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 = | |
| OpenBetPosition | |
| WaitToCheckProfit | |
| EndExecution | |
type HedgeOrLeaveRunningBotTrigger(market : Market, selection : Selection, _botName : string, botTriggerParameters : BotTriggerParameters, myBfexplorer : IMyBfexplorer) = | |
let mutable triggerStatus = TriggerStatus.OpenBetPosition | |
let mutable timeToCheckProfit = DateTime.MaxValue | |
let outputMessage message = | |
myBfexplorer.BfexplorerService.OutputMessage(message, market.Id) | |
let getTimeToCheckProfit() = | |
DateTime.Now.AddSeconds(defaultArg (botTriggerParameters.GetParameter<float>("TimeSpanToCheckProfit")) 15.0) | |
let getMyProfitInTicks() = | |
defaultArg (botTriggerParameters.GetParameter<int>("Profit")) 1 | |
let shouldClosePositionForProfitOrLoss profitInTicks = maybe { | |
let betPosition = selection.BetPosition | |
let betType = betPosition.BetType | |
let! price = selection.GetPrice(betType) | |
let! profit = betPosition.GetHedgeProfit(price) | |
return | |
if profit > 0.0 | |
then | |
let oddsDifference = abs (selection.OddsContext.GetOddsDifference(price, (if betType = BetType.Back then betPosition.LayPosition else betPosition.BackPosition).Price)) | |
oddsDifference >= profitInTicks | |
else | |
false | |
} | |
let cancelMyActionBot() = | |
let selectionId = selection.Id | |
market.RunningBots | |
|> Seq.tryFind (fun bot -> isNotNullObj bot.RunningOnSelection && bot.RunningOnSelection.Id = selectionId && not (bot :? ExecuteTriggerBot)) | |
|> Option.iter (fun botToCancel -> | |
botToCancel.Status <- BotStatus.ExecutionEnded | |
botToCancel.EndExecution() | |
) | |
let setupMyHedgeBot() = | |
typeof<ClosePositionAtOddsBot>, selection, ClosePositionAtOddsBotParameters() :> BotParameters, false | |
interface IBotTrigger with | |
member __.Execute() = | |
match triggerStatus with | |
| TriggerStatus.OpenBetPosition -> | |
triggerStatus <- TriggerStatus.WaitToCheckProfit | |
timeToCheckProfit <- getTimeToCheckProfit() | |
TriggerResult.ExecuteActionBotOnMarketSelectionAndContinueToExecute (market, selection, true) | |
| TriggerStatus.WaitToCheckProfit -> | |
if DateTime.Now >= timeToCheckProfit | |
then | |
triggerStatus <- TriggerStatus.EndExecution | |
let profitInTicks = getMyProfitInTicks() | |
match shouldClosePositionForProfitOrLoss profitInTicks with | |
| Some status -> | |
if status | |
then | |
triggerStatus <- TriggerStatus.EndExecution | |
cancelMyActionBot() | |
outputMessage "Hedging my bet position." | |
TriggerResult.ExecuteBfexplorerBotOnSelectionWithParametersAndContinueToExecute (setupMyHedgeBot()) | |
else | |
outputMessage "Not in a profit, leaving my bet position running." | |
TriggerResult.EndExecution | |
| None -> | |
outputMessage "No bet position is open." | |
TriggerResult.EndExecution | |
else | |
TriggerResult.WaitingForOperation | |
| TriggerStatus.EndExecution -> TriggerResult.EndExecution | |
member __.EndExecution() = | |
() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment