NextSignals Trend-Following Thinkscript

Iceman1205us

New member
I follow Nextsignals blog - a couple of his studies are posted on this site. He recently released a trend following study:

https://nextsignals.com/2022/08/15/modified-thinkscript-for-adaptive-moving-average/

His theory is chart forecasting is dead due to the auction market and that one has probably better success with the trend. I compared his study with a 4x EMA average study using Fib values 1, 5, 13, 21 and the lines look very similar. I don't know the math make up is but I thought I'd give his study a shot and put it my chart. First thing I wanted to do besides change colors is to display reversal arrows. I've added this code in and it works well enough, but I'm wondering why on different time frames the arrow is lagging and paints on the following candle? Is it because this is a single plot vs different plots that cross, like a 8 and 21 EMA or is it something ese?

Code:
# Arrows

plot Buy = MAA crosses above MAA[1];
Buy.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Buy.SetDefaultColor(createColor(0,255,0));
Buy.SetLineWeight(4);

plot Sell = MAA crosses below MAA[1];
Sell.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Sell.SetDefaultColor(createColor(255,0,0));
Sell.SetLineWeight(4);

Exc6Fxb.jpg


Secondly, I made a scan and watchlist from it. Scan works fine due to the addition of the arrows, but for the worklist columns, instead of displaying the default label "Long" or "Short" I'd like copy the Keltner Momentum Channels column that Pensar created. I'm not a coder, more a dabbler and I have no idea what his count code is doing:

https://usethinkscript.com/threads/momentum-keltner-channels-for-thinkorswim.2855/

47mNnsE.jpg


I thought maybe I all needed to do was just have copy the two codes together, rewrite "plot MAA" to def MAA and redirect "def avg = MAA" but, alas, the numbers values are off. Obviously there's some black counting magic going on.

Code:
# Worklist column combining Nextsignal and Keltner Momentum Channel, trying to duplicate Keltner Column count for NextSignal

# NextSignal Adaptive  Moving Average
# https://nextsignals.com/2022/08/15/modified-thinkscript-for-adaptive-moving-average/


def lastBar = BarNumber();
def current = HighestAll(If(IsNaN(close), 0, lastBar));

## Chop Thinkorswim’s MovAvgAdaptive
input mode = {default KAMA, AMA};
def direction;
def volatility;
def ER;
switch (mode) {
case KAMA:
direction = AbsValue(close – close[10]);
volatility = Sum(AbsValue(close – close[1]), 10);
ER = if volatility != 0 then direction / volatility else 0;
case AMA:
direction = Double.NaN;
volatility = Double.NaN;
ER = AbsValue((close – Lowest(low, 10)) –
(Highest(high, 10) – close)) / (Highest(high,
10) – Lowest(low, 10));}
def SlowSF = 0.2222;
def ScaledSF = ER * (0.4 – SlowSF) + SlowSF;
def AMA = CompoundValue(1, AMA[1] + Sqr(ScaledSF) * (close – AMA[1]),
close);

def MAA = AMA;

# Keltner Momentum Channel Worklist code
# https://usethinkscript.com/threads/momentum-keltner-channels-for-thinkorswim.2855/
# Momentum Keltner Channels Watchlist Column
# Based on the FW_MOBO code by TOS user davidinc (aka Firstwave, aka David Elliott)
# The count displayed in the watchlist shows the number of bars since a break up or break down

input length       =     34;
input factor       =     0.5;
input displace     =     0;
input price        =     close;
input type         =     AverageType.Simple;

def nan      =   double.nan;
def shift    =   factor * MovingAverage(type, TrueRange(high, close, low), length);
def avg      =   MAA;
def line1    =   avg[-displace] - shift[-displace];
def line2    =   avg[-displace] + shift[-displace];
def Chg      =   if(close > line2, 1, if(close < line1, -1, 0));
def Hold     =   CompoundValue(1,if(Hold[1] == Chg or Chg == 0, Hold[1], if(Chg == 1, 1, -1)), 0);

def n1 = Hold[0] == 1;
def n2 = Hold[0] == -1;
def count1 = if n1 and !n1[1] then 1 else count1[1]+1;
def count2 = if n2 and !n2[1] then 1 else count2[1]+1;

plot n = if n1 then count1 else if n2 then count2 else double.nan;
     n.setdefaultcolor(color.black);
 assignbackgroundcolor(if n1 and count1 then color.green else color.red);

#End of code

Any help would be appreciated. Thanks
 
Last edited by a moderator:
I follow Nextsignals blog - a couple of his studies are posted on this site. He recently released a trend following study:

https://nextsignals.com/2022/08/15/modified-thinkscript-for-adaptive-moving-average/

His theory is chart forecasting is dead due to the auction market and that one has probably better success with the trend. I compared his study with a 4x EMA average study using Fib values 1, 5, 13, 21 and the lines look very similar. I don't know the math make up is but I thought I'd give his study a shot and put it my chart. First thing I wanted to do besides change colors is to display reversal arrows. I've added this code in and it works well enough, but I'm wondering why on different time frames the arrow is lagging and paints on the following candle? Is it because this is a single plot vs different plots that cross, like a 8 and 21 EMA or is it something ese?

Code:
# Arrows

plot Buy = MAA crosses above MAA[1];
Buy.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Buy.SetDefaultColor(createColor(0,255,0));
Buy.SetLineWeight(4);

plot Sell = MAA crosses below MAA[1];
Sell.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Sell.SetDefaultColor(createColor(255,0,0));
Sell.SetLineWeight(4);

Exc6Fxb.jpg


Secondly, I made a scan and watchlist from it. Scan works fine due to the addition of the arrows, but for the worklist columns, instead of displaying the default label "Long" or "Short" I'd like copy the Keltner Momentum Channels column that Pensar created. I'm not a coder, more a dabbler and I have no idea what his count code is doing:

https://usethinkscript.com/threads/momentum-keltner-channels-for-thinkorswim.2855/

47mNnsE.jpg


I thought maybe I all needed to do was just have copy the two codes together, rewrite "plot MAA" to def MAA and redirect "def avg = MAA" but, alas, the numbers values are off. Obviously there's some black counting magic going on.

Code:
# Worklist column combining Nextsignal and Keltner Momentum Channel, trying to duplicate Keltner Column count for NextSignal

# NextSignal Adaptive  Moving Average
# https://nextsignals.com/2022/08/15/modified-thinkscript-for-adaptive-moving-average/


def lastBar = BarNumber();
def current = HighestAll(If(IsNaN(close), 0, lastBar));

## Chop Thinkorswim’s MovAvgAdaptive
input mode = {default KAMA, AMA};
def direction;
def volatility;
def ER;
switch (mode) {
case KAMA:
direction = AbsValue(close – close[10]);
volatility = Sum(AbsValue(close – close[1]), 10);
ER = if volatility != 0 then direction / volatility else 0;
case AMA:
direction = Double.NaN;
volatility = Double.NaN;
ER = AbsValue((close – Lowest(low, 10)) –
(Highest(high, 10) – close)) / (Highest(high,
10) – Lowest(low, 10));}
def SlowSF = 0.2222;
def ScaledSF = ER * (0.4 – SlowSF) + SlowSF;
def AMA = CompoundValue(1, AMA[1] + Sqr(ScaledSF) * (close – AMA[1]),
close);

def MAA = AMA;

# Keltner Momentum Channel Worklist code
# https://usethinkscript.com/threads/momentum-keltner-channels-for-thinkorswim.2855/
# Momentum Keltner Channels Watchlist Column
# Based on the FW_MOBO code by TOS user davidinc (aka Firstwave, aka David Elliott)
# The count displayed in the watchlist shows the number of bars since a break up or break down

input length       =     34;
input factor       =     0.5;
input displace     =     0;
input price        =     close;
input type         =     AverageType.Simple;

def nan      =   double.nan;
def shift    =   factor * MovingAverage(type, TrueRange(high, close, low), length);
def avg      =   MAA;
def line1    =   avg[-displace] - shift[-displace];
def line2    =   avg[-displace] + shift[-displace];
def Chg      =   if(close > line2, 1, if(close < line1, -1, 0));
def Hold     =   CompoundValue(1,if(Hold[1] == Chg or Chg == 0, Hold[1], if(Chg == 1, 1, -1)), 0);

def n1 = Hold[0] == 1;
def n2 = Hold[0] == -1;
def count1 = if n1 and !n1[1] then 1 else count1[1]+1;
def count2 = if n2 and !n2[1] then 1 else count2[1]+1;

plot n = if n1 then count1 else if n2 then count2 else double.nan;
     n.setdefaultcolor(color.black);
 assignbackgroundcolor(if n1 and count1 then color.green else color.red);

#End of code

Any help would be appreciated. Thanks
Hello, is there a way to add this to the standard Keltner Channel? Im looking to have this the center line of the Keltner channel. Below is the code for the indicator in TOS. Im basically looking to have the center line green when moving up, red when moving down and grey when sideways. Im not a coder so wouldn't know how.. Thanks! KeltnerChannels:
#
# TD Ameritrade IP Company, Inc. (c) 2007-2022
#

declare weak_volume_dependency;

input displace = 0;
input factor = 1.5;
input length = 20;
input price = close;
input averageType = AverageType.SIMPLE;
input trueRangeAverageType = AverageType.SIMPLE;

def shift = factor * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);

def average = MovingAverage(averageType, price, length);

plot Avg = average[-displace];
Avg.SetDefaultColor(GetColor(1));

plot Upper_Band = average[-displace] + shift[-displace];
Upper_Band.SetDefaultColor(GetColor(8));

plot Lower_Band = average[-displace] - shift[-displace];
Lower_Band.SetDefaultColor(GetColor(5));
 
Hello, is there a way to add this to the standard Keltner Channel? Im looking to have this the center line of the Keltner channel. Below is the code for the indicator in TOS. Im basically looking to have the center line green when moving up, red when moving down and grey when sideways. Im not a coder so wouldn't know how.. Thanks! KeltnerChannels:
#
# TD Ameritrade IP Company, Inc. (c) 2007-2022
#

declare weak_volume_dependency;

input displace = 0;
input factor = 1.5;
input length = 20;
input price = close;
input averageType = AverageType.SIMPLE;
input trueRangeAverageType = AverageType.SIMPLE;

def shift = factor * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);

def average = MovingAverage(averageType, price, length);

plot Avg = average[-displace];
Avg.SetDefaultColor(GetColor(1));

plot Upper_Band = average[-displace] + shift[-displace];
Upper_Band.SetDefaultColor(GetColor(8));

plot Lower_Band = average[-displace] - shift[-displace];
Lower_Band.SetDefaultColor(GetColor(5));

Here is a modified version that colors the average the way you requested, plus more bands and clouds.

If you do not like it, then replace from the code you posted
Code:
Avg.SetDefaultColor(GetColor(1));
with
Code:
Avg.assignvalueColor(if Avg > Avg[1] then color.green else if Avg < Avg[1] then color.red else color.gray);

Capture.jpg
Ruby:
#
# TD Ameritrade IP Company, Inc. (c) 2007-2016
#

declare weak_volume_dependency;

input displace = 0;
input factor   = 1.0;
input factor1  = 1.5;
input factor2  = 2.0;
input length   = 20;
input price    = close;
input averageType          = AverageType.SIMPLE;
input trueRangeAverageType = AverageType.SIMPLE;

def shift  = factor  * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);
def shift1 = factor1 * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);
def shift2 = factor2 * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);

def average = MovingAverage(averageType, price, length);

plot Avg = average[-displace];
Avg.assignvalueColor(if Avg > Avg[1] then color.green else if Avg < Avg[1] then color.red else color.gray);

plot Upper_Band = average[-displace] + shift[-displace];
Upper_Band.SetDefaultColor(GetColor(8));

plot Lower_Band = average[-displace] - shift[-displace];
Lower_Band.SetDefaultColor(GetColor(5));

plot Upper_Band1 = average[-displace] + shift1[-displace];
Upper_Band1.SetDefaultColor(GetColor(8));

plot Lower_Band1 = average[-displace] - shift1[-displace];
Lower_Band1.SetDefaultColor(GetColor(5));

plot Upper_Band2 = average[-displace] + shift2[-displace];
Upper_Band2.SetDefaultColor(GetColor(8));

plot Lower_Band2 = average[-displace] - shift2[-displace];
Lower_Band2.SetDefaultColor(GetColor(5));

input showclouds = yes;
AddCloud(if showclouds then Upper_Band  else Double.NaN, Upper_Band1, Color.LIGHT_GREEN, Color.LIGHT_GREEN);
AddCloud(if showclouds then Upper_Band1 else Double.NaN, Upper_Band2, Color.GREEN,       Color.GREEN);
AddCloud(if showclouds then Lower_Band  else Double.NaN, Lower_Band1, Color.LIGHT_RED,   Color.LIGHT_RED);
AddCloud(if showclouds then Lower_Band1 else Double.NaN, Lower_Band2, Color.RED,         Color.RED);
 
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
371 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