Minor update

This commit is contained in:
Miha Kralj
2023-10-14 17:54:45 -07:00
parent 97f0c78a70
commit 1faf47e85f
5 changed files with 146 additions and 221 deletions
+4 -4
View File
@@ -2,9 +2,9 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<Title>QuanTAlib</Title> <Title>QuanTAlib</Title>
<Version>0.2.5</Version> <Version>0.2.27</Version>
<AssemblyVersion>0.2.5</AssemblyVersion> <AssemblyVersion>0.2.27</AssemblyVersion>
<FileVersion>0.2.5</FileVersion> <FileVersion>0.2.27</FileVersion>
<Product>Library of TA Calculations, Charts and Strategies for Quantower</Product> <Product>Library of TA Calculations, Charts and Strategies for Quantower</Product>
<Description>Quantitative Technical Analysis Library in C# for Quantower</Description> <Description>Quantitative Technical Analysis Library in C# for Quantower</Description>
<RepositoryType>git</RepositoryType> <RepositoryType>git</RepositoryType>
@@ -24,7 +24,7 @@
<IsPublishable>True</IsPublishable> <IsPublishable>True</IsPublishable>
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>AnyCPU</PlatformTarget>
<AllowUnsafeBlocks>False</AllowUnsafeBlocks> <AllowUnsafeBlocks>False</AllowUnsafeBlocks>
<DebugType>embedded</DebugType> <DebugType>full</DebugType>
<ProduceReferenceAssembly>True</ProduceReferenceAssembly> <ProduceReferenceAssembly>True</ProduceReferenceAssembly>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild> <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageTags> <PackageTags>
+138 -126
View File
@@ -21,144 +21,156 @@ Issues:
</summary> */ </summary> */
public class JMA_Series : TSeries { public class JMA_Series : TSeries
protected readonly int _period; {
protected readonly bool _NaN; protected readonly int _period;
protected readonly TSeries _data; protected readonly bool _NaN;
private readonly System.Collections.Generic.List<double> volty_short = new(); protected readonly TSeries _data;
private readonly System.Collections.Generic.List<double> vsum_buff = new(); private readonly System.Collections.Generic.List<double> volty_short = new();
private readonly double pr; private readonly System.Collections.Generic.List<double> vsum_buff = new();
private double upperBand, lowerBand, vsum, Kv; private readonly double pr;
private double prev_ma1, prev_det0, prev_det1, prev_vsum, prev_jma; private double upperBand, lowerBand, vsum, Kv;
private double p_upperBand, p_lowerBand, p_Kv, p_prev_ma1, p_prev_det0, p_prev_det1, p_prev_vsum, p_prev_jma; private double prev_ma1, prev_det0, prev_det1, prev_vsum, prev_jma;
private readonly int _voltyS, _voltyL; private double p_upperBand, p_lowerBand, p_Kv, p_prev_ma1, p_prev_det0, p_prev_det1, p_prev_vsum, p_prev_jma;
private readonly int _voltyS, _voltyL;
//core constructors //core constructors
public JMA_Series(int period, double phase, int vshort, int vlong, bool useNaN) { public JMA_Series(int period, double phase, int vshort, int vlong, bool useNaN)
_period = period; {
_NaN = useNaN; _period = period;
Name = $"JMA({period})"; _NaN = useNaN;
upperBand = lowerBand = prev_ma1 = prev_det0 = prev_det1 = prev_vsum = prev_jma = Kv = 0.0; Name = $"JMA({period})";
pr = (phase * 0.01) + 1.5; upperBand = lowerBand = prev_ma1 = prev_det0 = prev_det1 = prev_vsum = prev_jma = Kv = 0.0;
if (phase < -100) { pr = 0.5; } pr = (phase * 0.01) + 1.5;
if (phase > 100) { pr = 2.5; } if (phase < -100) { pr = 0.5; }
_voltyS = vshort; if (phase > 100) { pr = 2.5; }
_voltyL = vlong; _voltyS = vshort;
} _voltyL = vlong;
}
public JMA_Series(TSeries source, int period, double phase, int vshort, int vlong, bool useNaN) : this(period, phase, vshort, vlong, useNaN) { public JMA_Series(TSeries source, int period, double phase, int vshort, int vlong, bool useNaN) : this(period, phase, vshort, vlong, useNaN)
_data = source; {
Name = Name.Substring(0, Name.IndexOf(")")) + $", {(string.IsNullOrEmpty(_data.Name) ? "data" : _data.Name)})"; _data = source;
_data.Pub += Sub; Name = Name.Substring(0, Name.IndexOf(")")) + $", {(string.IsNullOrEmpty(_data.Name) ? "data" : _data.Name)})";
Add(_data); _data.Pub += Sub;
} Add(_data);
public JMA_Series() : this(period: 0, phase: 0, vshort:10, vlong:65, useNaN: false) { } }
public JMA_Series(int period) : this(period: period, phase: 0, vshort: 10, vlong: 65, useNaN: false) { } public JMA_Series() : this(period: 0, phase: 0, vshort: 10, vlong: 65, useNaN: false) { }
public JMA_Series(TBars source) : this(source.Close, period:0, phase:0.0, vshort:10, vlong:65, useNaN:false) { } public JMA_Series(int period) : this(period: period, phase: 0, vshort: 10, vlong: 65, useNaN: false) { }
public JMA_Series(TBars source, int period) : this(source.Close, period, phase: 0.0, vshort: 10, vlong: 65, useNaN: false) { } public JMA_Series(TBars source) : this(source.Close, period: 0, phase: 0.0, vshort: 10, vlong: 65, useNaN: false) { }
public JMA_Series(TBars source, int period, bool useNaN) : this(source.Close, period, phase: 0.0, vshort: 10, vlong: 65, useNaN: useNaN) { } public JMA_Series(TBars source, int period) : this(source.Close, period, phase: 0.0, vshort: 10, vlong: 65, useNaN: false) { }
public JMA_Series(TSeries source) : this(source, period:0, phase: 0.0, vshort: 10, vlong: 65, useNaN: false) { } public JMA_Series(TBars source, int period, bool useNaN) : this(source.Close, period, phase: 0.0, vshort: 10, vlong: 65, useNaN: useNaN) { }
public JMA_Series(TSeries source, int period) : this(source: source, period: period, phase: 0.0, vshort: 10, vlong: 65, useNaN: false) { } public JMA_Series(TSeries source) : this(source, period: 0, phase: 0.0, vshort: 10, vlong: 65, useNaN: false) { }
public JMA_Series(TSeries source, int period, bool useNaN) : this(source: source, period: period, phase: 0.0, vshort: 10, vlong: 65, useNaN: useNaN) { } public JMA_Series(TSeries source, int period) : this(source: source, period: period, phase: 0.0, vshort: 10, vlong: 65, useNaN: false) { }
public JMA_Series(TSeries source, int period, bool useNaN) : this(source: source, period: period, phase: 0.0, vshort: 10, vlong: 65, useNaN: useNaN) { }
////////////////// //////////////////
// core Add() algo // core Add() algo
public override (DateTime t, double v) Add((DateTime t, double v) TValue, bool update = false) { public override (DateTime t, double v) Add((DateTime t, double v) TValue, bool update = false)
if (this.Count == 0) { prev_ma1 = prev_jma = TValue.v; } {
if (update) { if (this.Count == 0) { prev_ma1 = prev_jma = TValue.v; }
upperBand = p_upperBand; if (update)
lowerBand = p_lowerBand; {
Kv = p_Kv; upperBand = p_upperBand;
prev_vsum = p_prev_vsum; lowerBand = p_lowerBand;
prev_ma1 = p_prev_ma1; Kv = p_Kv;
prev_det0 = p_prev_det0; prev_vsum = p_prev_vsum;
prev_det1 = p_prev_det1; prev_ma1 = p_prev_ma1;
prev_jma = p_prev_jma; prev_det0 = p_prev_det0;
} prev_det1 = p_prev_det1;
else { prev_jma = p_prev_jma;
p_upperBand = upperBand; }
p_lowerBand = lowerBand; else
p_Kv = Kv; {
p_prev_vsum = prev_vsum; p_upperBand = upperBand;
p_prev_ma1 = prev_ma1; p_lowerBand = lowerBand;
p_prev_det0 = prev_det0; p_Kv = Kv;
p_prev_det1 = prev_det1; p_prev_vsum = prev_vsum;
p_prev_jma = prev_jma; p_prev_ma1 = prev_ma1;
} p_prev_det0 = prev_det0;
p_prev_det1 = prev_det1;
p_prev_jma = prev_jma;
}
if (double.IsNaN(TValue.v)) { if (double.IsNaN(TValue.v))
return base.Add((TValue.t, double.NaN),update); {
} return base.Add((TValue.t, double.NaN), update);
}
// from Tvalue to volty // from Tvalue to volty
double del1 = TValue.v - upperBand; double del1 = TValue.v - upperBand;
double del2 = TValue.v - lowerBand; double del2 = TValue.v - lowerBand;
upperBand = (del1 > 0) ? TValue.v : TValue.v - (Kv * del1); upperBand = (del1 > 0) ? TValue.v : TValue.v - (Kv * del1);
lowerBand = (del2 < 0) ? TValue.v : TValue.v - (Kv * del2); lowerBand = (del2 < 0) ? TValue.v : TValue.v - (Kv * del2);
double volty = 0; double volty = Math.Abs(del1) > Math.Abs(del2) ? Math.Abs(del1) :
if (Math.Abs(del1) > Math.Abs(del2)) { volty = Math.Abs(del1); } (Math.Abs(del1) < Math.Abs(del2) ? Math.Abs(del2) :
if (Math.Abs(del1) < Math.Abs(del2)) { volty = Math.Abs(del2); } Math.Abs(0.5 * (del1 + del2)));
//// from volty to avolty //// from volty to avolty
if (update) { volty_short[volty_short.Count - 1] = volty; } if (update) { volty_short[volty_short.Count - 1] = volty; }
else { volty_short.Add(volty); } else { volty_short.Add(volty); }
if (volty_short.Count > _voltyS) { volty_short.RemoveAt(0); } if (volty_short.Count > _voltyS) { volty_short.RemoveAt(0); }
vsum = prev_vsum + 0.1 * (volty - volty_short.First()); vsum = prev_vsum + 0.1 * (volty - volty_short.First());
prev_vsum = vsum; prev_vsum = vsum;
if (update) { vsum_buff[vsum_buff.Count - 1] = vsum; } if (update) { vsum_buff[vsum_buff.Count - 1] = vsum; }
else { vsum_buff.Add(vsum); } else { vsum_buff.Add(vsum); }
if (vsum_buff.Count > _voltyL) { vsum_buff.RemoveAt(0); } if (vsum_buff.Count > _voltyL) { vsum_buff.RemoveAt(0); }
double avolty = 0; double avolty = 0;
for (int i = 0; i < vsum_buff.Count; i++) { avolty += vsum_buff[i]; } for (int i = 0; i < vsum_buff.Count; i++) { avolty += vsum_buff[i]; }
avolty /= vsum_buff.Count; avolty /= vsum_buff.Count;
/// from avolty to rolty /// from avolty to rolty
double rvolty = (avolty != 0) ? volty / avolty : 0; double rvolty = (avolty != 0) ? volty / avolty : 0;
double len1 = (Math.Log(Math.Sqrt(_period)) / Math.Log(2.0)) + 2; double len1 = (Math.Log(Math.Sqrt(_period)) / Math.Log(2.0)) + 2;
if (len1 < 0) { len1 = 0; } if (len1 < 0) { len1 = 0; }
double pow1 = Math.Max(len1 - 2.0, 0.5); double pow1 = Math.Max(len1 - 2.0, 0.5);
if (rvolty > Math.Pow(len1, 1.0 / pow1)) { rvolty = Math.Pow(len1, 1.0 / pow1); } if (rvolty > Math.Pow(len1, 1.0 / pow1)) { rvolty = Math.Pow(len1, 1.0 / pow1); }
if (rvolty < 1) { rvolty = 1; } if (rvolty < 1) { rvolty = 1; }
//// from rvolty to second smoothing //// from rvolty to second smoothing
double pow2 = Math.Pow(rvolty, pow1); double pow2 = Math.Pow(rvolty, pow1);
double beta = 0.45 * (_period - 1) / (0.45 * (_period - 1) + 2); double beta = 0.45 * (_period - 1) / (0.45 * (_period - 1) + 2);
Kv = Math.Pow(beta, Math.Sqrt(pow2)); Kv = Math.Pow(beta, Math.Sqrt(pow2));
double alpha = Math.Pow(beta, pow2); double alpha = Math.Pow(beta, pow2);
double ma1 = (1 - alpha) * TValue.v + alpha * prev_ma1; double ma1 = (1 - alpha) * TValue.v + alpha * prev_ma1;
prev_ma1 = ma1; prev_ma1 = ma1;
double det0 = (1 - beta) * (TValue.v - ma1) + beta * prev_det0; double det0 = (1 - beta) * (TValue.v - ma1) + beta * prev_det0;
prev_det0 = det0; prev_det0 = det0;
double ma2 = ma1 + pr * det0; double ma2 = ma1 + pr * det0;
double det1 = ((1 - alpha) * (1 - alpha) * (ma2 - prev_jma)) + (alpha * alpha * prev_det1); double det1 = ((1 - alpha) * (1 - alpha) * (ma2 - prev_jma)) + (alpha * alpha * prev_det1);
prev_det1 = det1; prev_det1 = det1;
double jma = prev_jma + det1; double jma = prev_jma + det1;
prev_jma = jma; prev_jma = jma;
var res = (TValue.t, Count < _period - 1 && _NaN ? double.NaN : jma); var res = (TValue.t, Count < _period - 1 && _NaN ? double.NaN : jma);
return base.Add(res, update); return base.Add(res, update);
} }
public override (DateTime t, double v) Add(TSeries data) { public override (DateTime t, double v) Add(TSeries data)
if (data == null) { return (DateTime.Today, Double.NaN); } {
foreach (var item in data) { Add(item, false); } if (data == null) { return (DateTime.Today, Double.NaN); }
return _data.Last; foreach (var item in data) { Add(item, false); }
} return _data.Last;
public (DateTime t, double v) Add(bool update) { }
return this.Add(TValue: _data.Last, update: update); public (DateTime t, double v) Add(bool update)
} {
public (DateTime t, double v) Add() { return this.Add(TValue: _data.Last, update: update);
return Add(TValue: _data.Last, update: false); }
} public (DateTime t, double v) Add()
private new void Sub(object source, TSeriesEventArgs e) { {
Add(TValue: _data.Last, update: e.update); return Add(TValue: _data.Last, update: false);
} }
private new void Sub(object source, TSeriesEventArgs e)
{
Add(TValue: _data.Last, update: e.update);
}
//reset calculation //reset calculation
public override void Reset() { public override void Reset()
upperBand = lowerBand = prev_ma1 = prev_det0 = prev_det1 = prev_vsum = prev_jma = Kv = 0.0; {
} upperBand = lowerBand = prev_ma1 = prev_det0 = prev_det1 = prev_vsum = prev_jma = Kv = 0.0;
}
} }
+1 -1
View File
@@ -22,7 +22,7 @@ public class TSeriesEventArgs : EventArgs {
public class TSeries : List<(DateTime t, double v)> { public class TSeries : List<(DateTime t, double v)> {
public List<DateTime> t => this.Select(item => item.t).ToList(); public List<DateTime> t => this.Select(item => item.t).ToList();
public List<double> v => this.Select(item => item.v).ToList(); public List<double> v => this.Select(item => item.v).ToList();
public (DateTime t, double v) Last => this[this.Count - 1]; public (DateTime t, double v) Last => this[^1];
public int Length => Count; public int Length => Count;
public string Name { get; set; } public string Name { get; set; }
-89
View File
@@ -1,89 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using TradingPlatform.BusinessLayer;
using QuanTAlib;
using System.Drawing;
namespace SimpleMACross {
public class SimpleMACross1 : Strategy, ICurrentAccount, ICurrentSymbol {
[InputParameter("Symbol", 0)]
public Symbol CurrentSymbol { get; set; }
[InputParameter("Account", 1)]
public Account CurrentAccount { get; set; }
[InputParameter("Fast MA", 2, minimum: 1, maximum: 100, increment: 1, decimalPlaces: 0)]
private int FastMA = 5;
[InputParameter("Slow MA", 3, minimum: 1, maximum: 100, increment: 1, decimalPlaces: 0)]
private int SlowMA = 10;
[InputParameter("Quantity", 4, 0.1, 99999, 0.1, 2)]
private double Quantity = 1.0;
[InputParameter("Period", 5)]
private Period period = Period.MIN1;
public override string[] MonitoringConnectionsIds => new string[] { this.CurrentSymbol?.ConnectionId, this.CurrentAccount?.ConnectionId };
private HistoricalData hdm;
private DateTime prev_time;
private readonly TBars bars = new();
public SimpleMACross1() {
this.Name = "MA Cross strategy 3";
this.Description = "Raw strategy without any additional functional";
}
protected override void OnRun() {
if (this.CurrentAccount != null && this.CurrentAccount.State == BusinessObjectState.Fake) {this.CurrentAccount = Core.Instance.GetAccount(this.CurrentAccount.CreateInfo());}
if (this.CurrentSymbol != null && this.CurrentSymbol.State == BusinessObjectState.Fake) {this.CurrentSymbol = Core.Instance.GetSymbol(this.CurrentSymbol.CreateInfo());}
if (this.CurrentSymbol == null || this.CurrentAccount == null || this.CurrentSymbol.ConnectionId != this.CurrentAccount.ConnectionId) {
this.Log("Incorrect input parameters... Symbol or Account are not specified or they have different connectionID.", StrategyLoggingLevel.Error);
return; }
/////////////////////////////////////////////////////
this.hdm = this.CurrentSymbol.GetHistory(Period.MIN1, this.CurrentSymbol.HistoryType, Core.TimeUtils.DateTimeUtcNow.AddDays(-1));
////////////////////////////////////////////////////
this.LogInfo($"Symbol: {CurrentSymbol.Name} period: {this.period} :-: {this.CurrentSymbol.HistoryType.ToString()} :-: {this.hdm.Count} bars loaded");
this.hdm.HistoryItemUpdated += this.Hdm_HistoryItemUpdated;
}
private void Hdm_HistoryItemUpdated(object sender, HistoryEventArgs e) {
this.OnUpdate();
}
private void OnUpdate() {
bool update = hdm.Last().TimeLeft - prev_time < this.period.Duration ? true : false;
if (!update) {prev_time = hdm.Last().TimeLeft;}
bars.Add(hdm.Last().TimeLeft, hdm.Last()[PriceType.Open], hdm.Last()[PriceType.High],
hdm.Last()[PriceType.Low], hdm.Last()[PriceType.Close], hdm.Last()[PriceType.Volume], update);
if (!update) {this.LogInfo($"{bars.Close.Last().t} OHLC4:{(double)bars.OHLC4.Last.v}");}
}
protected override List<StrategyMetric> OnGetMetrics() {
var result = base.OnGetMetrics();
// An example of adding custom strategy metrics:
result.Add("Bars processed", this.bars.Count.ToString());
return result;
}
protected override void OnStop() {
if (this.hdm != null) {
this.hdm.HistoryItemUpdated -= this.Hdm_HistoryItemUpdated;
this.hdm.Dispose();
}
base.OnStop();
}
}
}
@@ -0,0 +1,2 @@
"To use unique insights from EPAM's history, expertise, and innovative spirit we want to recalibrate technology strategies to deliver solutions that are not just innovative, but driven by value creation. We envision a future where every client engagement is delivers integrated value from strategy to optimization, and where our technical thought leadership is a benchmark for the industry, ensuring that EPAM is synonymous with transformative digital engineering."