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.
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.