Adding breakouts and DSS to MACD indicator for ThinkorSwim?

H

HighBredCloud

Active member
Establishing the parameters sounds like a good idea to me...Tough choice...both have benefits as far as I can see...I think pairing with the PercentR_MAC would be my choice...


@HighBredCloud I'm so glad to read that...Thank you! You know, in the short span of time we have worked/collaborated together, your positive confirmations have bolstered my confidence and determination to keep on coding as best I can :cool:


I'll look into it and get back to you...
@netarchitech You're very welcome! I hope that this experience will lead you to another level of coding...

I was thinking about the same thing...pairing it with PercentR_MAC once you implement the OFF function or another PercentR line. I am also looking into SMI with the EUO for higher time frames...that also seems to be giving early signals for an entry and exit.
 
netarchitech

netarchitech

Active member
VIP
Warehouse
@HighBredCloud Regarding the AA issue, it's part of the FREMAS:

# Forward / Reverse EMA
# (c) 2017 John F. Ehlers
# Ported to TOS 07.16.2017
# Mobius

# Inputs:
input
AA = .1;

# Vars:
def CC;
def RE1;
def RE2;
def RE3;
def RE4;
def RE5;
def RE6;
def RE7;
def RE8;
def EMA;
plot EMA_Signal;
plot plot0;

CC = if CC[1] == 0 then .9 else 1 –
AA;
EMA = AA * close + CC * EMA[1];
RE1 = CC * EMA + EMA[1];
RE2 = Power(CC, 2) * RE1 + RE1[1];
RE3 = Power(CC, 4) * RE2 + RE2[1];
RE4 = Power(CC, 8) * RE3 + RE3[1];
RE5 = Power(CC, 16) * RE4 + RE4[1];
RE6 = Power(CC, 32) * RE5 + RE5[1];
RE7 = Power(CC, 64) * RE6 + RE6[1];
RE8 = Power(CC, 128) * RE7 + RE7[1];

EMA_Signal = EMA –
AA * RE8;

What this means: Since I focused so intently on the MACD I overlooked the EMA SIgnal in the FREMAS...This means I have to retool the MMAC (Multiple Moving Averages Component), so I'll start working on it tonight...
 
H

HighBredCloud

Active member
@HighBredCloud Regarding the AA issue, it's part of the FREMAS:

# Forward / Reverse EMA
# (c) 2017 John F. Ehlers
# Ported to TOS 07.16.2017
# Mobius

# Inputs:
input
AA = .1;

# Vars:
def CC;
def RE1;
def RE2;
def RE3;
def RE4;
def RE5;
def RE6;
def RE7;
def RE8;
def EMA;
plot EMA_Signal;
plot plot0;

CC = if CC[1] == 0 then .9 else 1 –
AA;
EMA = AA * close + CC * EMA[1];
RE1 = CC * EMA + EMA[1];
RE2 = Power(CC, 2) * RE1 + RE1[1];
RE3 = Power(CC, 4) * RE2 + RE2[1];
RE4 = Power(CC, 8) * RE3 + RE3[1];
RE5 = Power(CC, 16) * RE4 + RE4[1];
RE6 = Power(CC, 32) * RE5 + RE5[1];
RE7 = Power(CC, 64) * RE6 + RE6[1];
RE8 = Power(CC, 128) * RE7 + RE7[1];

EMA_Signal = EMA –
AA * RE8;

What this means: Since I focused so intently on the MACD I overlooked the EMA SIgnal in the FREMAS...This means I have to retool the MMAC (Multiple Moving Averages Component), so I'll start working on it tonight...
OK fair enough...curious to see how that change will effect things...perhaps even earlier entry/exit signals would be my guess if able to be changed to a lower period.
 
netarchitech

netarchitech

Active member
VIP
Warehouse
@netarchitech You're very welcome! I hope that this experience will lead you to another level of coding...

I was thinking about the same thing...pairing it with PercentR_MAC once you implement the OFF function or another PercentR line. I am also looking into SMI with the EUO for higher time frames...that also seems to be giving early signals for an entry and exit.
I guess great minds do think alike...hahaha :) OK, I'm off to see what I can do about the FREMAS and AA...
 
netarchitech

netarchitech

Active member
VIP
Warehouse
@HighBredCloud I think I need a second set of eyes with this "AA" issue...I've tried several approaches to remedy the situation with no luck. Is there anyone that you can think of that could take a look? @diazlaz, perhaps? Then again, with all the testing to be done, then adding this item on top of that, maybe its' too much to ask...What do you think?

I'll continue to work on it in any event. I will keep you apprised of any future developments...
 
D

diazlaz

Active member
VIP
Warehouse
@HighBredCloud I think I need a second set of eyes with this "AA" issue...I've tried several approaches to remedy the situation with no luck. Is there anyone that you can think of that could take a look? @diazlaz, perhaps? Then again, with all the testing to be done, then adding this item on top of that, maybe its' too much to ask...What do you think?

I'll continue to work on it in any event. I will keep you apprised of any future developments...
hi @netarchitech what is the issue with the AA?
 
H

HighBredCloud

Active member
@HighBredCloud I think I need a second set of eyes with this "AA" issue...I've tried several approaches to remedy the situation with no luck. Is there anyone that you can think of that could take a look? @diazlaz, perhaps? Then again, with all the testing to be done, then adding this item on top of that, maybe its' too much to ask...What do you think?

I'll continue to work on it in any event. I will keep you apprised of any future developments...
@netarchitech its not the AA input...its the input "length" 14 that under AA input...when you change the length from 14 to X...nothing happens at all...Don't worry about the testing...I knew you'd be busy with the coding and making sure it runs how its suppose to so I am testing and looking at various timeframes up to 30 min as on now. The best way for you to test will be live market tomorrow...
 
H

HighBredCloud

Active member
@netarchitech I do, however, have a request once you check out the input "length" -14 section...

Would it be possible to add a cloud type feature into the inputs to turn ON or OFF...That way we'd be able to uncheck the EMA_Signal_Line from the plots...

Reason being...with the additional moving averages it is now getting very hard to see what is going on when all the lines merge...A cloud with NO border...just basic GREEN/RED background would possibly eliminate that issue by having 1 less line in the study...

What do you think? Or perhaps you have another solution?
 
D

dolcea23

New member
@dolcea23

This is the version I am personally using...until @netarchitech or anyone else can implement the other moving averages...BUT to be honest...the version below works great.

Code:

# MACD with a more Normal Distribution
# Mobius
# V01.09.2015
#Hint: Plots a Gaussian distribution. If Normal Distribution is met, then at minimum, 68.2% of the close values should be inside a One Standard Deviation Envelope and 95.4% of the close values should be inside a 2 Standard Deviation Envelope.

# V11.01.2019 - netarchitech added standard TOS Breakout Signals per HighBredCloud request
# V11.01.2019 - netarchitech added Ehlers and Mobius Forward/Reverse EMA per HighBredCloud request

declare lower;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input showBreakoutSignals = no;

# Four Pole Filter
script g{
input length = 4;
input betaDev = 2;
input price = OHLC4;
def c;
def w;
def beta;
def alpha;
def G;
c = price;
w = (2 * Double.Pi / length);
beta = (1 - Cos(w)) / (Power(1.414, 2.0 / betaDev) - 1 );
alpha = (-beta + Sqrt(beta * beta + 2 * beta));
G = Power(alpha, 4) * c +
4 * (1 – alpha) * G[1] – 6 * Power( 1 - alpha, 2 ) * G[2] +
4 * Power( 1 - alpha, 3 ) * G[3] - Power( 1 - alpha, 4 ) * G[4];
plot Line = G;
}
# Modified MACD
plot Value = g(length = fastLength) - g(length = slowLength);
plot Avg = g(price = Value, length = MACDLength);
plot Diff = Value - Avg;
plot ZeroLine = 0;

Value.SetDefaultColor(GetColor(1));
#Value.SetLineWeight(2);
Avg.SetDefaultColor(GetColor(8));
#Avg.SetLineWeight(2);
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(0));

plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);


# Forward / Reverse EMA
# (c) 2017 John F. Ehlers
# Ported to TOS 07.16.2017
# Mobius

# Inputs:
input AA = .1;

# Vars:
def CC;
def RE1;
def RE2;
def RE3;
def RE4;
def RE5;
def RE6;
def RE7;
def RE8;
def EMA;
plot Signal;
plot plot0;

CC = if CC[1] == 0 then .9 else 1 – AA;
EMA = AA * Close + CC * EMA[1];
RE1 = CC * EMA + EMA[1];
RE2 = Power(CC, 2) * RE1 + RE1[1];
RE3 = Power(CC, 4) * RE2 + RE2[1];
RE4 = Power(CC, 8) * RE3 + RE3[1];
RE5 = Power(CC, 16) * RE4 + RE4[1];
RE6 = Power(CC, 32) * RE5 + RE5[1];
RE7 = Power(CC, 64) * RE6 + RE6[1];
RE8 = Power(CC, 128) * RE7 + RE7[1];

Signal = EMA – AA * RE8;
Signal.AssignValueColor(if Signal > Signal[1]
then Color.GREEN
else Color.RED);
Signal.SetLineWeight(3);
Plot0 = if isNaN(close) then double.nan else 0;
Plot0.SetDefaultColor(Color.GRAY);

#addCloud(0, Signal, color.RED, color.GREEN);
thanks. question the new maid coding with 12 averages is it better and done?
 
H

HighBredCloud

Active member
thanks. question the new maid coding with 12 averages is it better and done?
@dolcea23 the latest version is in post #125...there will be more updates coming out so keep checking this thread. IMO with the addition of the 12 moving averages to select from and an addition to plot 2 more moving averages along with the MACD lines...YES it makes this MACD better. I would use the HULL 7 period or the DEMA 5 period and I would plot on the X line and uncheck the Y line until more testing of this indicator is available. Try it out and let us know what you think in live trading environment.
 
netarchitech

netarchitech

Active member
VIP
Warehouse
@netarchitech its not the AA input...its the input "length" 14 that under AA input...
@HighBredCloud Sorry for the delay with this reply...I'm glad to read that the AA issue was not a problem because it worked for me. As for the "length" issue, I disabled it so it will not be available/visible in the Options panel...

Would it be possible to add a cloud type feature into the inputs to turn ON or OFF...That way we'd be able to uncheck the EMA_Signal_Line from the plots...
I can't do anything for a single line, like the EMA Signal line, but I have added a Cloud for the MACD_Value and MACD_Avg lines, so now you can turn those lines ON and OFF and have the Cloud remain...I hope this proves helpful :)

Below please find the updated MACD/FREMA code:

Code:
# filename: MR__EZ_MACD_FREMA_
# original source: https://usethinkscript.com/threads/blast-off-indicator-for-thinkorswim.621/post-7785
# current source: https://usethinkscript.com/threads/adding-breakouts-and-dss-to-macd-indicator-for-thinkorswim.957/post-8541

# MACD with a more Normal Distribution
# Mobius
# V01.09.2015
#Hint: Plots a Gaussian distribution. If Normal Distribution is met, then at minimum, 68.2% of the close values should be inside a One Standard Deviation Envelope and 95.4% of the close values should be inside a 2 Standard Deviation Envelope.

# V11.01.2019 - netarchitech added standard TOS Breakout Signals per HighBredCloud request
# V11.01.2019 - netarchitech added Ehlers and Mobius Forward/Reverse EMA per HighBredCloud request
# V11.07.2019 - netarchitech added multiple Moving Average selection per HighBredCloud request
# V11.08.2019 - netarchitech added cloud feature to MACD_Value - MACD_Avg lines per HighBredCloud request

declare lower;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input showBreakoutSignals = yes;
input ma_length = 21; #Length(180-200 for floating S/R , 55 for swing entry)


# Four Pole Filter
script g {
    input length = 4;
    input betaDev = 2;
    input price = OHLC4;
    def c;
    def w;
    def beta;
    def alpha;
    def G;
    c = price;
    w = (2 * Double.Pi / length);
    beta = (1 - Cos(w)) / (Power(1.414, 2.0 / betaDev) - 1 );
    alpha = (-beta + Sqrt(beta * beta + 2 * beta));
    G = Power(alpha, 4) * c +
4 * (1 – alpha) * G[1] – 6 * Power( 1 - alpha, 2 ) * G[2] +
4 * Power( 1 - alpha, 3 ) * G[3] - Power( 1 - alpha, 4 ) * G[4];
    plot Line = G;
}
# Modified MACD
plot MACD_Value = g(length = fastLength) - g(length = slowLength);
MACD_Value.Hide();
plot MACD_Avg = g(price = MACD_Value, length = MACDLength);
MACD_Avg.Hide();
addCloud(MACD_Value, MACD_Avg, Color.GREEN, Color.RED);

plot Diff = MACD_Value - MACD_Avg;
plot ZeroLine = 0;

MACD_Value.SetDefaultColor(GetColor(1));
MACD_Avg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.Color("Positive and Up") else Diff.Color("Positive and Down") else if Diff < Diff[1] then Diff.Color("Negative and Down") else Diff.Color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(4));

plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;

UpSignal.SetHiding(!showBreakoutSignals);
Upsignal.SetLineWeight(3);
DownSignal.SetHiding(!showBreakoutSignals);
Downsignal.SetLineWeight(3);

UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

# Forward / Reverse EMA
# (c) 2017 John F. Ehlers
# Ported to TOS 07.16.2017
# Mobius

# Inputs:
input AA = .1;

# Vars:
def CC;
def RE1;
def RE2;
def RE3;
def RE4;
def RE5;
def RE6;
def RE7;
def RE8;
def EMA;
plot EMA_Signal;
plot plot0;

CC = if CC[1] == 0 then .9 else 1 – AA;
EMA = AA * close + CC * EMA[1];
RE1 = CC * EMA + EMA[1];
RE2 = Power(CC, 2) * RE1 + RE1[1];
RE3 = Power(CC, 4) * RE2 + RE2[1];
RE4 = Power(CC, 8) * RE3 + RE3[1];
RE5 = Power(CC, 16) * RE4 + RE4[1];
RE6 = Power(CC, 32) * RE5 + RE5[1];
RE7 = Power(CC, 64) * RE6 + RE6[1];
RE8 = Power(CC, 128) * RE7 + RE7[1];

EMA_Signal = EMA – AA * RE8;
EMA_Signal.AssignValueColor(if EMA_Signal > EMA_Signal[1]
                        then Color.GREEN
                        else Color.RED);
EMA_Signal.SetLineWeight(3);

plot0 = if IsNaN(close) then Double.NaN else 0;
plot0.SetDefaultColor(Color.GRAY);

#addCloud(0, Signal, color.RED, color.GREEN);

# Multi-moving average Component

#input length = 14; #hint Length: Number of periods to average the data.
input movingAverageType = {default "Simple MA", "Exponential MA", "Wilders Smoothing", "Weighted MA", "Hull MA", "Adaptive MA", "Triangular MA", "Variable MA", "Dema MA", "Tema MA", "EHMA", "THMA"};
input displace = 0;

def avg_MACD_Value = ExpAverage(2 * ExpAverage(MACD_Value, ma_length / 2) - ExpAverage(MACD_Value, ma_length), Round(Sqrt(ma_length)));
def avg_MACD_Avg = ExpAverage(2 * ExpAverage(MACD_Avg, ma_length / 2) - ExpAverage(MACD_Avg, ma_length), Round(Sqrt(ma_length)));

plot X;
X.SetDefaultColor(CreateColor(0, 102, 204));
X.SetLineWeight(3);
plot Y;
Y.SetDefaultColor(Color.WHITE);
Y.SetLineWeight(3);

switch (movingAverageType) {
case "Simple MA":
X = Average(MACD_Value, ma_length);
Y = Average(MACD_Avg, ma_length);

case "Exponential MA":
X = ExpAverage(MACD_Value, ma_length);
Y = ExpAverage(MACD_Avg, ma_length);

case "Wilders Smoothing":
X = WildersAverage(MACD_Value, ma_length);
Y = WildersAverage(MACD_Avg, ma_length);

case "Weighted MA":
X = wma(MACD_Value, ma_length);
Y = wma(MACD_Avg, ma_length);

case "Hull MA":
X = HullMovingAvg(MACD_Value, ma_length);
Y = HullMovingAvg(MACD_Avg, ma_length);

case "Adaptive MA":
X = MovAvgAdaptive(MACD_Value, ma_length);
Y = MovAvgAdaptive(MACD_Avg, ma_length);

case "Triangular MA":
X = MovAvgTriangular(MACD_Value, ma_length);
Y = MovAvgTriangular(MACD_Avg, ma_length);

case "Variable MA":
X = VariableMA(MACD_Value, ma_length);
Y = VariableMA(MACD_Avg, ma_length);

case "Dema MA":
X = DEMA(MACD_Value, ma_length);
Y = DEMA(MACD_Avg, ma_length);

case "Tema MA":
X = TEMA(MACD_Value, ma_length);
Y = TEMA(MACD_Avg, ma_length);

case EHMA:
X = ExpAverage(2 * ExpAverage(MACD_Value, ma_length / 2) - ExpAverage(MACD_Value, ma_length), Round(Sqrt(ma_length)));
Y = ExpAverage(2 * ExpAverage(MACD_Avg, ma_length / 2) - ExpAverage(MACD_Avg, ma_length), Round(Sqrt(ma_length)));

case THMA:
X = WMA(WMA(MACD_Value, (ma_length / 2) / 3) * 3 - WMA(MACD_Value, (ma_length / 2) / 2) - WMA(MACD_Value, (ma_length / 2)), (ma_length / 2));
Y = WMA(WMA(MACD_Avg, (ma_length / 2) / 3) * 3 - WMA(MACD_Avg, (ma_length / 2) / 2) - WMA(MACD_Avg, (ma_length / 2)), (ma_length / 2));
}
;
 
netarchitech

netarchitech

Active member
VIP
Warehouse
hi @netarchitech what is the issue with the AA?
Hi @diazlaz Thanks for the swift reponse :) ...Sorry for the delay with this reply...The situation with the AA was a non-issue thankfully...

In any event, I'm hoping you might have a moment to take a look at the following code, specifically a switch/case statement at the bottom of the PercentR_MAC indicator below:

Code:
# filename: MR__EZ_PercentR_MAC_
# idea source: @HighBredCloud and https://usethinkscript.com/threads/moving-average-crossover-rsi-indicator-for-thinkorswim.185/

declare lower;

# Gmode

def lengthRSI = 14;
def price = close;

input averageType = AverageType.WILDERS;

def NetChgAvg = MovingAverage(averageType, price - price[1], lengthRSI);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), lengthRSI);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

plot RSI = 50 * (ChgRatio + 1);
RSI.SetDefaultColor(CreateColor(0, 102, 204));
RSI.SetLineWeight(3);
#RSI.HideTitle();
RSI.HideBubble();
RSI.Hide();

input n1 = 9; #Channel Length
input n2 = 26; #Average Length
input n3 = 13; #Short length
def multi = no; #Multi-exchange?

def src0 = hlc3;

def src1 = hlc3(period = AggregationPeriod.MONTH);
def src2 = hlc3(period = AggregationPeriod.MONTH);
def src3 = hlc3(period = AggregationPeriod.MONTH);
def src4 = hlc3(period = AggregationPeriod.MONTH);

script tci {

    input src = hlc3;
    input n1 = 9; #Channel Length
    input n2 = 26; #Average Length
    plot tci = MovAvgExponential((src - MovAvgExponential(src, n1)) / (0.025 * MovAvgExponential(AbsValue(src - MovAvgExponential(src, n1)), n1)), n2) + 50;
    tci.Hide();
}

#mf(src) => rsi(sum(volume * (change(src) <= 0 ? 0 : src), n3), sum(volume * (change(src) >= 0 ? 0 : src), n3))

script mf {

    input src = hlc3;
    input n3 = 13; #Short length


    def rsi = reference RSI();


    #plot mf = rsi(Sum(volume * (if Average(src) <= 0 then 0 else src), n3), Sum(volume * (if Average(src) >= 0 then 0 else src), n3));
    plot mf = RSI(n3, src);
    mf.Hide();
}

script willy {

    input src = hlc3;
    input n2 = 26; #Average Length
    plot willy = 60 * (src - Highest(src, n2)) / (Highest(src, n2) - Lowest(src, n2)) + 80;
    willy.Hide();

}

#csi(src) => avg(rsi(src, n3),tsi(src0,n1,n2)*50+50)

script csi {

    input src = hlc3;
    input n1 = 9; #Channel Length
    input n2 = 26; #Average Length
    input n3 = 13; #Short length

    def rsi = reference RSI();

    plot csi = RSI(n3, src) + TrueStrengthIndex(n1, n2) / 2 * 50 + 50;
    csi.Hide();

}

script godmode {

    input src = hlc3;
    def rsi = reference RSI();
    plot godmode = (tci(src) + CSI(src) + mf(src) + willy(src)) / 4;
    godmode.Hide();

}

script tradition {

    input src = hlc3;
    input n3 = 13; #Short length

    def rsi = reference RSI();

    plot tradition = (tci(src) + mf(src) + RSI(n3, src)) / 3;
    tradition.Hide();

}


def wt1 = if multi then (godmode(src0) + godmode(src1) + godmode(src2) + godmode(src3) + godmode(src4) / 5) else tradition(src0);

def wt2 = SimpleMovingAvg(wt1, 6);

def extended = if wt2 < 20 then wt2 + 5 else if wt2 > 80 then wt2 - 5 else Double.NaN;

plot wta = wt1;
wta.SetDefaultColor(Color.GREEN);
wta.SetLineWeight(2);
wta.Hide();

plot wtb = wt2;
wtb.SetDefaultColor(Color.RED);
wtb.SetLineWeight(2);
wtb.Hide();

plot ext = extended; #Caution!
ext.SetPaintingStrategy(PaintingStrategy.POINTS);
ext.SetDefaultColor(Color.YELLOW);
ext.SetLineWeight(3);


# PercentR_MAC

input length = 14;
input over_Bought = 80;
input over_Sold = 20;
input showBreakoutSignals = yes;
input PercentRMALength5 = 5; #hint rsiMALength: RSI Moving Average Length
input PercentRMALength8 = 8;
input PercentRAverageType = AverageType.EXPONENTIAL;

input lowBand = 10; #Smoothing LowerBand
input data = close;
input lower = low; #Research Lower = Low?

def highest = Highest(high, length);
def divisor = highest - Lowest(low, length);
def PI = 3.14159265359;
def a1 = Exp(-PI * Sqrt(2) / lower);
def coeff2 = 2 * a1 * Cos(Sqrt(2) * PI / lower);
def coeff3 = - Power(a1, 2);
def coeff1 = 1 - coeff2 - coeff3;
def filt = coeff1 * (data + (data[1])) / 2 + coeff2 * (filt[1]) + coeff3 * (filt[2]);

#input applySmoothing = yes;
def rough_it = if divisor equals 0 then 0 else (100 - 100 * (highest - close) / divisor);

# plot and smooth PercentR
#plot "%R" = if applySmoothing then EhlersSuperSmootherFilter(if divisor equals 0 then 0 else 100 - 100 * (highest - close) / divisor, LowBand) else rough_it;
plot "%R" = if divisor equals 0 then 0 else 100 - 100 * (highest - close) / divisor;
"%R".DefineColor("OverBought", GetColor(9));
"%R".DefineColor("Normal", GetColor(7));
"%R".DefineColor("OverSold", GetColor(1));
"%R".AssignValueColor(if "%R" > over_Bought then "%R".Color("OverBought") else if "%R" < over_Sold then "%R".Color("OverSold") else "%R".Color("Normal"));
"%R".SetLineWeight(3);


# plot the PercentR Moving Averages
def PercentRMA5 = MovingAverage(PercentRAverageType, "%R", PercentRMALength5);
plot PercentRMovAvg5 = PercentRMA5;
PercentRMovAvg5.SetDefaultColor(Color.GREEN);
PercentRMovAvg5.SetLineWeight(3);

def PercentRMA8 = MovingAverage(PercentRAverageType, "%R", PercentRMALength8);
plot PercentRMovAvg8 = PercentRMA8;
PercentRMovAvg8.SetDefaultColor(Color.RED);
PercentRMovAvg8.SetLineWeight(3);


plot OverBought = over_Bought;
OverBought.SetDefaultColor(Color.DARK_RED);
OverBought.HideTitle();

plot OverSold = over_Sold;
OverSold.SetDefaultColor(Color.DARK_GREEN);
OverSold.HideTitle();

plot fifty_line = 50;
fifty_line.SetDefaultColor(GetColor(8));
fifty_line.HideTitle();
fifty_line.SetStyle(Curve.SHORT_DASH);


# plot the Breakout Signals
plot UpSignal = if "%R" crosses above OverSold then OverSold else Double.NaN;
UpSignal.SetHiding(!showBreakoutSignals);
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UpSignal.SetLineWeight(3);
UpSignal.HideTitle();

plot DownSignal = if "%R" crosses below OverBought then OverBought else Double.NaN;
DownSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
DownSignal.SetLineWeight(3);
DownSignal.HideTitle();


AddCloud(0, over_Sold, Color.DARK_GREEN, Color.DARK_GREEN);
AddCloud(over_Bought, 100, Color.DARK_RED, Color.DARK_RED);


input applySmoothing = yes;
input Smooth = {default "No Smoothing", "PercentR Only", "MAs Only", "PercentR & MAs"};

def PRMA5 = PercentRMovAvg5;
def PRMA8 = PercentRMovAvg8;
def PR = "%R";

plot cond;

switch (Smooth) {
    case "No Smoothing":
        cond = if applySmoothing then EhlersSuperSmootherFilter(if divisor equals 0 then 0 else 100 - 100 * (highest - close) / divisor, LowBand) else rough_it;
    case "PercentR Only":
        cond = if applySmoothing and PR then EhlersSuperSmootherFilter(if divisor equals 0 then 0 else 100 - 100 * (highest - close) / divisor, lowBand) else rough_it;
    case "MAs Only":
        cond = if applySmoothing and PRMA5 and PRMA8 then EhlersSuperSmootherFilter(if divisor equals 0 then 0 else 100 - 100 * (highest - close) / divisor, lowBand) else rough_it;
    case "PercentR & MAs":
        cond = if applySmoothing and PR and PRMA5 and PRMA8 then EhlersSuperSmootherFilter(if divisor equals 0 then 0 else 100 - 100 * (highest - close) / divisor, lowBand) else rough_it;
}
;
For some unknown reason, no matter what I've tried, it will not work...There are no syntax errors...so I'm guessing it's a logic error(s), but I am just at a loss to figure it out...Could you take a look when you have a chance? Thanks in advance :)
 
T

tomsk

Active member
VIP
Warehouse
@netarchitech There are no syntax errors, so as you eluded to it's gotta be some kind of logic problem.
I looked at your switch/case statement at the end of the code, looks like it is formed properly.
There are quite a few script() function calls, perhaps you can simplify those as well

Also do look at the following defs - not sure if this is intended.

def src1 = hlc3(period = AggregationPeriod.MONTH);
def src2 = hlc3(period = AggregationPeriod.MONTH);
def src3 = hlc3(period = AggregationPeriod.MONTH);
def src4 = hlc3(period = AggregationPeriod.MONTH);
 
H

HighBredCloud

Active member
@netarchitech Will check it out...Thanks! And its all good...I know that you're busy...I too was testing. Really trying to figure out what the best setting are and what a possible strategy behind it.
 
H

HighBredCloud

Active member
@netarchitech SO a little update...I've been testing the MACD_FREMAS on multiple timeframes not exceeding 30 min as I don't think for intraday trading is necessary. Here is what I am finding out thus far: ALL with the Y plot removed to enhance visibility and readability when the all the MA's merge together. Even after you updated the cloud...I find that its easier to actually look at the MA lines crossing. It's just a personal preference.

The best 5 min settings:

MACD_FREMAS 12, 26, 9
X Plot 8 period Triangular Moving Average

15 and 30 min best settings:

MACD_FREMAS 8, 17, 9
X Plot 5 period TEMA

On ALL the time frames I am using the the MACD VALUE to go ABOVE or BELOW the X Plot...basically on the 15 and 30 min time charts awaiting the one bar where the VALUE breaks away from the X Plot.

5 min is a bit different as I am using more of FREMA to get in on LONG positions and I am looking to stay in those positions until the MACD Histogram changes from Bright GREEN to Dark GREEN. SHORT position would be having FREMA Bright RED as well as having the VALUE below the X Plot.

For comparison purposes I have also used PPO with Histogram...and it kind of gave me an idea that I would like to try out. From what I have noticed...the PPO Histogram provides a more accurate findings than the MACD itself. I was thinking if you could possibly add the FREMA part to the PPO Histogram code below...Hopefully then having the two side by side would allow for a better ENTRY or EXIT. Here's what I am thinking:

By having the FREMA moved to the PPO...It would be easier to read the MACD...possibly even see IF having the Y Plot would help with anything as my current findings don't necessarily require the Y Plot.

The most important aspect of having the MACD and PPO together is observing the Histograms on both.

There are times where the FREMA on the MACD kicks in first...especially on the 5 min chart I've witnessed instances where there is a BIG pullback until the MACD Histogram aligns itself to the same color as FREMA. SO you might get stopped out when that happens OR just freak out. The PPO Histogram is much different. It almost seems as you can get in on one and get out on the other as they seem to work well in confluence. See pic below...


Notice in the above pic how on the MACD Histogram the Dark GREEN already appeared even though FREMA and all the MA's including X Plot are still moving up...Normally such an indication is the first sign in candle change and possible pullback...BUT now...look at the PPO Histogram as its now crossing the ZERO line almost telling you a stronger move up will happen and confirming that the MACD Histogram Dark GREEN change is only most likely a pullback.

I am also using Tillson T3 MA's set to 5 dotted...13 solid and 34 dashed along with TMO with fisher with time aggregation to 10 min and Mobius SuperTrend also time aggregated to 10 min for a more confirmed entry.

SO is it possible to do the following the the PPO with Histogram script below:

1. add FREMA
2. add breakout signals
3. add the same moving average menu as on the MACD_FREMAS only if possible...to see how those would react in crossovers to the default PPO and PPOEMA averages.

I have another idea how to possibly "intermingle" or combine the MACD Histogram...PPO Histogram and FREMA into one indicator...but that would eliminate the MA's...which I am not sure about if that's the best way to go...hence my request above first. BTW...any updates on the PercentR_MAC? Were you able to add the Percent R line to the smoothed moving averages?

PPO with Histogram code:

input fastPeriod = 12;
input slowPeriod = 26;
input signalPeriod = 9;
input price = close;

def fastEma = ExpAverage(price, fastPeriod);
def slowEma = ExpAverage(price, slowPeriod);
def periodOK = fastPeriod < slowPeriod;
AddLabel(!periodOK, "ERROR: fastPeriod MUST be less than slowPeriod");
def _ppo = if periodOK then ((fastEma - slowEma) / slowEma) * 100 else 0;
def _signal = ExpAverage(_ppo, signalPeriod);

plot Ppo = _ppo;
Ppo.SetDefaultColor(Color.CYAN);

plot PpoEma = _signal;
PpoEma.SetDefaultColor(Color.YELLOW);

plot zeroLine = 0;
zeroLine.HideBubble();
zeroLine.setdefaultColor(GetColor(0));

plot PpoH1 = _ppo - _signal;
PpoH1.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
PpoH1.DefineColor("Up", Color.UPTICK);
PpoH1.DefineColor("Down", Color.DOWNTICK);
PpoH1.DefineColor("Flat", Color.GRAY);
PpoH1.AssignValueColor( if PpoH1 > PpoH1[1] then
PpoH1.Color("Up")
else
if PpoH1 < PpoH1[1] then
PpoH1.Color("Down")
else
PpoH1.Color("Flat"));
PpoH1.HideBubble();
PpoH1.SetLineWeight(3);
 
netarchitech

netarchitech

Active member
VIP
Warehouse
@HighBredCloud I'll take a look at the PPO Histogram...Thanks for supplying the code :) If I understand correctly, you want to replicate the features of the MACD/FREMAS on the PPO and possibly combine the MACD, PPO and FREMAS at some point? I'll start the MACD to PPO conversion...

Honestly, I spent all weekend trying to get the PercentR_MAC smoothing working, but it remains incredibly frustrating...ARGH!...It'll be good to take a breather from that for a little bit...

Thanks for the update and the PPO code :cool: Looking forward to seeing additional updates when you have a chance and I'll make sure to do the same...
 
H

HighBredCloud

Active member
@HighBredCloud I'll take a look at the PPO Histogram...Thanks for supplying the code :) If I understand correctly, you want to replicate the features of the MACD/FREMAS on the PPO and possibly combine the MACD, PPO and FREMAS at some point? I'll start the MACD to PPO conversion...

Honestly, I spent all weekend trying to get the PercentR_MAC smoothing working, but it remains incredibly frustrating...ARGH!...It'll be good to take a breather from that for a little bit...

Thanks for the update and the PPO code :cool: Looking forward to seeing additional updates when you have a chance and I'll make sure to do the same...
@netarchitech at this time ONLY replicate the features of the MACD_FREMAS on the PPO but DO NOT add the MACD portion to the PPO just yet...ONLY add the breakout signals...FREMA Signal Line...

Reason being not to combine those two yet as I have an idea on how to do that...but first I would like to see how PPO with the FREMA features act by entering on the FREMA and exiting on the PPO Histogram...I would then test how Plot X and Y along with the other moving averages from the MACD_FREMA react with the PPO and PPOEMA...

The PercentR_MAC is difficult...and I can tell you right now that when you figure out how to add the secondary jagged PercentR line to the 5 and 8 moving average...we will most likely have to play around with the 5 and 8 moving averages to see what periods crossover quicker on when the smoothing is turned ON...but that's testing and part of the game ONLY when you figure out everything else.
 
H

HighBredCloud

Active member
@HighBredCloud Are you trying to make me cry? o_O
@MBF NO...but sometimes I feel very guilty throwing my new ideas at @netarchitech with more information overload on new potential projects when others are not yet done...That's just how my fickle mind works. Hopefully everything that is done here is for everyone's benefit one its all said and done.
 
H

HighBredCloud

Active member
@netarchitech here's what I was thinking about how we can combine the MACD Histogram...FREMA Signal Line...and PPO Histogram eventually into one indicator with no moving average lines. It's the same concept as the MTF SuperTrend script by Mobius. ONLY the dots in the study will represent the colors on the Histogram on both the MACD and the PPO...I am assuming that same can be done to the FREMA Signal Line and convert it to dots...

So the way to enter LONG would be having at least two of the 3 criteria match...and exit when either the MACD dot would turn Dark GREEN or either the FREMA or PPO Histogram dot to turn RED. I find that this would be an alternative to a SuperTrend indicator that just paints the candlesticks.

 

Top