Code: Select all
using System;
using cAlgo.API;
using cAlgo.API.Internals;
namespace cAlgo.Indicators
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class LiquiditySweep : Indicator
{
[Parameter("Pivot Left Bars", DefaultValue = 5)]
public int LeftBars { get; set; }
[Parameter("Pivot Right Bars", DefaultValue = 5)]
public int RightBars { get; set; }
private double _lastHigh = double.NaN;
private double _lastLow = double.NaN;
private int _lastHighIndex;
private int _lastLowIndex;
public override void Calculate(int index)
{
// Wait until we have enough bars to form a pivot
if (index < LeftBars + RightBars)
return;
int pivotIndex = index - RightBars;
// 1. Identify Pivot High
bool isPivotHigh = true;
for (int i = pivotIndex - LeftBars; i <= index; i++)
{
if (i == pivotIndex) continue;
if (Bars.High[i] >= Bars.High[pivotIndex])
{
isPivotHigh = false;
break;
}
}
if (isPivotHigh)
{
_lastHigh = Bars.High[pivotIndex];
_lastHighIndex = pivotIndex;
}
// 2. Identify Pivot Low
bool isPivotLow = true;
for (int i = pivotIndex - LeftBars; i <= index; i++)
{
if (i == pivotIndex) continue;
if (Bars.Low[i] <= Bars.Low[pivotIndex])
{
isPivotLow = false;
break;
}
}
if (isPivotLow)
{
_lastLow = Bars.Low[pivotIndex];
_lastLowIndex = pivotIndex;
}
// 3. Draw and dynamically extend structural lines
if (!double.IsNaN(_lastHigh))
{
// Using a static name "ResLine" automatically updates/extends the existing line on each tick
var resLine = Chart.DrawTrendLine("ResLine", _lastHighIndex, _lastHigh, index, _lastHigh, Color.FromArgb(120, Color.Red));
resLine.LineStyle = LineStyle.Lines;
}
if (!double.IsNaN(_lastLow))
{
var supLine = Chart.DrawTrendLine("SupLine", _lastLowIndex, _lastLow, index, _lastLow, Color.FromArgb(120, Color.Green));
supLine.LineStyle = LineStyle.Lines;
}
// 4. Sweep Detection Logic
// Checks if the current bar swept the previous structure but rejected and closed inside.
if (!double.IsNaN(_lastHigh) && Bars.High[index] > _lastHigh && Bars.Close[index] < _lastHigh)
{
Chart.DrawIcon("SweepHigh_" + index, ChartIconType.DownArrow, index, Bars.High[index] + (Symbol.PipSize * 3), Color.Red);
}
if (!double.IsNaN(_lastLow) && Bars.Low[index] < _lastLow && Bars.Close[index] > _lastLow)
{
Chart.DrawIcon("SweepLow_" + index, ChartIconType.UpArrow, index, Bars.Low[index] - (Symbol.PipSize * 3), Color.Green);
}
}
}
}