Sonarcloud changes

This commit is contained in:
Miha
2022-04-19 23:29:04 -07:00
parent df9023b4d6
commit bb361177ce
11 changed files with 10 additions and 732 deletions
-114
View File
@@ -1,114 +0,0 @@
// ADD - adding TSeries+TSeries together, or TSeries+double, or double+TSeries
using System;
namespace QuantLib;
public class ADD_Series : TSeries
{
readonly TSeries _d1;
readonly TSeries _d2;
readonly double _dd;
readonly byte _type;
public ADD_Series(TSeries d1, TSeries d2)
{
this._d1 = d1;
this._d2 = d2;
this._dd = 0;
this._type = 1;
d1.Pub += this.Sub;
d2.Pub += this.Sub;
if (d1.Count > 0 && d2.Count > 0)
{
for (int i = 0; i < Math.Min(d1.Count, d2.Count); i++)
{
this.Add(d1[i], d2[i], false);
}
}
}
public ADD_Series(TSeries d1, double dd)
{
this._d1 = d1;
this._d2 = null;
this._dd = dd;
this._type = 2;
d1.Pub += this.Sub;
if (d1.Count > 0)
{
for (int i = 0; i < d1.Count; i++)
{
this.Add(d1[i], dd, false);
}
}
}
public ADD_Series(double dd, TSeries d1)
{
this._d1 = d1;
this._d2 = null;
this._dd = dd;
this._type = 3;
d1.Pub += this.Sub;
if (d1.Count > 0)
{
for (int i = 0; i < d1.Count; i++)
{
this.Add(d1[i], dd, false);
}
}
}
public void Add((System.DateTime t, double v) d1,
(System.DateTime t, double v) d2, bool update = false)
{
(System.DateTime t, double v) result =
((d1.t > d2.t) ? d1.t : d2.t, d1.v + d2.v);
if (update)
{
base[base.Count - 1] = result;
}
else
{
base.Add(result);
}
}
public void Add((System.DateTime t, double v) d1, double dd,
bool update = false)
{
(System.DateTime t, double v) result = (d1.t, d1.v + dd);
if (update)
{
base[base.Count - 1] = result;
}
else
{
base.Add(result);
}
}
public void Add(double dd, (System.DateTime t, double v) d1, bool update = false) => this.Add(d1, dd, update);
public void Add(bool update = false)
{
if (update || (this._d1.Count > 0 && this._d1.Count == this._d2.Count &&
this.Count != this._d1.Count))
{
if (this._type == 1)
{
this.Add(this._d1[this._d1.Count - 1], this._d2[this._d2.Count - 1],
update);
}
else if (this._type == 2)
{
this.Add(this._d1[this._d1.Count - 1], this._dd, update);
}
else
{
this.Add(this._dd, this._d1[this._d1.Count - 1], update);
}
}
}
public new void Sub(object source, TSeriesEventArgs e) => this.Add(e.update);
}
-42
View File
@@ -1,42 +0,0 @@
namespace QuantLib;
using System;
public abstract class Single_TSeries_Indicator : TSeries
{
protected readonly int _p;
protected readonly bool _NaN;
protected readonly TSeries _data;
// Default Constructor
protected Single_TSeries_Indicator(TSeries source, int period, bool useNaN)
{
this._data = source;
this._p = period;
this._NaN = useNaN;
this._data.Pub += this.Sub;
}
// overridable Add() method for the whole series (should be replaced with faster algo than default)
public virtual void Add(TSeries data)
{
for (int i = 0; i < data.Count; i++)
{
this.Add(data[i], false);
}
}
// overridable Add() method to add/update a single value at the end of the list
public virtual new void Add((System.DateTime t, double v) tuple, bool update) => base.Add(tuple, update);
// Add() without update parameter assumes this is an insert of new data (update=false)
public new void Add((System.DateTime t, double v) d) => this.Add(d, update: false);
// Add() without a tuple assumes add/update using the last item in the source as new data
public void Add(bool update) => this.Add(this._data[this._data.Count - 1], update);
// Add() without any parameters assumes this is an insert of new data using the last item in the source
public void Add() => this.Add(this._data[this._data.Count - 1], update: false);
// When event is triggered, call Add(bool update)
public new void Sub(object source, TSeriesEventArgs e) => this.Add(this._data[this._data.Count - 1], e.update);
}
-125
View File
@@ -1,125 +0,0 @@
// DIV - divide TSeries/TSeries , or TSeries/double, or double/TSeries
using System;
namespace QuantLib;
public class DIV_Series : TSeries
{
readonly TSeries _d1, _d2;
readonly double _dd;
readonly byte _type;
public DIV_Series(TSeries d1, TSeries d2)
{
this._d1 = d1;
this._d2 = d2;
this._type = 1;
d1.Pub += this.Sub;
d2.Pub += this.Sub;
if (d1.Count > 0 && d2.Count > 0)
{
for (int i = 0; i < Math.Min(d1.Count, d2.Count); i++)
{
this.Add(d1[i], d2[i], false);
}
}
}
public DIV_Series(TSeries d1, double dd)
{
this._d1 = d1;
this._d2 = d1;
this._dd = dd;
this._type = 2;
d1.Pub += this.Sub;
if (d1.Count > 0)
{
for (int i = 0; i < d1.Count; i++)
{
this.Add(d1[i], dd, false);
}
}
}
public DIV_Series(double dd, TSeries d1)
{
this._d1 = d1;
this._d2 = d1;
this._dd = dd;
this._type = 3;
d1.Pub += this.Sub;
if (d1.Count > 0)
{
for (int i = 0; i < d1.Count; i++)
{
this.Add(dd, d1[i], false);
}
}
}
public void Add((System.DateTime t, double v) d1,
(System.DateTime t, double v) d2, bool update = false)
{
(System.DateTime t, double v) result =
((d1.t > d2.t) ? d1.t : d2.t,
(d2.v > double.Epsilon) ? d1.v / d2.v : double.PositiveInfinity);
if (update)
{
base[base.Count - 1] = result;
}
else
{
base.Add(result);
}
}
public void Add((System.DateTime t, double v) d1, double dd,
bool update = false)
{
(System.DateTime t, double v) result =
(d1.t, (dd > double.Epsilon) ? d1.v / dd : double.PositiveInfinity);
if (update)
{
base[base.Count - 1] = result;
}
else
{
base.Add(result);
}
}
public void Add(double dd, (System.DateTime t, double v) d1,
bool update = false)
{
(System.DateTime t, double v) result =
(d1.t, (d1.v > double.Epsilon) ? dd / d1.v : double.PositiveInfinity);
if (update)
{
base[base.Count - 1] = result;
}
else
{
base.Add(result);
}
}
public void Add(bool update = false)
{
if (update || (this._d1.Count > 0 && this._d1.Count == this._d2.Count &&
this.Count != this._d1.Count))
{
if (this._type == 1)
{
this.Add(this._d1[this._d1.Count - 1], this._d2[this._d2.Count - 1],
update);
}
else if (this._type == 2)
{
this.Add(this._d1[this._d1.Count - 1], this._dd, update);
}
else
{
this.Add(this._dd, this._d1[this._d1.Count - 1], update);
}
}
}
public new void Sub(object source, TSeriesEventArgs e) { this.Add(e.update); }
}
+7 -10
View File
@@ -1,3 +1,4 @@
namespace QuanTAlib;
using System.Drawing;
using TradingPlatform.BusinessLayer;
@@ -6,7 +7,7 @@ public class WMAPE_chart : Indicator
#region Parameters
[InputParameter("Smoothing period", 0, 1, 999, 1, 1)]
private int Period = 10;
private readonly int Period = 10;
[InputParameter("Data source", 1, variants: new object[]{
"Open", 0,
@@ -20,7 +21,7 @@ public class WMAPE_chart : Indicator
"OHLC4", 8,
"Weighted (HLCC4)", 9
})]
private int DataSource = 8;
private readonly int DataSource = 8;
#endregion Parameters
@@ -40,17 +41,13 @@ public class WMAPE_chart : Indicator
protected override void OnInit()
{
this.ShortName = "WMAPE (" + QuantLib.TBars.SelectStr(this.DataSource) + ", " + this.Period + ")";
this.indicator = new(source: bars.Select(this.DataSource), period: this.Period, useNaN: true);
this.ShortName = "WMAPE (" + QuanTAlib.TBars.SelectStr(this.DataSource) + ", " + this.Period + ")";
this.indicator = new(source: this.bars.Select(this.DataSource), period: this.Period, useNaN: true);
}
protected void OnNewData(bool update = false)
{
this.indicator.Add(update);
protected void OnNewData(bool update = false) => this.indicator.Add(update);
}
protected override void OnUpdate(UpdateArgs args)
protected override void OnUpdate(UpdateArgs args)
{
bool update = !(args.Reason == UpdateReason.NewBar || args.Reason == UpdateReason.HistoricalBar);
this.bars.Add(this.Time(), this.GetPrice(PriceType.Open), this.GetPrice(PriceType.High), this.GetPrice(PriceType.Low), this.GetPrice(PriceType.Close), this.GetPrice(PriceType.Volume), update);
-117
View File
@@ -1,117 +0,0 @@
// MUL - multiply TSeries*TSeries together, or TSeries*double, or double*TSeries
using System;
namespace QuantLib;
public class MUL_Series : TSeries
{
readonly TSeries _d1, _d2;
readonly double _dd;
readonly byte _type;
public MUL_Series(TSeries d1, TSeries d2)
{
this._d1 = d1;
this._d2 = d2;
this._dd = 0;
this._type = 1;
d1.Pub += this.Sub;
d2.Pub += this.Sub;
if (d1.Count > 0 && d2.Count > 0)
{
for (int i = 0; i < Math.Min(d1.Count, d2.Count); i++)
{
this.Add(d1[i], d2[i], false);
}
}
}
public MUL_Series(TSeries d1, double dd)
{
this._d1 = d1;
this._d2 = d1;
this._dd = dd;
this._type = 2;
d1.Pub += this.Sub;
if (d1.Count > 0)
{
for (int i = 0; i < d1.Count; i++)
{
this.Add(d1[i], dd, false);
}
}
}
public MUL_Series(double dd, TSeries d1)
{
this._d1 = d1;
this._d2 = d1;
this._dd = dd;
this._type = 3;
d1.Pub += this.Sub;
if (d1.Count > 0)
{
for (int i = 0; i < d1.Count; i++)
{
this.Add(d1[i], dd, false);
}
}
}
public void Add((System.DateTime t, double v) d1,
(System.DateTime t, double v) d2, bool update = false)
{
(System.DateTime t, double v) result =
((d1.t > d2.t) ? d1.t : d2.t, d1.v * d2.v);
if (update)
{
base[base.Count - 1] = result;
}
else
{
base.Add(result);
}
}
public void Add((System.DateTime t, double v) d1, double dd,
bool update = false)
{
(System.DateTime t, double v) result = (d1.t, d1.v * dd);
if (update)
{
base[base.Count - 1] = result;
}
else
{
base.Add(result);
}
}
public void Add(double dd, (System.DateTime t, double v) d1,
bool update = false)
{
this.Add(d1, dd, update);
}
public void Add(bool update = false)
{
if (update || (this._d1.Count > 0 && this._d1.Count == this._d2.Count &&
this.Count != this._d1.Count))
{
if (this._type == 1)
{
this.Add(this._d1[this._d1.Count - 1], this._d2[this._d2.Count - 1],
update);
}
else if (this._type == 2)
{
this.Add(this._d1[this._d1.Count - 1], this._dd, update);
}
else
{
this.Add(this._dd, this._d1[this._d1.Count - 1], update);
}
}
}
public new void Sub(object source, TSeriesEventArgs e) { this.Add(e.update); }
}
-20
View File
@@ -1,20 +0,0 @@
using System;
using QuantLib;
public class RND_Feed : TBars
{
public RND_Feed(int days, double volatility = 0.05, double startvalue = 100.0)
{
Random rnd = new();
double c = startvalue;
for (int i = 0; i < days; i++)
{
double o = Math.Round(c + c * (volatility * 0.1 * rnd.NextDouble() - 0.005), 2);
double h = Math.Round(o + c * volatility * rnd.NextDouble(), 2);
double l = Math.Round(o - c * volatility * rnd.NextDouble(), 2);
c = Math.Round(l + (h - l) * rnd.NextDouble(), 2);
double v = Math.Round(1000 * rnd.NextDouble(), 2);
this.Add(DateTime.Today.AddDays(i - days), o, h, l, c, v);
}
}
}
-125
View File
@@ -1,125 +0,0 @@
// SUB - subtracting TSeries-TSeries, or TSeries-double, or double-TSeries
using System;
namespace QuantLib;
public class SUB_Series : TSeries
{
readonly TSeries _d1, _d2;
readonly double _dd;
readonly byte _type;
public SUB_Series(TSeries d1, TSeries d2)
{
this._d1 = d1;
this._d2 = d2;
this._dd = 0;
this._type = 1;
d1.Pub += this.Sub;
d2.Pub += this.Sub;
if (d1.Count > 0 && d2.Count > 0)
{
for (int i = 0; i < Math.Min(d1.Count, d2.Count); i++)
{
this.Add(d1[i], d2[i], false);
}
}
}
public SUB_Series(TSeries d1, double dd)
{
this._d1 = d1;
this._d2 = d1;
this._dd = dd;
this._type = 2;
d1.Pub += this.Sub;
if (d1.Count > 0)
{
for (int i = 0; i < d1.Count; i++)
{
this.Add(d1[i], dd, false);
}
}
}
public SUB_Series(double dd, TSeries d1)
{
this._d1 = d1;
this._d2 = d1;
this._dd = dd;
this._type = 3;
d1.Pub += this.Sub;
if (d1.Count > 0)
{
for (int i = 0; i < d1.Count; i++)
{
this.Add(dd, d1[i], false);
}
}
}
public void Add((System.DateTime t, double v) d1,
(System.DateTime t, double v) d2, bool update = false)
{
(System.DateTime t, double v) result =
((d1.t > d2.t) ? d1.t : d2.t, d1.v - d2.v);
if (update)
{
base[base.Count - 1] = result;
}
else
{
base.Add(result);
}
}
public void Add((System.DateTime t, double v) d1, double dd,
bool update = false)
{
(System.DateTime t, double v) result = (d1.t, d1.v - dd);
if (update)
{
base[base.Count - 1] = result;
}
else
{
base.Add(result);
}
}
public void Add(double dd, (System.DateTime t, double v) d1,
bool update = false)
{
(System.DateTime t, double v) result = (d1.t, dd - d1.v);
if (update)
{
base[base.Count - 1] = result;
}
else
{
base.Add(result);
}
}
public void Add(bool update = false)
{
if (update || (this._d1.Count > 0 && this._d1.Count == this._d2.Count &&
this.Count != this._d1.Count))
{
if (this._type == 1)
{
this.Add(this._d1[this._d1.Count - 1], this._d2[this._d2.Count - 1],
update);
}
else if (this._type == 2)
{
this.Add(this._d1[this._d1.Count - 1], this._dd, update);
}
else
{
this.Add(this._dd, this._d1[this._d1.Count - 1], update);
}
}
}
public new void Sub(object source, TSeriesEventArgs e) { this.Add(e.update); }
}
-104
View File
@@ -1,104 +0,0 @@
namespace QuantLib;
using System;
public class TBars : System.Collections.Generic.List<(DateTime t, double o, double h, double l, double c, double v)>
{
private readonly TSeries _open = new();
private readonly TSeries _high = new();
private readonly TSeries _low = new();
private readonly TSeries _close = new();
private readonly TSeries _volume = new();
private readonly TSeries _hl2 = new();
private readonly TSeries _oc2 = new();
private readonly TSeries _ohl3 = new();
private readonly TSeries _hlc3 = new();
private readonly TSeries _ohlc4 = new();
private readonly TSeries _hlcc4 = new();
public TSeries Open => this._open;
public TSeries High => this._high;
public TSeries Low => this._low;
public TSeries Close => this._close;
public TSeries Volume => this._volume;
public TSeries HL2 => this._hl2;
public TSeries OC2 => this._oc2;
public TSeries OHL3 => this._ohl3;
public TSeries HLC3 => this._hlc3;
public TSeries OHLC4 => this._ohlc4;
public TSeries HLCC4 => this._hlcc4;
public TSeries Select(int source)
{
return source switch
{
0 => _open,
1 => _high,
2 => _low,
3 => _close,
4 => _hl2,
5 => _oc2,
6 => _ohl3,
7 => _hlc3,
8 => _ohlc4,
_ => _hlcc4,
};
}
public static string SelectStr(int source)
{
return source switch
{
0 => "Open",
1 => "High",
2 => "Low",
3 => "Close",
4 => "HL2",
5 => "OC2",
6 => "OHL3",
7 => "Typical",
8 => "Mean",
_ => "Weighted",
};
}
public void
Add((DateTime t, double o, double h, double l, double c, double v) i, bool update = false)
=> Add(i.t, i.o, i.h, i.l, i.c, i.v, update);
public void Add(DateTime t, decimal o, decimal h, decimal l, decimal c, decimal v, bool update = false)
=> Add(t, (double)o, (double)h, (double)l, (double)c, (double)v, update);
public void Add(DateTime t, double o, double h, double l, double c, double v, bool update = false)
{
if (update)
{
this[this.Count - 1] = (t, o, h, l, c, v);
_open[_open.Count - 1] = (t, o);
_high[_high.Count - 1] = (t, h);
_low[_low.Count - 1] = (t, l);
_close[_close.Count - 1] = (t, c);
_volume[_volume.Count - 1] = (t, v);
_hl2[_hl2.Count - 1] = (t, (h + l) * 0.5);
_oc2[_oc2.Count - 1] = (t, (o + c) * 0.5);
_ohl3[_ohl3.Count - 1] = (t, (o + h + l) * 0.333333333333333);
_hlc3[_hlc3.Count - 1] = (t, (h + l + c) * 0.333333333333333);
_ohlc4[_ohlc4.Count - 1] = (t, (o + h + l + c) * 0.25);
_hlcc4[_hlcc4.Count - 1] = (t, (h + l + c + c) * 0.25);
}
else
{
base.Add((t, o, h, l, c, v));
_open.Add((t, o));
_high.Add((t, h));
_low.Add((t, l));
_close.Add((t, c));
_volume.Add((t, v));
_hl2.Add((t, (h + l) * 0.5));
_oc2.Add((t, (o + c) * 0.5));
_ohl3.Add((t, (o + h + l) * 0.333333333333333));
_hlc3.Add((t, (h + l + c) * 0.333333333333333));
_ohlc4.Add((t, (o + h + l + c) * 0.25));
_hlcc4.Add((t, (h + l + c + c) * 0.25));
}
}
}
-72
View File
@@ -1,72 +0,0 @@
namespace QuantLib;
using System;
using System.Linq;
public class TSeries : System.Collections.Generic.List<(DateTime t, double v)>
{
// when asked for a (t,v) tuple, return the last (t,v) on the List
public static implicit operator (DateTime t, double v)(TSeries l) => l[l.Count - 1];
// when asked for a (double), return the value part of the last tuple on the list
public static implicit operator double(TSeries l) => l[l.Count - 1].v;
// when asked for a (DateTime), return the DateTime part of the last tuple on the list
public static implicit operator DateTime(TSeries l) => l[l.Count - 1].t;
public System.Collections.Generic.List<DateTime> t =>
this.Select(x => (DateTime)x.t).ToList();
public System.Collections.Generic.List<double> v =>
this.Select(x => (double)x.v).ToList();
public int Length => this.Count;
// add/update one (t,v) tuple to/at the end of the list
public void Add((DateTime t, double v) TValue, bool update = false)
{
if (update) { this[this.Count - 1] = TValue; }
else { base.Add(TValue); }
this.OnEvent(update);
}
public void Add(DateTime t, double v, bool update = false) => this.Add((t, v), update);
public void Add(double v, bool update = false) => this.Add((DateTime.Now, v), update);
// Broadcast handler - only to valid targets
protected virtual void OnEvent(bool update = false)
{
if (Pub != null && Pub.Target != this)
{
Pub(this, new TSeriesEventArgs { update = update });
}
}
// delegate used by event handler + event handler (Pub == publisher)
public delegate
void NewDataEventHandler(object source, TSeriesEventArgs args);
public event NewDataEventHandler Pub;
public void Sub(object source, TSeriesEventArgs e)
{
TSeries ss = (TSeries)source;
if (ss.Count > 0)
{
for (int i = 0; i < ss.Count; i++)
{
this.Add(ss[i]);
}
}
else
{
this.Add(ss[ss.Count - 1], e.update);
}
}
}
// EventArgs extension - carries the update field
public class TSeriesEventArgs : EventArgs
{
public bool update { get; set; }
}
+2 -2
View File
@@ -26,7 +26,7 @@ public class ATR_Series : Single_TBars_Indicator
this._k = 1.0 / (double)(this._p);
this._k1m = 1.0 - this._k;
this._lastema = this._lastlastema = double.NaN;
if (_bars.Count > 0) { base.Add(_bars); }
if (this._bars.Count > 0) { base.Add(this._bars); }
}
public override void Add((DateTime t, double o, double h, double l, double c, double v) TBar, bool update = false)
@@ -36,7 +36,7 @@ public class ATR_Series : Single_TBars_Indicator
this._cm1 = this._lastcm1;
}
if (_cm1 is double.NaN) { _cm1 = TBar.c; }
if (this._cm1 is double.NaN) { this._cm1 = TBar.c; }
double d1 = Math.Abs(TBar.h - TBar.l);
double d2 = Math.Abs(_cm1 - TBar.h);
double d3 = Math.Abs(_cm1 - TBar.l);
+1 -1
View File
@@ -1,4 +1,4 @@
namespace QuanTAlib;
namespace QuanTAlib;
using System;
/* <summary>