I got the code from one of the posts on these forums (forgot which) and customized it to the way I use the SuperTrend on my charts. On other platforms, you can find it as TSSuperTrend, so that's what I named it. The basic difference from others posted is:
- It uses a Hull moving average
- The Plot is optional in case you just want to retrieve latest value in the UpTrend/DownTrend
- The Plot is a non-continuous dashed line above or below price
I left the dn variable usage in place, but commented out, as the original author used this for some _TRACE() debugging.
I have used this for years and it is excellent for finding one of the last good support / resistance areas for a nice bounce. I tend not to use this for detection of trend reversals as it is late for that kind of detection relative to other things I use. But I would also say that if the current SuperTrend S/R doesn't hold up, you most likely have much deeper retracements or all-out trend change on your hands.
A sample call is:
lastST = 0;
inUptrend = 0;
TSSuperTrend(14, 2.618, 1, &lastST, &inUptrend);
And here's the procedure:
procedure TSSuperTrend(lenATR, multiplier, showPlot, lastST, inUptrend)
{
local i, nATR, _hma, isUptrend, dn;
local lower, lowerBand, upper, upperBand;
nATR = ATR(lenATR);
_hma = HMA(Close, 14);
upperBand = _hma + multiplier * nATR;
lowerBand = _hma - multiplier * nATR;
isUpTrend = True;
//dn = DateNum();
for (i=lenATR; i < BarCount; i++)
{
if (Close[i] > upperBand[i-1])
isUpTrend[i] = True;
else if (Close[i] < lowerBand[i-1])
isUpTrend[i] = False;
else
isUpTrend[i] = isUpTrend[i-1];
if (isUpTrend[i])
lowerBand[i] = Max(lowerBand[i], lowerBand[i-1]);
else
upperBand[i] = Min(upperBand[i], upperBand[i-1]);
//_TRACE(NumToStr(dn[i], 8.0, False)+": Close="+C[i]+" isUpTrend="+isUpTrend[i]+" upperBand="+upperBand[i]+" lowerBand="+lowerBand[i]);
}
if (showPlot)
{
lower = IIf(isUpTrend, lowerBand, Null);
upper = IIf(isUpTrend, Null, upperBand);
PlotOHLC(lower, lower, lower, lower, "", colorBlue, styleCandle);
PlotOHLC(upper, upper, upper, upper, "", colorRed, styleCandle);
}
if (LastValue(isUpTrend))
{
inUptrend = 1;
lastST = LastValue(lower);
}
else
{
isUptrend = 0;
lastST = LastValue(upper);
}
}