MACD Divergence setup
Forums › ProRealTime English forum › ProOrder support › MACD Divergence setup
- This topic has 2 replies, 2 voices, and was last updated 4 years ago by
Mike Boorman.
-
-
06/28/2021 at 3:26 PM #172660
I know there have been plenty of indicators written for divergence in this forum, but having looked through all of them I still can’t quite grasp how to adapt them for my purposes, so I wonder if anyone can help me with this scenario?
I want to capture the bar that contains the previous lowest low in the range (in this case it would be the 9th bar – see screengrab), and then calculate whether or not the MACD at that bar is lower than the MACD at current bar close.
Currently my code is only able to calculate divergence of the MACD based on current bar close versus the bar that is defined by the static lookback range (in this case 15).
Does anyone know how I could query the MACD with a lookback bar that is derived from a previous low?
Thank you in advance…
Divergence rough code123456789101112131415161718192021222324DEFPARAM CumulateOrders = Falseback=15//bars to look back for calculating lowlongentryflag=0macconf= MACD[12,26,9](close) > MACD[12,26,9][back]IF low < lowest[back] and close > open and close > ExponentialAverage[5](low) THENlongentryflag=1if not onmarket and macconf and longentryflag=1 thensl=max(5,AverageTrueRange[14](close))tp= sl*2Set target profit tpSet stop loss slBUY 1 PERPOINT AT MARKETendifENDIFgraph macconf06/29/2021 at 10:40 AM #1727061. assume the current LOW is the lowest within back bars
2. scan the previous back bars for a previous low, starting from back-1 bars, for another back bars
3. us a FOR…NEXT iteration to find the bar where that happened, THAT will be the bar when you can check MACD for a divergence123456789101112CurLow = lowest[back](low)IF low = CurLow THENCurMACD = MACD(12,26,9)(close)PriorLow = lowest[back](low[back])FOR i = 0 TO (Back - 1)IF low[i + Back] = PriorLow THENPriorMACD = MACD(12,26,9)(close)[i + back]PriorBAR = BarIndex[i + back]BreakENDIFNEXTENDIFNot tested.
1 user thanked author for this post.
06/29/2021 at 1:26 PM #172722Thank you very much Roberto. I think this is close but there is still something I am doing incorrectly. The only way I could get my whole code to work was by querying the MACD with this: CurMACD > PriorMACD…….. but that is only doing a static read of [back], as before.
I’ve attached a screengrab of where there should be an entry at 15:00, based on a rising MACD from the low at 14:05. My “macconf” needs to be true for there to be an entry at 15:00. I’m guessing that maybe I need to use PriorBAR somewhere else in the code? But I’m not sure how. Would you mind helping me here?
Thank you 🙂
Divergence lookback code1234567891011121314151617181920212223242526272829303132333435DEFPARAM CumulateOrders = Falseback=15longentryflag=0CurLow = lowest[back](low)IF low = CurLow THENCurMACD = MACD[12,26,9](close)PriorLow = lowest[back](low[back])FOR i = 0 TO (Back - 1)IF low[i + Back] = PriorLow THENPriorMACD = MACD[12,26,9](close)[i + back]PriorBAR = BarIndex[i + back]BreakENDIFNEXTENDIFmacconf= CurMACD > PriorMACDIF close < PriorLow and close > ExponentialAverage[5](low) THENlongentryflag=1if not onmarket and macconf and longentryflag=1 thensl=max(5,AverageTrueRange[14](close))tp= sl*1Set target profit tpSet stop loss slBUY 1 PERPOINT AT MARKETendifENDIFgraph macconf -
AuthorPosts
Find exclusive trading pro-tools on