From 94d66a8e994f10324878f5cd7f99e4e567bdf075 Mon Sep 17 00:00:00 2001 From: Michael Harding Date: Mon, 1 Jul 2019 12:40:16 -0400 Subject: [PATCH] Add Margin tab to Spark Admin --- src/web/ui/src/Access.fs | 26 +++++++++++++ src/web/ui/src/Accounts/State.fs | 30 ++++++++++++--- src/web/ui/src/Accounts/Types.fs | 14 +++++++ src/web/ui/src/Accounts/View.fs | 62 ++++++++++++++++++++++++++++++- src/web/ui/src/Controls/Fields.fs | 7 +++- 5 files changed, 130 insertions(+), 9 deletions(-) diff --git a/src/web/ui/src/Access.fs b/src/web/ui/src/Access.fs index 3de9afcff..1c715c97b 100644 --- a/src/web/ui/src/Access.fs +++ b/src/web/ui/src/Access.fs @@ -1,6 +1,7 @@ [] module Access open bts.api.admin +open bts.api.position open bts.api open Elmish open bts.api.orderman @@ -20,6 +21,12 @@ let getAccountOrdersData (admin:IAdminService) (success) (fail) = success (fun e -> fail e.Message) +let getMarginData (span:IMarginService) (account_name: string) (success) (fail) = + Cmd.ofPromise + span.get_excess_liquidity_report + { account = account_name } + success + (fun e -> fail e.Message) module Lens = // type Setter<'a,'b> = { @@ -108,6 +115,25 @@ module Accounts = } } + let emptyMargin : ExcessLiquidityReport = { + account = "ERROR NO DATA" + marginResult = { + account = "ERROR NO DATA" + status = bts.api.position.MarginResult.ResultStatus.Error + portfolioId = "" + settleIndicator = "" + settleQualifier = "" + asOfTime = "" + calculationId = "" + optValue = 0.0 + nonoptValue = 0.0 + maintMargin = 0.0 + initialMargin = 0.0 + } + leverageFactor = 0.0 + cashBalance = 0.0 + excessLiquidity = 0.0 + } diff --git a/src/web/ui/src/Accounts/State.fs b/src/web/ui/src/Accounts/State.fs index fcbaac147..d13f9a5b4 100644 --- a/src/web/ui/src/Accounts/State.fs +++ b/src/web/ui/src/Accounts/State.fs @@ -8,6 +8,7 @@ open Fable.Import open bts.co2 open Fable open Fable.PowerPack +open bts.api.position let isNewAccount (account:BTSAccount) = account.id = 0u && account.isPresent @@ -17,8 +18,12 @@ let currentAccountIsDefault (account:BTSAccount) = (account.id = 0u) let init (context:ServiceContext.ServiceContext) = let admin = AdminService.Create context "" let mdr = MarketDataRelay.Create context "" + let span = MarginService.Create context "" { tab = General admin = admin + marginService = span + marginData = emptyMargin + marginScratch = emptyMargin adminData = None ordersData = [] SelectedOrders = [] @@ -75,7 +80,9 @@ let applySIMUpdate (data:SIMAccountData) = function let saveAccount (admin:IAdminService) (account:BTSAccount) (model:Model) = promise { let! result = admin.update_account { completeData = account } - if result.isSuccess then + let! result2 = model.marginService.set_cash_factor { account = model.scratch.name; leverageFactor = model.marginScratch.leverageFactor } + let! result3 = model.marginService.set_cash_balance { account = model.scratch.name; cashBalance = model.marginScratch.cashBalance } + if result.isSuccess && result2.success && result3.success then let! data = admin.get_filtered_admin_data () return UpdateAdminData data else @@ -194,7 +201,18 @@ let update msg (model:Model) : Model * Cmd = | _ -> [] {model with SelectedOrders = so}, Cmd.Empty | SelectAccount account -> - { (clearSelection model) with previousScratch = model.scratch; scratch = account }, Cmd.Empty + { (clearSelection model) with previousScratch = model.scratch; scratch = account }, Cmd.ofMsg <| GetMarginData + | GetMarginData -> + { model with ShowLoading = false}, (getMarginData model.marginService model.scratch.name UpdateMarginData (box>>ErrorOccured)) + | UpdateMarginData newData -> + match (model.marginScratch = emptyMargin || model.marginScratch.account <> model.scratch.name) with + | true -> {model with marginData = newData; marginScratch = newData}, Cmd.Empty + | false -> {model with marginData = newData}, Cmd.Empty + + | SetMarginCashBalance newBalance -> + { model with marginScratch = { model.marginScratch with cashBalance = newBalance}},Cmd.Empty + | SetMarginCashFactor newFactor -> + { model with marginScratch = { model.marginScratch with leverageFactor = newFactor}},Cmd.Empty | DeleteAccount account -> let mdl = { model with OptionsMenuOpen = ""} match Fable.Import.Browser.window.confirm (sprintf "Are you sure you want to delete '%s'" account.name) with @@ -217,6 +235,8 @@ let update msg (model:Model) : Model * Cmd = match tab with | OrdersTab -> { (clearSelection model) with tab = tab }, Cmd.ofMsg GetOrdersData + | MarginTab -> + { (clearSelection model) with tab = tab }, Cmd.ofMsg GetMarginData | _ -> { (clearSelection model) with tab = tab }, Cmd.Empty | Update field -> @@ -227,8 +247,8 @@ let update msg (model:Model) : Model * Cmd = { model with scratch = { model.scratch with securityClipLimits = { limit = limits } }; SelectedClipLimits = selectedlimits }, Cmd.Empty | AddClipLimit limit -> let limits = match (Array.exists (fun (elm:AccountSecurityClipLimitUnit) -> elm.security.securitySymbol = "") model.scratch.securityClipLimits.limit) with - | true -> model.scratch.securityClipLimits.limit - | false -> Array.concat [ model.scratch.securityClipLimits.limit; [| limit |] ] + | true -> model.scratch.securityClipLimits.limit + | false -> Array.concat [ model.scratch.securityClipLimits.limit; [| limit |] ] { model with scratch = { model.scratch with securityClipLimits = { limit = limits } }}, Cmd.Empty | ChangeMultipleClipLimits (value, selectedLimits) -> @@ -320,7 +340,7 @@ let update msg (model:Model) : Model * Cmd = | RequestExpirationNames -> model, Cmd.ofExistingPromise (requestExpNames model.mdRelay) id (box>>ErrorOccured) | UpdateExpirationNames (data:ExpirationNameList[]) -> - { model with mdRelayExpNameData = data }, Cmd.Empty + { model with mdRelayExpNameData = data }, Cmd.ofMsg GetMarginData | OpenOptionsMenu acc -> {model with OptionsMenuOpen = acc}, Cmd.Empty | OpenConfirmModal (title, okMessage) -> diff --git a/src/web/ui/src/Accounts/Types.fs b/src/web/ui/src/Accounts/Types.fs index f249085bf..be4e54e8c 100644 --- a/src/web/ui/src/Accounts/Types.fs +++ b/src/web/ui/src/Accounts/Types.fs @@ -5,6 +5,7 @@ open bts.api open bts.api.orderman open JSMap open bts.co2 +open bts.api.position type Tab = | General @@ -13,6 +14,7 @@ type Tab = | ExchangeSettingsTab | ConnectionsTab | OrdersTab + | MarginTab let venues = [| Venue.GLBX; Venue.SIM |] @@ -50,10 +52,19 @@ type Msg = | UpdateOrdersData of OrdersSummary | SelectOrder of bool * OrdersSummaryUnit list | CancelOrders of OrdersSummaryUnit list + // Margin / Span + | GetMarginData + | UpdateMarginData of ExcessLiquidityReport + | SetMarginCashBalance of double + | SetMarginCashFactor of double + | SelectAccount of BTSAccount | DeleteAccount of BTSAccount | AddAccount + | SaveLeverageFactor + | SaveCashBalance | SaveScratch + | ErrorOccured of obj // Tabs | SelectTab of Tab @@ -91,6 +102,9 @@ type Msg = type Model = { tab : Tab admin : IAdminService + marginService: IMarginService + marginData: ExcessLiquidityReport + marginScratch: ExcessLiquidityReport adminData : AdminData option ordersData : OrdersSummaryUnit list SelectedOrders: OrdersSummaryUnit list diff --git a/src/web/ui/src/Accounts/View.fs b/src/web/ui/src/Accounts/View.fs index 0b66c54cb..65c0f7925 100644 --- a/src/web/ui/src/Accounts/View.fs +++ b/src/web/ui/src/Accounts/View.fs @@ -23,9 +23,12 @@ let selectedAccount model = | None -> None let isDirty model = + let marginDirty = model.marginData.cashBalance <> model.marginScratch.cashBalance || + model.marginData.leverageFactor <> model.marginScratch.leverageFactor match selectedAccount model with | Some account -> - account <> model.scratch + account <> model.scratch || + marginDirty | None -> false let saveButton model dispatch = @@ -80,6 +83,58 @@ let generalView model dispatch = accountControls model dispatch ] +let marginView model dispatch = + + let headerRow:TableControl.row = + { + cells = + [ + //TableControl.Cell (str "Account") + TableControl.Cell (str "As Of Time") + //TableControl.Cell (str "Cash Balance") + TableControl.Cell (str "Excess Liquidity") + TableControl.Cell (str "Initial Margin") + //TableControl.Cell (str "Leverage Factor") + TableControl.Cell (str "Maint Margin") + // TableControl.Cell (str "nonoptValue") + // TableControl.Cell (str "optValue") + // TableControl.Cell (str "portfolioId") + // TableControl.Cell (str "settleIndicator") + // TableControl.Cell (str "settleQualifier") + ] + className = "" + header = None + } + + let rws:TableControl.row = + { + cells = + [ + //TableControl.Cell (str model.marginScratch.account) + TableControl.Cell (str model.marginScratch.marginResult.asOfTime) + //TableControl.Cell (str (sprintf "%f" model.marginScratch.cashBalance)) + TableControl.Cell (str (sprintf "%f" model.marginScratch.excessLiquidity)) + TableControl.Cell (str (sprintf "%f" model.marginScratch.marginResult.initialMargin)) + //TableControl.Cell (str (sprintf "%f" model.marginScratch.leverageFactor)) + TableControl.Cell (str (sprintf "%f" model.marginScratch.marginResult.maintMargin)) + // TableControl.Cell (str (sprintf "%f" margin.nonoptValue)) + // TableControl.Cell (str (sprintf "%f" margin.optValue)) + // TableControl.Cell (str margin.portfolioId) + // TableControl.Cell (str margin.settleIndicator) + // TableControl.Cell (str margin.settleQualifier) + ] + className = "" + header = None + } + + div [][ + TableControl.createDataTable (Some headerRow) [rws] (span [][]) + Fields.double "Cash Factor:" model.marginScratch.leverageFactor [] (SetMarginCashFactor >> dispatch) + Fields.double "Cash Balance:" model.marginScratch.cashBalance [] (SetMarginCashBalance >> dispatch) + accountControls model dispatch + ] + + let GetSymbolArray (model:Model) = Array.map (fun a -> a.symbol ) model.mdRelayProductData |> Array.distinct @@ -771,7 +826,8 @@ let tabContent model dispatch = | ConnectionsTab, _ -> ConnectionsView model dispatch | CreditLimitsTab, _ -> creditLimitsView model dispatch | ExchangeSettingsTab, _ -> exchangeSettings model dispatch - | OrdersTab, _ -> ordersView model dispatch) + | OrdersTab, _ -> ordersView model dispatch + | marginTab, _ -> marginView model dispatch) ] let isActive account = function @@ -905,6 +961,7 @@ let tabName = function | ConnectionsTab -> "Connections" | ExchangeSettingsTab -> "Exchange Settings" | OrdersTab -> "Orders" + | MarginTab -> "Margin" | other -> string other let tabHeader tab currentTab dispatch = @@ -939,6 +996,7 @@ let root (model:Model) dispatch = // tabHeader CreditLimits model.tab dispatch (if (Array.length data.accounts > 0) then (tabHeader ExchangeSettingsTab model.tab dispatch) else span [][] ) (if (Array.length data.accounts > 0) then (tabHeader OrdersTab model.tab dispatch) else span [][] ) + (if (Array.length data.accounts > 0) then (tabHeader MarginTab model.tab dispatch) else span [][] ) ] ] tabContent model dispatch diff --git a/src/web/ui/src/Controls/Fields.fs b/src/web/ui/src/Controls/Fields.fs index 2fbd6cd2f..719e7705d 100644 --- a/src/web/ui/src/Controls/Fields.fs +++ b/src/web/ui/src/Controls/Fields.fs @@ -134,6 +134,9 @@ let password (lbl:string) (dv:string) (attrs:IHTMLProp list) onchange = let number (lbl:string) (dv:int) (attrs:IHTMLProp list) onchange = text lbl (string dv) attrs (int>>onchange) +let double (lbl:string) (dv:double) (attrs:IHTMLProp list) onchange = + text lbl (string dv) attrs (double>>onchange) + let enumSelect<'a> (lbl:string) (dv:'a) (values:'a[]) (attrs:IHTMLProp list) (onchange:'a->unit) = let vals = match (Array.tryFindIndex (fun elm -> unbox elm = unbox dv) values) with | None -> Array.concat [[| dv |]; values] @@ -194,7 +197,7 @@ let radioButton (lbl:string) (description:string) (dv:bool) (attrs:IHTMLProp lis let mapSelectEx (lbl:string) (options:Map) (defaultText:string option) (defaultValue:string option) (onchange:'a->unit) (is_disabled:bool option) = - let dis = + let dis = match is_disabled with | None -> false | Some a -> a @@ -214,7 +217,7 @@ let mapSelectEx (lbl:string) (options:Map) (defaultText:string op Label lbl div [ ClassName "control" ] [ - div [ ClassName "select"] + div [ ClassName "select"] [ select [ Value dv