-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MDAPI-67] [.NET] Integrate IndexedEventTxModel
- Loading branch information
Konstantin Ivaschenko
committed
Jul 25, 2024
1 parent
a775236
commit e481f47
Showing
40 changed files
with
3,922 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Application xmlns="https://github.com/avaloniaui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
x:Class="CandleChartSample.App" | ||
RequestedThemeVariant="Default"> | ||
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. --> | ||
|
||
<Application.Styles> | ||
<FluentTheme /> | ||
</Application.Styles> | ||
</Application> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Avalonia; | ||
using Avalonia.Controls.ApplicationLifetimes; | ||
using Avalonia.Markup.Xaml; | ||
|
||
namespace CandleChartSample; | ||
|
||
public partial class App : Application | ||
{ | ||
public override void Initialize() | ||
{ | ||
AvaloniaXamlLoader.Load(this); | ||
} | ||
|
||
public override void OnFrameworkInitializationCompleted() | ||
{ | ||
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) | ||
{ | ||
desktop.MainWindow = new MainWindow(); | ||
} | ||
|
||
base.OnFrameworkInitializationCompleted(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>WinExe</OutputType> | ||
<TargetFrameworks>net6.0</TargetFrameworks> | ||
<Nullable>enable</Nullable> | ||
<BuiltInComInteropSupport>true</BuiltInComInteropSupport> | ||
<ApplicationManifest>app.manifest</ApplicationManifest> | ||
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Avalonia" Version="11.0.10"/> | ||
<PackageReference Include="Avalonia.Desktop" Version="11.0.10"/> | ||
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.10"/> | ||
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.0.10"/> | ||
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.--> | ||
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.10"/> | ||
<PackageReference Include="ScottPlot.Avalonia" Version="5.0.36"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\DxFeed.Graal.Net\DxFeed.Graal.Net.csproj"/> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// <copyright file="CandleExtension.cs" company="Devexperts LLC"> | ||
// Copyright © 2024 Devexperts LLC. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. | ||
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
// </copyright> | ||
|
||
using System; | ||
using DxFeed.Graal.Net.Events.Candles; | ||
using ScottPlot; | ||
|
||
namespace CandleChartSample; | ||
|
||
/// <summary> | ||
/// Provides extension methods for converting <see cref="Candle"/> objects to <see cref="OHLC"/> objects. | ||
/// </summary> | ||
public static class CandleExtension | ||
{ | ||
/// <summary> | ||
/// Converts a <see cref="Candle"/> object to an <see cref="OHLC"/> object. | ||
/// </summary> | ||
/// <param name="candle">The candle to convert.</param> | ||
/// <returns>An <see cref="OHLC"/> object representing the candle.</returns> | ||
public static OHLC ToOHLC(this Candle candle) | ||
{ | ||
var open = GetValueWithPriority(candle.Open, candle.High, candle.Low, candle.Close); | ||
var high = GetValueWithPriority(candle.High, candle.Open, candle.Low, candle.Close); | ||
var low = GetValueWithPriority(candle.Low, candle.Close, candle.Open, candle.High); | ||
var close = GetValueWithPriority(candle.Close, candle.Low, candle.Open, candle.High); | ||
|
||
return new OHLC | ||
{ | ||
Open = open, | ||
High = high, | ||
Low = low, | ||
Close = close, | ||
DateTime = DateTimeOffset.FromUnixTimeMilliseconds(candle.Time).DateTime.ToLocalTime(), | ||
TimeSpan = TimeSpan.FromMilliseconds(candle.CandleSymbol!.Period!.PeriodIntervalMillis) | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// Returns the first non-NaN value from the provided list of values. | ||
/// </summary> | ||
/// <param name="values">An array of double values to check.</param> | ||
/// <returns>The first non-NaN value, or NaN if all values are NaN.</returns> | ||
private static double GetValueWithPriority(params double[] values) | ||
{ | ||
foreach (var value in values) | ||
{ | ||
if (!double.IsNaN(value)) | ||
{ | ||
return value; | ||
} | ||
} | ||
return double.NaN; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
// <copyright file="CandleList.cs" company="Devexperts LLC"> | ||
// Copyright © 2024 Devexperts LLC. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. | ||
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
// </copyright> | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using DxFeed.Graal.Net.Events; | ||
using DxFeed.Graal.Net.Events.Candles; | ||
using ScottPlot; | ||
|
||
namespace CandleChartSample; | ||
|
||
/// <summary> | ||
/// The CandleList class manages a list of OHLC (Open, High, Low, Close) objects for candle chart visualization. | ||
/// This class provides methods for updating the list based on incoming candle events, | ||
/// supporting both snapshot and incremental updates. | ||
/// </summary> | ||
public class CandleList : List<OHLC> | ||
{ | ||
/// <summary> | ||
/// Updates the candle list with a new set of candles. | ||
/// Depending on whether the update is a snapshot or incremental, it will replace or update the existing list. | ||
/// </summary> | ||
/// <param name="candles">The collection of candles to update the list with.</param> | ||
/// <param name="isSnapshot">Indicates whether the update is a snapshot (true) or incremental (false).</param> | ||
public void Update(IEnumerable<Candle> candles, bool isSnapshot) | ||
{ | ||
// Sort candles by their index. This is stable sort. | ||
var sortedCandles = candles.OrderBy(c => c.Index); | ||
|
||
if (isSnapshot) | ||
{ | ||
// Handle snapshot update | ||
UpdateSnapshot(sortedCandles); | ||
} | ||
else | ||
{ | ||
// Handle incremental update | ||
UpdateIncremental(sortedCandles); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Updates the list with a snapshot of candles. | ||
/// Clears the existing list and adds the new set of candles, ensuring to remove any that should be removed. | ||
/// </summary> | ||
/// <param name="candles">The snapshot of candles to update the list with.</param> | ||
private void UpdateSnapshot(IEnumerable<Candle> candles) | ||
{ | ||
// Clear the current list of OHLC objects | ||
Clear(); | ||
foreach (var candle in candles) | ||
{ | ||
// Check if the candle should be removed | ||
if (!ShouldRemove(candle)) | ||
{ | ||
// Convert the candle to OHLC and add to the list | ||
Add(candle.ToOHLC()); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Updates the list incrementally with a set of candles. | ||
/// Adds, updates, or removes candles as necessary based on the provided list. | ||
/// </summary> | ||
/// <param name="candles">The incremental set of candles to update the list with.</param> | ||
private void UpdateIncremental(IEnumerable<Candle> candles) | ||
{ | ||
foreach (var candle in candles) | ||
{ | ||
// Check if the candle should be removed. | ||
if (ShouldRemove(candle)) | ||
{ | ||
// Remove the corresponding OHLC object. | ||
RemoveCandle(candle); | ||
continue; | ||
} | ||
|
||
// Convert the candle to OHLC. | ||
var ohlc = candle.ToOHLC(); | ||
// Gets last OHLC. | ||
var lastOhlc = LastOrDefault(); | ||
|
||
// Compare the date and time of the OHLC object with the last one in the list | ||
switch (DateTime.Compare(ohlc.DateTime, lastOhlc.DateTime)) | ||
{ | ||
case < 0: | ||
// If the new OHLC is older, insert or update it in the correct position. | ||
InsertOrUpdate(ohlc); | ||
break; | ||
case 0: | ||
// If the new OHLC has the same date and time, update the last one. | ||
AddOrUpdateLast(ohlc); | ||
break; | ||
case > 0: | ||
// If the new OHLC is newer, add it to the list. | ||
Add(ohlc); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Adds a new OHLC object or updates the last one if it exists. | ||
/// </summary> | ||
/// <param name="ohlc">The OHLC object to add or update.</param> | ||
private void AddOrUpdateLast(OHLC ohlc) | ||
{ | ||
if (IsEmpty()) | ||
{ | ||
// Add the OHLC if the list is empty. | ||
Add(ohlc); | ||
} | ||
else | ||
{ | ||
// Update the last OHLC object. | ||
this[Count - 1] = ohlc; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Inserts a new OHLC object into the list or updates an existing one based on its date and time. | ||
/// </summary> | ||
/// <param name="ohlc">The OHLC object to insert or update.</param> | ||
private void InsertOrUpdate(OHLC ohlc) | ||
{ | ||
// Find the index of the OHLC object to insert or update. | ||
var index = FindIndex(o => DateTime.Compare(ohlc.DateTime, o.DateTime) <= 0); | ||
if (index >= 0 && this[index].DateTime.Equals(ohlc.DateTime)) | ||
{ | ||
// Update the existing OHLC object if the date and time match. | ||
this[index] = ohlc; | ||
} | ||
else | ||
{ | ||
// Insert the new OHLC object at the correct position. | ||
Insert(index >= 0 ? index : 0, ohlc); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Removes a candle from the list based on its date and time. | ||
/// </summary> | ||
/// <param name="candle">The candle to remove.</param> | ||
private void RemoveCandle(Candle candle) => | ||
RemoveAll(ohlc => candle.ToOHLC().DateTime.Equals(ohlc.DateTime)); | ||
|
||
/// <summary> | ||
/// Checks if the list is empty. | ||
/// </summary> | ||
/// <returns><c>true</c> if the list is empty; otherwise, <c>false</c>.</returns> | ||
private bool IsEmpty() => | ||
Count == 0; | ||
|
||
/// <summary> | ||
/// Returns the last OHLC object in the list or a new OHLC object if the list is empty. | ||
/// </summary> | ||
/// <returns>The last OHLC object or a new OHLC object if the list is empty.</returns> | ||
private OHLC LastOrDefault() => | ||
IsEmpty() ? new OHLC() : this[Count - 1]; | ||
|
||
/// <summary> | ||
/// Determines whether a candle should be removed. | ||
/// </summary> | ||
/// <param name="candle">The candle to check.</param> | ||
/// <returns><c>true</c> if the candle should be removed; otherwise, <c>false</c>.</returns> | ||
private static bool ShouldRemove(Candle candle) => | ||
EventFlags.IsRemove(candle) || | ||
(double.IsNaN(candle.Open) && | ||
double.IsNaN(candle.High) && | ||
double.IsNaN(candle.Low) && | ||
double.IsNaN(candle.Close)); | ||
} |
Oops, something went wrong.