[First Time User] Backtesting Supertrend Indicator

EDIT: I made a mistake for which I apologized to @mradtke. So hopefully others can learn from my mistake and give credit where it's due when posting code.

Hello everyone,

I found a very simple Supertrend indicator on the website QuantForHire written by Matt Radke, for which I have provided the code below. I am hoping someone can guide me as to how I can backtest this indicator and run a walk forward optimization on the same assuming I take all trades (long and short). I am assuming I need to add something to this code (probably Buy and Sell flags) but am not totally sure where.

Further, I would love some guidance on how clean my code is and if it can be improved in any way. I know there are ways to encapsulate into functions such that the same code can be used in both my backtest and indicator without duplication.

Thank you for the help!

function SuperTrend(lenATR, width)
{
    nATR = ATR(lenATR);
    pAvg = (H+L) / 2;

    upperBand = pAvg + width * nATR;
    lowerBand = pAvg - width * nATR;
    isUpTrend = True;

    for (i=lenATR; i<BarCount; ++i)
    {
        if (H[i] > upperBand[i-1])
            isUpTrend[i] = True;
        else if (L[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]);
    }

    super = IIf(isUpTrend, lowerBand, upperBand); 
    return super;
}

lengthATR = Param("ATR Length", 10, 1, 100, 1);
widthBands = Param("Band Width", 3, 1, 20, 0.1);
st = Supertrend(lengthATR,widthBands);

Plot(st, "Supertrend("+lengthATR+","+widthBands+")", ParamColor( "Color", colorCycle ), ParamStyle("Style") );
1 Like

You can save this function into separate file and then use #include or #include_once to utilize it in other formulas
http://www.amibroker.com/guide/afl/_include.html

1 Like

You did not write this AFL, I did. It came from my web site: https://quantforhire.com/super-trend-indicator/, line for line. You should not be posting someone else's code without giving proper credit and you DEFINITELY should not claim that you wrote it yourself when you did not.

9 Likes

@gambleditall - what a disgrace! Why would you do that? You won't get much help on this forum when you treat other forum members and the forum itself with such disrespect!

1 Like

Hey @mradtke, let me just apologize to you first. Plagiarizing is definitely not something I condone and I made a mistake here so sorry for that. You are right, I did find this code on the internet. I used it as the base and modified it to my needs but unfortunately posted the unmodified version without giving credit. And, regardless even if I posted the modified version, I should have given you credit. I would love to edit the first post and give you credit now but doesn't seem like I can.

@TrendSurfer Yes, you are right. It was a mistake.

Obviously, the topic is shot now but that is my own fault. I would love to edit the main topic and give you credit but regardless I doubt I will get much help now.

1 Like

@gambleditall - since you marked @mradtke post as the solution, the information about original author is now included in first post. And quite frankly, hundreds of my formulas are being copy-pasted without credit, that I stopped paying attention long time ago. So I hope @mradtke will accept your apologies and we can still be friends :slight_smile:

2 Likes

I unmarked it from being the solution but seems like the post is hidden now because it says 'View ignored content' under my post.

That is because your post has been reported to moderation.

1 Like

@Tomasz: I agree that code we post on the internet often gets reused without credit. And obviously we wouldn't post things publicly if we didn't want others to be able to use (and hopefully benefit from) our code. It was the OP's comment that "I have written a very simple Supertrend indicator..." that irritated me. As you recommended, I am happy to accept the OP's apology and move on.

@gambleditall: if you return to where you found the AFL, you will find that there are also working examples of using the SuperTrend indicator in a very simple backtest. I will not repost the URL, because I respect @Tomasz's rules about not promoting other web sites in the forum. However, you can contact me privately if you can't find what you're looking for. Also, related to your original question, you can have both charting and backtesting code in a single AFL file. Whether you do it that way or create two files and use #include as @Tomasz suggested is a design decision.

Hi @mradtke ,

I assume you are referring to this backtest that I found on your website:

include <SuperTrendFunction.afl>
 SUPERTREND-V1.AFL
nEntryTiming = 1; nExitTiming = 1;
nMaxPos = Param("Max Positions", 1, 1, 10, 1);
acctMargin = 100;
pctPosSize = 100/nMaxPos * 100/acctMargin;
////////////////////////////////////////////////////
// Back Test Parameters
stLengthATR = Param("SuperTrend: ATR Length", 10, 1, 100, 1); stWidthBands = Param("SuperTrend: Band Width", 3, 1, 20, 0.1);
////////////////////////////////////////////////////
// Set up the AB environment
SetOption("CommissionMode",3);
SetOption("CommissionAmount", 0.01); SetOption("MaxOpenPositions",nMaxPos); SetOption("AccountMargin",acctMargin); SetTradeDelays(nEntryTiming,nExitTiming,nEntryTiming,nExitTiming); SetPositionSize(pctPosSize,spsPercentOfEquity);
RoundLotSize = 1;
// Initialize all arrays so the backtest will always run Buy = Sell = Short = Cover = 0;
BuyPrice = ShortPrice = IIf(nEntryTiming==0,C,O); SellPrice = CoverPrice = IIf(nExitTiming==0,C,O);
SuperT = SuperTrend(stLengthATR,stWidthBands);
Buy =   C > SuperT;
Sell =  C < SuperT;
Short = C < SuperT;
Cover = C > SuperT;

The problem with this is that it only buys and sells when the Close is greater/lesser than the ATR band. I have actually modified your Supertrend indicator so that it plots a Buy/Sell signal taking wicks into consideration (High/Low). The code for this is given below.

Hence, I am not sure how to modify the backtest so that it takes that into consideration. It should not be waiting for the close of the candle (since that introduces black swan risk), it should Buy and Sell using a market order as soon as the trend changes.

function SuperTrend(lenATR, width)
{
    nATR = ATR(lenATR);
    pAvg = (H+L) / 2;

    upperBand = pAvg + width * nATR;
    lowerBand = pAvg - width * nATR;
    isUpTrend = True;

    for (i=lenATR; i<BarCount; ++i)
    {
        if (H[i] > upperBand[i-1])
            isUpTrend[i] = True;
        else if (L[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]);
    }

    super = IIf(isUpTrend, lowerBand, upperBand); 
    return super;
}

lengthATR = Param("ATR Length", 10, 1, 100, 1);
widthBands = Param("Band Width", 3, 1, 20, 0.1);
st = Supertrend(lengthATR,widthBands);

Plot(st, "Supertrend("+lengthATR+","+widthBands+")", ParamColor( "Color", colorCycle ), ParamStyle("Style") );

thanks for the code mradkte!