Skip to content

Commit

Permalink
Gateways
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiusgreat committed Jul 10, 2024
1 parent e35d14a commit acaac27
Show file tree
Hide file tree
Showing 156 changed files with 4,670 additions and 1,973 deletions.
1 change: 1 addition & 0 deletions Client/Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

<ItemGroup>
<ProjectReference Include="..\Gateways\Alpaca\Libs\Alpaca.csproj" />
<ProjectReference Include="..\Gateways\InteractiveBrokers\Libs\InteractiveBrokers.csproj" />
<ProjectReference Include="..\Gateways\Schwab\Libs\Schwab.csproj" />
<ProjectReference Include="..\Gateways\Simulation\Libs\Simulation.csproj" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Client/Components/PageComponent.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public virtual void Subscribe()
try
{
IsSubscription = true;
Adapter.Account.Instruments.ForEach(o => Adapter.Subscribe(o.Key));
Adapter.Account.Instruments.ForEach(o => Adapter.Subscribe(o.Value));
}
catch (Exception e)
{
Expand All @@ -80,7 +80,7 @@ public virtual void Unsubscribe()
try
{
IsSubscription = false;
Adapter.Account.Instruments.ForEach(o => Adapter.Unsubscribe(o.Key));
Adapter.Account.Instruments.ForEach(o => Adapter.Unsubscribe(o.Value));
}
catch (Exception e)
{
Expand Down
6 changes: 3 additions & 3 deletions Client/Pages/Orders.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ protected virtual void CreateAccounts()
Account = new Account
{
Descriptor = "Demo",
Instruments = new Dictionary<string, Instrument>
Instruments = new Dictionary<string, InstrumentModel>
{
[_asset] = new Instrument { Name = _asset }
[_asset] = new InstrumentModel { Name = _asset }
}
};

Expand Down Expand Up @@ -126,7 +126,7 @@ private async Task OnData(PointModel point)
await View.PositionsView.UpdateItems(Account.ActivePositions);
}

private void OpenPositions(IInstrument assetBuy, IInstrument assetSell)
private void OpenPositions(InstrumentModel assetBuy, InstrumentModel assetSell)
{
var orderSell = new OrderModel
{
Expand Down
8 changes: 4 additions & 4 deletions Client/Pages/Pairs.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ protected virtual void CreateAccounts()
Account = new Account
{
Balance = 25000,
Instruments = new Dictionary<string, Instrument>
Instruments = new Dictionary<string, InstrumentModel>
{
[_assetX] = new Instrument { Name = _assetX },
[_assetY] = new Instrument { Name = _assetY }
[_assetX] = new InstrumentModel { Name = _assetX },
[_assetY] = new InstrumentModel { Name = _assetY }
}
};

Expand Down Expand Up @@ -163,7 +163,7 @@ private async Task OnData(PointModel point)
await View.PositionsView.UpdateItems(Account.ActivePositions);
}

private void OpenPositions(IInstrument assetBuy, IInstrument assetSell)
private void OpenPositions(InstrumentModel assetBuy, InstrumentModel assetSell)
{
var orderSell = new OrderModel
{
Expand Down
25 changes: 25 additions & 0 deletions Core/Arguments/InstrumentArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Terminal.Core.Models
{
public class InstrumentArgs
{
/// <summary>
/// Symbol name
/// </summary>
public string Name { get; set; }

/// <summary>
/// Security type
/// </summary>
public string Security { get; set; }

/// <summary>
/// Exchange
/// </summary>
public string Exchange { get; set; }

/// <summary>
/// Currency
/// </summary>
public string Currency { get; set; }
}
}
32 changes: 32 additions & 0 deletions Core/Arguments/OptionsArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;

namespace Terminal.Core.Models
{
public class OptionsArgs
{
/// <summary>
/// Symbol name
/// </summary>
public string Name { get; set; }

/// <summary>
/// Min strike
/// </summary>
public double? MinPrice { get; set; }

/// <summary>
/// Max strike
/// </summary>
public double? MaxPrice { get; set; }

/// <summary>
/// Start date
/// </summary>
public DateTime MinDate { get; set; }

/// <summary>
/// End date
/// </summary>
public DateTime MaxDate { get; set; }
}
}
18 changes: 18 additions & 0 deletions Core/Arguments/OrdersArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;

namespace Terminal.Core.Models
{
public class OrdersArgs
{
/// <summary>
/// Order status
/// </summary>
public string Status { get; set; }

/// <summary>
/// Symbols
/// </summary>
public IList<string> Names { get; set; }
}
}
27 changes: 27 additions & 0 deletions Core/Arguments/PointsArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace Terminal.Core.Models
{
public class PointsArgs
{
/// <summary>
/// Symbol name
/// </summary>
public string Name { get; set; }

/// <summary>
/// Security type
/// </summary>
public string Security { get; set; }

/// <summary>
/// Start date
/// </summary>
public DateTime MinDate { get; set; }

/// <summary>
/// End date
/// </summary>
public DateTime MaxDate { get; set; }
}
}
12 changes: 12 additions & 0 deletions Core/Arguments/PositionsArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;

namespace Terminal.Core.Models
{
public class PositionsArgs
{
/// <summary>
/// Symbols
/// </summary>
public IList<string> Names { get; set; }
}
}
6 changes: 3 additions & 3 deletions Core/Domains/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public interface IAccount
/// <summary>
/// List of instruments
/// </summary>
IDictionary<string, Instrument> Instruments { get; set; }
IDictionary<string, InstrumentModel> Instruments { get; set; }
}

/// <summary>
Expand Down Expand Up @@ -117,7 +117,7 @@ public class Account : IAccount
/// <summary>
/// List of instruments
/// </summary>
public virtual IDictionary<string, Instrument> Instruments { get; set; }
public virtual IDictionary<string, InstrumentModel> Instruments { get; set; }

/// <summary>
/// Constructor
Expand All @@ -131,7 +131,7 @@ public Account()

Orders = [];
Positions = [];
Instruments = new Dictionary<string, Instrument>();
Instruments = new Dictionary<string, InstrumentModel>();
ActiveOrders = new Dictionary<string, OrderModel>();
ActivePositions = new Dictionary<string, PositionModel>();
}
Expand Down
84 changes: 71 additions & 13 deletions Core/Domains/Gateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,63 @@ public interface IGateway : IDisposable
/// <summary>
/// Subscribe
/// </summary>
Task<IList<ErrorModel>> Subscribe(string name);
/// <param name="instrument"></param>
/// <returns></returns>
Task<IList<ErrorModel>> Subscribe(InstrumentModel instrument);

/// <summary>
/// Unsubscribe
/// </summary>
Task<IList<ErrorModel>> Unsubscribe(string name);
/// <param name="instrument"></param>
/// <returns></returns>
Task<IList<ErrorModel>> Unsubscribe(InstrumentModel instrument);

/// <summary>
/// Get account state
/// </summary>
/// <param name="criteria"></param>
/// <returns></returns>
Task<ResponseModel<IAccount>> GetAccount(Hashtable criteria);

/// <summary>
/// Get latest quote
/// </summary>
/// <param name="args"></param>
/// <param name="criteria"></param>
/// <returns></returns>
Task<ResponseItemModel<IDictionary<string, PointModel>>> GetPoint(Hashtable criteria);
Task<ResponseModel<DomModel>> GetDom(InstrumentArgs args, Hashtable criteria);

/// <summary>
/// Get historical bars
/// </summary>
/// <param name="args"></param>
/// <param name="criteria"></param>
/// <returns></returns>
Task<ResponseItemModel<IList<PointModel>>> GetPoints(Hashtable criteria);
Task<ResponseModel<IList<PointModel>>> GetPoints(PointsArgs args, Hashtable criteria);

/// <summary>
/// Get options
/// </summary>
/// <param name="props"></param>
/// <param name="args"></param>
/// <param name="criteria"></param>
/// <returns></returns>
Task<ResponseModel<IList<OptionModel>>> GetOptions(OptionsArgs args, Hashtable criteria);

/// <summary>
/// Get positions
/// </summary>
/// <param name="args"></param>
/// <param name="criteria"></param>
/// <returns></returns>
Task<ResponseModel<IList<PositionModel>>> GetPositions(PositionsArgs args, Hashtable criteria);

/// <summary>
/// Get orders
/// </summary>
/// <param name="args"></param>
/// <param name="criteria"></param>
/// <returns></returns>
Task<ResponseItemModel<IList<OptionModel>>> GetOptions(Hashtable criteria);
Task<ResponseModel<IList<OrderModel>>> GetOrders(OrdersArgs args, Hashtable criteria);

/// <summary>
/// Send new orders
Expand Down Expand Up @@ -107,9 +137,9 @@ public Gateway()
/// <summary>
/// Subscribe
/// </summary>
/// <param name="name"></param>
/// <param name="instrument"></param>
/// <returns></returns>
public abstract Task<IList<ErrorModel>> Subscribe(string name);
public abstract Task<IList<ErrorModel>> Subscribe(InstrumentModel instrument);

/// <summary>
/// Disconnect
Expand All @@ -120,28 +150,56 @@ public Gateway()
/// <summary>
/// Unsubscribe
/// </summary>
public abstract Task<IList<ErrorModel>> Unsubscribe(string name);
/// <param name="instrument"></param>
/// <returns></returns>
public abstract Task<IList<ErrorModel>> Unsubscribe(InstrumentModel instrument);

/// <summary>
/// Get account state
/// </summary>
/// <param name="criteria"></param>
/// <returns></returns>
public abstract Task<ResponseModel<IAccount>> GetAccount(Hashtable criteria);

/// <summary>
/// Get latest quote
/// </summary>
/// <param name="args"></param>
/// <param name="criteria"></param>
/// <returns></returns>
public abstract Task<ResponseItemModel<IDictionary<string, PointModel>>> GetPoint(Hashtable criteria);
public abstract Task<ResponseModel<DomModel>> GetDom(InstrumentArgs args, Hashtable criteria);

/// <summary>
/// Get historical bars
/// </summary>
/// <param name="args"></param>
/// <param name="criteria"></param>
/// <returns></returns>
public abstract Task<ResponseItemModel<IList<PointModel>>> GetPoints(Hashtable criteria);
public abstract Task<ResponseModel<IList<PointModel>>> GetPoints(PointsArgs args, Hashtable criteria);

/// <summary>
/// Get options
/// </summary>
/// <param name="args"></param>
/// <param name="criteria"></param>
/// <returns></returns>
public abstract Task<ResponseModel<IList<OptionModel>>> GetOptions(OptionsArgs args, Hashtable criteria);

/// <summary>
/// Get positions
/// </summary>
/// <param name="args"></param>
/// <param name="criteria"></param>
/// <returns></returns>
public abstract Task<ResponseModel<IList<PositionModel>>> GetPositions(PositionsArgs args, Hashtable criteria);

/// <summary>
/// Get orders
/// </summary>
/// <param name="args"></param>
/// <param name="criteria"></param>
/// <returns></returns>
public abstract Task<ResponseItemModel<IList<OptionModel>>> GetOptions(Hashtable criteria);
public abstract Task<ResponseModel<IList<OrderModel>>> GetOrders(OrdersArgs args, Hashtable criteria);

/// <summary>
/// Send new orders
Expand Down Expand Up @@ -208,7 +266,7 @@ protected virtual ResponseMapModel<OrderModel> ValidateOrders(params OrderModel[
})));

response.Count += errors.Count;
response.Items.Add(new ResponseItemModel<OrderModel>
response.Items.Add(new ResponseModel<OrderModel>
{
Data = order,
Errors = errors
Expand Down
29 changes: 29 additions & 0 deletions Core/Extensions/Hashtable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections;
using System.Collections.Specialized;
using System.Web;

namespace Terminal.Core.Extensions
{
public static class HashtableExtensions
{
public static T Get<T>(this Hashtable input, string index)
{
return (T)(index is not null && input.ContainsKey(index) ? input[index] : default);
}

public static NameValueCollection Merge(this Hashtable source, params Hashtable[] maps)
{
var response = HttpUtility.ParseQueryString(string.Empty);

foreach (Hashtable map in maps ?? [])
{
foreach (DictionaryEntry o in map ?? [])
{
response[$"{o.Key}"] = $"{o.Value}";
}
}

return response;
}
}
}
Loading

0 comments on commit acaac27

Please sign in to comment.