Bullish Stack SMA's Scan

Ktracho13

New member
Hello Everyone,

I've been a reader and user of useThinkScript for quite some time, thank you all for your collaborations it really helps. The work and the ideas are phenomenal in this community.

I would like to see if its possible to get assistance on creating a SMA stack scanner utilizing the 8, 21, 34, 55, 89, 100 and 200 period SMA's. I've been trying to use the moving average scan and try build from there.

see below code;

Code:
input price = close;
input lenght1 = 8;
input lenght2 = 21;
input lenght3 = 34;
input lenght4 = 55;
input lenght5 = 89;
input lenght6 = 100;
input lenght7 = 200;

input averageType1 = AverageType.Simple;
input averageType2 = AverageType.Simple;
input averageType3 = AverageType.Simple;
input averageType4 = AverageType.Simple;
input averageType5 = AverageType.Simple;
input averageType6 = AverageType.Simple;
input averageType7 = AverageType.Simple;
input percentdiff = 0;

def avg1 = movingAverage(averageType1,price,lenght1);
def avg2 = movingAverage(averageType2,price,lenght2);
def avg3 = movingAverage(averageType3,price,lenght3);
def avg4 = movingAverage(averageType4,price,lenght4);
def avg5 = movingAverage(averageType5,price,lenght5);
def avg6 = movingAverage(averageType6,price,lenght6);
def avg7 = movingAverage(averageType7,price,lenght7);

plot scan = close >= avg1 > avg2 > avg3 > avg4 > avg5 > avg6 > avg7 >(1 + percentdiff / 100 );

If it can be done also with EMA's Stack.

Any help will be appreciated.

Thank you.
 

MerryDay

Well-known member
VIP
@Ktracho13,
The Madrid Ribbons is a study used to chart stacked moving averages. You will find Strategies which are useful for backtesting have an ADDORDER statement at the bottom of the code.

The code you provided in your initial post is your scanning script. There are no matches for your scan given your parameters but it is a functional script.
 

Ktracho13

New member
@MerryDay Hi, Wow, so the scanner code is functional, I guess I'm not so far off for someone with little knowledge in thinkscript.

Back testing strategies in thinkorswim is something I always been interested in trying but don't know how to actually do it. Is there a step by step instructions on how to create it one here?

I appreciate your help.
 

MerryDay

Well-known member
VIP
A search on this site shows almost 700 results for 'moving average strategy'

They are very popular. Test them out and get a feel for what works for you.

When I first started backtesting I was so surprised to learn my strategies fared so poorly in backtesting.
It explained why my actual trading results were chaotic: my strategy was full of holes, who knew?

Some things to keep in mind when backtesting is that backtesting will show results more favorable than real performance because many live conditions cannot be simulated. Backtesting cannot be used in conjunction with any indicator that repaints. There are other caveats but if you read enough of the posts here, you will be a backtesting guru in no time.
 

Ktracho13

New member
I will do a search for that, I've been using some indicators created here and strategies. There is one indicator I would like to do a backtesting. Found some nice scanners here as well.

Thank you for your time.
 

mmturner21

New member
Is there a code that can I can use for my watchlist to determine if EMAs are positively stacked ? I am looking to see if the 8, 21, 34, 55, and 89 are all stacked in order. Thanks!
 

Beltrame1

New member
VIP
I would appreciate it if anyone in here could add a piece of code to this indicator that would sound an alert and plot a bubble for when the EMAS are stacked in this order: ema0>ema1>ema2 (3 above the 8 and 8 above the 20) and also the opposite, only when going through the body of the same candle (wicks included) like in the pictures attached down below, the second picture has 2 valid signals, but just need the first. I am also trying to scan for this condition and the scan code is below, but I think it needs some fine tuning which is also very appreciated @BenTen, @horserider, @MerryDay, @YungTraderFromMontana

Code:
#MAs_Wave Code

declare upper;

input showlabels = yes;

input crossingType = {default above, below};

input price = close;

plot ema0 = ExpAverage (close, 3);
plot ema1 = ExpAverage (close, 8);
plot ema2 = ExpAverage (close, 20);

ema0.SetDefaultColor(Color.CYAN);
ema0.SetLineWeight(1);
ema1.SetDefaultColor(Color.GREEN);
ema1.SetLineWeight(1);
ema2.SetDefaultColor(Color.RED);
ema2.SetLineWeight(1);

AddCloud(ema0, ema1, Color.DARK_GRAY, Color.DARK_GRAY);
AddCloud(ema1, ema2, Color.GRAY, Color.GRAY);

AddLabel(showlabels, "9ema = " + Round(ema0, 2), if close >= ema0 then Color.uptick else Color.downtick);
AddLabel(showlabels, "20ema = " + Round(ema1, 2), if close >= ema1 then Color.uptick else Color.downtick);
addlabel(showlabels, "200ema = " + round(ema2, 2), if close >= ema2 then color.uptick else color.downtick);

IMG-20201229-WA0138.png


20210108_211154.jpg


Scan conditions:

Code:
MovAvgExponential("length" = 3)."AvgExp" is less than or equal to high from 1 bars ago and MovAvgExponential("length" = 3)."AvgExp" is greater than MovAvgExponential("length" = 8)."AvgExp" from 1 bars ago and MovAvgExponential("length" = 8)."AvgExp" is greater than MovAvgExponential("length" = 20)."AvgExp" from 1 bars ago and MovAvgExponential("length" = 20)."AvgExp" is greater than or equal to low from 1 bars ago and close is greater than close from 1 bars ago
 
Last edited:

JedF

New member
Thank you for the reply, that looks like a strategy, but how can I scan for stocks with close above the stacks SMA's. Is there a scanner for this version?
I was wondering if you were ever able to get this stacked SMA scanner to return results. I'm trying to use your code and it's not returning anything even though the scan panel indicates there are thousands of matches. Thanks!
 

rad14733

Well-known member
VIP
I was wondering if you were ever able to get this stacked SMA scanner to return results. I'm trying to use your code and it's not returning anything even though the scan panel indicates there are thousands of matches. Thanks!

For scanning you surely don't need the Madrid Moving Average Ribbons as they are WAY overkill... An EMA_x3 or EMA_x5 is more than enough to go by and will reduce the load on the TOS servers that causes the lag that so many members complain about... Don't use anything more than needed as that's just smart coding... I use an EMA_x5_Stack and often turn two of those EMA's off, reducing it the an EMA_x3_Stack... I use the code for Charts, Watchlist Column, and Scanning so, yes, it can be done... Please don't ask me to share my code as I won't support it... It's not difficult to code and would be a good learning example for you... Some of my code, as well as some EMA_x3_Stack code should be posted here in the forums if you do some scouting around... You may need to mix around the verbiage to find what you're looking for but I know there is code here...
 

JedF

New member
For scanning you surely don't need the Madrid Moving Average Ribbons as they are WAY overkill... An EMA_x3 or EMA_x5 is more than enough to go by and will reduce the load on the TOS servers that causes the lag that so many members complain about... Don't use anything more than needed as that's just smart coding... I use an EMA_x5_Stack and often turn two of those EMA's off, reducing it the an EMA_x3_Stack... I use the code for Charts, Watchlist Column, and Scanning so, yes, it can be done... Please don't ask me to share my code as I won't support it... It's not difficult to code and would be a good learning example for you... Some of my code, as well as some EMA_x3_Stack code should be posted here in the forums if you do some scouting around... You may need to mix around the verbiage to find what you're looking for but I know there is code here...
(y) Thanks for weighing in!!
 

cabe1332

Member
(y) Thanks for weighing in!!

@JedF add the following code as a custom study in your scan. This works and provides you with what you're looking for the EMA's stacked.

Code:
#EMA_Stack Label
#cabe1332

# start of code used for scanning

def stackedUp = MovAvgExponential("length" = 8)."AvgExp" is greater than MovAvgExponential("length" = 21)."AvgExp"
and MovAvgExponential("length" = 21)."AvgExp" is greater than MovAvgExponential("length" = 34)."AvgExp"
and MovAvgExponential("length" = 34)."AvgExp" is greater than MovAvgExponential("length" = 55)."AvgExp"
and MovAvgExponential("length" = 55)."AvgExp" is greater than MovAvgExponential("length" = 89)."AvgExp";

def state = if stackedUp then 1 else 0;
plot data = state;

# end of code


#EMA_Stack Label
#cabe1332

# start of code
def stackedUp = MovAvgExponential("length" = 8)."AvgExp" is greater than MovAvgExponential("length" = 21)."AvgExp"
and MovAvgExponential("length" = 21)."AvgExp" is greater than MovAvgExponential("length" = 34)."AvgExp"
and MovAvgExponential("length" = 34)."AvgExp" is greater than MovAvgExponential("length" = 55)."AvgExp"
and MovAvgExponential("length" = 55)."AvgExp" is greater than MovAvgExponential("length" = 89)."AvgExp";

def stackedDn = MovAvgExponential("length" = 8)."AvgExp" is less than MovAvgExponential("length" = 21)."AvgExp"
and MovAvgExponential("length" = 21)."AvgExp" is less than MovAvgExponential("length" = 34)."AvgExp"
and MovAvgExponential("length" = 34)."AvgExp" is less than MovAvgExponential("length" = 55)."AvgExp"
and MovAvgExponential("length" = 55)."AvgExp" is less than MovAvgExponential("length" = 89)."AvgExp";

def state = if stackedUp then 1 else if stackedDn then -1 else 0;

AddLabel(yes, "EMA Stacks", if stackedUp then Color.GREEN else Color.RED);

#end of code

I hope that helps. Good luck!
 

Ktracho13

New member
I was wondering if you were ever able to get this stacked SMA scanner to return results. I'm trying to use your code and it's not returning anything even though the scan panel indicates there are thousands of matches. Thanks!
Use this code it should work, all you have to do is set up the conditions for price volume etc

Code:
def SMA8 = SimpleMovingAvg(close,8);
def SMA13 = SimpleMovingAvg(close,13);
def SMA21 = SimpleMovingAvg(close,21);
def SMA34 = SimpleMovingAvg(close,34);
def SMA55 = SimpleMovingAvg(close,55);
def SMA89 = SimpleMovingAvg(close,89);

def bullish = SMA8 > SMA13 and SMA13 > SMA21 and SMA21 > SMA34 and SMA34 > SMA55 and SMA55 > SMA89;

input price = close;
input length = 20;
input AverageType = averageType.SIMPLE;
input percentdiff = 10.0;
input choice = {default "Above","Below"};
def avg = movingAverage(averageType,price,length);

plot scan;
switch (Choice){
case "Above":
    scan = price >= avg * (1 + percentdiff / 100 );
case "Below":
    scan = price <= avg * (1 -  percentdiff / 100 );
}
 

Similar threads

Top