Thank you.
//
// Copyright (C) 2018, NinjaTrader LLC <www.ninjatrader.com>.
// NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
//
#region Using declarations
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion
//This namespace holds indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
/// <summary>
/// The Swing indicator plots lines that represents the swing high and low points.
/// </summary>
public class Swing : Indicator
{
private int constant;
private double currentSwingHigh;
private double currentSwingLow;
private ArrayList lastHighCache;
private double lastSwingHighValue;
private ArrayList lastLowCache;
private double lastSwingLowValue;
private int saveCurrentBar;
private Series<double> swingHighSeries;
private Series<double> swingHighSwings;
private Series<double> swingLowSeries;
private Series<double> swingLowSwings;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDe scriptionSwing;
Name = NinjaTrader.Custom.Resource.NinjaScriptIndicatorNa meSwing;
DisplayInDataBox = false;
PaintPriceMarkers = false;
IsSuspendedWhileInactive = true;
IsOverlay = true;
Strength = 5;
AddPlot(new Stroke(Brushes.DarkCyan, 2), PlotStyle.Dot, NinjaTrader.Custom.Resource.SwingHigh);
AddPlot(new Stroke(Brushes.Goldenrod, 2), PlotStyle.Dot, NinjaTrader.Custom.Resource.SwingLow);
}
else if (State == State.Configure)
{
currentSwingHigh = 0;
currentSwingLow = 0;
lastSwingHighValue = 0;
lastSwingLowValue = 0;
saveCurrentBar = -1;
constant = (2 * Strength) + 1;
}
else if (State == State.DataLoaded)
{
lastHighCache = new ArrayList();
lastLowCache = new ArrayList();
swingHighSeries = new Series<double>(this);
swingHighSwings = new Series<double>(this);
swingLowSeries = new Series<double>(this);
swingLowSwings = new Series<double>(this);
}
}
protected override void OnBarUpdate()
{
double high0 = (Input is Indicator || Input is Series<double>) ? Input[0] : High[0];
double low0 = (Input is Indicator || Input is Series<double>) ? Input[0] : Low[0];
double close0 = (Input is Indicator || Input is Series<double>) ? Input[0] : Close[0];
double highStrength = High[Math.Min(Strength, CurrentBar)];
double lowStrength = Low[Math.Min(Strength, CurrentBar)];
if (BarsArray[0].BarsType.IsRemoveLastBarSupported && CurrentBar < saveCurrentBar)
{
currentSwingHigh = SwingHighPlot.IsValidDataPoint(0) ? SwingHighPlot[0] : 0;
currentSwingLow = SwingLowPlot.IsValidDataPoint(0) ? SwingLowPlot[0] : 0;
lastSwingHighValue = swingHighSeries[0];
lastSwingLowValue = swingLowSeries[0];
swingHighSeries[Strength] = 0;
swingLowSeries[Strength] = 0;
lastHighCache.Clear();
lastLowCache.Clear();
for (int barsBack = Math.Min(CurrentBar, constant) - 1; barsBack >= 0; barsBack--)
{
lastHighCache.Add((Input is Indicator || Input is Series<double>) ? Input[barsBack] : High[barsBack]);
lastLowCache.Add((Input is Indicator || Input is Series<double>) ? Input[barsBack] : Low[barsBack]);
}
saveCurrentBar = CurrentBar;
return;
}
if (saveCurrentBar != CurrentBar)
{
swingHighSwings[0] = 0; // initializing important internal
swingLowSwings[0] = 0; // initializing important internal
swingHighSeries[0] = 0; // initializing important internal
swingLowSeries[0] = 0; // initializing important internal
lastHighCache.Add(high0);
if (lastHighCache.Count > constant)
lastHighCache.RemoveAt(0);
lastLowCache.Add(low0);
if (lastLowCache.Count > constant)
lastLowCache.RemoveAt(0);
if (lastHighCache.Count == constant)
{
bool isSwingHigh = true;
double swingHighCandidateValue = (double) lastHighCache[Strength];
for (int i=0; i < Strength; i++)
if (((double) lastHighCache[i]).ApproxCompare(swingHighCandidateValue) >= 0)
isSwingHigh = false;
for (int i=Strength+1; i < lastHighCache.Count; i++)
if (((double) lastHighCache[i]).ApproxCompare(swingHighCandidateValue) > 0)
isSwingHigh = false;
swingHighSwings[Strength] = isSwingHigh ? swingHighCandidateValue : 0.0;
if (isSwingHigh)
lastSwingHighValue = swingHighCandidateValue;
if (isSwingHigh)
{
currentSwingHigh = swingHighCandidateValue;
for (int i=0; i <= Strength; i++)
SwingHighPlot[i] = currentSwingHigh;
}
else if (high0 > currentSwingHigh || currentSwingHigh.ApproxCompare(0.0) == 0)
{
currentSwingHigh = 0.0;
SwingHighPlot[0] = close0;
SwingHighPlot.Reset();
}
else
SwingHighPlot[0] = currentSwingHigh;
if (isSwingHigh)
{
for (int i=0; i<=Strength; i++)
swingHighSeries[i] = lastSwingHighValue;
}
else
{
swingHighSeries[0] = lastSwingHighValue;
}
}
if (lastLowCache.Count == constant)
{
bool isSwingLow = true;
double swingLowCandidateValue = (double) lastLowCache[Strength];
for (int i=0; i < Strength; i++)
if (((double) lastLowCache[i]).ApproxCompare(swingLowCandidateValue) <= 0)
isSwingLow = false;
for (int i=Strength+1; i < lastLowCache.Count; i++)
if (((double) lastLowCache[i]).ApproxCompare(swingLowCandidateValue) < 0)
isSwingLow = false;
swingLowSwings[Strength] = isSwingLow ? swingLowCandidateValue : 0.0;
if (isSwingLow)
lastSwingLowValue = swingLowCandidateValue;
if (isSwingLow)
{
currentSwingLow = swingLowCandidateValue;
for (int i=0; i <= Strength; i++)
SwingLowPlot[i] = currentSwingLow;
}
else if (low0 < currentSwingLow || currentSwingLow.ApproxCompare(0.0) == 0)
{
currentSwingLow = double.MaxValue;
SwingLowPlot[0] = close0;
SwingLowPlot.Reset();
}
else
SwingLowPlot[0] = currentSwingLow;
if (isSwingLow)
{
for (int i=0; i<=Strength; i++)
swingLowSeries[i] = lastSwingLowValue;
}
else
{
swingLowSeries[0] = lastSwingLowValue;
}
}
saveCurrentBar = CurrentBar;
}
else if (CurrentBar >= constant - 1)
{
if (high0 > highStrength && swingHighSwings[Strength] > 0.0)
{
swingHighSwings[Strength] = 0.0;
for (int i = 0; i <= Strength; i++)
{
SwingHighPlot[i] = close0;
SwingHighPlot.Reset(i);
currentSwingHigh = 0.0;
}
}
else if (high0 > highStrength && currentSwingHigh.ApproxCompare(0.0) != 0)
{
SwingHighPlot[0] = close0;
SwingHighPlot.Reset();
currentSwingHigh = 0.0;
}
else if (high0 <= currentSwingHigh)
SwingHighPlot[0] = currentSwingHigh;
if (low0 < lowStrength && swingLowSwings[Strength] > 0.0)
{
swingLowSwings[Strength] = 0.0;
for (int i = 0; i <= Strength; i++)
{
SwingLowPlot[i] = close0;
SwingLowPlot.Reset(i);
currentSwingLow = double.MaxValue;
}
}
else if (low0 < lowStrength && currentSwingLow.ApproxCompare(double.MaxValue) != 0)
{
SwingLowPlot.Reset();
currentSwingLow = double.MaxValue;
}
else if (low0 >= currentSwingLow)
SwingLowPlot[0] = currentSwingLow;
}
Comment