Add new data structures and event handling classes for trading platform. Include base classes, value and bar structs, event arguments, emitters, listeners. Update ruleset for SonarLint.

This commit is contained in:
Miha Kralj
2024-07-25 17:42:09 -07:00
parent f7fd3fbf9f
commit 7dd938c368
86 changed files with 9367 additions and 9153 deletions
+92
View File
@@ -0,0 +1,92 @@
using System;
public struct TValue
{
public DateTime Timestamp { get; set; }
public double Value { get; set; }
public TValue(DateTime timestamp, double value)
{
Timestamp = timestamp;
Value = value;
}
public override string ToString()
{
return $"[{this.Timestamp:yyyy-MM-dd HH:mm:ss}: {this.Value:F2}]";
}
public override bool Equals(object obj)
{
if (obj is TValue other)
{
return Timestamp == other.Timestamp && Value == other.Value;
}
return false;
}
public override int GetHashCode()
{
return HashCode.Combine(Timestamp, Value);
}
}
public struct TBar
{
public DateTime Timestamp { get; set; }
public double Open { get; set; }
public double High { get; set; }
public double Low { get; set; }
public double Close { get; set; }
public double Volume { get; set; }
public TBar(DateTime timestamp, double open, double high, double low, double close, double volume)
{
Timestamp = timestamp;
Open = open;
High = high;
Low = low;
Close = close;
Volume = volume;
}
public override string ToString()
{
return $"[{this.Timestamp:yyyy-MM-dd HH:mm:ss}: O={this.Open:F2}, H={this.High:F2}, L={this.Low:F2}, C={this.Close:F2}, V={this.Volume:F2}]";
}
public override bool Equals(object obj)
{
if (obj is TBar other)
{
return Timestamp == other.Timestamp &&
Open == other.Open &&
High == other.High &&
Low == other.Low &&
Close == other.Close &&
Volume == other.Volume;
}
return false;
}
public override int GetHashCode()
{
return HashCode.Combine(Timestamp, Open, High, Low, Close, Volume);
}
}
public class TValueEventArg<T> : EventArgs
{
public T Data { get; }
public bool IsClosed { get; }
public bool IsHot { get; }
public TValueEventArg(T data, bool isClosed, bool isHot)
{
Data = data;
IsClosed = isClosed;
IsHot = isHot;
}
}
+92
View File
@@ -0,0 +1,92 @@
#!meta
{"kernelInfo":{"defaultKernelName":"csharp","items":[{"aliases":[],"name":"csharp"}]}}
#!csharp
#load "./base.cs"
#!csharp
TValue vv = new(DateTime.Now, 100);
display(vv.ToString());
#!csharp
TBar bb = new();
display(bb.ToString());
#!csharp
public class Emitter {
private Random random = new Random();
public event EventHandler<TValueEventArg<TValue>> Pub;
public void Emit() {
DateTime now = DateTime.Now;
double randomValue = random.NextDouble() * 100; // Generates a random number between 0 and 100
TValue value = new TValue(now, randomValue);
TValueEventArg<TValue> eventArg = new TValueEventArg<TValue>(value, true, true);
OnValuePub(eventArg);
}
protected virtual void OnValuePub(TValueEventArg<TValue> eventArg) {
Pub?.Invoke(this, eventArg);
}
}
public class BarEmitter
{
private Random random = new Random();
public event EventHandler<TValueEventArg<TBar>> Pub;
private double lastClose = 100.0; // Starting price
public void Emit()
{
double open = lastClose;
double close = open * (1 + (random.NextDouble() - 0.5) * 0.02); // +/- 1% change
double high = Math.Max(open, close) * (1 + random.NextDouble() * 0.005); // Up to 0.5% higher
double low = Math.Min(open, close) * (1 - random.NextDouble() * 0.005); // Up to 0.5% lower
double volume = random.NextDouble() * 1000000; // Random volume between 0 and 1,000,000
TBar bar = new TBar(DateTime.Now, open, high, low, close, volume);
lastClose = close;
TValueEventArg<TBar> eventArg = new TValueEventArg<TBar>(bar, true, true);
OnBarPub(eventArg);
}
protected virtual void OnBarPub(TValueEventArg<TBar> eventArg)
{
Pub?.Invoke(this, eventArg);
}
}
public class Listener
{
public void Sub(object sender, EventArgs e)
{
if (e is TValueEventArg<TValue> tValueArg) {
Console.WriteLine($"TValue: {tValueArg.Data.Value:F2}");
} else if (e is TValueEventArg<TBar> tBarArg) {
Console.WriteLine($"TBar: o={tBarArg.Data.Open:F2}, v={tBarArg.Data.Volume:F2}");
} else {
Console.WriteLine($"Unknown type: {e.GetType().Name}");
}
}
}
#!csharp
Emitter em1 = new();
BarEmitter em2 = new();
Listener list = new();
em1.Pub += list.Sub;
em2.Pub += list.Sub;
// Emit 5 random values
for (int i = 0; i < 3; i++) {
em1.Emit();
em2.Emit();
}