Skip to content

Commit

Permalink
Feature: new Tick constructor for TickType.OpenInterest (#8323)
Browse files Browse the repository at this point in the history
* feat: create OpenInterest constrcutor of Tick

* feat: GetSubscribedSymbols by TickType

* test:feat: GetSubscribeSymbolsBySpecificTickType

* refactor: equal channel name with InvariantCultureIgnoreCase
  • Loading branch information
Romazes authored Sep 17, 2024
1 parent 6351773 commit c556d16
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Common/Data/DataQueueHandlerSubscriptionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ public IEnumerable<Symbol> GetSubscribedSymbols()
.Distinct();
}

/// <summary>
/// Retrieves the list of unique <see cref="Symbol"/> instances that are currently subscribed for a specific <see cref="TickType"/>.
/// </summary>
/// <param name="tickType">The type of tick data to filter subscriptions by.</param>
/// <returns>A collection of unique <see cref="Symbol"/> objects that match the specified <paramref name="tickType"/>.</returns>
public IEnumerable<Symbol> GetSubscribedSymbols(TickType tickType)
{
var channelName = ChannelNameFromTickType(tickType);
#pragma warning disable CA1309
return SubscribersByChannel.Keys.Where(x => x.Name.Equals(channelName, StringComparison.InvariantCultureIgnoreCase))
#pragma warning restore CA1309
.Select(c => c.Symbol)
.Distinct();
}

/// <summary>
/// Checks if there is existing subscriber for current channel
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions Common/Data/Market/Tick.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,21 @@ public Tick(DateTime time, Symbol symbol, decimal bid, decimal ask)
AskPrice = ask;
}

/// <summary>
/// Initializes a new instance of the <see cref="Tick"/> class to <see cref="TickType.OpenInterest"/>.
/// </summary>
/// <param name="time">The time at which the open interest tick occurred.</param>
/// <param name="symbol">The symbol associated with the open interest tick.</param>
/// <param name="openInterest">The value of the open interest for the specified symbol.</param>
public Tick(DateTime time, Symbol symbol, decimal openInterest)
{
Time = time;
Symbol = symbol;
Value = openInterest;
DataType = MarketDataType.Tick;
TickType = TickType.OpenInterest;
}

/// <summary>
/// Initializer for a last-trade equity tick with bid or ask prices.
/// </summary>
Expand Down
27 changes: 27 additions & 0 deletions Tests/Common/Data/DataQueueHandlerSubscriptionManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,33 @@ public void SubscribeManyPerChannel()
Assert.IsFalse(subscriptionManager.IsSubscribed(Symbols.AAPL, TickType.Quote));
}

[TestCase(TickType.Trade, MarketDataType.TradeBar, 1)]
[TestCase(TickType.Trade, MarketDataType.QuoteBar, 0)]
[TestCase(TickType.Quote, MarketDataType.QuoteBar, 1)]
[TestCase(TickType.OpenInterest, MarketDataType.Tick, 1)]
[TestCase(TickType.OpenInterest, MarketDataType.TradeBar, 0)]
public void GetSubscribeSymbolsBySpecificTickType(TickType tickType, MarketDataType dataType, int expectedCount)
{
using var fakeDataQueueHandler = new FakeDataQueuehandlerSubscriptionManager((tickType) => tickType!.ToString());

switch (dataType)
{
case MarketDataType.TradeBar:
fakeDataQueueHandler.Subscribe(GetSubscriptionDataConfig<TradeBar>(Symbols.AAPL, Resolution.Minute));
break;
case MarketDataType.QuoteBar:
fakeDataQueueHandler.Subscribe(GetSubscriptionDataConfig<QuoteBar>(Symbols.AAPL, Resolution.Minute));
break;
case MarketDataType.Tick:
fakeDataQueueHandler.Subscribe(GetSubscriptionDataConfig<OpenInterest>(Symbols.AAPL, Resolution.Minute));
break;
}

var subscribeSymbols = fakeDataQueueHandler.GetSubscribedSymbols(tickType).ToList();

Assert.That(subscribeSymbols.Count, Is.EqualTo(expectedCount));
}

#region helper

private SubscriptionDataConfig GetSubscriptionDataConfig(Type T, Symbol symbol, Resolution resolution, TickType? tickType = null)
Expand Down

0 comments on commit c556d16

Please sign in to comment.