Trend Quality Indicator (With Alert)

BenTen

BenTen

Administrative
Staff
VIP
Warehouse
This is a Trend Quality Indicator for ThinkorSwim shared by Anupam Bagchi who is a data scientist. This indicator is useful for showing buying, selling, and sideways action.
  • Green = buying pressure
  • Selling = selling pressure
  • Yellow = sideways



thinkScript Code

Rich (BB code):
declare lower;

input fastLength = 7;
input slowLength = 15;
input trendLength = 4;
input noiseType = {default linear, squared};
input noiseLength = 250;
input correctionFactor = 2;
input ThresholdValue = 3;
input AlertEnabled = {true, default false};

def smf = 2 / (1 + trendLength);

def reversal = TrendPeriods(fastLength, slowLength);

rec cpc = if isNaN(reversal[1]) then 0 else if reversal[1] != reversal then 0 else cpc[1] + close - close[1];

rec trend = if isNaN(reversal[1]) then 0 else if reversal[1] != reversal then 0 else trend[1] * (1 - smf) + cpc * smf;

def noise;
def diff = AbsValue(cpc - trend);
switch(noiseType) {
case linear:
    noise = correctionFactor * Average(diff, noiseLength);
case squared:
    noise = correctionFactor * Sqrt(Average(diff*diff, noiseLength));
}

plot TQ = if noise == 0 then 0 else trend / noise;
plot ZeroLine = 0;

TQ.SetPaintingStrategy(PaintingStrategy.Histogram);
TQ.SetLineWeight(3);
TQ.DefineColor("Positive", Color.UPTICK);
TQ.DefineColor("Negative", Color.DOWNTICK);
TQ.DefineColor("Neutral", Color.YELLOW);
TQ.AssignValueColor(if TQ > ThresholdValue then TQ.color("Positive") else if TQ < -ThresholdValue then TQ.color("Negative") else TQ.color("Neutral"));
ZeroLine.SetDefaultColor(GetColor(5));

def crossthresholdup = TQ > ThresholdValue && TQ[1] <= ThresholdValue && AlertEnabled;
def crossthresholddown = TQ < -ThresholdValue && TQ[1] >= -ThresholdValue && AlertEnabled;
alert(crossthresholdup, "Trend Quality just crossed above threshold", Alert.BAR, Sound.DING);
alert(crossthresholddown, "Trend Quality just crossed below threshold", Alert.BAR, Sound.DING);

Shareable Link

Original: http://tos.mx/Drcgp5

Modified: http://tos.mx/wbZ5TC
 
Last edited:
I

Ian_Tai

New member
I use the Trend Quality indicator and I wanted an MTF version. I had some help with this, but it's not working correctly and I'm not sure why. Seems the second time frame is always incorrect. I wonder if anyone has an idea what's wrong.

declare lower;
declare zerobase;

input timeFrame1 = aggregationPeriod.HOUR;
input timeFrame2 = aggregationPeriod.TWO_HOURS;
input timeFrame3 = aggregationPeriod.FOUR_HOURS;
input timeFrame4 = aggregationPeriod.DAY;
input timeFrame5 = aggregationPeriod.TWO_DAYS;

input fastLength = 7;
input slowLength = 15;
input trendLength = 4;
input noiseType = {default linear, squared};
input noiseLength = 250;
input correctionFactor = 2;

assert(trendLength > 0, "'trend length' must be positive: " + trendLength);
assert(correctionFactor > 0, "'correction factor' must be positive: " + correctionFactor);

script TrendQuality {

input timeFrame = aggregationPeriod.HOUR;
input fastLength = 7;
input slowLength = 15;
input trendLength = 4;
input noiseType = {default linear, squared};
input noiseLength = 250;
input correctionFactor = 2;

def smf = 2 / (1 + trendLength);
def reversal = TrendPeriods(fastLength, slowLength);
def close = close(period = timeFrame);
def cpc = if isNaN(reversal[1]) then 0 else if reversal[1] != reversal then 0 else cpc[1] + close - close[1];

def trend = if isNaN(reversal[1]) then 0 else if reversal[1] != reversal then 0 else trend[1] * (1 - smf) + cpc * smf;

def noise;
def diff = AbsValue(cpc - trend);
switch(noiseType) {
case linear:
noise = correctionFactor * Average(diff, noiseLength);
case squared:
noise = correctionFactor * Sqrt(Average(diff*diff, noiseLength));
}

plot TQ = if noise == 0 then 0 else trend / noise;}

def tf1 = TrendQuality(timeFrame1, fastLength, slowLength, trendLength, noiseType, noiseLength, correctionFactor).TQ;
def tf2 = TrendQuality(timeFrame2, fastLength, slowLength, trendLength, noiseType, noiseLength, correctionFactor).TQ;
def tf3 = TrendQuality(timeFrame3, fastLength, slowLength, trendLength, noiseType, noiseLength, correctionFactor).TQ;
def tf4 = TrendQuality(timeFrame4, fastLength, slowLength, trendLength, noiseType, noiseLength, correctionFactor).TQ;
def tf5 = TrendQuality(timeFrame5, fastLength, slowLength, trendLength, noiseType, noiseLength, correctionFactor).TQ;

plot MTF1 = if IsNaN(close) then double.NaN else 1;
plot MTF2 = if IsNaN(close) then double.NaN else 2;
plot MTF3 = if IsNaN(close) then double.NaN else 3;
plot MTF4 = if IsNaN(close) then double.NaN else 4;
plot MTF5 = if IsNaN(close) then double.NaN else 5;

MTF1.setPaintingStrategy(PaintingStrategy.POINTS);
MTF2.setPaintingStrategy(PaintingStrategy.POINTS);
MTF3.setPaintingStrategy(PaintingStrategy.POINTS);
MTF4.setPaintingStrategy(PaintingStrategy.POINTS);
MTF5.setPaintingStrategy(PaintingStrategy.POINTS);

MTF1.setLineWeight(5);
MTF2.setLineWeight(5);
MTF3.setLineWeight(5);
MTF4.setLineWeight(5);
MTF5.setLineWeight(5);

MTF1.hideBubble();
MTF2.hideBubble();
MTF3.hideBubble();
MTF4.hideBubble();
MTF5.hideBubble();

MTF1.hideTitle();
MTF2.hideTitle();
MTF3.hideTitle();
MTF4.hideTitle();
MTF5.hideTitle();

defineGlobalColor("UpTrend", Color.GREEN);
defineGlobalColor("DownTrend", Color.MAGENTA);

MTF1.AssignValueColor(if TF1 > 0 then globalColor("UpTrend") else globalColor("DownTrend"));
MTF2.AssignValueColor(if TF2 > 0 then globalColor("UpTrend") else globalColor("DownTrend"));
MTF3.AssignValueColor(if TF3 > 0 then globalColor("UpTrend") else globalColor("DownTrend"));
MTF4.AssignValueColor(if TF4 > 0 then globalColor("UpTrend") else globalColor("DownTrend"));
MTF5.AssignValueColor(if TF5 > 0 then globalColor("UpTrend") else globalColor("DownTrend"));[/CODE]
 

Top