Swing Indicator

M333

New member
Joined
Jan 6, 2020
Posts
2
Likes
0
Hello.

NinjaTrader has the following Swing Indicator that I would like on MotiveWave.

Here is the NT code:-


public class Swing : Indicator
{
#region Variables
private double currentSwingHigh = 0;
private double currentSwingLow = 0;
private ArrayList lastHighCache;
private double lastSwingHighValue = 0;
private ArrayList lastLowCache;
private double lastSwingLowValue = 0;
private int saveCurrentBar = -1;
private int strength = 20;
private DataSeries swingHighSeries;
private DataSeries swingHighSwings;
private DataSeries swingLowSeries;
private DataSeries swingLowSwings;
#endregion
/// This method is used to configure the indicator and is called once before any bar data is loaded.

protected override void Initialize()
{
Add(new Plot(Color.Green, PlotStyle.Dot, "Swing high"));
Add(new Plot(Color.Orange, PlotStyle.Dot, "Swing low"));
Plots[0].Pen.Width = 2;
Plots[0].Pen.DashStyle = DashStyle.Dot;
Plots[1].Pen.Width = 2;
Plots[1].Pen.DashStyle = DashStyle.Dot;

lastHighCache = new ArrayList();
lastLowCache = new ArrayList();
swingHighSeries = new DataSeries(this);
swingHighSwings = new DataSeries(this);
swingLowSeries = new DataSeries(this);
swingLowSwings = new DataSeries(this);
DisplayInDataBox = false;
PaintPriceMarkers = false;
Overlay = true;
}
/// Called on each bar update event (incoming tick)

protected override void OnBarUpdate()
{
if (saveCurrentBar != CurrentBar)
{
swingHighSwings.Set(0); // initializing important internal
swingLowSwings.Set(0); // initializing important internal
swingHighSeries.Set(0); // initializing important internal
swingLowSeries.Set(0); // initializing important internal
lastHighCache.Add(High[0]);
if (lastHighCache.Count > (2 * strength) + 1)
lastHighCache.RemoveAt(0);
lastLowCache.Add(Low[0]);
if (lastLowCache.Count > (2 * strength) + 1)
lastLowCache.RemoveAt(0);
if (lastHighCache.Count == (2 * strength) + 1)
{
bool isSwingHigh = true;
double swingHighCandidateValue = (double) lastHighCache[strength];
for (int i=0; i < strength; i++)
if ((double) lastHighCache >= swingHighCandidateValue - double.Epsilon)
isSwingHigh = false;
for (int i=strength+1; i < lastHighCache.Count; i++)
if ((double) lastHighCache > swingHighCandidateValue - double.Epsilon)
isSwingHigh = false;
swingHighSwings.Set(strength, isSwingHigh ? swingHighCandidateValue : 0.0);
if (isSwingHigh)
lastSwingHighValue = swingHighCandidateValue;

if (isSwingHigh)
{
currentSwingHigh = swingHighCandidateValue;
for (int i=0; i <= strength; i++)
SwingHighPlot.Set(i, currentSwingHigh);
}
else if (High[0] > currentSwingHigh)
{
currentSwingHigh = 0.0;
SwingHighPlot.Reset();
}
else
SwingHighPlot.Set(currentSwingHigh);
if (isSwingHigh)
{
for (int i=0; i<=strength; i++)
swingHighSeries.Set(i, lastSwingHighValue);
}
else
{
swingHighSeries.Set(lastSwingHighValue);
}
}
if (lastLowCache.Count == (2 * strength) + 1)
{
bool isSwingLow = true;
double swingLowCandidateValue = (double) lastLowCache[strength];
for (int i=0; i < strength; i++)
if ((double) lastLowCache <= swingLowCandidateValue + double.Epsilon)
isSwingLow = false;
for (int i=strength+1; i < lastLowCache.Count; i++)
if ((double) lastLowCache < swingLowCandidateValue + double.Epsilon)
isSwingLow = false;
swingLowSwings.Set(strength, isSwingLow ? swingLowCandidateValue : 0.0);
if (isSwingLow)
lastSwingLowValue = swingLowCandidateValue;
if (isSwingLow)
{
currentSwingLow = swingLowCandidateValue;
for (int i=0; i <= strength; i++)
SwingLowPlot.Set(i, currentSwingLow);
}
else if (Low[0] < currentSwingLow)
{
currentSwingLow = double.MaxValue;
SwingLowPlot.Reset();
}
else
SwingLowPlot.Set(currentSwingLow);
if (isSwingLow)
{
for (int i=0; i<=strength; i++)
swingLowSeries.Set(i, lastSwingLowValue);
}
else
{
swingLowSeries.Set(lastSwingLowValue);
}
}
saveCurrentBar = CurrentBar;
}
else
{
if (High[0] > High[strength] && swingHighSwings[strength] > 0.0)
{
swingHighSwings.Set(strength, 0.0);
for (int i=0; i<=strength; i++)
SwingHighPlot.Reset(i);
currentSwingHigh = 0.0;
}
else if (High[0] > High[strength] && currentSwingHigh != 0.0)
{
SwingHighPlot.Reset();
currentSwingHigh = 0.0;
}
else if (High[0] <= currentSwingHigh)
SwingHighPlot.Set(currentSwingHigh);
if (Low[0] < Low[strength] && swingLowSwings[strength] > 0.0)
{
swingLowSwings.Set(strength, 0.0);
for (int i=0; i<=strength; i++)
SwingLowPlot.Reset(i);
currentSwingLow = double.MaxValue;
}
else if (Low[0] < Low[strength] && currentSwingLow != double.MaxValue)
{
SwingLowPlot.Reset();
currentSwingLow = double.MaxValue;
}
else if (Low[0] >= currentSwingLow)
SwingLowPlot.Set(currentSwingLow);
}
}
#region Functions

/// Returns the number of bars ago a swing low occurred. Returns a value of -1 if a swing low is not found within the look back period.

/// <param name="barsAgo"></param>
/// <param name="instance"></param>
/// <param name="lookBackPeriod"></param>
/// <returns></returns>
public int SwingLowBar(int barsAgo, int instance, int lookBackPeriod)
{
if (instance < 1)
throw new Exception(GetType().Name + ".SwingLowBar: instance must be greater/equal 1 but was " + instance);
else if (barsAgo < 0)
throw new Exception(GetType().Name + ".SwingLowBar: barsAgo must be greater/equal 0 but was " + barsAgo);
else if (barsAgo >= Count)
throw new Exception(GetType().Name + ".SwingLowBar: barsAgo out of valid range 0 through " + (Count - 1) + ", was " + barsAgo + ".");
Update();
for (int idx=CurrentBar - barsAgo - strength; idx >= CurrentBar - barsAgo - strength - lookBackPeriod; idx--)
{
if (idx < 0)
return -1;
if (idx >= swingLowSwings.Count)
continue;
if (swingLowSwings.Get(idx).Equals(0.0))
continue;
if (instance == 1) // 1-based, < to be save
return CurrentBar - idx;
instance--;
}

return -1;
}


/// Returns the number of bars ago a swing high occurred. Returns a value of -1 if a swing high is not found within the look back period.

/// <param name="barsAgo"></param>
/// <param name="instance"></param>
/// <param name="lookBackPeriod"></param>
/// <returns></returns>
public int SwingHighBar(int barsAgo, int instance, int lookBackPeriod)
{
if (instance < 1)
throw new Exception(GetType().Name + ".SwingHighBar: instance must be greater/equal 1 but was " + instance);
else if (barsAgo < 0)
throw new Exception(GetType().Name + ".SwingHighBar: barsAgo must be greater/equal 0 but was " + barsAgo);
else if (barsAgo >= Count)
throw new Exception(GetType().Name + ".SwingHighBar: barsAgo out of valid range 0 through " + (Count - 1) + ", was " + barsAgo + ".");
Update();
for (int idx=CurrentBar - barsAgo - strength; idx >= CurrentBar - barsAgo - strength - lookBackPeriod; idx--)
{
if (idx < 0)
return -1;
if (idx >= swingHighSwings.Count)
continue;
if (swingHighSwings.Get(idx).Equals(0.0))
continue;
if (instance <= 1) // 1-based, < to be save
return CurrentBar - idx;
instance--;
}

return -1;
}
#endregion
#region Properties
[Description("Number of bars required on each side of the swing point.")]
[GridCategory("Parameters")]
public int Strength
{
get { return strength; }
set { strength = Math.Max(1, value); }
}

/// Gets the high swings.
/// </summary>
[Browsable(false)]
[XmlIgnore()]
public DataSeries SwingHigh
{
get
{
Update();
return swingHighSeries;
}
}
private DataSeries SwingHighPlot
{
get
{
Update();
return Values[0];
}
}

/// Gets the low swings.

[Browsable(false)]
[XmlIgnore()]
public DataSeries SwingLow
{
get
{
Update();
return swingLowSeries;
}
}
private DataSeries SwingLowPlot
{
get
{
Update();
return Values[1];
}
}
#endregion



and an image attached.

Many thx
 

Attachments

  • NT Swing Indicator.JPG
    NT Swing Indicator.JPG
    21.5 KB · Views: 29

MotiveWave_Jason

Moderator
Staff member
Joined
Mar 26, 2019
Posts
224
Likes
101
Hello,

I have added this to out feature request list.

Thank you for the code.

Regards.
 

Spin

Well-known member
Joined
May 22, 2019
Posts
472
Likes
188
Coded :)

Coming to a MW-Marketplace near you soon !

167
 

Spin

Well-known member
Joined
May 22, 2019
Posts
472
Likes
188
The coding was easy, compared to building a website & getting organised to appear in MW's Marketplace.

Anyway: live in a couple of days, so watch this space 🥳
 

Johnedoe

Well-known member
Joined
Feb 29, 2020
Posts
82
Likes
10
Hey Spin......
Can you fix the Trade Manager for me?
Jason sent me the file and I can forward it to you.
 

Spin

Well-known member
Joined
May 22, 2019
Posts
472
Likes
188
Hello Johnedoe,

I am not sure I can 'fix' that Trade Manager for you, but I am totally willing to look at it to see if I can be of assistance :)

How about you PM me with the code, tell me what is 'wrong' with it and relate me what you expect from it.

I sincerely hope I can help !
 
Top