Tweaking divergence code
Forums › ProRealTime English forum › ProOrder support › Tweaking divergence code
- This topic has 13 replies, 3 voices, and was last updated 4 years ago by
Mike Boorman.
-
-
11/04/2021 at 11:55 AM #180943
Hello everybody… I wonder if you could help me?
I have some divergence code that looks back a certain number of bars which seems to work okay when I’m comparing a current low that is lower than a previous low, but doesn’t work so well when a current low is higher than a previous low.
My graph command tells me that ‘priorlow’ is 13,801.3 which is nearly a hundred bars back from the current low (the lows are labelled on screengrab), and yet the code only permits a lookback of 30 bars.
I’m wondering if this also has something to do with a market that is constantly rising, but that is just a guess.
Does anyone know how to solve this?
Thank you in advance.
Divergence code sample1234567891011121314151617181920212223242526272829303132333435363738394041424344DEFPARAM CumulateOrders = Falseback=30longhigherlow=0CurLow = lowest[back/2](low)IF low = CurLow THENPriorLow = lowest[back](low[back])CurMACD = MACD[12,26,9](close)FOR i = 0 TO (back - 1)IF low[i + back] = PriorLow THENPriorMACD = MACD[12,26,9](close)[i + back]PriorBAR = BarIndex[i + back]BreakENDIFNEXTENDIFmacconflong= CurMACD < PriorMACDc1 = Curlow > PriorLowc2 = Curlow =lowest[3](low)c3 = close < ExponentialAverage[5](high) and close > ExponentialAverage[5](low)c6 = macconflonglonghigherlow= c1 and c2 and c3 and c6Limitbuy=close-5if not onmarket and longhigherlow=1 thensl=abs(close-lowest[5](low))*Vlstoptp=sl * VLtakeSet target profit tpSet stop loss slBUY 1 PERPOINT AT Limitbuy STOPlonghigherlow=0endifgraph priorlow11/04/2021 at 12:14 PM #180947I did not test it, but it seems you are referencing BACK, while you should reference HALF (if I got what you meant to achieve):
1234567891011121314151617181920212223242526272829303132DEFPARAM CumulateOrders = Falseback=30half=round(back/2)longhigherlow=0CurLow = lowest[half](low)IF low = CurLow THENPriorLow = lowest[half](low[half])CurMACD = MACD[12,26,9](close)FOR i = 0 TO (half - 1)IF low[i + half] = PriorLow THENPriorMACD = MACD[12,26,9](close)[i + half]PriorBAR = BarIndex[i + half]BreakENDIFNEXTENDIFmacconflong= CurMACD < PriorMACDc1 = Curlow > PriorLowc2 = Curlow =lowest[3](low)c3 = close < ExponentialAverage[5](high) and close > ExponentialAverage[5](low)c6 = macconflonglonghigherlow= c1 and c2 and c3 and c6Limitbuy=close-5if not onmarket and longhigherlow=1 thensl=abs(close-lowest[5](low))*Vlstoptp=sl * VLtakeSet target profit tpSet stop loss slBUY 1 PERPOINT AT Limitbuy STOPlonghigherlow=0endifgraph priorlow11/04/2021 at 12:40 PM #180953Thanks for your suggestion, Roberto. It changes things slightly, but doesn’t solve the problem. Priorlow now refers to a bar which is 90 back from the current low, which is not the intention of the code.
I notice that when I reduced the lookback to 28 it found a much more recent low on May14 (where the leftmost blue arrow is on the original screengrab). I hope this helps.
11/04/2021 at 3:17 PM #180959Add this indicator to your chart (see attached screenshot):
12345678910111213141516171819202122232425262728DEFPARAM DrawOnLastBarOnly = trueONCE PriorLow = 0ONCE PriorBar = 0ONCE CurLow = 0ONCE CurBar = 0ONCE back = 30ONCE half = round(back/2)longhigherlow=0CurLow = lowest[half](low)FOR j = 0 TO Half - 1IF low[j] = CurLow THENCurBar = BarIndex[j]CurMACD = MACD[12,26,9](close)[j]//PriorLow = lowest[half](low[half])PriorLow = lowest[half](low[Half])FOR i = Half TO Back-1IF low[i] = PriorLow THENPriorMACD = MACD[12,26,9](close)[i]PriorBAR = BarIndex[i]BreakENDIFNEXTBreakENDIFNEXTDrawArrowUP(PriorBar,PriorLow - highest[5](range)*2) coloured(0,0,255,255)DrawArrowUP(CurBar,CurLow - highest[5](range)*2) coloured(0,0,255,255)RETURNThis is the code for the strategy (I could just test it for syntax errors only):
123456789101112131415161718192021222324252627282930313233343536373839404142DEFPARAM CumulateOrders = FalseONCE VLstop = 1ONCE VLtake = 2ONCE PriorLow = 0ONCE PriorBar = 0ONCE CurLow = 0ONCE CurBar = 0ONCE back = 30ONCE half = round(back/2)longhigherlow=0CurLow = lowest[half](low)FOR j = 0 TO Half - 1IF low[j] = CurLow THENCurBar = BarIndex[j]CurMACD = MACD[12,26,9](close)[j]PriorLow = lowest[half](low[Half])FOR i = Half TO Back-1IF low[i] = PriorLow THENPriorMACD = MACD[12,26,9](close)[i]PriorBAR = BarIndex[i]BreakENDIFNEXTBreakENDIFNEXTmacconflong= CurMACD < PriorMACDc1 = Curlow > PriorLowc2 = Curlow =lowest[3](low)c3 = close < ExponentialAverage[5](high) and close > ExponentialAverage[5](low)c6 = macconflonglonghigherlow= c1 and c2 and c3 and c6Limitbuy=close-5if not onmarket and longhigherlow=1 thensl=abs(close-lowest[5](low))*Vlstoptp=sl * VLtakeSet target profit tpSet stop loss slBUY 1 PERPOINT AT Limitbuy STOPendifgraphonprice priorlow coloured(255,0,0,255)graphonprice curlow coloured(0,128,0,155)2 users thanked author for this post.
11/04/2021 at 7:15 PM #180985Thank you Roberto! 🙂
On first look, the code looks like it is working properly. I will do a longer check tomorrow.
11/05/2021 at 11:12 AM #181019Hey Roberto,
The code looks great, but I can’t get the indicator to work. When I add it, it simply creates a blank box under the chart with a scale on the right-hand side…. no arrows appear on the chart. Are there other settings I need to tweak in order to get it to appear on the chart?
11/05/2021 at 12:31 PM #181028Add it ON your chart, not UNDER.
You have to click the PRICE tag on the top left corner of your chart, then select ADD INDICATOR and choose from the list.
11/05/2021 at 5:07 PM #181061Ah okay, thanks – I’ve added it to the right place now.
Currently I only see the indicator arrows at the most recent bar range. Is it possible to make the indicator return them multiple times going back months or years?
I’m sorry if these are very basic questions, but I’ve never programmed an indicator before. I’ve had a look in the manual and tried a few things but I can’t get them to work. It’d be great if I could see all the instances of new curlows and new priorlows.
Thanks.
11/05/2021 at 7:44 PM #181070You can use this version, but the output is not so tidy:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556DEFPARAM DrawOnLastBarOnly = trueONCE PriorLow = 0ONCE PriorBar = 0ONCE CurLow = 0ONCE CurBar = 0ONCE back = 30ONCE half = round(back/2)ONCE MaxLows = 50 //plot no more than these many arrow pairsIF BarIndex = 0 THENFOR k = 1 TO MaxLows$PriorL[k] = 0$PriorB[k] = 0$CurL[k] = 0$CurB[k] = 0NEXTENDIFlonghigherlow=0CurLow = lowest[half](low)FOR j = 0 TO Half - 1IF low[j] = CurLow THENCurBar = BarIndex[j]CurMACD = MACD[12,26,9](close)[j]PriorLow = lowest[half](low[Half])FOR i = Half TO Back-1IF low[i] = PriorLow THENPriorMACD = MACD[12,26,9](close)[i]PriorBAR = BarIndex[i]IF CurLow <> CurLow[1] AND PriorLow <> PriorLow[1] THEN //shift previous data 1 place backwardsFOR k = MaxLows DOWNTO 2$PriorL[k] = $PriorL[k - 1]$PriorB[k] = $PriorB[k - 1]$CurL[k] = $CurL[k - 1]$CurB[k] = $CurB[k - 1]NEXT$PriorL[1] = PriorLow //add a new #1 set of data$PriorB[1] = PriorBar$CurL[1] = CurLow$CurB[1] = CurBARENDIFBreakENDIFNEXTBreakENDIFNEXTFOR k = 1 TO MaxLowsIF $CurB[k] <> 0 THEN //plot only if there's data in itpl = $PriorL[k]pb = $PriorB[k]cl = $CurL[k]cb = $CurB[k]DrawArrowUP(pb,pl - highest[5](range)*2) coloured(0,255,0,155) //GREEN = prior lowDrawArrowUP(cb,cl - highest[5](range)*2) coloured(0,0,255,255) //BLUE = current lowENDIFNEXTRETURN1 user thanked author for this post.
11/05/2021 at 8:10 PM #181075Oh wow! This is brilliant!!
I’ve adapted it so it only shows arrows when my price-action entry criteria is met, so now the chart doesn’t look so crowded. This is going to save me a lot of time when I cross-check the validity of my divergence parameters (I think they are entering a bit too often using lows that are not significant enough).
Thank you so much Roberto 🙂
11/06/2021 at 3:04 PM #181128I hope this is the final thing on this, Roberto 🙂
I’ve decided to use the PSAR as a way of improving the accuracy of priorlow. I want priorlow to be less than the PSAR in order to allow for an entry but I can’t do it with the code “priorlow < SAR[0.02,0.02,0.2][back]” “priorlow < SAR[0.02,0.02,0.2]”. I assume this is because the priorlow is derived from a lowest bar in a range (which could be bar 26 or whatever) rather than simply the absolute value of the 30th bar back.
How would I do this?
Thank you again.
(I’ve attached a screengrab which graphs “priorlow < SAR[0.02,0.02,0.2][back]” with a black arrow denoting the priorlow bar)
Divergence code12345678910111213141516171819202122232425262728293031323334353637383940414243DEFPARAM CumulateOrders = FalseONCE VLstop = 1ONCE VLtake = 2ONCE PriorLow = 0ONCE PriorBar = 0ONCE CurLow = 0ONCE CurBar = 0ONCE back = 30ONCE half = round(back/2)longhigherlow=0CurLow = lowest[half](low)FOR j = 0 TO Half - 1IF low[j] = CurLow THENCurBar = BarIndex[j]CurMACD = MACD[12,26,9](close)[j]PriorLow = lowest[half](low[Half])FOR i = Half TO Back-1IF low[i] = PriorLow and low[i] THENPriorMACD = MACD[12,26,9](close)[i]PriorBAR = BarIndex[i]BreakENDIFNEXTBreakENDIFNEXTmacconflong= CurMACD < PriorMACDc1 = Curlow > PriorLowc2 = Curlow =lowest[3](low)c3 = close < ExponentialAverage[5](high) and close > ExponentialAverage[5](low)c6 = macconflonglonghigherlow= c1 and c2 and c3 and c6Limitbuy=close-5if not onmarket and longhigherlow=1 and priorlow < SAR[0.02,0.02,0.2][back] thensl=abs(close-lowest[5](low))*Vlstoptp=sl * VLtakeSet target profit tpSet stop loss slBUY 1 PERPOINT AT Limitbuy STOPendifgraph priorlow < SAR[0.02,0.02,0.2][back]11/06/2021 at 6:23 PM #18114311/08/2021 at 1:50 AM #181204What is “and low[i]” for, in line 18?
11/09/2021 at 6:33 PM #181325Hey Roberto,
That’s actually there in error, but it doesn’t make any difference. I’ve deleted it since. I worked out that using [i] was what I needed to do in order to measure the priorlow bar against something else, and I think that I’ve got it working okay…. I hope! 🙂
-
AuthorPosts
Find exclusive trading pro-tools on