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
A quick search on Google will allow you to learn more about the Half Trend indicator, potential setups, and usage.
You can find the scanner for this indicator here. Credit to @john5788
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
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];
}
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;
}
halftrend.SetStyle(Curve.Firm);
halftrend.AssignValueColor(if up > 0 then color.cyan else color.red);
halftrend.SetLineWeight(2);
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;
UpSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
DownSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
UpSignal.SetDefaultColor(Color.Cyan);
DownSignal.SetDefaultColor(Color.Red);
UpSignal.SetLineWeight(3);
DownSignal.SetLineWeight(3);
# End Code
Backtesting Snippet
Add the following to your existing script, then add the entire indicator as a new strategy:
Code:
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: