mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-07-31 10:57:43 +00:00
75 lines
2.4 KiB
Plaintext
75 lines
2.4 KiB
Plaintext
#!meta
|
|
|
|
{"kernelInfo":{"defaultKernelName":"csharp","items":[{"aliases":[],"languageName":"csharp","name":"csharp"}]}}
|
|
|
|
#!csharp
|
|
|
|
// Reference the library
|
|
#r "..\..\bin\QuanTAlib.dll"
|
|
|
|
using QuanTAlib;
|
|
|
|
// 1. Creating a TBar
|
|
// TBar represents a single OHLCV bar (Open, High, Low, Close, Volume)
|
|
// It is an immutable struct optimized for memory and performance
|
|
|
|
long now = DateTime.UtcNow.Ticks;
|
|
var bar = new TBar(now, 100.0, 105.0, 95.0, 102.0, 1000.0);
|
|
|
|
Console.WriteLine($"Created TBar: {bar}");
|
|
Console.WriteLine($"Time: {bar.AsDateTime}");
|
|
Console.WriteLine($"Open: {bar.Open}");
|
|
Console.WriteLine($"High: {bar.High}");
|
|
Console.WriteLine($"Low: {bar.Low}");
|
|
Console.WriteLine($"Close: {bar.Close}");
|
|
Console.WriteLine($"Volume: {bar.Volume}");
|
|
|
|
#!csharp
|
|
|
|
// 2. Computed Properties
|
|
// TBar provides on-demand calculation of common price averages
|
|
// These are calculated when accessed, saving storage space
|
|
|
|
Console.WriteLine($"HL2 (High+Low)/2: {bar.HL2}");
|
|
Console.WriteLine($"OC2 (Open+Close)/2: {bar.OC2}");
|
|
Console.WriteLine($"OHL3 (Open+High+Low)/3: {bar.OHL3}");
|
|
Console.WriteLine($"HLC3 (High+Low+Close)/3: {bar.HLC3}");
|
|
Console.WriteLine($"OHLC4 (Open+High+Low+Close)/4: {bar.OHLC4}");
|
|
Console.WriteLine($"HLCC4 (High+Low+Close+Close)/4: {bar.HLCC4}");
|
|
|
|
#!csharp
|
|
|
|
// 3. TValue Accessors
|
|
// You can efficiently access individual components as TValue (Time-Value pair)
|
|
// This is useful when you need to treat a specific price component as a time series point
|
|
|
|
Console.WriteLine($"Open TValue: {bar.O}");
|
|
Console.WriteLine($"High TValue: {bar.H}");
|
|
Console.WriteLine($"Low TValue: {bar.L}");
|
|
Console.WriteLine($"Close TValue: {bar.C}");
|
|
Console.WriteLine($"Volume TValue: {bar.V}");
|
|
|
|
#!csharp
|
|
|
|
// 4. Implicit Conversions
|
|
// TBar supports implicit conversions to double (Close price), TValue (Close), and DateTime
|
|
|
|
double closePrice = bar;
|
|
TValue value = bar;
|
|
DateTime dt = bar;
|
|
|
|
Console.WriteLine($"Implicit double (Close): {closePrice}");
|
|
Console.WriteLine($"Implicit TValue (Close): {value}");
|
|
Console.WriteLine($"Implicit DateTime: {dt}");
|
|
|
|
#!csharp
|
|
|
|
// 5. Equality and Immutability
|
|
// Being a struct, TBar has value semantics
|
|
|
|
var bar2 = new TBar(now, 100.0, 105.0, 95.0, 102.0, 1000.0);
|
|
var bar3 = new TBar(now, 101.0, 106.0, 96.0, 103.0, 1100.0);
|
|
|
|
Console.WriteLine($"bar equals bar2? {bar == bar2}"); // True, same values
|
|
Console.WriteLine($"bar equals bar3? {bar == bar3}"); // False, different values
|