• Get $40 off VIP by signing up for a free account! Sign Up

Half Trend (Halftrend) Scalper Indicator for ThinkorSwim


Staff member
Half Trend is a scalping indicator for ThinkorSwim converted by developer Mobius. This is my first time hearing about the script, but Half Trend seems to be quite popular among Forex traders. Also, it was only available for other platforms such as MT4. Now you can use it on ThinkorSwim :)

18:57 Mobius: Here's a code that was dropped in my lap earlier to port to TOS. The folks that gave it to me didn't think it could be done in ThinkScript. You know how I love a challenge. No restrictions on sharing and I thought it was a nice take on trend following. I've no information about the original coder other than what's in the header.

A quick search on Google will allow you to learn more about the Half Trend indicator, potential setups, and usage.


thinkScript Code

# PRC_HalfTrend | indicator
# 10.03.2017
# Ported by Mobius Apr 2020
# Arrows added by @shortyuk (https://usethinkscript.com/threads/half-trend-scalper-indicator-for-thinkorswim.3305/post-30413)

input Amplitude = 3;

def lowpricei;
def highpricei;
def lowma;
def highma;
def barindex = barNumber();
def nexttrend;
def maxlowprice;
def trend;
def minhighprice;
def up;
def down;

plot halftrend;

lowpricei = Lowest(low, Amplitude);
highpricei = Highest(high, Amplitude);
lowma = average(low, Amplitude);
highma = average(high, Amplitude);
if barindex > Amplitude and
          nexttrend[1] == 1
     maxlowprice = Max(lowpricei, maxlowprice[1]);
     trend = if highma < maxlowprice[1] and close < low[1]
             then 1
             else trend[1];
     nexttrend = if highma < maxlowprice[1] and close < low[1]
                 then 0
                 else nexttrend[1];
     minhighprice = if highma < maxlowprice[1] and close < low[1]
                    then highpricei
                    else minhighprice[1];
else if nexttrend[1] == 0
     minhighprice = Min(highpricei, minhighprice[1]);
     trend = if lowma > minhighprice[1] and close > high[1]
             then 0
             else trend[1];
     nexttrend = if lowma > minhighprice[1] and close > high[1]
                 then 1
                 else nexttrend[1];
     maxlowprice = if lowma > minhighprice[1] and close > high[1]
                   then lowpricei
                   else maxlowprice[1];
     maxlowprice = maxlowprice[1];
     trend = trend[1];
     nexttrend = nexttrend[1];
     minhighprice = minhighprice[1];
if trend == 0
     up = if trend[1] <> 0
          then down[1]
          else Max(maxlowprice[1], up[1]);
    down = 0;
else if trend[1] <> 1
     down = up[1];
     up = 0;# up[1] este era el error
else if trend == 1
     down = Min(minhighprice, down[1]);
     up = up[1];
     up =up[1];
     down = down[1];
if up > 0
     halftrend = up;
    halftrend = down;
halftrend.AssignValueColor(if up > 0 then color.cyan else color.red);

plot UpSignal = if up[1]<1 and up>0 then low else Double.NaN;
plot DownSignal = if down[1]<1 and down>0 then high else Double.NaN;
# End Code

Backtesting Snippet

Add the following to your existing script, then add the entire indicator as a new strategy:

AddOrder(OrderType.BUY_TO_OPEN, condition = UpSignal, price = close,1, tickcolor = Color.GREEN, arrowcolor = Color.GREEN, name = "Long");
AddOrder(OrderType.SELL_TO_CLOSE, condition = DownSignal, price = close,1, tickcolor = Color.GREEN, arrowcolor = Color.GREEN, name = "Cover");

You can find the scanner for this indicator here. Credit to @john5788
Last edited by a moderator:
This looks very similar to Trend Magic indicator...I like those as it appears that such indicators are not lagging especially when you can trade ABOVE or BELOW the moving average...Anyone want to give a try on an MTF version of this?
It looks too good to be true on the dailies... what does repaint mean? @TosTrd

As of today's Daily SPY, it has turned flat red, indicating possibly "repainting"? as it might go back and continue to green uptrend? I'm guessing that's why it is too good to be true, because when used it gives false signals then you're unsure what to do, similar to the ZigZag indicator, though this might be more efficient with longer aggregation bars like Daily
What other trend indicators do you all work with? I use GannHighLow, Concativity, williams fix fix, LBR_SmartADX... likely will add the Half Trend indicator as well
@bubz Not sure, but if you add this to the end of your script, it will give you a sound alert for the particular ticker you're currently viewing.

# Alerts
Alert(UpSignal, " ", Alert.Bar, Sound.Chimes);
Alert(DownSignal, " ", Alert.Bar, Sound.Bell);
@petey150 You can't set up a scanner for this indicator. I'm not sure why, maybe due to its complexity, but the indicator will break your ToS if you try to scan it.
Hey @BenTen Sorry if this is a dumb question as I don't fully understand how this indicator works. But any idea why the Half Trend line will sometimes "freeze"? Looks like it just stops moving with price action and just stays as a straight line across. Generally, removing the indicator and then adding it back will solve the problem. But this solution doesn't work when it starts to freeze at/near market open, which makes me wonder does the indicator weight pre-market price action differently or something? Any advice is greatly appreciated!
@LLSxCLC I don't think it was freezing; if you observe the indicator from the past, it doesn't move like a moving average. If the stock isn't moving in a strong uptrend or downtrend, you're likely to see small movement in the line. Does that make sense? You can also disable extended hours on your chart for further testing.
@BenTen I understand, thank you! However, in this case, I actually do believe the line is freezing in some cases because when I remove the indicator and then re-add it, it fixes the "frozen" line so I'm pretty confused lol. I'll upload a screenshot so you can get a better idea.

Well I tried Inserting the image, but it's not working so this is the link.


As you can see starting at the 11:03 candle, the Half Trend line stays flat despite the large move down.

And this is how it looks after I "fixed" it by removing and re-adding the indicator.

Last edited by a moderator:
@BenTen Ohhh interesting. I never would have considered something like that lol. Any idea why the expansion area would mess with the indicator? Either way, thanks for your help. I really appreciate it!
  • Like
Reactions: ALF

Hello, I am having a similar issue with the HalfTrend. The indicator just continues all the way to the right. It still tracks the price candles but Like I said it continues across the screen. I already tried adjusting the expansion but did not change. Any ideas? Thank you.
Last edited:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
291 Online
Create Post

Similar threads

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.