MTF Stochastic "Stoplight" Upper Study


New member
Hey everyone, new to the site. I've enjoyed sifting through the collection of indicators on this site and it's inspired me to try some scripting of my own. Disclaimer: I suck and am still learning. I've tried piecing together some code (some borrowed, some not) to make an MTF Stochastic stoplight-like upper indicator. It'll end up as a collection of red/green squares at the top of the chart that will tell you what the current status of the StochasticFull is (%K > %D = green and vice verse) for multiple charts -- 1D, 4hr, 1hr, 30 min, whatever you want. I've been successful getting one timeframe's stoplight to appear on it's own chart, but can't figure out how to have all timeframes report on a single chart (30min, 15min, 5min, 1 min all on the 1min chart). The code for I've been working on to get multiple timeframes reporting on a single chart is below, but it has an error with the "script SymbolStochastic {" line. I'm sure it's something silly due to me not knowing what I'm doing, but would appreciate it if someone could show me the error of my ways. Thanks!

declare upper;
input period = AggregationPeriod.DAY;
input over_bought = 80;
input over_sold = 20;
input KPeriod = 5;
input DPeriod = 3;
input slowing_period = 3;
input priceH = high;
input priceL = low;
input priceC = close;
input aP = AggregationPeriod.DAY;
input averageType = AverageType.SIMPLE;
DefineGlobalColor("CrossDown", Color.GREEN);
DefineGlobalColor("CrossUp", Color.RED);
script SymbolStochastic {
def CrossUp = FullK >= FullD;
def CrossDown = FullK < FullD;
def lowest_k = Lowest(priceL, KPeriod);
def c1 = priceC – lowest_k;
def c2 = Highest(priceH, KPeriod) – lowest_k;
def FastK = if c2 != 0 then c1 / c2 * 100 else 0;
def FullK = MovingAverage(averageType, FastK, slowing_period);
def FullD = MovingAverage(averageType, FullK, DPeriod);
def currentPeriod = GetAggregationPeriod();
if period >= currentPeriod {
s1 = SymbolStochastic(aP = period);
} else {
s1 = Double.NaN;
AddLabel(!IsNaN(s1), if period == AggregationPeriod.MONTH then "M" else
if period == AggregationPeriod.WEEK then "W" else
if period == AggregationPeriod.FOUR_DAYS then "4D" else
if period == AggregationPeriod.THREE_DAYS then "3D" else
if period == AggregationPeriod.TWO_DAYS then "2D" else
if period == AggregationPeriod.DAY then "D" else
if period == AggregationPeriod.FOUR_HOURS then "4H" else
if period == AggregationPeriod.TWO_HOURS then "2H" else
if period == AggregationPeriod.HOUR then "60m" else
if period == AggregationPeriod.THIRTY_MIN then "30m" else
if period == AggregationPeriod.TWENTY_MIN then "20m" else
if period == AggregationPeriod.FIFTEEN_MIN then "15m" else
if period == AggregationPeriod.TEN_MIN then "10m" else
if period == AggregationPeriod.FIVE_MIN then "5m" else
if period == AggregationPeriod.FOUR_MIN then "4m" else
if period == AggregationPeriod.THREE_MIN then "3m" else
if period == AggregationPeriod.TWO_MIN then "2m" else
if period == AggregationPeriod.MIN then "1m"
else "", if s1 > 0 then GlobalColor("CrossUp") else GlobalColor("CrossDown"));


Well-known member
@Dixon72 Nice start from the looks of things. Please go through the Universe of Thinkscript, located in the Tutorial section. You'll find examples there.
Please fill out you signature line so we know where your comin' from. :)

"Something simple like you not knowing what you're doing." :LOL::ROFLMAO: It looks to me like you've got a very good grip on things for a beginner.

Take a look at this indicator below, open it up and go thru the code. It will give you an idea of what might be needed. (Or not!)


Well-known member
@Dixon72 line 15 comes back with an invalid statement error

@Dixon72 Yep, you are quite right, I took a quick look through the code, seems like there is a missing brace for the script() function, the use of the script function appears to be incomplete. Another example is that FullK and FullD are used before those variables were defined.

It seems you are planning to implement some sort of Stochastic MTF study. I recently posted a full solution for how to construct a DMI MTF study on another thread for another requester, you might want to look into that and then retrofit into your study
Last edited:


Well-known member
@Dixon72 Here is a MTF Stochastic. Maybe it will help in constructing your study. Just need to repeat your code for all your time frames. Then add labels.

# // ==========================
declare lower;

input length = 14;
input over_bought = 80;
input over_sold = 20;
input smoothK = 3;
input smoothD = 3;
#input priceH = high;
#input priceL = low;
#input priceC = close;
input showBreakoutSignals = {default "Yes", "No"};

def k = SimpleMovingAvg(100*(close-lowest(low,length))/(highest(high,length)-lowest(low,length)), smoothK);
def d = SimpleMovingAvg(k, smoothD);

plot outK = k;
plot outD = d;

plot OverBought = over_bought;
plot OverSold = over_sold;

#need to add cloud with these
#def upK = outK crosses above OverSold;
#def downK = outK crosses below OverBought;
def crossUp = (outK[1] < outD[1] and outK[1] < OverSold[1]) and (outK > outD);
def crossDn = (outK[1] > outD[1] and outK[1] > OverBought[1]) and (outK < outD);

plot UpSignal;
plot DownSignal;
switch (showBreakoutSignals) {
case "No":
    UpSignal = Double.NaN;
    DownSignal = Double.NaN;
case "Yes":
    UpSignal = if crossUp then OverSold else Double.NaN;
    DownSignal = if crossDn then OverBought else Double.NaN;}

UpSignal.setHiding(showBreakoutSignals == showBreakoutSignals."No");

input aggregationPeriod = AggregationPeriod.DAY;
input KPeriod = 3;
input DPeriod = 3;
input slowing_period = 3;

plot FullK = Average((close(period = aggregationPeriod) - Lowest(low(period = aggregationperiod), KPeriod)) / (Highest(high(period = aggregationperiod), KPeriod) - Lowest(low(period = aggregationperiod), KPeriod)) * 100, slowing_period);
plot FullD = Average(Average((close(period = aggregationPeriod) - Lowest(low(period = aggregationperiod), KPeriod)) / (Highest(high(period = aggregationperiod), KPeriod) - Lowest(low(period = aggregationperiod), KPeriod)) * 100, slowing_period), DPeriod);

Similar threads