Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiusgreat committed Dec 10, 2024
1 parent cc1e336 commit 3ef79e4
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 38 deletions.
7 changes: 3 additions & 4 deletions Core/Collections/ConcurrentGroup.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Terminal.Core.Collections
{
public class ConcurrentGroup<T> : SynchronizedCollection<T> where T : class, IGroup<T>
public class ConcurrentGroup<T> : SynchronizedCollection<T> where T : class, ICloneable, IGroup<T>
{
private object _sync;

Expand Down Expand Up @@ -35,13 +34,13 @@ public virtual void Add(T item, TimeSpan? span)

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

_groups[index] = Count;

Add(item.Update(null));
Add((item.Clone() as T).Update(null));
}
}
}
Expand Down
30 changes: 19 additions & 11 deletions Core/Models/Points/PointModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text.Json;
using Terminal.Core.Collections;
using Terminal.Core.Domains;
using Terminal.Core.Extensions;
Expand Down Expand Up @@ -108,22 +110,28 @@ public virtual long GetIndex()
/// <summary>
/// Grouping implementation
/// </summary>
/// <param name="previous"></param>
/// <param name="o"></param>
/// <returns></returns>
public virtual PointModel Update(PointModel previous)
public virtual PointModel Update(PointModel o)
{
var price = (Last ?? Bid ?? Ask ?? previous?.Last ?? previous?.Bid ?? previous?.Ask).Value;
var price = (Last ?? Bid ?? Ask ?? o?.Last ?? o?.Bid ?? o?.Ask).Value;

Ask ??= previous?.Ask ?? price;
Bid ??= previous?.Bid ?? price;
AskSize += previous?.AskSize ?? 0.0;
BidSize += previous?.BidSize ?? 0.0;
Time = Time.Round(Instrument.TimeFrame) ?? previous?.Time;
Ask ??= o?.Ask ?? price;
Bid ??= o?.Bid ?? price;
AskSize += o?.AskSize ?? 0.0;
BidSize += o?.BidSize ?? 0.0;
Time = Time.Round(Instrument.TimeFrame) ?? o?.Time;
Bar ??= new BarModel();
Bar.Close = Last = price;
Bar.Open = Bar.Open ?? previous?.Bar?.Open ?? price;
Bar.Low = Math.Min(Bar?.Low ?? Bid.Value, previous?.Bar?.Low ?? price);
Bar.High = Math.Max(Bar?.High ?? Ask.Value, previous?.Bar?.High ?? price);
Bar.Open = Bar.Open ?? o?.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);

Console.WriteLine(
Instrument.Name + " : " +
Time + " : " +
Ask + " / " + Bid + " / " + AskSize + " / " + BidSize + " : " +
Bar.Open + " / " + Bar.High + " / " + Bar.Low + " / " + Bar.Close);

return this;
}
Expand Down
3 changes: 2 additions & 1 deletion Gateway/Alpaca/Libs/Maps/InternalMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public static PointModel GetPrice(IQuote message, InstrumentModel instrument)
Bid = (double)message.BidPrice,
AskSize = (double)message.AskSize,
BidSize = (double)message.BidSize,
Last = (double)message.BidPrice
Last = (double)message.BidPrice,
Time = message.TimestampUtc
};

point.Instrument = instrument;
Expand Down
13 changes: 6 additions & 7 deletions Gateway/InteractiveBrokers/Libs/Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public override async Task<ResponseModel<StatusEnum>> Subscribe(InstrumentModel
SubscribeToPoints(id, instrument, point =>
{
point.Time ??= DateTime.Now;
point.Instrument = instrument;

instrument.Points.Add(point);
instrument.PointGroups.Add(point, instrument.TimeFrame);
Expand Down Expand Up @@ -556,10 +557,10 @@ void subscribe(TickPriceMessage message)
{
switch (ExternalMap.GetField(message.Field))
{
case FieldCodeEnum.BidSize: point.BidSize = message.Value; break;
case FieldCodeEnum.AskSize: point.AskSize = message.Value; break;
case FieldCodeEnum.BidPrice: point.Last = point.Bid = message.Value; break;
case FieldCodeEnum.AskPrice: point.Last = point.Ask = message.Value; break;
case FieldCodeEnum.BidSize: point.BidSize = message.Data ?? point.BidSize; break;
case FieldCodeEnum.AskSize: point.AskSize = message.Data ?? point.AskSize; break;
case FieldCodeEnum.BidPrice: point.Last = point.Bid = message.Data ?? point.Bid; break;
case FieldCodeEnum.AskPrice: point.Last = point.Ask = message.Data ?? point.Ask; break;
}

if (point.Bid is null || point.Ask is null || point.BidSize is null || point.AskSize is null)
Expand All @@ -569,16 +570,14 @@ void subscribe(TickPriceMessage message)

point.Time = DateTime.Now;
point.Instrument = instrument;
action(point.Clone() as PointModel);
action(point);
}
}

var contract = _contracts.Get(instrument.Name) ?? ExternalMap.GetContract(instrument);

_client.TickPrice += subscribe;
_client.ClientSocket.reqMktData(id, contract, string.Empty, false, false, null);

action(point.Clone() as PointModel);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ namespace InteractiveBrokers.Messages
{
public class TickGenericMessage : MarketDataMessage
{
public TickGenericMessage(int requestId, int field, double price) : base(requestId, field)
public TickGenericMessage(int requestId, int field, double? price) : base(requestId, field)
{
Value = price;
Data = price;
Value = price.GetValueOrDefault();
}

public double Value { get; set; }

public double? Data { get; set; }
}
}
29 changes: 20 additions & 9 deletions Gateway/Schwab/Libs/Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Schwab.Messages;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
Expand Down Expand Up @@ -533,6 +534,9 @@ protected virtual async Task<ClientWebSocket> GetConnection(ClientWebSocket ws,

var adminResponse = await ReceiveStream<StreamLoginResponseMessage>(ws);
var adminCode = adminResponse?.Response?.FirstOrDefault()?.Content?.Code;
var pointMap = Account
.Instruments
.ToDictionary(o => o.Key, o => new PointModel());

if (adminCode is not 0)
{
Expand All @@ -557,7 +561,7 @@ protected virtual async Task<ClientWebSocket> GetConnection(ClientWebSocket ws,
.AsArray()
.Select(o => o.Deserialize<StreamDataMessage>());

OnPoint(streamPoints.Where(o => InternalMap.GetSubscriptionType(o.Service) is not null));
OnPoint(pointMap, streamPoints.Where(o => InternalMap.GetSubscriptionType(o.Service) is not null));
}
}
catch (Exception e)
Expand All @@ -574,25 +578,32 @@ protected virtual async Task<ClientWebSocket> GetConnection(ClientWebSocket ws,
/// <summary>
/// Process quote from the stream
/// </summary>
/// <param name="pointMap"></param>
/// <param name="streamPoints"></param>
protected virtual void OnPoint(IEnumerable<StreamDataMessage> streamPoints)
protected virtual void OnPoint(IDictionary<string, PointModel> pointMap, IEnumerable<StreamDataMessage> streamPoints)
{
foreach (var streamPoint in streamPoints)
{
var map = InternalMap.GetStreamMap(streamPoint.Service);

foreach (var data in streamPoint.Content)
{
var point = new PointModel();
var instrumentName = $"{data.Get("key")}";
var instrument = Account.Instruments.Get(instrumentName) ?? new InstrumentModel();
var instrument = Account.Instruments.Get(instrumentName);
var point = pointMap.Get(instrumentName);

point.Time = DateTime.Now;
point.Instrument = instrument;
point.Bid = double.TryParse($"{data.Get(map.Get("Bid Price"))}", out var x1) ? x1 : null;
point.Ask = double.TryParse($"{data.Get(map.Get("Ask Price"))}", out var x2) ? x2 : null;
point.BidSize = double.TryParse($"{data.Get(map.Get("Bid Size"))}", out var x3) ? x3 : 0;
point.AskSize = double.TryParse($"{data.Get(map.Get("Ask Size"))}", out var x4) ? x4 : 0;
point.Last = double.TryParse($"{data.Get(map.Get("Last Price"))}", out var x5) ? x5 : (point.Bid ?? point.Ask);
point.Bid = InternalMap.GetValue($"{data.Get(map.Get("Bid Price"))}", point.Bid);
point.Ask = InternalMap.GetValue($"{data.Get(map.Get("Ask Price"))}", point.Ask);
point.BidSize = InternalMap.GetValue($"{data.Get(map.Get("Bid Size"))}", point.BidSize);
point.AskSize = InternalMap.GetValue($"{data.Get(map.Get("Ask Size"))}", point.AskSize);
point.Last = InternalMap.GetValue($"{data.Get(map.Get("Last Price"))}", point.Last);

if (point.Bid is null || point.Ask is null || point.BidSize is null || point.AskSize is null)
{
return;
}

instrument.Name = instrumentName;
instrument.Point = point;
Expand Down
13 changes: 11 additions & 2 deletions Gateway/Schwab/Libs/Maps/InternalMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Terminal.Core.Domains;
using Terminal.Core.Enums;
using Terminal.Core.Models;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace Schwab.Mappers
{
Expand Down Expand Up @@ -465,9 +466,17 @@ public static OrderSideEnum GetSubOrderSide(string status)
/// <summary>
/// Get value or default
/// </summary>
/// <param name="price"></param>
/// <param name="v"></param>
/// <param name="origin"></param>
/// <returns></returns>
public static double GetValue(double? price, double? origin) => (price is 0 or null ? origin : price).Value;
public static double? GetValue(double? v, double? origin) => v is 0 or null ? origin : v;

/// <summary>
/// Get value or default
/// </summary>
/// <param name="v"></param>
/// <param name="origin"></param>
/// <returns></returns>
public static double? GetValue(string v, double? origin) => double.TryParse(v, out var o) ? o : origin;
}
}
1 change: 0 additions & 1 deletion Gateway/Simulation/Libs/Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.IO.Compression;
using System.Linq;
Expand Down
1 change: 0 additions & 1 deletion Terminal/Components/ChartsComponent.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ public virtual void UpdateItems(long index, string area, string series, params I
View.Update(domain, Shapes);
}


/// <summary>
/// Update
/// </summary>
Expand Down

0 comments on commit 3ef79e4

Please sign in to comment.