From 15bc2c7ca1b5bfca555efce732ab0ccc7bdbdc3b Mon Sep 17 00:00:00 2001 From: Michael Harding Date: Fri, 3 Jan 2020 14:53:48 -0500 Subject: [PATCH] SP-54: PnL filters --- .gitignore | 1 + src/web/ui/src/App.fs | 2 +- src/web/ui/src/Reports/State.fs | 116 +++++++++++++++++++++++++++++++- src/web/ui/src/Reports/Types.fs | 11 +++ src/web/ui/src/Reports/View.fs | 91 ++++++++++++------------- 5 files changed, 174 insertions(+), 47 deletions(-) diff --git a/.gitignore b/.gitignore index b8b69fa2d..2c4aa85ae 100644 --- a/.gitignore +++ b/.gitignore @@ -33,4 +33,5 @@ trace_data/ /src/lib/orderman/perf_analysis/log.csv /doc/user/public/* .ionide/* +*.ionide /.ignore diff --git a/src/web/ui/src/App.fs b/src/web/ui/src/App.fs index 5fa13fe74..dd41af6f5 100644 --- a/src/web/ui/src/App.fs +++ b/src/web/ui/src/App.fs @@ -45,7 +45,7 @@ Program.mkProgram init update root #if DEBUG //|> Program.withConsoleTrace //|> Program.withDebugger -//|> Program.withHMR +|> 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 792fd07ae..9607267ec 100644 --- a/src/web/ui/src/Reports/State.fs +++ b/src/web/ui/src/Reports/State.fs @@ -152,6 +152,9 @@ let init (context:ServiceContext.ServiceContext) : Model * Cmd = PnLColumns = defaultPnLColumns PnLSortFun = (fun (left:PNLUnit) (right:PNLUnit) -> (left.pos.securityId - right.pos.securityId) |> int) PnLSortDirection = "Security Key", true + PnLFilter = Map.empty + PnLFilterValues = Map.empty + FilterOpen = "", Empty, false GridSettingsOpen = false }, getAdminData admin context.token @@ -171,6 +174,90 @@ let calcPnL (bid:double) (ask:double) (longPrice:double) (shortPrice:double) lon * cashValue ) +let calcTotalPnL data = + (calcPnL + data.bidPrice + data.askPrice + data.pos.sod.longAvgPrice + data.pos.sod.shortAvgPrice + data.pos.sod.longPos + data.pos.sod.shortPos + data.cashValue) + + (calcPnL + data.bidPrice + data.askPrice + data.pos.day.longAvgPrice + data.pos.day.shortAvgPrice + data.pos.day.longPos + data.pos.day.shortPos + data.cashValue) + +let calcSODPnL data = + (calcPnL + data.bidPrice + data.askPrice + data.pos.sod.longAvgPrice + data.pos.sod.shortAvgPrice + data.pos.sod.longPos + data.pos.sod.shortPos + data.cashValue) + +let calcDayPnL data = + calcPnL + data.bidPrice + data.askPrice + data.pos.day.longAvgPrice + data.pos.day.shortAvgPrice + data.pos.day.longPos + data.pos.day.shortPos + data.cashValue + +let getPnLFilterType header = + match header = "Symbol" || header = "Account"|| header = "Exchange" || header = "Type" || header = "Security" || header = "Traded Today" with + | true -> StringSearch "" + | false -> NumberRange (Double.MinValue, Double.MaxValue) + +let getValFromPNLUnit header (data:PNLUnit) (securityList: Map option) = + let sec = match securityList with + | Some secL -> getSec <| secL <| uint32 data.pos.securityId + | None -> { securityName = ""; venue = bts.api.Venue.UNSET; symbol = ""; securityType = bts.api.SecurityType.UNSET} + + match header with + | "Symbol" -> sec.symbol, 0.0 + | "Account" -> data.pos.account, 0.0 + | "Exchange" -> venueToString sec.venue, 0.0 + | "Type" -> securityTypeToString <| sec.securityType, 0.0 + | "Security" -> sec.securityName, 0.0 + | "Security Key" -> "", data.pos.securityId |> double + | "Total PnL" -> "", calcTotalPnL data + | "Open PnL" -> "", calcSODPnL data + | "Day PnL" -> "", calcDayPnL data + | "Total Pos" -> "", ((data.pos.day.longPos + data.pos.sod.longPos) - (data.pos.day.shortPos + data.pos.sod.shortPos)) + | "Total Long" -> "", (data.pos.day.longPos + data.pos.sod.longPos) + | "Total Long Avg" -> "", ((data.pos.day.longAvgPrice + data.pos.sod.longAvgPrice) / 2.0 ) + | "Total Short" -> "", data.pos.day.shortPos + data.pos.sod.shortPos + | "Total Short Avg" -> "", (data.pos.day.shortAvgPrice + data.pos.sod.shortAvgPrice) / 2.0 + | "Open Pos" -> "", data.pos.sod.longPos - data.pos.sod.shortPos + | "Open Long" -> "", data.pos.sod.longPos + | "Open Long Avg" -> "", data.pos.sod.longAvgPrice + | "Open Short" -> "", data.pos.sod.shortPos + | "Open Short Avg" -> "", data.pos.sod.shortAvgPrice + | "Day Pos" -> "", data.pos.day.longPos - data.pos.day.shortPos + | "Day Long" -> "", data.pos.day.longPos + | "Day Long Avg" -> "", data.pos.day.longAvgPrice + | "Day Short" -> "", data.pos.day.shortPos + | "Day Short Avg" -> "", data.pos.day.shortAvgPrice + | "Raw Day Long" -> "", data.pos.rawDay.longPos + | "Raw Day Short" -> "", data.pos.rawDay.shortPos + | "Raw Day Pos" -> "", data.pos.rawDay.longPos - data.pos.rawDay.shortPos + | "Traded Today" -> (if (data.pos.day.longPos > 0.0) || (data.pos.day.shortPos > 0.0) then "yes" else "no"), 0.0 + | "Bid" -> "", data.bidPrice + | "Ask" -> "", data.askPrice + | "Mid" -> "", (data.askPrice + data.bidPrice) / 2.0 + | "Last" -> "", data.lastPrice + | "Settlement" -> "", data.settlementPrice + | "CPP" -> "", data.cashValue + | _ -> "", 0.0 let update msg (model:Reports.Types.Model) : Model * Cmd = match msg with @@ -231,6 +318,34 @@ let update msg (model:Reports.Types.Model) : Model * Cmd = { model with PnLColumns = data}, Cmd.Empty | ShowGridSettings data -> {model with GridSettingsOpen = data}, Cmd.Empty + | ShowFilterBox (header, filter, isOpen) -> + let newFilter = match filter, Map.containsKey header model.PnLFilterValues with + | Empty, false -> getPnLFilterType header + | Empty , true -> model.PnLFilterValues.Item header + | _ -> filter + + {model with FilterOpen = header, newFilter, isOpen}, Cmd.Empty + + | SetPnLFilter (header, filter) -> + let newFilterValues = + match Map.containsKey header model.PnLFilterValues with + | true -> (Map.remove header model.PnLFilterValues).Add(header, filter) + | false -> model.PnLFilterValues.Add(header, filter) + + let newFilter = + match filter with + | StringSearch s -> + function pnl -> + let v = (getValFromPNLUnit header pnl (Some model.securityList)) |> fst + if s.Trim() = "" || v.ToLower().Contains (s.Trim().ToLower()) then Some pnl else None + | NumberRange (min, max) -> + function pnl -> + let curVal = (getValFromPNLUnit header pnl None) |> snd + if (min <= curVal && curVal <= max) then Some pnl else None + | Empty -> Some + let newMap = model.PnLFilter.Add (header, newFilter) + { model with PnLFilter = newMap; PnLFilterValues = newFilterValues }, ("", Empty, false) |> ShowFilterBox |> Cmd.ofMsg + | SetPnLSort header -> Browser.console.log header let direction = fst <| model.PnLSortDirection <> header || not <| snd model.PnLSortDirection @@ -444,5 +559,4 @@ let update msg (model:Reports.Types.Model) : Model * Cmd = (fun (left:PNLUnit) (right:PNLUnit) -> (compNum left.cashValue right.cashValue) * 1000.0 |> int )// CPP/cash value | _ -> (fun (left:PNLUnit) (right:PNLUnit) -> 0) - Browser.console.log sortFn { model with PnLSortFun = sortFn; PnLSortDirection = header, direction }, Cmd.Empty diff --git a/src/web/ui/src/Reports/Types.fs b/src/web/ui/src/Reports/Types.fs index 88e46c4cc..e79e830b4 100644 --- a/src/web/ui/src/Reports/Types.fs +++ b/src/web/ui/src/Reports/Types.fs @@ -9,6 +9,11 @@ open JSMap type Tab = | PnL +type ColFilter = + | StringSearch of string + | NumberRange of min:double * max:double + | Empty + type PositionSecurity = { securityName: string @@ -31,7 +36,10 @@ type Msg = | ChangePnLColumnVisibility of string * bool | SetPnLColumns of (string * bool) List | SetPnLSort of string + | SetPnLFilter of string * ColFilter + //| ClearPnLFilter of string | ShowGridSettings of bool + | ShowFilterBox of string * ColFilter * bool type Model = { currentTab:Tab @@ -48,7 +56,10 @@ type Model = { PnLColumns: (string * bool) list PnLSortFun: PNLUnit -> PNLUnit -> int PnLSortDirection: string * bool + PnLFilter: Map PNLUnit option> + PnLFilterValues: Map GridSettingsOpen: bool + FilterOpen: string * ColFilter * bool 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 b00967874..99813d26f 100644 --- a/src/web/ui/src/Reports/View.fs +++ b/src/web/ui/src/Reports/View.fs @@ -44,12 +44,39 @@ let PnLTabView (model:Model) (dispatch:Msg->Unit) = (Button.button "Close" "" "times" Button.Enabled (fun _ -> false |> ShowGridSettings |> dispatch)) (fun _ -> false |> ShowGridSettings |> dispatch) + let filterBox = + let filterHeader, filterType, filterIsOpen = model.FilterOpen + match filterType, filterIsOpen with + | _, false | Empty, _ -> span [][] + | StringSearch searchStr, true -> + Modal.show + "" + (div[][ + Fields.text "Search:" (searchStr) [] (fun v -> (filterHeader, StringSearch v, filterIsOpen) |> ShowFilterBox |> dispatch) + ]) + (Button.button "Apply" "" "" Button.Enabled (fun _ -> (filterHeader, filterType) |> SetPnLFilter |> dispatch)) + (fun _ -> ("", Empty, false) |> ShowFilterBox |> dispatch) + | NumberRange (min, max), true -> + Modal.show + "" + (div[][ + Fields.number_novalidate "Min" min [] (fun v -> (filterHeader, NumberRange (v, max), filterIsOpen) |> ShowFilterBox |> dispatch) + Fields.number_novalidate "Max" max [] (fun v -> (filterHeader, NumberRange (min, v), filterIsOpen) |> ShowFilterBox |> dispatch) + ]) + (Button.button "Apply" "" "" Button.Enabled (fun _ -> (filterHeader, filterType) |> SetPnLFilter |> dispatch)) + (fun _ -> ("", Empty, false) |> ShowFilterBox |> dispatch) + + + let sortIcon header visible = match fst model.PnLSortDirection = header, snd model.PnLSortDirection with | true, true -> i [ ClassName (sprintf "fa fa-sort-up")] [ ] | true, false -> i [ ClassName (sprintf "fa fa-sort-down")] [ ] | _, _ -> i [][] + let filterIcon header = + i [ ClassName ("fa fa-filter"); OnClick (fun _ -> (header, Empty, true) |> ShowFilterBox |> dispatch) ] [ ] + let headerRow:TableControl.row = { cells = @@ -57,6 +84,7 @@ let PnLTabView (model:Model) (dispatch:Msg->Unit) = ( div [][ span [ OnClick (fun _ -> header |> SetPnLSort |> dispatch)] [str <| sprintf "%s " header] sortIcon header visible + filterIcon header ]) ) ] className = "" @@ -74,7 +102,14 @@ let PnLTabView (model:Model) (dispatch:Msg->Unit) = | true -> "Yes" | false -> "No" - let sortedPositionData = model.positionData |> Array.sortWith model.PnLSortFun + let bind a b i = + match a i with + | Some i -> b i + | None -> None + + let uberfilter = if model.PnLFilter.Count > 0 then model.PnLFilter |> Map.toArray |> Array.map snd |> Array.reduce bind else Some + let filteredPositionData = (model.positionData |> Array.choose uberfilter) + let sortedPositionData = filteredPositionData |> Array.sortWith model.PnLSortFun let rws:TableControl.row list = [ @@ -84,53 +119,17 @@ let PnLTabView (model:Model) (dispatch:Msg->Unit) = model.PnLColumns do if visible then yield ( - let scrty = getSec <| model.securityList <| uint32 data.pos.securityId + let sec = getSec <| model.securityList <| uint32 data.pos.securityId match header with - | "Symbol" -> TableControl.Cell (str (sprintf "%s" scrty.symbol)) + | "Symbol" -> TableControl.Cell (str (sprintf "%s" sec.symbol)) | "Account" -> TableControl.Cell (str data.pos.account) - | "Exchange" -> TableControl.Cell (str (sprintf "%s" (venueToString scrty.venue))) - | "Type" -> TableControl.Cell (str (sprintf "%s" (securityTypeToString <| scrty.securityType))) - | "Security" -> TableControl.Cell (str (sprintf "%s" scrty.securityName)) + | "Exchange" -> TableControl.Cell (str (sprintf "%s" (venueToString sec.venue))) + | "Type" -> TableControl.Cell (str (sprintf "%s" (securityTypeToString <| sec.securityType))) + | "Security" -> TableControl.Cell (str (sprintf "%s" sec.securityName)) | "Security Key" -> TableControl.Cell (str (sprintf "%i" data.pos.securityId)) - | "Total PnL" -> - TableControl.Cell (str (numberOrNot ((calcPnL - data.bidPrice - data.askPrice - data.pos.sod.longAvgPrice - data.pos.sod.shortAvgPrice - data.pos.sod.longPos - data.pos.sod.shortPos - data.cashValue) - + (calcPnL - data.bidPrice - data.askPrice - data.pos.day.longAvgPrice - data.pos.day.shortAvgPrice - data.pos.day.longPos - data.pos.day.shortPos - data.cashValue) - ) - ))//Total PNL - | "Open PnL" -> - TableControl.Cell (str (numberOrNot (calcPnL - data.bidPrice - data.askPrice - data.pos.sod.longAvgPrice - data.pos.sod.shortAvgPrice - data.pos.sod.longPos - data.pos.sod.shortPos - data.cashValue) - ))// SOD Pnl (Open PnL) - | "Day PnL" -> - TableControl.Cell (str (numberOrNot (calcPnL - data.bidPrice - data.askPrice - data.pos.day.longAvgPrice - data.pos.day.shortAvgPrice - data.pos.day.longPos - data.pos.day.shortPos - data.cashValue) - ))//Day PnL + | "Total PnL" -> TableControl.Cell (str (numberOrNot (calcTotalPnL data))) + | "Open PnL" -> TableControl.Cell (str (numberOrNot (calcSODPnL data))) + | "Day PnL" -> TableControl.Cell (str (numberOrNot (calcDayPnL data))) | "Total Pos" -> TableControl.Cell (str (sprintf "%.2f" ((data.pos.day.longPos + data.pos.sod.longPos) - (data.pos.day.shortPos + data.pos.sod.shortPos))))//Total Pos | "Total Long" -> @@ -192,6 +191,7 @@ let PnLTabView (model:Model) (dispatch:Msg->Unit) = div [] [ TableControl.createDataTable (Some headerRow) rws (Button.fabutton "Settings" "cog" "" Button.Enabled ((fun _ -> true |> ShowGridSettings |> dispatch))) settingsModal + filterBox ] let PnLAreEqual (one:PNLUnit []) (two:PNLUnit []) = @@ -216,6 +216,7 @@ let lazyPnLView model dispatch = && a.GridSettingsOpen = b.GridSettingsOpen && a.PnLColumns = b.PnLColumns && a.PnLSortDirection = b.PnLSortDirection + && a.FilterOpen = b.FilterOpen ) PnLTabView model dispatch