Half Trend (Halftrend) Scalper Indicator for ThinkorSwim

Modified the study to be imported as a scanner. Comment/uncomment the desired signal arrow to scan for at the bottom of the code.

Shareable link: https://tos.mx/H79KxEJ

Code:
# PRC_HalfTrend Scanner
# Original indicator study by Mobius Apr 2020
# Comment/uncomment desired plot at end of script
# 2020.08.20 -john5788

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;

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];
}
else
{
    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;
}
else if trend == 1
{
    down = Min(minhighprice, down[1]);
    up = up[1];
}
else
{
    up = up[1];
    down = down[1];
}

def UpSignal = up[1] < 1 and up > 0;
def DownSignal = down[1] < 1 and down > 0;

plot UpSignalArrow = UpSignal within 5 bars;
#plot DownSignalArrow = DownSignal within 5 bars;

# End Code
 
Last edited:

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

@BenTen Is there a way we can have this as a watchlist column? For like last 2 / 5 bars or configurable for that matter?
 
@sobiswas Here is the watchlist column. If price is above the Halftrend = green. If below then red.

Code:
# PRC_HalfTrend | indicator
# 10.03.2017
# Ported by Mobius Apr 2020

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];
    }
else
    {
     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];
    }
else
    {
     up =up[1];
     down = down[1];
    }
if up > 0
    {
     halftrend = up;
    }
else
    {
    halftrend = down;
    }
AssignBackgroundcolor(if close > halftrend then color.green else color.red);
 
Hello all!

Really appreciate all the efforts in this forum! great job.

I am wondering is there a final version of this study/WL column/scanner? and greatly appreciate if you can share TOS sharable links for the same?
 
@ngunda You can get the script on the first page of this thread. Watch the video from this thread if you don't know how to use the scanner.

The watchlist column can be found here.
 
I managed to change this successfully to label the line colour that the study paints after a bit of trial and error! Very pleased with myself for managing to complete such a simple task :D. Thanks for your help! Code below if anyone else is interested.

Code:
AddLabel(yes, if Up then "Bullish" + "" else "", color.GREEN);
AddLabel(yes, if Down then "Bearish" + "" else "", color.RED);
 
Last edited by a moderator:
I was linked to this by a friend unfortunately I cannot use thinkscript as I do not have a thinkorswimaccount and I am not allowed to make one as I do not live in the US.

Would it be possible to get this in Pinescript so that I can use it in tradingview?

Thank you
 
To make post#1 into a strategy:
Screenshot (125).png
 
Last edited:
Thank you but maybe a couple of pictures would help to illustrate what I'm asking about. I've already played with the strategy of the code and I like what I'm seeing so I was moving on to papertrading and trying to get buy orders to initiate using code. So, let me ask, if I was to use this code, would the set up I have for the buy orders be correct as pictured or would some changes have to be made to the code or the order? Thank you.
dVqsTQf.jpg

0LjAAmX.jpg
 
Modified the study to be imported as a scanner. Comment/uncomment the desired signal arrow to scan for at the bottom of the code.

Shareable link: https://tos.mx/H79KxEJ

Code:
# PRC_HalfTrend Scanner
# Original indicator study by Mobius Apr 2020
# Comment/uncomment desired plot at end of script
# 2020.08.20 -john5788

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;

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];
}
else
{
    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;
}
else if trend == 1
{
    down = Min(minhighprice, down[1]);
    up = up[1];
}
else
{
    up = up[1];
    down = down[1];
}

def UpSignal = up[1] < 1 and up > 0;
def DownSignal = down[1] < 1 and down > 0;

plot UpSignalArrow = UpSignal within 5 bars;
#plot DownSignalArrow = DownSignal within 5 bars;

# End Code
@john5788 is it possible to expand this scanner, based on this criteria. Adding RSI, but changing period from 14 to 2. Also Overbought is 90, Oversold is 10. Sell Signals are generated when "Half-Trend" is red and RSI reaches first at above 90. Sell below the bar that generated RSI bar above 90. or Next Bar if first bar is not triggered, as long as RSI is still above 90. Reverse is for buy signals.
 
@ssmike123 The directions instruct you to:
MaryDay, Don't see Comment/Uncomment, at the bottom of code
Code:
# PRC_HalfTrend Scanner
# Original indicator study by Mobius Apr 2020
# Comment/uncomment desired plot at end of script
# 2020.08.20 -john5788

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;

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];
}
else
{
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;
}
else if trend == 1
{
down = Min(minhighprice, down[1]);
up = up[1];
}
else
{
up = up[1];
down = down[1];
}

def UpSignal = up[1] < 1 and up > 0;
def DownSignal = down[1] < 1 and down > 0;

plot UpSignalArrow = UpSignal within 5 bars;
#plot DownSignalArrow = DownSignal within 5 bars;

# End Code
 
MaryDay, Don't see Comment/Uncomment, at the bottom of code
This is set to scan for the upSignalArrow
plot UpSignalArrow = UpSignal within 5 bars;
#plot DownSignalArrow = DownSignal within 5 bars;
Make it look like this, to scan for the DownSignalArrow
#plot UpSignalArrow = UpSignal within 5 bars;
plot DownSignalArrow = DownSignal within 5 bars;
(y)
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
490 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.
Back
Top