From 9beeb038ff705bf0db910ff34bb89de3e534fa51 Mon Sep 17 00:00:00 2001 From: Michael Harding Date: Tue, 3 Dec 2019 14:58:07 -0500 Subject: [PATCH] Add security to the PnL form and make table lazy --- src/web/ui/src/App.fs | 6 +- src/web/ui/src/Reports/State.fs | 60 +++++++++++++++++--- src/web/ui/src/Reports/Types.fs | 17 ++++++ src/web/ui/src/Reports/View.fs | 99 ++++++++++++++++++++++++--------- 4 files changed, 145 insertions(+), 37 deletions(-) diff --git a/src/web/ui/src/App.fs b/src/web/ui/src/App.fs index c0272e28e..5fa13fe74 100644 --- a/src/web/ui/src/App.fs +++ b/src/web/ui/src/App.fs @@ -43,9 +43,9 @@ open Elmish.HMR // App Program.mkProgram init update root #if DEBUG -|> Program.withConsoleTrace -|> Program.withDebugger -|> Program.withHMR +//|> Program.withConsoleTrace +//|> Program.withDebugger +//|> Program.withHMR #endif |> Program.withReactUnoptimized "elmish-app" |> Program.run diff --git a/src/web/ui/src/Reports/State.fs b/src/web/ui/src/Reports/State.fs index 651846bc1..61ce07ce3 100644 --- a/src/web/ui/src/Reports/State.fs +++ b/src/web/ui/src/Reports/State.fs @@ -2,6 +2,7 @@ module Reports.State open Elmish open Types +open JSMap open System open System.Runtime.InteropServices open Fable.Import @@ -11,6 +12,7 @@ open System.Xml open Fable.PowerPack.PromiseImpl open bts.api.admin open bts.api.position +open bts.co2 open Global @@ -27,12 +29,15 @@ let PnLSub model dispatch = let svc = model.positionService svc.subscribe_pnl_data "" ( fun (subUpdate:StreamUtils.IUpdate) -> - subUpdate.all - |> JSMap.toArray - |> Array.map snd - |> SetPositionData - |> dispatch - ) + match subUpdate.added |> isEmpty, subUpdate.changed |> isEmpty with + | true, true -> () + | _, _ -> + subUpdate.all + |> JSMap.toArray + |> Array.map snd + |> SetPositionData + |> dispatch + ) |> SetUnsub |> dispatch @@ -47,10 +52,21 @@ let selectSub (tab:Tab option) model = // () // Cmd.none +let requestProducts (mdRelay:IMarketDataRelay) = promise { + let! result = mdRelay.request_products() + return UpdateProducts result.products +} + +let requestExpNames (mdRelay:IMarketDataRelay) = promise { + let! result = mdRelay.request_names() + return UpdateExpirationNames result.securities +} + let init (context:ServiceContext.ServiceContext) : Model * Cmd = let admin = AdminService.Create context "" let posServ = PositionService.Create context "" let basePosServ = BasePositionService.Create context "" + let mdr = MarketDataRelay.Create context "" { currentTab = Tab.PnL admin = admin @@ -62,6 +78,10 @@ let init (context:ServiceContext.ServiceContext) : Model * Cmd = ConfirmModal = "", (Msg.SwitchTab Tab.PnL) token = context.token this_username = context.username + mdRelay = mdr + mdRelayExpNameData = Array.empty + mdRelayProductData = [||] + securityList = Map.empty }, getAdminData admin context.token let update msg (model:Reports.Types.Model) : Model * Cmd = @@ -69,7 +89,10 @@ let update msg (model:Reports.Types.Model) : Model * Cmd = | UpdateAdminData adm -> { model with adminData = Some adm - }, selectSub (Some PnL) model + }, Cmd.batch [ + selectSub (Some PnL) model + Cmd.ofExistingPromise (requestProducts model.mdRelay) id (box>>ErrorOccured) + ] | RpcResponse str -> printf "%s" str model, Cmd.Empty @@ -85,6 +108,25 @@ let update msg (model:Reports.Types.Model) : Model * Cmd = | SetUnsub u -> model, Cmd.Empty | SetPositionData data -> - Browser.console.log("set data") - Browser.console.log(data) { model with positionData = data }, Cmd.none + | RequestExpirationNames -> + model, Cmd.ofExistingPromise (requestExpNames model.mdRelay) id (box>>ErrorOccured) + | UpdateExpirationNames (data:ExpirationNameList[]) -> + let secs:(uint32 * PositionSecurity) list = + data + |> List.ofArray + |> List.collect (fun i -> + JSMap.toList i.names + |> List.map (fun x -> + (uint32 <| fst x), + ({ securityName = (snd x) + venue = i.product.exchange + symbol = i.product.symbol + securityType = i.product.``type`` + }: PositionSecurity) + ) + ) + + { model with mdRelayExpNameData = data; securityList = secs |> Map.ofList }, selectSub None model + | UpdateProducts data -> + { model with mdRelayProductData = data}, Cmd.ofExistingPromise (requestExpNames model.mdRelay) id (box>>ErrorOccured) diff --git a/src/web/ui/src/Reports/Types.fs b/src/web/ui/src/Reports/Types.fs index 7bb720c3c..40c42cde1 100644 --- a/src/web/ui/src/Reports/Types.fs +++ b/src/web/ui/src/Reports/Types.fs @@ -2,11 +2,21 @@ module Reports.Types open System open bts.api.admin open bts.api.position +open bts.api +open bts.co2 open JSMap type Tab = | PnL +type PositionSecurity = + { + securityName: string + venue: bts.api.Venue + symbol: string + securityType: bts.api.SecurityType + } + type Msg = | RpcResponse of string | UpdateAdminData of AdminData @@ -15,6 +25,9 @@ type Msg = | OpenConfirmModal of string * Msg | SetUnsub of System.IDisposable | SetPositionData of PNLUnit [] + | RequestExpirationNames + | UpdateExpirationNames of ExpirationNameList[] + | UpdateProducts of Product[] type Model = { currentTab:Tab @@ -23,7 +36,11 @@ type Model = { positionData : PNLUnit [] basePositionService: IBasePositionService positionService: IPositionService + mdRelay : IMarketDataRelay + mdRelayProductData: Product[] + mdRelayExpNameData: ExpirationNameList[] unsub : System.IDisposable option + securityList : Map ConfirmModal: string * Msg token: string this_username: string diff --git a/src/web/ui/src/Reports/View.fs b/src/web/ui/src/Reports/View.fs index b3cf9411e..f321ed720 100644 --- a/src/web/ui/src/Reports/View.fs +++ b/src/web/ui/src/Reports/View.fs @@ -6,12 +6,14 @@ open Fable.Helpers.React open Fable.Helpers.React.Props open Fable.Core.DynamicExtensions open Elmish +open Elmish.React open Fable.Import open Global.Reflection open Types open Reports open bts.api.admin open bts.api +open bts.api.position open EnumUtils open Microsoft.FSharp.Collections; @@ -21,6 +23,41 @@ let isActiveTab tab currentTab = | Tab.PnL, Tab.PnL -> ia // | _ -> "" +let venueToString (venue:bts.api.Venue) = + match venue with + | bts.api.Venue.UNSET -> "Unset" + | bts.api.Venue.BTS -> "BTS" + | bts.api.Venue.SIM -> "SIM" + | bts.api.Venue.CBOE -> "CBOE" + | bts.api.Venue.CBOECFE -> "CBOECFE" + | bts.api.Venue.GLBX -> "GLBX" + | bts.api.Venue.ICE -> "ICE" + | bts.api.Venue.SIMICE -> "SIMICE" + +let securityTypeToString (venue:bts.api.SecurityType) = + match venue with + | bts.api.SecurityType.UNSET -> "Unset" + | bts.api.SecurityType.STOCK -> "Stock" + | bts.api.SecurityType.OPTION -> "Option" + | bts.api.SecurityType.FUTURE -> "Future" + | bts.api.SecurityType.SPREAD -> "Spread" + | bts.api.SecurityType.SYNTHETIC -> "Synthetic" + +let calcPnL (bid:double) (ask:double) (longPrice:double) (shortPrice:double) longPos shortPos (cashValue:double) = + match bid, ask with + | 0.0, _ | _,0.0 -> 0.0 + | _ ,_ -> + let mid = (bid + ask) / 2.0 + let longDelta:double = mid - longPrice + let shortDelta:double = shortPrice - mid + + ( + ( + (longDelta * longPos) + + ( shortDelta * shortPos) + ) + * cashValue + ) let PnLTabView (model:Model) (dispatch:Msg->Unit) = // Browser.window.localStorage.setItem("thing", "some") @@ -83,42 +120,31 @@ let PnLTabView (model:Model) (dispatch:Msg->Unit) = | 0.0 -> "" | _ -> sprintf "%.2f" number - let boolToYesNo yes = match yes with | true -> "Yes" | false -> "No" - let calcPnL (bid:double) (ask:double) (longPrice:double) (shortPrice:double) longPos shortPos (cashValue:double) = - match bid, ask with - | 0.0, _ | _,0.0 -> 0.0 - | _ ,_ -> - let mid = (bid + ask) / 2.0 - let longDelta:double = mid - longPrice - let shortDelta:double = shortPrice - mid - - ( - ( - (longDelta * longPos) - + ( shortDelta * shortPos) - ) - * cashValue - ) - // ( - // ((bidp - longp) * longPos * cashValue) - // + (((shortp - askp) * shortPos * cashValue)) - // ) + let getSec ndx = + match model.securityList.Count with + | 0 -> { + securityName = "" + venue = bts.api.Venue.UNSET + symbol = "" + securityType = bts.api.SecurityType.UNSET + } + | _ -> model.securityList.[ndx] let rws:TableControl.row list = [ for data in model.positionData do yield { cells = [ - TableControl.Cell (str (sprintf "%i" data.pos.securityId)) + TableControl.Cell (str (sprintf "%s" (getSec <| uint32 data.pos.securityId).symbol)) TableControl.Cell (str data.pos.account) - TableControl.Cell (str (sprintf "%i" data.pos.securityId)) - TableControl.Cell (str (sprintf "%i" data.pos.securityId)) - TableControl.Cell (str (sprintf "%i" data.pos.securityId)) + TableControl.Cell (str (sprintf "%s" (venueToString (getSec <| uint32 data.pos.securityId).venue))) + TableControl.Cell (str (sprintf "%s" (securityTypeToString <| (getSec <| uint32 data.pos.securityId).securityType))) + TableControl.Cell (str (sprintf "%s" (getSec <| uint32 data.pos.securityId).securityName)) TableControl.Cell (str (sprintf "%i" data.pos.securityId)) TableControl.Cell (str (numberOrNot ((calcPnL data.bidPrice @@ -201,11 +227,34 @@ let PnLTabView (model:Model) (dispatch:Msg->Unit) = TableControl.createDataTable (Some headerRow) rws (span [][]) ] +let PnLAreEqual (one:PNLUnit []) (two:PNLUnit []) = + match one.Length = two.Length with + | false -> false + | true -> + let mutable acc = true + for i = 0 to (one.Length - 1) do + acc <- acc + && (int <| one.[i].pos.dayLongPos) = (int <| two.[i].pos.dayLongPos) + && (int <| one.[i].pos.dayShortPos) = (int <| two.[i].pos.dayShortPos) + && (int <| one.[i].pos.sodLongPos) = (int <| two.[i].pos.sodLongPos) + && (int <| one.[i].pos.sodShortPos) = (int <| two.[i].pos.sodShortPos) + && (int <| one.[i].askPrice) = (int <| two.[i].askPrice) + && (int <| one.[i].bidPrice) = (int <| two.[i].bidPrice) + acc + +let lazyPnLView model dispatch = + lazyView2With (fun a b -> + //LanguagePrimitives.PhysicalEquality a.positionData b.positionData + PnLAreEqual a.positionData b.positionData + ) + PnLTabView + model dispatch + let tabContent (model:Model) (dispatch:Msg->Unit) = div [ ClassName "is-tab-content"] [ // match model.currentTab with // | Tab.PnL -> PnLTabView model dispatch - PnLTabView model dispatch + lazyPnLView model dispatch ] let root (model:Model) (dispatch:Msg->Unit) =