Skip to content

Commit

Permalink
Version
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiusgreat committed May 29, 2024
1 parent 8f4378b commit fded0ae
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 139 deletions.
58 changes: 58 additions & 0 deletions Core/Collections/ObservableGroupCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Terminal.Core.Collections
{
public interface IGroup
{
/// <summary>
/// Group index
/// </summary>
/// <returns></returns>
long GetIndex();

/// <summary>
/// Grouping implementation
/// </summary>
/// <param name="previous"></param>
/// <returns></returns>
IGroup Update(IGroup previous);
}

public class ObservableGroupCollection<T> : ObservableCollection<T> where T : IGroup
{
/// <summary>
/// Groups
/// </summary>
protected virtual IDictionary<long, int> Groups { get; set; }

/// <summary>
/// Constructor
/// </summary>
public ObservableGroupCollection()
{
Groups = new Dictionary<long, int>();
}

/// <summary>
/// Grouping implementation
/// </summary>
/// <param name="item"></param>
/// <param name="span"></param>
public virtual void Add(IGroup item, TimeSpan? span)
{
var index = item.GetIndex();

if (Groups.TryGetValue(index, out var position))
{
this[position] = (T)item.Update(this[position]);
return;
}

Groups[index] = Count;

Add((T)item.Update(null));
}
}
}
126 changes: 0 additions & 126 deletions Core/Collections/ObservableTimeCollection.cs

This file was deleted.

8 changes: 4 additions & 4 deletions Core/Domains/Instrument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public interface IInstrument
/// <summary>
/// List of all ticks from the server
/// </summary>
ObservableTimeCollection<PointModel> Points { get; set; }
ObservableGroupCollection<PointModel> Points { get; set; }

/// <summary>
/// List of all ticks from the server aggregated into bars
/// </summary>
ObservableTimeCollection<PointModel> PointGroups { get; set; }
ObservableGroupCollection<PointModel> PointGroups { get; set; }
}

public class Instrument : IInstrument
Expand Down Expand Up @@ -102,12 +102,12 @@ public class Instrument : IInstrument
/// <summary>
/// List of all ticks from the server
/// </summary>
public virtual ObservableTimeCollection<PointModel> Points { get; set; }
public virtual ObservableGroupCollection<PointModel> Points { get; set; }

/// <summary>
/// List of all ticks from the server aggregated into bars
/// </summary>
public virtual ObservableTimeCollection<PointModel> PointGroups { get; set; }
public virtual ObservableGroupCollection<PointModel> PointGroups { get; set; }

/// <summary>
/// Constructor
Expand Down
38 changes: 37 additions & 1 deletion Core/Models/Points/PointModel.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using Terminal.Core.Collections;
using Terminal.Core.Domains;
using Terminal.Core.Extensions;

namespace Terminal.Core.Models
{
public class PointModel : ICloneable
public class PointModel : ICloneable, IGroup
{
/// <summary>
/// Bid
Expand Down Expand Up @@ -77,5 +79,39 @@ public virtual object Clone()

return clone;
}

/// <summary>
/// Grouping index
/// </summary>
/// <returns></returns>
public virtual long GetIndex() => Time.Value.Ticks;

/// <summary>
/// Grouping implementation
/// </summary>
/// <param name="previous"></param>
/// <returns></returns>
public virtual IGroup Update(IGroup previous)
{
if (previous is not null)
{
var o = previous as PointModel;
var price = (Last ?? Bid ?? Ask ?? o.Last ?? o.Bid ?? o.Ask).Value;

Ask ??= o.Ask ?? price;
Bid ??= o.Bid ?? price;
AskSize += o.AskSize ?? 0.0;
BidSize += o.BidSize ?? 0.0;
Bar ??= new BarModel();
Bar.Close = Last = price;
Bar.Open = o.Bar?.Open ?? Bar.Open ?? price;
Bar.Low = Math.Min(Bar.Low ?? price, o.Bar?.Low ?? price);
Bar.High = Math.Max(Bar.High ?? price, o.Bar?.High ?? price);
TimeFrame ??= o.TimeFrame;
Time = Time.Round(TimeFrame) ?? o.Time;
}

return this;
}
}
}
9 changes: 9 additions & 0 deletions Core/Services/NotificationService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Terminal.Core.Services
{
public class NotificationService
{
public virtual Action<string> OnMessage { get; set; }
}
}
15 changes: 9 additions & 6 deletions Gateways/Alpaca/Libs/Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,16 @@ public async Task GetAccountData()
var positions = await SendData<JsonPosition[]>("/v2/positions");
var orders = await SendData<JsonOrder[]>("/v2/orders");

Account.Balance = account.Data.Equity;
Account.Descriptor = account.Data.AccountNumber;
Account.ActiveOrders = orders.Data.Select(GetInternalOrder).ToDictionary(o => o.Transaction.Id, o => o);
Account.ActivePositions = positions.Data.Select(GetInternalPosition).ToDictionary(o => o.Order.Transaction.Id, o => o);
if (account.Data is not null)
{
Account.Balance = account.Data.Equity;
Account.Descriptor = account.Data.AccountNumber;
Account.ActiveOrders = orders.Data.Select(GetInternalOrder).ToDictionary(o => o.Transaction.Id, o => o);
Account.ActivePositions = positions.Data.Select(GetInternalPosition).ToDictionary(o => o.Order.Transaction.Id, o => o);

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));
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));
}
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Gateways/Simulation/Libs/Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public override async Task<IList<ErrorModel>> Subscribe(string name)
point.TimeFrame = instrument.TimeFrame;

instrument.Points.Add(point);
instrument.PointGroups.Add(point, instrument.TimeFrame, true);
instrument.PointGroups.Add(point, instrument.TimeFrame);
}

interval.Enabled = true;
Expand Down
2 changes: 1 addition & 1 deletion Gateways/Simulation/Tests/SendPendingOrder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void CreatePendingOrder(
Instrument = new Instrument()
{
Name = "X",
Points = new ObservableTimeCollection<PointModel>
Points = new ObservableGroupCollection<PointModel>
{
new() { Bid = price, Ask = price }
}
Expand Down

0 comments on commit fded0ae

Please sign in to comment.