Skip to content

Commit

Permalink
Orders
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiusgreat committed Dec 16, 2024
1 parent 5189b96 commit 9b747e5
Show file tree
Hide file tree
Showing 27 changed files with 561 additions and 355 deletions.
45 changes: 45 additions & 0 deletions Core/Domains/Gateway.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Terminal.Core.Enums;
using Terminal.Core.Models;
Expand Down Expand Up @@ -239,5 +240,49 @@ protected virtual IList<IAccount> SetupAccounts(params IAccount[] accounts)

return accounts;
}

/// <summary>
/// Create separate orders when combo-orders are not supported
/// </summary>
/// <param name="order"></param>
/// <returns></returns>
protected virtual IList<OrderModel> ComposeOrders(OrderModel order)
{
OrderModel merge(OrderModel subOrder, OrderModel group)
{
var nextOrder = subOrder.Clone() as OrderModel;
var groupOrders = group
?.Orders
?.Where(o => o.Instruction is InstructionEnum.Brace)
?.Where(o => Equals(o.Name, nextOrder.Name)) ?? [];

nextOrder.Price ??= nextOrder.GetOpenEstimate();
nextOrder.Type ??= group.Type ?? OrderTypeEnum.Market;
nextOrder.TimeSpan ??= group.TimeSpan ?? OrderTimeSpanEnum.Gtc;
nextOrder.Instruction ??= InstructionEnum.Side;
nextOrder.Transaction.Price ??= nextOrder.Price;
nextOrder.Transaction.Time ??= DateTime.Now;
nextOrder.Transaction.CurrentVolume = nextOrder.Transaction.Volume;
nextOrder.Descriptor = group.Descriptor;
nextOrder.Orders = [.. nextOrder.Orders, .. groupOrders];

return nextOrder;
}

var nextOrders = order
.Orders
.Where(o => o.Instruction is InstructionEnum.Side)
.Select(o => merge(o, order))
.ToList();

if (order.Transaction is not null)
{
nextOrders.Add(merge(order, order));
}

order.Orders.Clear();

return nextOrders;
}
}
}
12 changes: 6 additions & 6 deletions Core/Enums/ActionEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ namespace Terminal.Core.Enums
{
public enum ActionEnum : byte
{
None = 0,
Create = 1,
Update = 2,
Delete = 3,
Connect = 4,
Disconnect = 5
None,
Create,
Update,
Delete,
Connect,
Disconnect
}
}
7 changes: 4 additions & 3 deletions Core/Enums/CurrencyEnum.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
namespace Terminal.Core.Enums
namespace Terminal.Core.Enums
{
public enum CurrencyEnum : byte
{
USD = 1,
EUR = 2
None,
USD,
EUR
}
}
19 changes: 10 additions & 9 deletions Core/Enums/InstrumentEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ namespace Terminal.Core.Enums
{
public enum InstrumentEnum : byte
{
None = 0,
Bonds = 1,
Coins = 2,
Shares = 3,
Options = 4,
Futures = 5,
Contracts = 6,
Currencies = 7,
FuturesOptions = 8
None,
Bonds,
Coins,
Shares,
Indices,
Options,
Futures,
Contracts,
Currencies,
FuturesOptions
}
}
6 changes: 3 additions & 3 deletions Core/Enums/OperationEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ namespace Terminal.Core.Enums
{
public enum OperationEnum : byte
{
None = 0,
In = 1,
Out = 2
None,
In,
Out
}
}
8 changes: 4 additions & 4 deletions Core/Enums/OptionSideEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ namespace Terminal.Core.Enums
{
public enum OptionSideEnum : byte
{
None = 0,
Put = 1,
Call = 2,
Share = 3
None,
Put,
Call,
Share
}
}
8 changes: 4 additions & 4 deletions Core/Enums/OrderInstructionEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ namespace Terminal.Core.Enums
{
public enum InstructionEnum : byte
{
None = 0,
Side = 1,
Brace = 2,
Group = 3
None,
Side,
Brace,
Group
}
}
6 changes: 3 additions & 3 deletions Core/Enums/OrderSideEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ namespace Terminal.Core.Enums
{
public enum OrderSideEnum : byte
{
None = 0,
Buy = 1,
Sell = 2
None,
Buy,
Sell
}
}
10 changes: 5 additions & 5 deletions Core/Enums/OrderStatusEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ namespace Terminal.Core.Enums
{
public enum OrderStatusEnum : byte
{
None = 0,
Filled = 1,
Pending = 2,
Canceled = 3,
Partitioned = 4
None,
Filled,
Pending,
Canceled,
Partitioned
}
}
14 changes: 7 additions & 7 deletions Core/Enums/OrderTimeSpanEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ namespace Terminal.Core.Enums
{
public enum OrderTimeSpanEnum : byte
{
None = 0,
Day = 1,
Fok = 2,
Gtc = 3,
Ioc = 4,
Am = 5,
Pm = 6
None,
Day,
Fok,
Gtc,
Ioc,
Am,
Pm
}
}
10 changes: 5 additions & 5 deletions Core/Enums/OrderTypeEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ namespace Terminal.Core.Enums
{
public enum OrderTypeEnum : byte
{
None = 0,
Stop = 1,
Market = 2,
Limit = 3,
StopLimit = 4
None,
Stop,
Market,
Limit,
StopLimit
}
}
8 changes: 4 additions & 4 deletions Core/Enums/StatusEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ namespace Terminal.Core.Enums
{
public enum StatusEnum : byte
{
None = 0,
Error = 1,
Success = 2,
Progress = 3
None,
Error,
Success,
Progress
}
}
4 changes: 2 additions & 2 deletions Core/Models/Transactions/OrderModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public OrderModel()
{
Orders = [];
OrderStream = o => { };
Id = $"{Guid.NewGuid():N}".ToUpper();
Descriptor = $"{Guid.NewGuid():N}".ToUpper();
Id = $"{Guid.NewGuid()}";
Descriptor = $"{Guid.NewGuid()}";
}

/// <summary>
Expand Down
41 changes: 30 additions & 11 deletions Gateway/Alpaca/Libs/Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ public override async Task<ResponseModel<StatusEnum>> Subscribe(InstrumentModel

async Task subscribe<T>() where T : class, IStreamingDataClient
{
await Unsubscribe(instrument);

var client = _streamingClients[instrument.Type.Value] as T;
var onPointSub = client.GetQuoteSubscription(instrument.Name);
var onTradeSub = client.GetTradeSubscription(instrument.Name);
Expand All @@ -131,8 +133,6 @@ async Task subscribe<T>() where T : class, IStreamingDataClient

try
{
await Unsubscribe(instrument);

switch (instrument.Type)
{
case InstrumentEnum.Coins: await subscribe<IAlpacaCryptoStreamingClient>(); break;
Expand Down Expand Up @@ -448,30 +448,28 @@ public override async Task<ResponseModel<IList<PointModel>>> GetPoints(PointScre
/// <param name="orders"></param>
/// <returns></returns>
public override async Task<ResponseModel<IList<OrderModel>>> CreateOrders(params OrderModel[] orders)

{
var response = new ResponseModel<IList<OrderModel>> { Data = [] };

foreach (var order in orders)
{
try
{
Account.Orders[order.Id] = order;

var exOrder = ExternalMap.GetOrder(order);
var exResponse = await _tradingClient.PostOrderAsync(exOrder);
var inOrders = ComposeOrders(order);

order.Transaction.Id = $"{exResponse.OrderId}";
order.Transaction.Status = InternalMap.GetStatus(exResponse.OrderStatus);

response.Data.Add(order);
foreach (var inOrder in inOrders)
{
response.Data.Add((await CreateOrder(inOrder)).Data);
}
}
catch (Exception e)
{
response.Errors.Add(new ErrorModel { ErrorMessage = $"{e}" });
}
}

await GetAccount([]);

return response;
}

Expand All @@ -492,6 +490,27 @@ public override async Task<ResponseModel<IList<OrderModel>>> DeleteOrders(params
return response;
}

/// <summary>
/// Send order
/// </summary>
/// <param name="order"></param>
/// <returns></returns>
protected virtual async Task<ResponseModel<OrderModel>> CreateOrder(OrderModel order)
{
Account.Orders[order.Id] = order;

await Subscribe(order.Transaction.Instrument);

var exOrder = ExternalMap.GetOrder(order);
var response = new ResponseModel<OrderModel>();
var exResponse = await _tradingClient.PostOrderAsync(exOrder);

order.Transaction.Id = $"{exResponse.OrderId}";
order.Transaction.Status = InternalMap.GetStatus(exResponse.OrderStatus);

return response;
}

/// <summary>
/// Process quote from the stream
/// </summary>
Expand Down
Loading

0 comments on commit 9b747e5

Please sign in to comment.