Stochastic momentum index bearish bullish crossover scan


Category:
0
0

Hi! I’m looking to make a scan for bearish percentDLength/ percentKLength (fast/slow) crosses that occur above the 40 line and bullish percentDLength percentKLength (fast slow) crosses that occur below -40 for stochastic momentum index indicator.

fought with it for a while and clearly didn’t win haha (i’m new at this). i’ll post post the original code instead of the one i butchered. i hope to combine the stochastic cross over scan with the MACD cross (that i got from this forum) to make a double crossover scan (if it’s practical- if not, i’ll use them separately).

declare lower;

input over_bought = 40.0;
input over_sold = -40.0;
input percentDLength = 3;
input percentKLength = 5;

def min_low = lowest(low, percentKLength);
def max_high = highest(high, percentKLength);
def rel_diff = close – (max_high + min_low)/2;
def diff = max_high – min_low;

def avgrel = expaverage(expaverage(rel_diff, percentDLength), percentDLength);
def avgdiff = expaverage(expaverage(diff, percentDLength), percentDLength);

plot SMI = if avgdiff != 0 then avgrel / (avgdiff / 2) * 100 else 0;
smi.setDefaultColor(getColor(1));

plot AvgSMI = expaverage(smi, percentDLength);
avgsmi.setDefaultColor(getcolor(5));

plot overbought = over_bought;
overbought.setDefaultColor(getcolor(5));

plot oversold = over_sold;
oversold.setDefaultColor(getcolor(5));

Marked as spam
Posted by (Questions: 6, Answers: 8)
Asked on January 22, 2017 1:00 pm
1378 views
1
Private answer

Darn, I really wished you had posted your code. I think we missed an opportunity for learning.

Ok, here it goes. First thing I noticed is that you listed “percentDLength” and “percentKLength” in your specifications. Those are inputs, they can never cross. They are not the two lines that you see plotted on the Stochastic Momentum Index study. The two lines are “SMI” and “AvgSMI”. So those are the two elements we’ll use to generate the crossing signals. Notice in the code you posted they are near the bottom. They begin with the word “plot”. And we can only have one plot statement in a scan. But we can’t delete these two lines because those are the lines we need to test for crossing.

So, we change the word “plot” to “def”, and delete the style statements that follow each. The style statements are easy to spot because as soon as you change the “plot” to “def” the style statements turn red. You will also need to delete the last two “plot” statements, “overbought” and “oversold”. After making these changes the final two lines of code in your script should be:

def SMI = if avgdiff != 0 then avgrel / (avgdiff / 2) * 100 else 0;
def AvgSMI = expaverage(smi, percentDLength);

These are the two lines you want to test for crossing. More specifically, you want to test when SMI crossed back below AvgSMI while AvgSMI is still above the overbought line. And you want to test when SMI crosses back above AvgSMI while AvgSMI is still below the oversold line.

plot scan = AvgSMI > over_bought and SMI[1] > AvgSMI[1] and SMI < AvgSMI;

Let’s walk through that. We have a plot statement named scan. The statements consists of 3 “true/false” conditions, each is separated by the word “and”. Our first condition: AvgSMI of the current bar is greater than over_bought. Where did we get “over_bought” from? It’s one of the inputs, listed at the very top of the code. It has a default value of +40. Our second condition: we have SMI of the previous bar is greater than AvgSMI of the previous bar. The “[1]” is what tells the code to get the value of the previous bar. Our final condition: SMI of the current bar is less than AvgSMI of the current bar.

The other signal you want to scan for is listed below:

plot scan = AvgSMI < over_sold and SMI[1] < AvgSMI[1] and SMI > AvgSMI;

So that’s how it’s done. Hope it helps. Don’t forget to up-vote any answer that best solves you question.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on January 22, 2017 4:03 pm
0
Private answer

Thanks! highly appreciated. i thought combine the MACD cross over with the Stoch crossover to make a double cross over…. nope. i was denied. the error code said i couldnt have two subgraph studies in the scan. this is what i had:

 

#
# TD Ameritrade IP Company, Inc. (c) 2008-2016
#

declare lower;

input over_bought = 40.0;
input over_sold = -40.0;
input percentDLength = 3;
input percentKLength = 5;

def min_low = lowest(low, percentKLength);
def max_high = highest(high, percentKLength);
def rel_diff = close – (max_high + min_low)/2;
def diff = max_high – min_low;

def avgrel = expaverage(expaverage(rel_diff, percentDLength), percentDLength);
def avgdiff = expaverage(expaverage(diff, percentDLength), percentDLength);

def SMI = if avgdiff != 0 then avgrel / (avgdiff / 2) * 100 else 0;
def AvgSMI = expaverage(smi, percentDLength);

#bearish
def scan = AvgSMI > over_bought and SMI[1] > AvgSMI[1] and SMI < AvgSMI;

#bullish
#plot scan = AvgSMI < over_sold and SMI[1] < AvgSMI[1] and SMI > AvgSMI;

def bullishstochcross = AvgSMI < over_sold and SMI[1] < AvgSMI[1] and SMI > AvgSMI;

#—————— MACD section

declare lower;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
def Value = MovingAverage(averageType, close, fastLength) – MovingAverage(averageType, close, slowLength);
def Avg = MovingAverage(averageType, Value, MACDLength);
def diff = Value – Avg;
def ZeroLine = 0;
# now for the signal
def ValueCrossaboveZeroline = Value[1] > Avg[1] and Value[1] > 0 and Value > Avg;
def ValueCrossbelowZeroline = Value[1] < Avg[1] and Value[1] < 0 and Value < Avg;

#——–final—combined–doublecrossover–command—-

plot scan = ValueCrossAboveZeroline and bullishstochcross;

Marked as spam
Posted by (Questions: 6, Answers: 8)
Answered on January 22, 2017 11:53 pm
0
Private answer

Awesome, you attached your code. Now we can really learn some important concepts.

How to Debug the code:

Attached screenshot shows the list of compiler errors. The compiler tells us:

The identifier, diff has already been used. Notice that line is marked red in the attached. Change it from diff to diff2 to clear the error. Fortunately that identifier is not used anywhere else in the code so the change is the same as deleting that line.

The identifier scan has already been used. That line is also marked red in the attached. Change it from scan to scan2 to clear the error.

Error correction is now complete.

Copy Errors:

However there is an problem in the two lines of code for the MACD cross. I compared them to the original one I published and they are not the same. So we need to make sure we replace those two lines with these:

def ValueCrossBelowZeroline = Value[1] < Avg[1] and Value[1] < 0 and Value > Avg;
def ValueCrossAboveZeroline = Value[1] > Avg[1] and Value[1] > 0 and Value < Avg;

Proper Combination and Alignment of Signals:

Now that we can run the scan, it’s important to note the way you combine the two signals in your final plot statement are contradictory.

plot scan2 = ValueCrossAboveZeroline and bullishstochcross;

The value line cross above zero is a bearish signal and does not pair with the bullish stochastic cross. Also, be sure to build a chart and examine how these two indicators are timed. One signal will tend to occur several bars before the other. If you try to scan for both happening at the same time you will almost never get a single result. So you have to allow for some space between the two signals. There are many ways to accomplish this, some are more affective, while others are easier to code.

plot scan2 = ValueCrossbelowZeroline and bullishstochcross within 5 bars;

This one is easiest to code. “within 5 bars” will find bullish stochastic cross that occurs within 5 bars of the MACD cross occurring. If you reverse the two conditions you will get different results. Here is an example showing the two conditions reversed.

plot scan2 = bullishstochcross and ValueCrossbelowZeroline within 5 bars;

So we found that mashing together two lower studies into a single custom scan can be a very complex task. We also learned that there are several tools available to debug the code and get things lined up correctly. It takes a lot of experience to become proficient with those tools. I can debug this code in about 2 minutes. But it took me 30 minutes to draft this reply and explain the process step-by-step.

Hope it helps.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on January 23, 2017 9:28 am
0
Private answer

thanks! making progress now. the scan is working now but it seems like my MACD bullish cross above its zero line command is not working. the scan came up with LGND for the MACD isn’t what i would expect it to be. the stoch cross is correct however. here’s what i have so far:

#
# TD Ameritrade IP Company, Inc. (c) 2008-2016
#

declare lower;

input over_bought = 40.0;
input over_sold = -40.0;
input percentDLength = 3;
input percentKLength = 5;

def min_low = lowest(low, percentKLength);
def max_high = highest(high, percentKLength);
def rel_diff = close – (max_high + min_low)/2;
def diff = max_high – min_low;

def avgrel = expaverage(expaverage(rel_diff, percentDLength), percentDLength);
def avgdiff = expaverage(expaverage(diff, percentDLength), percentDLength);

def SMI = if avgdiff != 0 then avgrel / (avgdiff / 2) * 100 else 0;
def AvgSMI = expaverage(smi, percentDLength);

#bearish
#def = AvgSMI > over_bought and SMI[1] > AvgSMI[1] and SMI < AvgSMI;

#bullish
#def = AvgSMI < over_sold and SMI[1] < AvgSMI[1] and SMI > AvgSMI;

def bullishstochcross = AvgSMI < over_sold and SMI[1] < AvgSMI[1] and SMI > AvgSMI;

#—————— MACD section

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
def Value = MovingAverage(averageType, close, fastLength) – MovingAverage(averageType, close, slowLength);
def Avg = MovingAverage(averageType, Value, MACDLength);
def diff2 = Value – Avg;
def ZeroLine = 0;
# now for the signal
def ValueCrossBelowZeroline = Value[1] < Avg[1] and Value[1] < 0 and Value > Avg;
def ValueCrossAboveZeroline = Value[1] > Avg[1] and Value[1] > 0 and Value < Avg;

#——–final—combined–doublecrossover–command—-

plot scan = bullishstochcross and ValueCrossaboveZeroline > zeroLine within 5 bars;

Marked as spam
Posted by (Questions: 6, Answers: 8)
Answered on January 23, 2017 3:35 pm
0
Private answer

got it! i had to fixed this line:

def ValueCrossAboveZeroline = Value[1] < Avg[1] and Value[1] > 0 and Value > Avg;

the code didnt turn up results for nasdaq stocks more than 20 dollars or options volume more than 1000 but least i know the scan work.

time for me to try to do a simple TTM scan that includes a b c waves.

Marked as spam
Posted by (Questions: 6, Answers: 8)
Answered on January 23, 2017 4:01 pm
0

Heads-up, go back and review my write-up and check the section titled ”copy errors”. From what I see in your ”correction” you still don’t have the conditions setup correctly.

( at January 23, 2017 7:02 pm)