NT trader indicator translated to EL - d9 Haar Wavelet

Questions about MultiCharts and user contributed studies.
simon007
Posts: 60
Joined: 12 Apr 2007
Has thanked: 7 times
Been thanked: 1 time

NT trader indicator translated to EL - d9 Haar Wavelet

Postby simon007 » 29 Mar 2013

Hi @,

I found this indicator below on the NT forum, and I'm trying to convert this to easylanguage, but my C# knowledge is very poor.

Can this be done? Or can it only be done in easylanguage via a dll?

The OnBarUpdate part is the actual haar wavelet lifting, right?

Thanks,

Simon


Code: Select all

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using d9.NinjaScript.Utility;
using NT.Data;
using NT.Gui.Chart;
using NT.Gui.Design;

namespace NT.Indicator
{
[Description("Undecimated Haar Wavelet Transform implemented via Lifting")]
[Gui.Design.DisplayName(" d9 Haar Wavelet")]
public class d9HaarWavelet : Indicator
{
#region fields

private int _decompositionLevel=4;

private bool _showApproximation = false;
private bool _useAbsValue = false;
private string _detailListInput = "0";
private string _smoothListInput = "-1";
private string _thresholdListInput = "-1";
private string _roughListInput = "-1";
private List<int> _detailList;
private List<int> _smoothList;
private List<int> _thresholdList;
private List<int> _roughList;
private InputSignal inputType = InputSignal.Price;
private int _smoothAmt =5;
private int _smoothPhase = 0;

private List<string> _plotList;
private Dictionary<int, IDataSeries> c;
private Dictionary<int, DataSeries> r;
private Dictionary<int, DataSeries> d;
private Dictionary<int, int> zlr;

private bool _enableAutoColor = true;
private double _colorH = .05;
private double _colorL = .95;
private double _saturationH = 1;
private double _saturationL = 1;
private double _luminosityH = .6;
private double _luminosityL = .7;
private float _detailPenWidth = 1.0f;
private DashStyle _detailDashStyle = DashStyle.Dot;
private float _roughPenWidth = 2.0f;
private DashStyle _roughDashStyle = DashStyle.Dash;
private int _detailAlpha = 255;
private int _roughAlpha = 200;
private PlotStyle _detailPlotStyle = PlotStyle.Line;
private PlotStyle _roughPlotStyle = PlotStyle.Line;

private bool _showZLR = false;
private int _zlrMaxScale = 3;
private Color _zlrColorUp = Color.DarkCyan;
private Color _zlrColorDown = Color.DarkMagenta;
private symbolType _zlrSymbolUp = symbolType.ArrowUp;
private symbolType _zlrSymbolDown = symbolType.ArrowDown;

private int z { get { return (this.CalculateOnBarClose) ? 0 : 1; } }
private int zz { get { return (this.CalculateOnBarClose) ? 1 : 2; } }
#endregion

protected override void Initialize()
{
zlr = new Dictionary<int, int>();

c = new Dictionary<int, IDataSeries>();
c[0] = new DataSeries(this);
r = new Dictionary<int, DataSeries>();
d = new Dictionary<int, DataSeries>();

_plotList = new List<string>();

Add(new Line(Color.DimGray, 0, "zeroLine"));
Lines[0].Pen.DashStyle = DashStyle.Dash;

_detailList = Util.ParseInputList(_detailListInput);
_roughList = Util.ParseInputList(_roughListInput);
_smoothList = Util.ParseInputList(_smoothListInput);
_thresholdList = Util.ParseInputList(_thresholdListInput);

refreshPlotList();

for (int i = 0; i <= DecompositionLevel; i++)
{
c[i+1] = new DataSeries(this);
if (i >= 1) d[i] = new DataSeries(this);
if (i > 1) r[i] = new DataSeries(this);
if (i == 1) r[i] = (DataSeries) ((ShowApproximation) ? c[i] : d[i]);
}

foreach (string s in _plotList)
{
int level = int.Parse(s.Substring(1));

if (s.StartsWith("D") || s.StartsWith("C"))
Add(new Plot(new Pen(getColor(level, _detailAlpha), _detailPenWidth), _detailPlotStyle, s));
else
Add(new Plot(new Pen(getColor(level, _roughAlpha), _roughPenWidth), _roughPlotStyle, s));
}

Overlay = false;
PriceTypeSupported = false;
DrawOnPricePanel = false;
}

protected override void OnBarUpdate()
{
if (CurrentBar < 2) return;

#region AutoColor
if (AutoColorEnabled)
{
for ( int i = 0; i < Plots.Length; i++)
{
Plot p = Plots[i];
String s = (_plotList.Count > i) ? _plotList[i] : "unused";
p.Name = s;
if (s == "unused") //take care of stupid NT plot handling 'features'
{
p.Pen.Color = Color.Empty;
continue;
}

int level = int.Parse(p.Name.Substring(1));

if (p.Name.StartsWith("D") || p.Name.StartsWith("C"))
{
p.Pen.Width = DetailPenWidth;
p.PlotStyle = DetailPlotStyle;
p.Pen.DashStyle = DetailDashStyle;
p.Pen.Color = getColor(level, DetailAlpha);
}
else if (p.Name.StartsWith("R") || p.Name.StartsWith("S"))
{
p.Pen.Width = RoughPenWidth;
p.PlotStyle = RoughPlotStyle;
p.Pen.DashStyle = RoughDashStyle;
p.Pen.Color = getColor(level, RoughAlpha);
}
}
}
#endregion

#region Input
DataSeries inputSeries = (DataSeries)c[0];
if (inputType == InputSignal.Price) inputSeries[0] = Input[0];
else if (inputType == InputSignal.Volume) inputSeries[0] = Volume[0];
else if (inputType == InputSignal.Time)
{
TimeSpan span = Bars.GetTime(CurrentBar) - Bars.GetTime(CurrentBar - 1);
inputSeries[0] = Math.Min(span.TotalMinutes, 30);
}
if (_smoothList.Contains(0)) inputSeries[0] = JMA(inputSeries, _smoothAmt, _smoothPhase)[0];
#endregion

for (int i = 0; i <= DecompositionLevel; i++)
{
if (CurrentBar < Math.Pow(2, i)) return;

((DataSeries)c[i+1])[0] = .5*(c[i][(int) Math.Pow(2, i)]+c[i][0]); //predict


if (i == 0) continue;
if (_smoothList.Contains(i)) ((DataSeries)c[i])[0] = JMA(c[i], _smoothAmt, _smoothPhase)[0];
d[i][0] = c[i-1][0]-c[i][0]; //update

if (_thresholdList.Contains(i)) d[i][0] *= .618; //naive threshold =)

if (i == 1) continue;
r[i][0] = (ShowApproximation) ? (r[i-1][0] + (c[i-1][0]-d[i][0]))/2 : r[i-1][0] + d[i][0];
}

for (int i = 0; i < _plotList.Count; i++)
{
string s = _plotList[i];
int level = int.Parse(s.Substring(1));

if (s.StartsWith("D"))
{
Values[i][0] = d[level][0];
}
else if (s.StartsWith("C"))
{
Values[i][0] = c[level][0];
}
else
{
Values[i][0] = (UseAbsValue) ? Math.Abs(r[level][0]) : r[level][0];
}
}

if (DisplayZLR) checkZLR();

}

#region Pattern Detection


//zero line reject
private void checkZLR()
{
int n = 1;
int maxLookback = Math.Min(5, CurrentBar);
DataSeries ds = d[n];

//pZLR
if (ds[z] > 0 && ds[zz] < 0)
{
int bar = MRO(delegate { return ds[0] < 0 && ds[1] > 0; }, 1, maxLookback);
if (bar < 0) return;

for(int i = 1; i <= DecompositionLevel; i++)
{
if (i >= _zlrMaxScale && d[i][z] < 0 && Falling(c[i]))
return;
}
zlr[CurrentBar] = 1;
drawSymbol(ZlrSymbolUp, CurrentBar+"-zlrUp", z, 0, _zlrColorUp);
}

//nZLR
if (ds[z] < 0 && ds[zz] > 0)
{
int bar = MRO(delegate { return ds[0] > 0 && ds[1] < 0; }, 1, maxLookback);
if (bar < 0) return;

for (int i = 1; i <= DecompositionLevel; i++)
{
if (i >= _zlrMaxScale && d[i][z] > 0 && Rising(c[i]))
return;
}
zlr[CurrentBar] = -1;
drawSymbol(ZlrSymbolDown, CurrentBar + "-zlrDn", z, 0, _zlrColorDown);
}
}


#endregion

#region Convenience Methods

public int getZLR(int bar)
{
if (zlr.ContainsKey(bar)) return zlr[bar];
return 0;
}

#endregion

#region Helper Methods

private void refreshPlotList()
{
_plotList.Clear();

if (_detailList.Contains(-1)) _detailList.Clear();
if (_detailList.Contains(0))
{
_detailList.Clear();
for (int i = 1; i <= DecompositionLevel; i++) _detailList.Add(i);
}
foreach (int i in _detailList) _plotList.Add((ShowApproximation) ? "C"+i : "D"+i);

if (_roughList.Contains(-1)) _roughList.Clear();
if (_roughList.Contains(0))
{
_roughList.Clear();
for (int i = 2; i <= DecompositionLevel; i++) _roughList.Add(i);
}
foreach (int i in _roughList) _plotList.Add((i == DecompositionLevel) ? "S"+ i : "R"+i);
}

private Color getColor(int level, int alpha)
{
return new HSLColor(
ColorL + ((ColorH - ColorL)/(DecompositionLevel-1))*level,
SaturationL + ((SaturationH - SaturationL) / (DecompositionLevel - 1))*level,
LuminosityL + ((LuminosityH - LuminosityL)/(DecompositionLevel-1))*level,
alpha);
}

private Color getColor(int level)
{
return getColor(level, 255);
}

private void drawSymbol(symbolType symbol, string tag, int barsAgo, double y, Color color)
{
if (CalculateOnBarClose)
{
barsAgo += 1;
}
Util.DrawSymbol(this, symbol, tag, barsAgo, y, color);

}
#endregion

#region Properties

[Description("")]
[Category("Parameters")]
public int DecompositionLevel
{
get { return _decompositionLevel; }
set { _decompositionLevel = value; }
}

[Description("1-N comma delimited, 0 to display all, -1 to display none")]
[Category("Optional Parameters")]
public string DisplayDetails
{
get { return _detailListInput; }
set { _detailListInput = value; }
}

[Description("1-N comma delimited, 0 to display all, -1 to display none")]
[Category("Optional Parameters")]
public string DisplayRough
{
get { return _roughListInput; }
set { _roughListInput = value; }
}

[Description("")]
[Category("Parameters")]
public InputSignal InputType
{
get { return inputType; }
set { inputType = value; }
}
[Description("0 to smooth input signal, 1-N comma delimited to smooth coefficients")]
[Category("Optional Parameters")]
public string SmoothedDetails
{
get { return _smoothListInput; }
set { _smoothListInput = value; }
}
[Description("1-N comma delimited to threshold coefficients (generally only useful on the first few levels)")]
[Category("Optional Parameters")]
public string ThresholdDetails
{
get { return _thresholdListInput; }
set { _thresholdListInput = value; }
}

[Description("")]
[Category("Optional Parameters")]
public int SmoothAmt
{
get { return _smoothAmt; }
set { _smoothAmt = value; }
}
[Description("")]
[Category("Optional Parameters")]
public int SmoothPhase
{
get { return _smoothPhase; }
set { _smoothPhase = value; }
}
[Description("[0, 1]")]
[Category("Visual")]
public double ColorL
{
get { return _colorL; }
set { _colorL = value; }
}

[Description("[0, 1]")]
[Category("Visual")]
public double ColorH
{
get { return _colorH; }
set { _colorH = value; }
}
[Description("[0, 1]")]
[Category("Visual")]
public double SaturationL
{
get { return _saturationL; }
set { _saturationL = value; }
}

[Description("[0, 1]")]
[Category("Visual")]
public double SaturationH
{
get { return _saturationH; }
set { _saturationH = value; }
}
[Description("[0, 1]")]
[Category("Visual")]
public double LuminosityL
{
get { return _luminosityL; }
set { _luminosityL = value; }
}

[Description("[0, 1]")]
[Category("Visual")]
public double LuminosityH
{
get { return _luminosityH; }
set { _luminosityH = value; }
}

[Description("Enable/Disable auto color")]
[Category("Visual")]
public bool AutoColorEnabled
{
get { return _enableAutoColor; }
set { _enableAutoColor = value; }
}

[Description("Show Approximation rather than Detail Coefficients")]
[Category("Parameters")]
public bool ShowApproximation
{
get { return _showApproximation; }
set { _showApproximation = value; }
}

[Description("Apply Math.Abs() to wavelet rough (only valid in detail mode)")]
[Category("Optional Parameters")]
public bool UseAbsValue
{
get { return _useAbsValue; }
set { _useAbsValue = value; }
}

[Description("")]
[Category("ZLR Parameters")]
public bool DisplayZLR
{
get { return _showZLR; }
set { _showZLR = value; }
}

[Description("")]
[Category("ZLR Parameters")]
[XmlIgnore, VisualizationOnly]
public Color ZLRColorUp
{
get { return _zlrColorUp; }
set { _zlrColorUp = value; }
}

[Browsable(false)]
public string ZLRColorUpSerializer
{
get { return SerializableColor.ToString(_zlrColorUp); }
set { _zlrColorUp = SerializableColor.FromString(value); }
}

[Description("")]
[Category("ZLR Parameters")]
[XmlIgnore, VisualizationOnly]
public Color ZLRColorDown
{
get { return _zlrColorDown; }
set { _zlrColorDown = value; }
}

[Browsable(false)]
public string ZLRColorDownSerializer
{
get { return SerializableColor.ToString(_zlrColorDown); }
set { _zlrColorDown = SerializableColor.FromString(value); }
}

[Description("Max scale that can cross without invalidating ZLR setup")]
[Category("ZLR Parameters")]
public int ZLRMaxScale
{
get { return _zlrMaxScale; }
set { _zlrMaxScale = value; }
}

[Description("")]
[Category("Visual")]
[Gui.Design.DisplayName("y DetailPenWidth")]
public float DetailPenWidth
{
get { return _detailPenWidth; }
set { _detailPenWidth = value; }
}

[Description("")]
[Category("Visual")]
[Gui.Design.DisplayName("y DetailDashStyle")]
public DashStyle DetailDashStyle
{
get { return _detailDashStyle; }
set { _detailDashStyle = value; }
}

[Description("")]
[Category("Visual")]
[Gui.Design.DisplayName("z RoughPenWidth")]
public float RoughPenWidth
{
get { return _roughPenWidth; }
set { _roughPenWidth = value; }
}

[Description("")]
[Category("Visual")]
[Gui.Design.DisplayName("z RoughDashStyle")]
public DashStyle RoughDashStyle
{
get { return _roughDashStyle; }
set { _roughDashStyle = value; }
}

[Description("")]
[Category("Visual")]
[Gui.Design.DisplayName("y DetailAlpha")]
public int DetailAlpha
{
get { return _detailAlpha; }
set { _detailAlpha = value; }
}

[Description("")]
[Category("Visual")]
[Gui.Design.DisplayName("z RoughAlpha")]
public int RoughAlpha
{
get { return _roughAlpha; }
set { _roughAlpha = value; }
}

[Description("")]
[Category("Visual")]
[Gui.Design.DisplayName("y DetailPlotStyle")]
public PlotStyle DetailPlotStyle
{
get { return _detailPlotStyle; }
set { _detailPlotStyle = value; }
}

[Description("")]
[Category("Visual")]
[Gui.Design.DisplayName("z RoughPlotStyle")]
public PlotStyle RoughPlotStyle
{
get { return _roughPlotStyle; }
set { _roughPlotStyle = value; }
}

[Description("")]
[Category("ZLR Parameters")]
public symbolType ZlrSymbolUp
{
get { return _zlrSymbolUp; }
set { _zlrSymbolUp = value; }
}
[Description("")]
[Category("ZLR Parameters")]
public symbolType ZlrSymbolDown
{
get { return _zlrSymbolDown; }
set { _zlrSymbolDown = value; }
}
#endregion
}
}
#region NinjaScript generated code. Neither change nor remove.
// This namespace holds all indicators and is required. Do not change it.
namespace NT.Indicator
{
public partial class Indicator : IndicatorBase
{
private d9HaarWavelet[] cached9HaarWavelet = null;

private static d9HaarWavelet checkd9HaarWavelet = new d9HaarWavelet();

/// <summary>
/// Undecimated Haar Wavelet Transform implemented via Lifting
/// </summary>
/// <returns></returns>
public d9HaarWavelet d9HaarWavelet(int decompositionLevel, InputSignal inputType, bool showApproximation)
{
return d9HaarWavelet(Input, decompositionLevel, inputType, showApproximation);
}

/// <summary>
/// Undecimated Haar Wavelet Transform implemented via Lifting
/// </summary>
/// <returns></returns>
public d9HaarWavelet d9HaarWavelet(Data.IDataSeries input, int decompositionLevel, InputSignal inputType, bool showApproximation)
{
if (cached9HaarWavelet != null)
for (int idx = 0; idx < cached9HaarWavelet.Length; idx++)
if (cached9HaarWavelet[idx].DecompositionLevel == decompositionLevel && cached9HaarWavelet[idx].InputType == inputType && cached9HaarWavelet[idx].ShowApproximation == showApproximation && cached9HaarWavelet[idx].EqualsInput(input))
return cached9HaarWavelet[idx];

lock (checkd9HaarWavelet)
{
checkd9HaarWavelet.DecompositionLevel = decompositionLevel;
decompositionLevel = checkd9HaarWavelet.DecompositionLevel;
checkd9HaarWavelet.InputType = inputType;
inputType = checkd9HaarWavelet.InputType;
checkd9HaarWavelet.ShowApproximation = showApproximation;
showApproximation = checkd9HaarWavelet.ShowApproximation;

if (cached9HaarWavelet != null)
for (int idx = 0; idx < cached9HaarWavelet.Length; idx++)
if (cached9HaarWavelet[idx].DecompositionLevel == decompositionLevel && cached9HaarWavelet[idx].InputType == inputType && cached9HaarWavelet[idx].ShowApproximation == showApproximation && cached9HaarWavelet[idx].EqualsInput(input))
return cached9HaarWavelet[idx];

d9HaarWavelet indicator = new d9HaarWavelet();
indicator.BarsRequired = BarsRequired;
indicator.CalculateOnBarClose = CalculateOnBarClose;
#if NT7
indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
indicator.MaximumBarsLookBack = MaximumBarsLookBack;
#endif
indicator.Input = input;
indicator.DecompositionLevel = decompositionLevel;
indicator.InputType = inputType;
indicator.ShowApproximation = showApproximation;
Indicators.Add(indicator);
indicator.SetUp();

d9HaarWavelet[] tmp = new d9HaarWavelet[cached9HaarWavelet == null ? 1 : cached9HaarWavelet.Length + 1];
if (cached9HaarWavelet != null)
cached9HaarWavelet.CopyTo(tmp, 0);
tmp[tmp.Length - 1] = indicator;
cached9HaarWavelet = tmp;
return indicator;
}
}
}
}

// This namespace holds all market analyzer column definitions and is required. Do not change it.
namespace NT.MarketAnalyzer
{
public partial class Column : ColumnBase
{
/// <summary>
/// Undecimated Haar Wavelet Transform implemented via Lifting
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.d9HaarWavelet d9HaarWavelet(int decompositionLevel, InputSignal inputType, bool showApproximation)
{
return _indicator.d9HaarWavelet(Input, decompositionLevel, inputType, showApproximation);
}

/// <summary>
/// Undecimated Haar Wavelet Transform implemented via Lifting
/// </summary>
/// <returns></returns>
public Indicator.d9HaarWavelet d9HaarWavelet(Data.IDataSeries input, int decompositionLevel, InputSignal inputType, bool showApproximation)
{
return _indicator.d9HaarWavelet(input, decompositionLevel, inputType, showApproximation);
}
}
}

// This namespace holds all strategies and is required. Do not change it.
namespace NT.Strategy
{
public partial class Strategy : StrategyBase
{
/// <summary>
/// Undecimated Haar Wavelet Transform implemented via Lifting
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.d9HaarWavelet d9HaarWavelet(int decompositionLevel, InputSignal inputType, bool showApproximation)
{
return _indicator.d9HaarWavelet(Input, decompositionLevel, inputType, showApproximation);
}

/// <summary>
/// Undecimated Haar Wavelet Transform implemented via Lifting
/// </summary>
/// <returns></returns>
public Indicator.d9HaarWavelet d9HaarWavelet(Data.IDataSeries input, int decompositionLevel, InputSignal inputType, bool showApproximation)
{
if (InInitialize && input == null)
throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

return _indicator.d9HaarWavelet(input, decompositionLevel, inputType, showApproximation);
}
}
}
#endregion

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: NT trader indicator translated to easylanguage

Postby TJ » 29 Mar 2013

Hi @,
I found this indicator below on the NT forum, and I'm trying to convert this to easylanguage, but my C# knowledge is very poor.
Can this be done? Or can it only be done in easylanguage via a dll?
The OnBarUpdate part is the actual haar wavelet lifting, right?
Thanks,
Simon
[/code]
It would help if you have a description of what this indicator does, and the logic behind it.

It would also help if you can post a chart sample.

simon007
Posts: 60
Joined: 12 Apr 2007
Has thanked: 7 times
Been thanked: 1 time

Re: NT trader indicator translated to easylanguage

Postby simon007 » 29 Mar 2013

Hi TJ,

This indicator does a haar wavelet transform, so it calculates the wavelet coefficients and reconstructs the signal. I've attached a printscreen.

Thanks,

Simon
Attachments
wavelet.jpg
(85.29 KiB) Downloaded 854 times

SUPER
Posts: 646
Joined: 03 Mar 2007
Has thanked: 106 times
Been thanked: 84 times

Re: NT trader indicator translated to easylanguage

Postby SUPER » 29 Mar 2013

Hi TJ,

This indicator does a haar wavelet transform, so it calculates the wavelet coefficients and reconstructs the signal. I've attached a printscreen.

Thanks,

Simon

Simon,

Don't waste your time with dll conversion ....many years back I bought it from a company called http://www.tsresearch.com/ and subsequently found a simple EL version on TS forum, which does exactly same.

Attached find EL file.
Attachments
_GHW.ELD
(2.53 KiB) Downloaded 670 times

simon007
Posts: 60
Joined: 12 Apr 2007
Has thanked: 7 times
Been thanked: 1 time

Re: NT trader indicator translated to easylanguage

Postby simon007 » 29 Mar 2013

Hi Super,

Thanks a lot for the code. I'll have a look at it.

Cheers,

Simon


Return to “MultiCharts”