Description
- The Swing indicator plots lines that represents the swing high and low points.
- The Indicator was converted from NinjaTrader script to cAlgo.
Updates
- 31/01/2016 - Released.
Inputs
- High source - The source of High DataSeries.
- Low source - The source of Low DataSeries.
- Strength - Number of bars required on each side of the swing point.
Screenshot
Make a Donation
- If you like my work and effort then please consider to make a kind donation thru PayPal or any Credit Card at the top right corner.
#region Using declarations
using System;
using System.Collections;
using cAlgo.API;
#endregion
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class Swing : Indicator
{
#region Properties
[Parameter("High source")]
public DataSeries High { get; set; }
[Parameter("Low source")]
public DataSeries Low { get; set; }
[Parameter("Strength", DefaultValue = 5, MinValue = 1)]
public int strength { get; set; }
[Output("Swing high", Color = Colors.Green, Thickness = 4, PlotType = PlotType.Points)]
public IndicatorDataSeries SwingHighPlot { get; set; }
[Output("Swing low", Color = Colors.Orange, Thickness = 4, PlotType = PlotType.Points)]
public IndicatorDataSeries SwingLowPlot { get; set; }
#endregion
#region Variables
private double currentSwingHigh = 0;
private double currentSwingLow = 0;
private double lastSwingHighValue = 0;
private double lastSwingLowValue = 0;
private int CurrentBar, Count;
private int saveCurrentBar = -1;
private ArrayList lastHighCache, lastLowCache;
private IndicatorDataSeries swingHighSeries, swingHighSwings, swingLowSeries, swingLowSwings;
#endregion
protected override void Initialize()
{
lastHighCache = new ArrayList();
lastLowCache = new ArrayList();
swingHighSeries = CreateDataSeries();
swingHighSwings = CreateDataSeries();
swingLowSeries = CreateDataSeries();
swingLowSwings = CreateDataSeries();
}
public override void Calculate(int index)
{
CurrentBar = index;
Count = High.Count;
if (saveCurrentBar != CurrentBar)
{
swingHighSwings[index] = 0;
swingLowSwings[index] = 0;
swingHighSeries[index] = 0;
swingLowSeries[index] = 0;
lastHighCache.Add(High.Last(0));
if (lastHighCache.Count > (2 * strength) + 1)
lastHighCache.RemoveAt(0);
lastLowCache.Add(Low.Last(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[i] >= swingHighCandidateValue - double.Epsilon)
isSwingHigh = false;
for (int i = strength + 1; i < lastHighCache.Count; i++)
if ((double)lastHighCache[i] > swingHighCandidateValue - double.Epsilon)
isSwingHigh = false;
swingHighSwings[index - strength] = isSwingHigh ? swingHighCandidateValue : 0.0;
if (isSwingHigh)
lastSwingHighValue = swingHighCandidateValue;
if (isSwingHigh)
{
currentSwingHigh = swingHighCandidateValue;
for (int i = 0; i <= strength; i++)
SwingHighPlot[index - i] = currentSwingHigh;
}
else if (High.Last(0) > currentSwingHigh)
{
currentSwingHigh = 0.0;
SwingHighPlot[index] = double.NaN;
}
else
SwingHighPlot[index] = currentSwingHigh;
if (isSwingHigh)
{
for (int i = 0; i <= strength; i++)
swingHighSeries[index - i] = lastSwingHighValue;
}
else
{
swingHighSeries[index] = 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[i] <= swingLowCandidateValue + double.Epsilon)
isSwingLow = false;
for (int i = strength + 1; i < lastLowCache.Count; i++)
if ((double)lastLowCache[i] < swingLowCandidateValue + double.Epsilon)
isSwingLow = false;
swingLowSwings[index - strength] = isSwingLow ? swingLowCandidateValue : 0.0;
if (isSwingLow)
lastSwingLowValue = swingLowCandidateValue;
if (isSwingLow)
{
currentSwingLow = swingLowCandidateValue;
for (int i = 0; i <= strength; i++)
SwingLowPlot[index - i] = currentSwingLow;
}
else if (Low.Last(0) < currentSwingLow)
{
currentSwingLow = double.MaxValue;
SwingLowPlot[index] = double.NaN;
}
else
SwingLowPlot[index] = currentSwingLow;
if (isSwingLow)
{
for (int i = 0; i <= strength; i++)
swingLowSeries[index - i] = lastSwingLowValue;
}
else
{
swingLowSeries[index] = lastSwingLowValue;
}
}
saveCurrentBar = CurrentBar;
}
else
{
if (High.Last(0) > High.Last(strength) && swingHighSwings.Last(strength) > 0.0)
{
swingHighSwings[index - strength] = 0.0;
for (int i = 0; i <= strength; i++)
SwingHighPlot[index - i] = double.NaN;
currentSwingHigh = 0.0;
}
else if (High.Last(0) > High.Last(strength) && currentSwingHigh != 0.0)
{
SwingHighPlot[index] = double.NaN;
currentSwingHigh = 0.0;
}
else if (High.Last(0) <= currentSwingHigh)
SwingHighPlot[index] = currentSwingHigh;
if (Low.Last(0) < Low.Last(strength) && swingLowSwings.Last(strength) > 0.0)
{
swingLowSwings[index - strength] = 0.0;
for (int i = 0; i <= strength; i++)
SwingLowPlot[index - i] = double.NaN;
currentSwingLow = double.MaxValue;
}
else if (Low.Last(0) < Low.Last(strength) && currentSwingLow != double.MaxValue)
{
SwingLowPlot[index] = double.NaN;
currentSwingLow = double.MaxValue;
}
else if (Low.Last(0) >= currentSwingLow)
SwingLowPlot[index] = currentSwingLow;
}
}
}
}
whis.gg
Joined on 31.08.2015
- Distribution: Free
- Language: C#
- Trading platform: cTrader Automate
- File name: Swing.algo
- Rating: 4.29
- Installs: 5629
- Modified: 13/10/2021 09:55
Note that publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section, please use the Copyright Infringement Notification form to submit a claim.
Comments
Log in to add a comment.
LI
Download link will not work, I have donated.
HE
Hi tcm, you can change PlotType.Points, for a tag with the price. thanks
NE
Hello tmc,
thanks for your work. could you please confirm how I can use the swing high and low values, that is the plot levels in this swing trade from within a cbot-robot.
thank you very much
Test comment #1