Skip to content

Commit

Permalink
Gateways
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiusgreat committed May 30, 2024
1 parent fded0ae commit 5cd3497
Show file tree
Hide file tree
Showing 20 changed files with 45 additions and 51 deletions.
12 changes: 7 additions & 5 deletions Client/Components/PageComponent.razor.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -39,7 +41,7 @@ public virtual async Task OnConnect()
}
catch (Exception e)
{
Console.WriteLine(e);
InstanceService<MessageService>.Instance.OnMessage($"{e}");
}
}

Expand All @@ -57,7 +59,7 @@ public virtual void OnDisconnect()
}
catch (Exception e)
{
Console.WriteLine(e);
InstanceService<MessageService>.Instance.OnMessage($"{e}");
}
}

Expand All @@ -70,7 +72,7 @@ public virtual void OnSubscribe()
}
catch (Exception e)
{
Console.WriteLine(e);
InstanceService<MessageService>.Instance.OnMessage($"{e}");
}
}

Expand All @@ -83,15 +85,15 @@ public virtual void OnUnsubscribe()
}
catch (Exception e)
{
Console.WriteLine(e);
InstanceService<MessageService>.Instance.OnMessage($"{e}");
}
}

public virtual void OnOpenStatements()
{
if (Adapter?.Account is not null)
{
StatementsView.UpdateItems(new[] { Adapter.Account });
StatementsView.UpdateItems([Adapter.Account]);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions Client/Templates/BaseTemplate.razor
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
@using Distribution.Services
@using Terminal.Core.Services
@inject ISnackbar Messenger
@inherits LayoutComponentBase

<PageTitle>Terminal</PageTitle>
Expand All @@ -20,5 +23,7 @@
{
StateHasChanged();
}

InstanceService<MessageService>.Instance.OnMessage += message => Messenger.Add(message);
}
}
4 changes: 2 additions & 2 deletions Core/Extensions/Double.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
2 changes: 1 addition & 1 deletion Core/Indicators/MovingAverageIndicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public override MovingAverageIndicator Calculate(ObservableCollection<PointModel
var series = currentPoint.Series[Name] = currentPoint.Series.Get(Name) ?? new MovingAverageIndicator().Point;
var average = comService.LinearWeightAverage(Values, Values.Count - 1, Interval);

Point.Last = series.Last = series.Bar.Close = average.IsEqual(0) ? value : average;
Point.Last = series.Last = series.Bar.Close = average.Is(0) ? value : average;

return this;
}
Expand Down
2 changes: 1 addition & 1 deletion Core/Indicators/RelativeStrengthIndicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public override RelativeStrengthIndicator Calculate(ObservableCollection<PointMo

var averageUp = comService.SimpleAverage(ups, ups.Count - 1, Interval);
var averageDown = comService.SimpleAverage(downs, downs.Count - 1, Interval);
var average = averageDown.IsEqual(0) ? 1.0 : averageUp / averageDown;
var average = averageDown.Is(0) ? 1.0 : averageUp / averageDown;
var value = 100.0 - 100.0 / (1.0 + average);

switch (Values.Count < collection.Count)
Expand Down
2 changes: 1 addition & 1 deletion Core/Indicators/ScaleIndicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public override ScaleIndicator Calculate(ObservableCollection<PointModel> 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);
}
Expand Down
30 changes: 15 additions & 15 deletions Core/Services/AverageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ public class AverageService
/// <returns></returns>
public virtual double SimpleAverage(IList<double> 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;
}

/// <summary>
Expand All @@ -39,15 +39,15 @@ public virtual double SimpleAverage(IList<double> collection, int index, int int
/// <returns></returns>
public virtual double ExponentialAverage(IList<double> 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;
}

/// <summary>
Expand All @@ -60,27 +60,27 @@ public virtual double ExponentialAverage(IList<double> collection, int index, in
/// <returns></returns>
public virtual double SmoothAverage(IList<double> collection, int index, int interval, double previous)
{
var v = 0.0;
var response = 0.0;

if (interval > 0)
{
if (index == interval - 1)
{
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;
}

/// <summary>
Expand All @@ -92,9 +92,9 @@ public virtual double SmoothAverage(IList<double> collection, int index, int int
/// <returns></returns>
public virtual double LinearWeightAverage(IList<double> 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)
{
Expand All @@ -104,10 +104,10 @@ public virtual double LinearWeightAverage(IList<double> collection, int index, i
sum += collection.ElementAt(index - i + 1) * (interval - i + 1);
}

v = sum / wsum;
response = sum / wsum;
}

return v;
return response;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Terminal.Core.Services
{
public class NotificationService
public class MessageService
{
public virtual Action<string> OnMessage { get; set; }
}
Expand Down
13 changes: 8 additions & 5 deletions Gateways/Alpaca/Libs/Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Terminal.Core.Enums;
using Terminal.Core.Extensions;
using Terminal.Core.Models;
using Terminal.Core.Services;

namespace Alpaca
{
Expand Down Expand Up @@ -223,7 +224,7 @@ public async Task GetAccountData()
var positions = await SendData<JsonPosition[]>("/v2/positions");
var orders = await SendData<JsonOrder[]>("/v2/orders");

if (account.Data is not null)
try
{
Account.Balance = account.Data.Equity;
Account.Descriptor = account.Data.AccountNumber;
Expand All @@ -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<MessageService>.Instance.OnMessage($"{e}");
}
}

/// <summary>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -326,7 +329,7 @@ protected void ProcessPoint(string content)
}
catch (Exception e)
{
Console.WriteLine(e);
InstanceService<MessageService>.Instance.OnMessage(e.Message);
}
}

Expand All @@ -344,7 +347,7 @@ protected void ProcessTrade(string content)
}
catch (Exception e)
{
Console.WriteLine(e);
InstanceService<MessageService>.Instance.OnMessage(e.Message);
}
}

Expand Down
16 changes: 0 additions & 16 deletions Gateways/Ameritrade/Libs/Tracer.cs

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions Gateways/Simulation/Libs/Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -535,7 +535,7 @@ protected virtual PointModel Parse(string name, string input)
}
};

if (askSize.IsEqual(0))
if (askSize.Is(0))
{
response.Last = bid;
}
Expand Down
4 changes: 2 additions & 2 deletions Solution.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5cd3497

Please sign in to comment.