Custom Study using MACD and Stochastic crossing at same time


Category:
0
0

A viewer has asked to have some previously published code to be modified slightly. The code was originally published as nothing more than an exercise, teaching how to combine two indicators into a single custom study. Link: https://www.hahn-tech.com/thinkorswim-custom-scan-stochastic-macd/

In creating the video, I had randomly selected the MACD and the Stochastic. However, based on previous comments on YouTube, as well as this viewer’s request; it seems I accidentally landed on an actual trade setup. (well, pretty close anyway). So I did some research to see if what this viewer was requesting had actually been published before, by an authoritative source. Turns out it is quite a popular setup. I’ll provide just a couple links from the Google search that I performed:

http://www.investopedia.com/articles/trading/08/macd-stochastic-double-cross.asp?lgl=myfinance-layout-no-ads

http://www.investopedia.com/articles/forex/07/stochastic_snap.asp

Those articles line up very well with the specifications requested by this viewer.

Marked as spam
Posted by (Questions: 37, Answers: 4087)
Asked on June 11, 2017 12:26 pm
2231 views
0
Private answer

Now before asking questions, be sure to read those articles. Do some additional research on the topic. Then play around with this code to see how it may be able to do the job. I am providing three screenshots to help you see how the settings impact the signals. I will refer to those screenshots as I describe the settings. Each screenshot is numbered.

In screenshot ONE you see the settings. The comments point to specific settings. The first setting tells the code how many bars to allow between the Stochastic cross and the MACD cross. The rarely occur on the same bar. The next four settings are grouped in pairs. Each pair control the upper and lower range within which a Stochastic cross will be counted as valid. (you will likely want to experiment with different ranges after doing your research).

In screenshot TWO we see the custom study at the bottom, plotting green spikes. You see the Stochastic crossing above, within the range established by the default settings. Then you see the MACD cross above zero within 1-2 bars.

In screenshot THREE we see the same study at the bottom plotting red spikes. You see the Stochastic crossing below, within the range established by the default settings. Then you also see the MACD cross below zero within 1-2 bars.

The code is posted below. But I know many of you will be asking if we can make this into a scan. So immediately below the code I will show which lines to change in order to make this into a scan.

declare lower;
input signalBarsTolerance = 2;
input posX_RangeTop = 80;
input posX_RangeBottom = 50;
input negX_RangeTop = 50;
input negX_RangeBottom = 20;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageTypeMACD = AverageType.EXPONENTIAL;
def Diff = MACD(fastLength, slowLength, MACDLength, averageTypeMACD).Diff;
input KPeriod = 10;
input DPeriod = 10;
input priceH = high;
input priceL = low;
input priceC = close;
input averageTypeStoch = AverageType.SIMPLE;
def SlowK = reference StochasticFull(80,20,KPeriod,DPeriod,priceH,priceL,priceC,3,averageTypeStoch).FullK;
def SlowD = reference StochasticFull(80,20,KPeriod,DPeriod,priceH,priceL,priceC,3,averageTypeStoch).FullD;
def histogramCrossesAbove = Diff[1] < 0 and Diff > 0;
def histogramCrossesBelow = Diff[1] > 0 and Diff < 0;
def slowKCrossesAboveSlowD = slowK[1] < slowD[1] and slowK > slowD;
def slowKCrossesBelowSlowD = slowK[1] > slowD[1] and slowK < slowD; def greenRange = slowD[1] > posX_RangeBottom and slowD[1] < posX_RangeTop;
def redRange = slowD[1] < negX_RangeTop and slowD[1] > negX_RangeBottom;
def greenSignal = (histogramCrossesAbove and Highest(slowKCrossesAboveSlowD, signalBarsTolerance) > 0 and greenRange) or (slowKCrossesAboveSlowD and Highest(histogramCrossesAbove, signalBarsTolerance) > 0 and greenRange);
def redSignal = (histogramCrossesBelow and Highest(slowKCrossesBelowSlowD, signalBarsTolerance) > 0 and redRange) or slowKCrossesBelowSlowD and Highest(histogramCrossesBelow, signalBarsTolerance) > 0 and redRange;
plot greenSpike = greenSignal;
greenSpike.SetDefaultColor(Color.GREEN);
plot redSpike = redSignal;
redSpike.SetDefaultColor(Color.RED);

Ok, this next bit is for those looking to use this as a scan. From the code listed above, delete the last four lines. Then add these new lines to build your scans.

# use this scan to find stocks with green spikes
plot scan = greenSignal;
# use this scan to find stock with red spikes
#plot scan = redSignal;

The prerequisite to building and using this as a custom scan is to watch a few of our videos on that topic. You can find them listed at the top of the page under the menus item titled “Free Tutorials”.

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on June 11, 2017 12:27 pm
0

For those who would like to use workspace sharing I am providing a link. This link is for an entire chart grid, the StochMACD indicator is included. http://tos.mx/B7ebLF

( at June 14, 2017 11:22 am)

About Pete Hahn

For those trying to contact me about our professional services you will find all those details here: https://www.hahn-tech.com/about/ Any questions not related to our professional services or premium indicators should be directed to the comment section of the applicable video or you may post a question in our Q&A Forum: https://www.hahn-tech.com/thinkorswim-forum-topics/