Here's an ACSIL script for a ZigZag indicator in Sierra Chart:
code
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using SierraChart;
namespace SierraChartACSIL
{
public class ZigZagIndicator : IndicatorBase
{
private SCStudyGraphic zigZagGraphic;
private int deviation;
protected override void OnInitialize()
{
deviation = GetSettingInt("Deviation", 12);
zigZagGraphic = AddStudyGraphic(0, "ZigZag");
zigZagGraphic.PrimaryColor = Color.Blue;
zigZagGraphic.LineStyle = LineStyle.Solid;
zigZagGraphic.LineWidth = 2;
}
protected override void OnBarUpdate()
{
if (Bars.BarNumber < 2)
return;
double high = Bars.High.GetValueAt(0);
double low = Bars.Low.GetValueAt(0);
double lastHigh = Bars.High.GetValueAt(1);
double lastLow = Bars.Low.GetValueAt(1);
if (high > lastHigh && (high - lastLow) / lastLow * 100 >= deviation)
{
zigZagGraphic.AddPoint(Bars.DateTimes[1], lastLow);
zigZagGraphic.AddPoint(Bars.DateTimes[0], high);
}
else if (low < lastLow && (lastHigh - low) / low * 100 >= deviation)
{
zigZagGraphic.AddPoint(Bars.DateTimes[1], lastHigh);
zigZagGraphic.AddPoint(Bars.DateTimes[0], low);
}
}
}
}
This script defines a ZigZagIndicator class that inherits from IndicatorBase. Here's how it works:
In the OnInitialize method:
We retrieve the "Deviation" setting value using GetSettingInt. This setting determines the minimum percentage change required for a point to be considered a swing high or low.
We add a study graphic named "ZigZag" using AddStudyGraphic and customize its appearance (color, line style, and line width).
In the OnBarUpdate method, which is called for each new bar:
We check if there are at least two bars available. If not, we return.
We retrieve the current bar's high and low prices using Bars.High.GetValueAt(0) and Bars.Low.GetValueAt(0), respectively.
We retrieve the previous bar's high and low prices using Bars.High.GetValueAt(1) and Bars.Low.GetValueAt(1), respectively.
We check if the current bar's high is greater than the previous bar's high and if the percentage change from the previous bar's low to the current bar's high is greater than or equal to the specified deviation. If true, we add two points to the ZigZag graphic: the previous bar's low and the current bar's high.
Similarly, we check if the current bar's low is less than the previous bar's low and if the percentage change from the previous bar's high to the current bar's low is greater than or equal to the specified deviation. If true, we add two points to the ZigZag graphic: the previous bar's high and the current bar's low.
This script will draw a ZigZag line on the chart, connecting the swing highs and lows based on the specified deviation percentage.
Note: Make sure to compile this script and attach it to a chart in Sierra Chart to see the ZigZag indicator in action.