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.
 

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Top