MTF StochasticMACD

TOS007

New member
Hi everyone, I am trying to code Multi-timeframe StochasticMACD. I simply copy the formula from TOS, but the value is not correct at all. Can anyone correct my mistake, please? Thanks in advance and much appreciated.
Code:
declare lower;

input tf = {"MIN", "TWO_MIN", default "FIVE_MIN", "FIFTEEN_MIN", "HOUR", "TWO_HOURS", "FOUR_HOURS", "DAY", "WEEK", "MONTH"};

def timeframe;

switch (tf){
case "MIN":
timeframe = AggregationPeriod.MIN;
case "TWO_MIN":
timeframe = AggregationPeriod.TWO_MIN;
case "FIVE_MIN":
timeframe = AggregationPeriod.FIVE_MIN;
case "FIFTEEN_MIN":
timeframe = AggregationPeriod.FIFTEEN_MIN;
case "HOUR":
timeframe = AggregationPeriod.HOUR;
case "TWO_HOURS":
timeframe = AggregationPeriod.TWO_HOURS;
case "FOUR_HOURS":
timeframe = AggregationPeriod.FOUR_HOURS;
case "DAY":
timeframe = AggregationPeriod.DAY;
case "WEEK":
timeframe = AggregationPeriod.WEEK;
case "MONTH":
timeframe = AggregationPeriod.MONTH;
}


input stochLength = 45;
input fastLength = 12;
input slowLength = 26;
input signalLength = 9;
input over_bought = 10;
input over_sold = -10;
input averageType = AverageType.EXPONENTIAL;

def hh = Highest(high, stochLength);
def ll = Lowest(low, stochLength);

def fastMA = MovingAverage(averageType, close, fastLength);
def slowMA = MovingAverage(averageType, close, slowLength);

def stochFastMA;
def stochSlowMA;
if (hh - ll) != 0 {
stochFastMA = (fastMA - ll) / (hh - ll);
stochSlowMA = (slowMA - ll) / (hh - ll);
} else {
stochFastMA = 0;
stochSlowMA = 0;
}

plot StochasticMACD = (stochFastMA - stochSlowMA) * 100;
plot Signal = MovingAverage(averageType, StochasticMACD, signalLength);

plot overbought = over_bought;
overbought.SetDefaultColor(GetColor(5));
overbought.SetStyle(Curve.SHORT_DASH);

plot oversold = over_sold;
oversold.SetDefaultColor(GetColor(5));
oversold.SetStyle(Curve.SHORT_DASH);
 
Last edited by a moderator:
yeah, I am that dumb. I don't know how to code these two statements for MTF. 🤦‍♂️

plot StochasticMACD = (stochFastMA - stochSlowMA) * 100;
plot Signal = MovingAverage(averageType, StochasticMACD, signalLength);
 
Can anyone correct my statement for this line, please?

plot StochasticMACD = ((stochFastMA - stochSlowMA) * 100, close(period = Period));

This is what I have for the rest:

#
# TD Ameritrade IP Company, Inc. (c) 2019-2021
#

declare lower;

input Period = AggregationPeriod.HOUR;
input stochLength = 45;
input fastLength = 12;
input slowLength = 26;
input signalLength = 9;
input over_bought = 10;
input over_sold = -10;
input averageType = AverageType.EXPONENTIAL;

def hh = Highest(high, stochLength);
def ll = Lowest(low, stochLength);

def fastMA = MovingAverage(averageType, close, fastLength);
def slowMA = MovingAverage(averageType, close, slowLength);

def stochFastMA;
def stochSlowMA;
if (hh - ll) != 0 {
stochFastMA = (fastMA - ll) / (hh - ll);
stochSlowMA = (slowMA - ll) / (hh - ll);
} else {
stochFastMA = 0;
stochSlowMA = 0;
}

plot StochasticMACD = ((stochFastMA - stochSlowMA) * 100, close(period = Period);
plot Signal = MovingAverage(averageType, StochasticMACD, signalLength);
plot OverBought = over_bought;
plot ZeroLine = 0;
plot OverSold = over_sold;

StochasticMACD.SetDefaultColor(GetColor(1));
Signal.SetDefaultColor(GetColor(8));
OverBought.SetDefaultColor(GetColor(7));
ZeroLine.SetDefaultColor(GetColor(0));
OverSold.SetDefaultColor(GetColor(7));
 
I think I almost got it. The StochaMACD is more jacketed than its non MTF version.
The values for stochasticMACD and Signal line are not matching but the crossover at almost identical.
declare lower;
input Period = aggregationPeriod.HOUR;
input stochLength = 45;
input fastLength = 12;
input slowLength = 26;
input signalLength = 9;
input over_bought = 10;
input over_sold = -10;
input averageType = AverageType.EXPONENTIAL;

def hh = Highest(high, stochLength);
def ll = Lowest(low, stochLength);

def fastMA = MovingAverage(averageType, close(period = Period), fastLength);
def slowMA = MovingAverage(averageType, close(period = Period), slowLength);

def stochFastMA;
def stochSlowMA;
if (hh - ll) != 0 {
stochFastMA = (fastMA - ll) / (hh - ll);
stochSlowMA = (slowMA - ll) / (hh - ll);
} else {
stochFastMA = 0;
stochSlowMA = 0;
}

plot StochasticMACD = (stochFastMA - stochSlowMA) * 100;
plot Signal = MovingAverage(averageType, StochasticMACD, signalLength);
plot OverBought = over_bought;
plot ZeroLine = 0;
plot OverSold = over_sold;

StochasticMACD.SetDefaultColor(GetColor(1));
Signal.SetDefaultColor(GetColor(8));
OverBought.SetDefaultColor(GetColor(7));
ZeroLine.SetDefaultColor(GetColor(0));
OverSold.SetDefaultColor(GetColor(7));
 
I think I almost got it. The StochaMACD is more jacketed than its non MTF version.
The values for stochasticMACD and Signal line are not matching but the crossover at almost identical.
declare lower;
input Period = aggregationPeriod.HOUR;
input stochLength = 45;
input fastLength = 12;
input slowLength = 26;
input signalLength = 9;
input over_bought = 10;
input over_sold = -10;
input averageType = AverageType.EXPONENTIAL;

def hh = Highest(high, stochLength);
def ll = Lowest(low, stochLength);

def fastMA = MovingAverage(averageType, close(period = Period), fastLength);
def slowMA = MovingAverage(averageType, close(period = Period), slowLength);

def stochFastMA;
def stochSlowMA;
if (hh - ll) != 0 {
stochFastMA = (fastMA - ll) / (hh - ll);
stochSlowMA = (slowMA - ll) / (hh - ll);
} else {
stochFastMA = 0;
stochSlowMA = 0;
}

plot StochasticMACD = (stochFastMA - stochSlowMA) * 100;
plot Signal = MovingAverage(averageType, StochasticMACD, signalLength);
plot OverBought = over_bought;
plot ZeroLine = 0;
plot OverSold = over_sold;

StochasticMACD.SetDefaultColor(GetColor(1));
Signal.SetDefaultColor(GetColor(8));
OverBought.SetDefaultColor(GetColor(7));
ZeroLine.SetDefaultColor(GetColor(0));
OverSold.SetDefaultColor(GetColor(7));
try adding period = ...
to the hh and ll formulas
 
I noticed the missing commas, and I tried it again, and I got denied. 😅


stochFastMA = (fastMA - ll) / (hh - ll), close(period = Period);
stochSlowMA = (slowMA - ll) / (hh - ll), close(period = Period);
 
@TOS007 impressed w/ your stick-to-it-fulness
Code:
def agg_high = high (period = period);
def agg_low = low (period = period);
def hh = Highest(agg_high, stochLength);
def ll = Lowest(agg_low, stochLength);

It is alive! It is alive!!! Thank you so much MerryDay!

Here is the code for anyone who is interested!


declare lower;
input Period = aggregationPeriod.HOUR;
input stochLength = 45;
input fastLength = 12;
input slowLength = 26;
input signalLength = 9;
input over_bought = 10;
input over_sold = -10;
input averageType = AverageType.EXPONENTIAL;

def agg_high = high (period = period);
def agg_low = low (period = period);
def hh = Highest(agg_high, stochLength);
def ll = Lowest(agg_low, stochLength);

def fastMA = MovingAverage(averageType, close(period = Period), fastLength);
def slowMA = MovingAverage(averageType, close(period = Period), slowLength);


def stochFastMA;
def stochSlowMA;

if (hh - ll) != 0 {
stochFastMA = (fastMA - ll) / (hh - ll);
stochSlowMA = (slowMA - ll) / (hh - ll);
} else {
stochFastMA = 0;
stochSlowMA = 0;
}

plot StochasticMACD = (stochFastMA - stochSlowMA) * 100;
plot Signal = MovingAverage(averageType, StochasticMACD, signalLength);
plot OverBought = over_bought;
plot ZeroLine = 0;
plot OverSold = over_sold;

StochasticMACD.SetDefaultColor(GetColor(1));
Signal.SetDefaultColor(GetColor(8));
OverBought.SetDefaultColor(GetColor(7));
ZeroLine.SetDefaultColor(GetColor(0));
OverSold.SetDefaultColor(GetColor(7));
 
@MerryDay @BenTen
Good afternoon, Can someone please look to see if this code can be changed so that it doesn't project to the right ? (StochMACD error)
Looks like is trying to make a false future projection by standing over the zero line. This makes it difficult to read the correct signal.
Thanks.
This is the TOS original Coding:

#
# TD Ameritrade IP Company, Inc. (c) 2019-2021
#

declare lower;

input stochLength = 45;
input fastLength = 12;
input slowLength = 26;
input signalLength = 9;
input over_bought = 10;
input over_sold = -10;
input averageType = AverageType.EXPONENTIAL;

def hh = Highest(high, stochLength);
def ll = Lowest(low, stochLength);

def fastMA = MovingAverage(averageType, close, fastLength);
def slowMA = MovingAverage(averageType, close, slowLength);

def stochFastMA;
def stochSlowMA;
if (hh - ll) != 0 {
stochFastMA = (fastMA - ll) / (hh - ll);
stochSlowMA = (slowMA - ll) / (hh - ll);
} else {
stochFastMA = 0;
stochSlowMA = 0;
}

plot StochasticMACD = (stochFastMA - stochSlowMA) * 100;
plot Signal = MovingAverage(averageType, StochasticMACD, signalLength);
plot OverBought = over_bought;
plot ZeroLine = 0;
plot OverSold = over_sold;

StochasticMACD.SetDefaultColor(GetColor(1));
Signal.SetDefaultColor(GetColor(8));
OverBought.SetDefaultColor(GetColor(7));
ZeroLine.SetDefaultColor(GetColor(0));
OverSold.SetDefaultColor(GetColor(7));
 
@jaricarr this should remove the plots in the expansion area as you requested
Code:
declare lower;

input stochLength = 45;
input fastLength = 12;
input slowLength = 26;
input signalLength = 9;
input over_bought = 10;
input over_sold = -10;
input averageType = AverageType.EXPONENTIAL;

def hh = Highest(high, stochLength);
def ll = Lowest(low, stochLength);

def fastMA = MovingAverage(averageType, close, fastLength);
def slowMA = MovingAverage(averageType, close, slowLength);

def stochFastMA;
def stochSlowMA;
if (hh - ll) != 0 {
    stochFastMA = (fastMA - ll) / (hh - ll);
    stochSlowMA = (slowMA - ll) / (hh - ll);
} else {
    stochFastMA = 0;
    stochSlowMA = 0;
}

def n = double.nan;
def x = !isnan(close);

plot StochasticMACD = if x then (stochFastMA - stochSlowMA) * 100 else n;
plot Signal = if x then MovingAverage(averageType, StochasticMACD, signalLength) else n;
plot OverBought = over_bought;
plot ZeroLine = 0;
plot OverSold = over_sold;

StochasticMACD.SetDefaultColor(GetColor(1));
Signal.SetDefaultColor(GetColor(8));
OverBought.SetDefaultColor(GetColor(7));
ZeroLine.SetDefaultColor(GetColor(0));
OverSold.SetDefaultColor(GetColor(7));
 

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

Thread starter Similar threads Forum Replies Date
T Ehlers Stochastic and AMA into a MTF? Questions 0
B MTF Stochastic Questions 1
E MTF Stacked Moving Averages Questions 3
M MTF AMA Questions 2
C MTF EMA cloud Questions 1

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
194 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