• Get $30 off VIP with code SUMMER30. Ends July 27. Happy trading!

Using ATR to Create Profit Target

TreeDT

New member
I've been working on trying to reverse the ATRTrailingStop and create ATRTrailingProfit. I have put the following code together, but it's not plotting anything. What have I done wrong? Anyone have any ideas?

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 target = ATRFactor * MovingAverage(averageType, trueRange, ATRPeriod);

def state = {default init, long, short};
def trail;
switch (state[1]) {
case init:
    if (!IsNaN(target)) {
        switch (firstTrade) {
        case long:
            state = state.long;
            trail =  close + target;
        case short:
            state = state.short;
            trail = close - target;
        }
    } else {
        state = state.init;
        trail = Double.NaN;
    }
case long:
    if (close < trail[1]) {
        state = state.long;
        trail = Max(trail[1], close + target);
    } else {
        state = state.short;
        trail = close - target;
    }
case short:
    if (close > trail[1]) {
        state = state.short;
        trail = Min(trail[1], close - target);
    } else {
        state = state.long;
        trail =  close + target;
    }
}

def BuySignal = Crosses(state == state.long, 0, CrossingDirection.ABOVE);
def SellSignal = Crosses(state == state.short, 0, CrossingDirection.ABOVE);

plot TrailingTarget = trail;

TrailingTarget.SetPaintingStrategy(PaintingStrategy.POINTS);
TrailingTarget.DefineColor("Buy", GetColor(0));
TrailingTarget.DefineColor("Sell", GetColor(1));
TrailingTarget.AssignValueColor(if state == state.long
    then TrailingTarget.Color("Sell")
    else TrailingTarget.Color("Buy"));
 

rad14733

Well-known member
VIP
Go into the studies setting panel and switch the TrailingTarget Values from Numerical to Boolean... 💡

Just remember that switching something on or off requires a boolean... Displaying data on a chart requires numerical data...

Edited to Add: I may have answered prematurely... The power was out here for 7 hours due to severe wind and thunderstorms and I was just trying to get caught up before heading off to bed... I may have misconstrued what, exactly, you are trying to accomplish... If that is the case then more tweaking may be required but I will wait to hear back...
 
Last edited:

TreeDT

New member
Go into the studies setting panel and switch the TrailingTarget Values from Numerical to Boolean... 💡

Just remember that switching something on or off requires a boolean... Displaying data on a chart requires numerical data...

Edited to Add: I may have answered prematurely... The power was out here for 7 hours due to severe wind and thunderstorms and I was just trying to get caught up before heading off to bed... I may have misconstrued what, exactly, you are trying to accomplish... If that is the case then more tweaking may be required but I will wait to hear back...
Thanks for the reply. Sorry for the delayed response. I can't locate a studies setting panel.

Ultimately, I want to incorporate the ATR based profit take within a strategy, but I thought I would start with a study to make sure it was plotting correctly. I actually copied the ATRTrailingStop pre-built in TOS and just flipped it to be a Profit Take. For some reason it's not plotting correctly.
 
Top