Skip to content

Commit 1463d10

Browse files
committed
Gateways + Indicators
1 parent 5a09b8e commit 1463d10

29 files changed

+343
-66
lines changed

Core/Core/Conventions/Indicator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public interface IIndicator
2121
/// Calculate indicator values
2222
/// </summary>
2323
/// <param name="collection"></param>
24-
Task<IIndicator> Update(IList<Price> collection);
24+
IIndicator Update(IList<Price> collection);
2525
}
2626

2727
public class Indicator : IIndicator
@@ -45,6 +45,6 @@ public class Indicator : IIndicator
4545
/// Calculate indicator values
4646
/// </summary>
4747
/// <param name="collection"></param>
48-
public virtual Task<IIndicator> Update(IList<Price> collection) => Task.FromResult(this as IIndicator);
48+
public virtual IIndicator Update(IList<Price> collection) => this;
4949
}
5050
}

Core/Core/Indicators/AtrIndicator.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Linq;
6-
using System.Threading.Tasks;
76

87
namespace Core.Indicators
98
{
@@ -18,9 +17,9 @@ public class AtrIndicator : Indicator
1817
/// Calculate single value
1918
/// </summary>
2019
/// <param name="collection"></param>
21-
public override Task<IIndicator> Update(IList<Price> collection)
20+
public override IIndicator Update(IList<Price> collection)
2221
{
23-
var response = Task.FromResult<IIndicator>(this);
22+
var response = this;
2423
var currentPoint = collection.ElementAtOrDefault(collection.Count - 1);
2524
var previousPoint = collection.ElementAtOrDefault(collection.Count - 2);
2625

Core/Core/Indicators/ImbalanceIndicator.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Core.Models;
33
using System.Collections.Generic;
44
using System.Linq;
5-
using System.Threading.Tasks;
65

76
namespace Core.Indicators
87
{
@@ -17,9 +16,9 @@ public class ImbalanceIndicator : Indicator
1716
/// Calculate indicator value
1817
/// </summary>
1918
/// <param name="collection"></param>
20-
public override Task<IIndicator> Update(IList<Price> collection)
19+
public override IIndicator Update(IList<Price> collection)
2120
{
22-
var response = Task.FromResult<IIndicator>(this);
21+
var response = this;
2322
var currentPoint = collection.LastOrDefault();
2423

2524
if (currentPoint is null)

Core/Core/Indicators/MaIndicator.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7-
using System.Threading.Tasks;
87

98
namespace Core.Indicators
109
{
@@ -34,9 +33,9 @@ public class MaIndicator : Indicator
3433
/// Calculate single value
3534
/// </summary>
3635
/// <param name="collection"></param>
37-
public override Task<IIndicator> Update(IList<Price> collection)
36+
public override IIndicator Update(IList<Price> collection)
3837
{
39-
var response = Task.FromResult<IIndicator>(this);
38+
var response = this;
4039
var currentPoint = collection.LastOrDefault();
4140

4241
if (currentPoint is null)

Core/Core/Indicators/RsiIndicator.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
using Core.Conventions;
22
using Core.Extensions;
33
using Core.Models;
4-
using Newtonsoft.Json.Linq;
54
using System;
65
using System.Collections.Generic;
76
using System.Linq;
8-
using System.Threading.Tasks;
97

108
namespace Core.Indicators
119
{
@@ -20,9 +18,9 @@ public class RsiIndicator : Indicator
2018
/// Calculate single value
2119
/// </summary>
2220
/// <param name="collection"></param>
23-
public override Task<IIndicator> Update(IList<Price> collection)
21+
public override IIndicator Update(IList<Price> collection)
2422
{
25-
var response = Task.FromResult<IIndicator>(this);
23+
var response = this;
2624
var currentPoint = collection.LastOrDefault();
2725

2826
if (currentPoint is null)

Core/Core/Indicators/ScaleIndicator.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7-
using System.Threading.Tasks;
87

98
namespace Core.Indicators
109
{
@@ -34,9 +33,9 @@ public class ScaleIndicator : Indicator
3433
/// Calculate indicator value
3534
/// </summary>
3635
/// <param name="collection"></param>
37-
public override Task<IIndicator> Update(IList<Price> collection)
36+
public override IIndicator Update(IList<Price> collection)
3837
{
39-
var response = Task.FromResult<IIndicator>(this);
38+
var response = this;
4039
var currentPoint = collection.LastOrDefault();
4140

4241
if (currentPoint is null)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using Core.Conventions;
2+
using System;
3+
4+
namespace Core.Indicators
5+
{
6+
/// <summary>
7+
/// Calculates running statistics (Mean and Standard Deviation)
8+
/// using Welford's Algorithm for numerical stability.
9+
/// </summary>
10+
public class StatIndicator : Indicator
11+
{
12+
private int count;
13+
private double mean;
14+
private double summary;
15+
16+
/// <summary>
17+
/// Returns the Sample Standard Deviation.
18+
/// Returns 0.0 if fewer than 2 data points have been added.
19+
/// </summary>
20+
public virtual double Deviation => count > 1 ? Math.Sqrt(summary / (count - 1)) : 0;
21+
22+
/// <summary>
23+
/// Returns the Running Mean.
24+
/// </summary>
25+
public virtual double Mean => mean;
26+
27+
/// <summary>
28+
/// Returns the number of samples processed.
29+
/// </summary>
30+
public virtual int Count => count;
31+
32+
/// <summary>
33+
/// Returns the Sample Variance.
34+
/// </summary>
35+
public virtual double Variance => count > 1 ? summary / (count - 1) : 0;
36+
37+
/// <summary>
38+
/// Calculate
39+
/// </summary>
40+
/// <param name="value"></param>
41+
public virtual IIndicator Update(double value)
42+
{
43+
count++;
44+
45+
// Use the difference from the current mean
46+
double currentAverage = value - mean;
47+
48+
// Update the running mean
49+
mean += currentAverage / count;
50+
51+
// Use the difference from the newly updated mean
52+
double nextAverage = value - mean;
53+
54+
// Accumulate the squared differences from the mean
55+
summary += currentAverage * nextAverage;
56+
57+
return this;
58+
}
59+
}
60+
}

Core/Core/Indicators/VwapIndicator.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Linq;
6-
using System.Threading.Tasks;
76

87
namespace Core.Indicators
98
{
@@ -13,9 +12,9 @@ public class VwapIndicator : Indicator
1312
/// Calculate indicator value
1413
/// </summary>
1514
/// <param name="collection"></param>
16-
public override Task<IIndicator> Update(IList<Price> collection)
15+
public override IIndicator Update(IList<Price> collection)
1716
{
18-
var response = Task.FromResult<IIndicator>(this);
17+
var response = this;
1918
var currentPoint = collection.LastOrDefault();
2019

2120
if (currentPoint?.Bar is null)

Dashboard/Dashboard/Pages/BasePage.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using BitMart.Net.Enums;
21
using Canvas.Core.Models;
32
using Canvas.Core.Shapes;
43
using Core.Conventions;

Dashboard/Dashboard/Pages/Futures/Covariance.razor.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,21 @@
1313

1414
namespace Dashboard.Pages.Futures
1515
{
16-
public class Indexer : List<(long, double)>
16+
public partial class Covariance
1717
{
18-
public new void Add((long, double) item)
18+
public class Indexer : List<(long, double)>
1919
{
20-
if (Count is 0 || item.Item1 > this[^1].Item1)
20+
public new void Add((long, double) item)
2121
{
22-
base.Add(item);
23-
}
22+
if (Count is 0 || item.Item1 > this[^1].Item1)
23+
{
24+
base.Add(item);
25+
}
2426

25-
this[^1] = item;
27+
this[^1] = item;
28+
}
2629
}
27-
}
2830

29-
public partial class Covariance
30-
{
31-
ControlsComponent View { get; set; }
3231
ChartsComponent DataView { get; set; }
3332
ChartsComponent ScoreView { get; set; }
3433
ChartsComponent IndicatorsView { get; set; }
@@ -109,8 +108,8 @@ protected override async Task OnViewUpdate(Instrument instrument)
109108
}
110109

111110
var performance = await Performance.Update([adapter]);
112-
var scaleX = await Scales[assetX.Name].Update(seriesX);
113-
var scaleY = await Scales[assetY.Name].Update(seriesY);
111+
var scaleX = Scales[assetX.Name].Update(seriesX);
112+
var scaleY = Scales[assetY.Name].Update(seriesY);
114113
var priceX = seriesX.Last();
115114
var priceY = seriesY.Last();
116115
var retSeriesX = seriesX.Select(o => o.Last.Value * assetX.Leverage.Value).ToArray();

0 commit comments

Comments
 (0)