From 5cd3497b4468d1ea13c19eda795eef21644422b2 Mon Sep 17 00:00:00 2001 From: artemiusgreat Date: Thu, 30 May 2024 03:31:19 -0400 Subject: [PATCH] Gateways --- Client/Components/PageComponent.razor.cs | 12 ++++---- Client/Templates/BaseTemplate.razor | 5 ++++ Core/Extensions/Double.cs | 4 +-- Core/Indicators/MovingAverageIndicator.cs | 2 +- Core/Indicators/RelativeStrengthIndicator.cs | 2 +- Core/Indicators/ScaleIndicator.cs | 2 +- Core/Services/AverageService.cs | 30 +++++++++---------- ...tificationService.cs => MessageService.cs} | 2 +- Gateways/Alpaca/Libs/Adapter.cs | 13 ++++---- Gateways/Ameritrade/Libs/Tracer.cs | 16 ---------- .../{Ameritrade => Schwab}/Libs/Adapter.cs | 0 .../Libs/Authenticator.cs | 0 .../Libs/Models/ChainModels.cs | 0 .../Libs/Models/UserModels.cs | 0 .../{Ameritrade => Schwab}/Libs/Schwab.csproj | 0 .../Tests/Connectors.cs | 0 .../{Ameritrade => Schwab}/Tests/Tests.csproj | 0 .../{Ameritrade => Schwab}/Tests/Usings.cs | 0 Gateways/Simulation/Libs/Adapter.cs | 4 +-- Solution.sln | 4 +-- 20 files changed, 45 insertions(+), 51 deletions(-) rename Core/Services/{NotificationService.cs => MessageService.cs} (72%) delete mode 100644 Gateways/Ameritrade/Libs/Tracer.cs rename Gateways/{Ameritrade => Schwab}/Libs/Adapter.cs (100%) rename Gateways/{Ameritrade => Schwab}/Libs/Authenticator.cs (100%) rename Gateways/{Ameritrade => Schwab}/Libs/Models/ChainModels.cs (100%) rename Gateways/{Ameritrade => Schwab}/Libs/Models/UserModels.cs (100%) rename Gateways/{Ameritrade => Schwab}/Libs/Schwab.csproj (100%) rename Gateways/{Ameritrade => Schwab}/Tests/Connectors.cs (100%) rename Gateways/{Ameritrade => Schwab}/Tests/Tests.csproj (100%) rename Gateways/{Ameritrade => Schwab}/Tests/Usings.cs (100%) diff --git a/Client/Components/PageComponent.razor.cs b/Client/Components/PageComponent.razor.cs index 1f9ae9250..f8618dc80 100644 --- a/Client/Components/PageComponent.razor.cs +++ b/Client/Components/PageComponent.razor.cs @@ -1,9 +1,11 @@ +using Distribution.Services; using Microsoft.AspNetCore.Components; using Microsoft.Extensions.Configuration; using System; using System.Linq; using System.Threading.Tasks; using Terminal.Core.Domains; +using Terminal.Core.Services; namespace Client.Components { @@ -39,7 +41,7 @@ public virtual async Task OnConnect() } catch (Exception e) { - Console.WriteLine(e); + InstanceService.Instance.OnMessage($"{e}"); } } @@ -57,7 +59,7 @@ public virtual void OnDisconnect() } catch (Exception e) { - Console.WriteLine(e); + InstanceService.Instance.OnMessage($"{e}"); } } @@ -70,7 +72,7 @@ public virtual void OnSubscribe() } catch (Exception e) { - Console.WriteLine(e); + InstanceService.Instance.OnMessage($"{e}"); } } @@ -83,7 +85,7 @@ public virtual void OnUnsubscribe() } catch (Exception e) { - Console.WriteLine(e); + InstanceService.Instance.OnMessage($"{e}"); } } @@ -91,7 +93,7 @@ public virtual void OnOpenStatements() { if (Adapter?.Account is not null) { - StatementsView.UpdateItems(new[] { Adapter.Account }); + StatementsView.UpdateItems([Adapter.Account]); } } } diff --git a/Client/Templates/BaseTemplate.razor b/Client/Templates/BaseTemplate.razor index b87ef0f76..250b7331c 100644 --- a/Client/Templates/BaseTemplate.razor +++ b/Client/Templates/BaseTemplate.razor @@ -1,3 +1,6 @@ +@using Distribution.Services +@using Terminal.Core.Services +@inject ISnackbar Messenger @inherits LayoutComponentBase Terminal @@ -20,5 +23,7 @@ { StateHasChanged(); } + + InstanceService.Instance.OnMessage += message => Messenger.Add(message); } } diff --git a/Core/Extensions/Double.cs b/Core/Extensions/Double.cs index 2b9228625..87e9a91c8 100644 --- a/Core/Extensions/Double.cs +++ b/Core/Extensions/Double.cs @@ -4,9 +4,9 @@ namespace Terminal.Core.Extensions { public static class DoubleExtensions { - public static bool IsEqual(this double input, double num, double epsilon = double.Epsilon) + public static bool Is(this double input, double num, double precision = double.Epsilon) { - return Math.Abs(input - num) < epsilon; + return Math.Abs(input - num) < precision; } } } diff --git a/Core/Indicators/MovingAverageIndicator.cs b/Core/Indicators/MovingAverageIndicator.cs index 864a4f5f5..6fb7412b5 100644 --- a/Core/Indicators/MovingAverageIndicator.cs +++ b/Core/Indicators/MovingAverageIndicator.cs @@ -72,7 +72,7 @@ public override MovingAverageIndicator Calculate(ObservableCollection collec _min = _min is null ? value : Math.Min(_min.Value, value); _max = _max is null ? value : Math.Max(_max.Value, value); - if (_min.Value.IsEqual(_max.Value) is false) + if (_min.Value.Is(_max.Value) is false) { value = Min + (value - _min.Value) * (Max - Min) / (_max.Value - _min.Value); } diff --git a/Core/Services/AverageService.cs b/Core/Services/AverageService.cs index 0c510120c..23a3b13f9 100644 --- a/Core/Services/AverageService.cs +++ b/Core/Services/AverageService.cs @@ -14,19 +14,19 @@ public class AverageService /// public virtual double SimpleAverage(IList collection, int index, int interval) { - var v = 0.0; + var response = 0.0; if (interval > 0 && index >= interval - 1) { for (var i = 0; i < interval; i++) { - v += collection.ElementAt(index - i); + response += collection.ElementAt(index - i); } - v /= interval; + response /= interval; } - return v; + return response; } /// @@ -39,15 +39,15 @@ public virtual double SimpleAverage(IList collection, int index, int int /// public virtual double ExponentialAverage(IList collection, int index, int interval, double previous) { - var v = 0.0; + var response = 0.0; if (interval > 0) { var pr = 2.0 / (interval + 1.0); - v = collection.ElementAt(index) * pr + previous * (1 - pr); + response = collection.ElementAt(index) * pr + previous * (1 - pr); } - return v; + return response; } /// @@ -60,7 +60,7 @@ public virtual double ExponentialAverage(IList collection, int index, in /// public virtual double SmoothAverage(IList collection, int index, int interval, double previous) { - var v = 0.0; + var response = 0.0; if (interval > 0) { @@ -68,19 +68,19 @@ public virtual double SmoothAverage(IList collection, int index, int int { for (var i = 0; i < interval; i++) { - v += collection.ElementAt(index - i); + response += collection.ElementAt(index - i); } - v /= interval; + response /= interval; } if (index >= interval) { - v = (previous * (interval - 1) + collection.ElementAt(index)) / interval; + response = (previous * (interval - 1) + collection.ElementAt(index)) / interval; } } - return v; + return response; } /// @@ -92,9 +92,9 @@ public virtual double SmoothAverage(IList collection, int index, int int /// public virtual double LinearWeightAverage(IList collection, int index, int interval) { - var v = 0.0; var sum = 0.0; var wsum = 0.0; + var response = 0.0; if (interval > 0 && index >= interval - 1) { @@ -104,10 +104,10 @@ public virtual double LinearWeightAverage(IList collection, int index, i sum += collection.ElementAt(index - i + 1) * (interval - i + 1); } - v = sum / wsum; + response = sum / wsum; } - return v; + return response; } } } diff --git a/Core/Services/NotificationService.cs b/Core/Services/MessageService.cs similarity index 72% rename from Core/Services/NotificationService.cs rename to Core/Services/MessageService.cs index 4f2fdc168..de09d3c5a 100644 --- a/Core/Services/NotificationService.cs +++ b/Core/Services/MessageService.cs @@ -2,7 +2,7 @@ namespace Terminal.Core.Services { - public class NotificationService + public class MessageService { public virtual Action OnMessage { get; set; } } diff --git a/Gateways/Alpaca/Libs/Adapter.cs b/Gateways/Alpaca/Libs/Adapter.cs index c1956c838..0ed389809 100644 --- a/Gateways/Alpaca/Libs/Adapter.cs +++ b/Gateways/Alpaca/Libs/Adapter.cs @@ -17,6 +17,7 @@ using Terminal.Core.Enums; using Terminal.Core.Extensions; using Terminal.Core.Models; +using Terminal.Core.Services; namespace Alpaca { @@ -223,7 +224,7 @@ public async Task GetAccountData() var positions = await SendData("/v2/positions"); var orders = await SendData("/v2/orders"); - if (account.Data is not null) + try { Account.Balance = account.Data.Equity; Account.Descriptor = account.Data.AccountNumber; @@ -233,6 +234,10 @@ public async Task GetAccountData() Account.ActiveOrders.ForEach(async o => await Subscribe(o.Value.Transaction.Instrument.Name)); Account.ActivePositions.ForEach(async o => await Subscribe(o.Value.Order.Transaction.Instrument.Name)); } + catch (Exception e) + { + InstanceService.Instance.OnMessage($"{e}"); + } } /// @@ -282,8 +287,6 @@ await ws.ReceiveAsync(data, cancellation.Token).ContinueWith(async o => ?.AsArray() ?.FirstOrDefault(); - Console.WriteLine(content); - switch ($"{message?["T"]}".ToUpper()) { case "Q": ProcessPoint(content); break; @@ -326,7 +329,7 @@ protected void ProcessPoint(string content) } catch (Exception e) { - Console.WriteLine(e); + InstanceService.Instance.OnMessage(e.Message); } } @@ -344,7 +347,7 @@ protected void ProcessTrade(string content) } catch (Exception e) { - Console.WriteLine(e); + InstanceService.Instance.OnMessage(e.Message); } } diff --git a/Gateways/Ameritrade/Libs/Tracer.cs b/Gateways/Ameritrade/Libs/Tracer.cs deleted file mode 100644 index f6c90d800..000000000 --- a/Gateways/Ameritrade/Libs/Tracer.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Newtonsoft.Json.Serialization; -using System; -using System.Diagnostics; - -namespace Schwab -{ - public class ConsoleTraceWriter : ITraceWriter - { - public TraceLevel LevelFilter => TraceLevel.Verbose; - - public void Trace(TraceLevel state, string message, Exception e) - { - Console.WriteLine($"{state}: {message} Exception: {e?.Message}"); - } - } -} diff --git a/Gateways/Ameritrade/Libs/Adapter.cs b/Gateways/Schwab/Libs/Adapter.cs similarity index 100% rename from Gateways/Ameritrade/Libs/Adapter.cs rename to Gateways/Schwab/Libs/Adapter.cs diff --git a/Gateways/Ameritrade/Libs/Authenticator.cs b/Gateways/Schwab/Libs/Authenticator.cs similarity index 100% rename from Gateways/Ameritrade/Libs/Authenticator.cs rename to Gateways/Schwab/Libs/Authenticator.cs diff --git a/Gateways/Ameritrade/Libs/Models/ChainModels.cs b/Gateways/Schwab/Libs/Models/ChainModels.cs similarity index 100% rename from Gateways/Ameritrade/Libs/Models/ChainModels.cs rename to Gateways/Schwab/Libs/Models/ChainModels.cs diff --git a/Gateways/Ameritrade/Libs/Models/UserModels.cs b/Gateways/Schwab/Libs/Models/UserModels.cs similarity index 100% rename from Gateways/Ameritrade/Libs/Models/UserModels.cs rename to Gateways/Schwab/Libs/Models/UserModels.cs diff --git a/Gateways/Ameritrade/Libs/Schwab.csproj b/Gateways/Schwab/Libs/Schwab.csproj similarity index 100% rename from Gateways/Ameritrade/Libs/Schwab.csproj rename to Gateways/Schwab/Libs/Schwab.csproj diff --git a/Gateways/Ameritrade/Tests/Connectors.cs b/Gateways/Schwab/Tests/Connectors.cs similarity index 100% rename from Gateways/Ameritrade/Tests/Connectors.cs rename to Gateways/Schwab/Tests/Connectors.cs diff --git a/Gateways/Ameritrade/Tests/Tests.csproj b/Gateways/Schwab/Tests/Tests.csproj similarity index 100% rename from Gateways/Ameritrade/Tests/Tests.csproj rename to Gateways/Schwab/Tests/Tests.csproj diff --git a/Gateways/Ameritrade/Tests/Usings.cs b/Gateways/Schwab/Tests/Usings.cs similarity index 100% rename from Gateways/Ameritrade/Tests/Usings.cs rename to Gateways/Schwab/Tests/Usings.cs diff --git a/Gateways/Simulation/Libs/Adapter.cs b/Gateways/Simulation/Libs/Adapter.cs index 868b396a3..5a5d1b302 100644 --- a/Gateways/Simulation/Libs/Adapter.cs +++ b/Gateways/Simulation/Libs/Adapter.cs @@ -398,7 +398,7 @@ protected virtual PositionModel DecreasePosition(OrderModel order, PositionModel Next = nextOrder }; - if (nextPosition.Order.Transaction.Volume?.IsEqual(0) is false) + if (nextPosition.Order.Transaction.Volume?.Is(0) is false) { message.Action = ActionEnum.Delete; Account.ActivePositions.Add(nextPosition.Order.Transaction.Id, nextPosition); @@ -535,7 +535,7 @@ protected virtual PointModel Parse(string name, string input) } }; - if (askSize.IsEqual(0)) + if (askSize.Is(0)) { response.Last = bid; } diff --git a/Solution.sln b/Solution.sln index 438d5ed44..be1b16ab3 100644 --- a/Solution.sln +++ b/Solution.sln @@ -15,9 +15,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Simulation", "Simulation", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Simulation", "Gateways\Simulation\Libs\Simulation.csproj", "{1F838D97-6CF0-40E4-813F-B4C4707E4DDC}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Schwab", "Gateways\Ameritrade\Libs\Schwab.csproj", "{697A2F71-6DC7-4AA3-9D5B-61899605BE3F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Schwab", "Gateways\Schwab\Libs\Schwab.csproj", "{697A2F71-6DC7-4AA3-9D5B-61899605BE3F}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "Gateways\Ameritrade\Tests\Tests.csproj", "{8DA38824-8E1A-4E42-92AF-D1AA6F4AA730}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "Gateways\Schwab\Tests\Tests.csproj", "{8DA38824-8E1A-4E42-92AF-D1AA6F4AA730}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "Gateways\Simulation\Tests\Tests.csproj", "{7FFC95C5-48CA-4475-BEA3-740CB427345C}" EndProject