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: