Backtesting script for ATR Trail Stop

Chuck

Well-known member
Can anyone help me make this strategy work. It has an error or two in it and I need help to make it work. Any help would be greatly appreciated. I want it to buy on the way up and sell on the way down using only 1 contract in the futures market.

Code:
input trailType = {default modified, unmodified};
input ATRPeriod = 5;
input ATRFactor = 3.5;
input firstTrade = {default long, short};
input averageType = AverageType.WILDERS;
Assert(ATRFactor > 0, "'atr factor' must be positive: " + ATRFactor);
def HiLo = Min(high - low, 1.5 * Average(high - low, ATRPeriod));
def HRef = if low <= high[1] then high - close[1] else (high - close[1]) - 0.5 *
(low - high[1]);
def LRef = if high >= low[1]
then close[1] - low
else (close[1] - low) - 0.5 * (low[1] - high);
def trueRange;
switch (trailType) {
case modified:
    trueRange = Max(HiLo, Max(HRef, LRef));
case unmodified:
    trueRange = TrueRange(high, close, low);
}
def loss = ATRFactor * MovingAverage(averageType, trueRange, ATRPeriod);
def state = {default init, long, short};
def trail;
switch (state[1]) {
case init:
    if (!IsNaN(loss)) {
        switch (firstTrade) {
        case long:
            state = state.long;
            trail = close - loss;
        case short:
            state = state.short;
            trail = close + loss;
    }
    } else {
        state = state.init;
        trail = Double.NaN;
    }
case long:
    if (close > trail[1]) {
        state = state.long;
        trail = Max(trail[1], close - loss);
    } else {
        state = state.short;
        trail = close + loss;
    }
case short:
    if (close < trail[1]) {
        state = state.short;
        trail = Min(trail[1], close + loss);
    } else {
        state = state.long;
        trail = close - loss;
    }
}
def BuySignal = Crosses(state == state.long, 0, CrossingDirection.ABOVE);
def SellSignal = Crosses(state == state.short, 0, CrossingDirection.ABOVE);
# move comment markers dependin on which of these two you want to run
plot scan = BuySignal;
input trailType = {default modified, unmodified};
input ATRPeriod = 2;
input ATRFactor = 2;
input firstTrade = {default long, short};
input averageType = AverageType.WILDERS;
Assert(ATRFactor > 0, "'atr factor' must be positive: " + ATRFactor);
def HiLo = Min(high - low, 1.5 * Average(high - low, ATRPeriod));
def HRef = if low <= high[1] then high - close[1] else (high - close[1]) - 0.5 *
(low - high[1]);
def LRef = if high >= low[1]
then close[1] - low
else (close[1] - low) - 0.5 * (low[1] - high);
def trueRange;
switch (trailType) {
case modified:
    trueRange = Max(HiLo, Max(HRef, LRef));
case unmodified:
    trueRange = TrueRange(high, close, low);
}
def loss = ATRFactor * MovingAverage(averageType, trueRange, ATRPeriod);
def state = {default init, long, short};
def trail;
switch (state[1]) {
case init:
    if (!IsNaN(loss)) {
        switch (firstTrade) {
        case long:
            state = state.long;
            trail = close - loss;
        case short:
            state = state.short;
            trail = close + loss;
    }
    } else {
        state = state.init;
        trail = Double.NaN;
    }
case long:
    if (close > trail[1]) {
        state = state.long;
        trail = Max(trail[1], close - loss);
    } else {
        state = state.short;
        trail = close + loss;
    }
case short:
    if (close < trail[1]) {
        state = state.short;
        trail = Min(trail[1], close + loss);
    } else {
        state = state.long;
        trail = close - loss;
    }
}
def BuySignal = Crosses(state == state.long, 0, CrossingDirection.ABOVE);
def SellSignal = Crosses(state == state.short, 0, CrossingDirection.ABOVE);
# move comment markers dependin on which of these two you want to run
plot scan = BuySignal;
plot scan = SellSignal;

AddOrder(OrderType.BUY_AUTO, scan, tickcolor = GetColor(0), arrowcolor = GetColor(0));
AddOrder(OrderType.SELL_AUTO, scan, tickcolor = GetColor(0), arrowcolor = GetColor(0));

AddOrder(OrderType.BUY_AUTO, scan, tickcolor = GetColor(0), arrowcolor = GetColor(0));
AddOrder(OrderType.SELL_AUTO, scan, tickcolor = GetColor(0), arrowcolor = GetColor(0));
 

Chuck

Well-known member
You are trying to redefine variables and that is not allowed... Variables state, firstTrade, and many others, are defined more than once...
Rad14733, Thank you for your reply. I am not a coder lol. That is exactly what the error message says..... the problem is I have no clue as to how to fix that....lol. I know it is a simple fix .....but out of my range of capabilities, could you possibly assist me?
 

rad14733

Well-known member
VIP
Reduce the number of variable definitions or change some of them to use different names... I'm trying to get you to do the work rather than simply handing you code... That's how you learn...
 

Chuck

Well-known member
That would be great someday, however I haver no clue where to start. I have no clue what names to change anything to and no idea what variable definitions to change.

Could someone please correct these issues for me, I would appreciate it very much!
 

rad14733

Well-known member
VIP
That would be great someday, however I haver no clue where to start. I have no clue what names to change anything to and no idea what variable definitions to change.

Could someone please correct these issues for me, I would appreciate it very much!
I can take a look later if you can't get things figured out... I try to make it a point to not just fire off code upon request because I've found that doing so just brings on more requests rather than someone trying to learn... You've put forth effort so I'm confident that you'll try to learn from any code provided... Then perhaps someday you'll be helping someone else... I'll look closer later this evening...
 

Chuck

Well-known member
Thank you so much, I have been comparing it to other code, researching terms on the TOS website and trying other things. I have not had much success. Some things I can figure out, like adding snippets and style changes etc. Other things are still a little dawnting.

I would greatly appreciate any help and assure you I will learn from your effots.
 

Similar threads

Top