How to identify the closest pivot points level to current price
02/10/2019 at 9:13 PM #90989
Good Day PRC Community
Let’s say I have identified 10 major levels of support and resistance (some above and some below the current price).
Note: Each major level has its own variable name assigned to it.
How do I determine the level (variable name) directly above the current closing price as well as directly below?02/10/2019 at 9:18 PM #9099002/10/2019 at 9:20 PM #90991
Thinking of solutions as I type this:
First thought is to find all levels (variables) larger than the current close and then use the min of those
Then find all the levels (variables) lower than the current close and then use the max of those
Now to find a practical way to code it02/10/2019 at 9:22 PM #90992
Nested if statements won’t cut it as there are actually 21 levels which will have to be measured against close as well as themselves. Meaning we are looking at hundreds of outcomes. Will have to use a combination of If statements and loops in applying my last mentioned logic.02/10/2019 at 9:33 PM #9099402/10/2019 at 9:41 PM #9099602/10/2019 at 10:35 PM #9099702/10/2019 at 11:10 PM #90998
First thought is to find all levels (variables) larger than the current close and then use the min of those Then find all the levels (variables) lower than the current close and then use the max of those Now to find a practical way to code it
You answered your own question!
So you have 10 support/resistance levels recorded as a1 to a10 that could be anywhere compared to price.
Here is an example using just three levels that can be easily expanded to 10 levels.1234567891011121314151617181920212223242526272829303132333435363738394041424344once above1 = 100000000once above2 = 100000000once above3 = 100000000once below1 = 0once below2 = 0once below3 = 0//Repeat until you have ten start values for each(your code that finds the last ten support and resistance levels and stores them as a1 to a10)//first valueif a1 < 100000000 and a1 > close thenabove1 = a1 - closeendifif a1 > 0 and a1 < close thenbelow1 = close - a1endif//2nd valueif a2 < 100000000 and a2 > close thenabove2 = a2 - closeendifif a2 > 0 and a2 < close thenbelow2 = close - a2endif//3nd valueif a3 < 100000000 and a3 > close thenabove3 = a3 - closeendifif a3 > 0 and a3 < close thenbelow3 = close - a3endif///repeat the above until all 10 options are covered!nearestabove = min(min(above1,above2),above3) //expand to 10nearestbelow = min(min(below1,below2),below3) //expand to 10lb = close - nearestbelowha = close + nearestabove
Written after quite a few glasses of red wine and a beer so might be complete cobblers and usually there is a more efficient way to do it than my first idea!02/11/2019 at 12:19 AM #91000
My above code assumes that every time you find a new support or resistance level then one is dropped and the new one is added like this:123456789newlevel = (value returned by your code for finding new support or resistance levels)if a3 <> newlevel thena1 = a2a2 = a3a3 = newlevelendif// and then onto the code posted above.02/11/2019 at 8:58 AM #91006
Thanks Vonasi, you seem to code just fine with your wine. Probably due to the Ballmer Peak 😉 (https://xkcd.com/323/)
I have added the code, plotting all Monthly, Weekly and Daily Pivot Levels.
But for some reason the result is wacky, The ‘Pivot Above’ in reality is actually the Pivot Directly Below? And the ‘Pivot Below’ is just the lowest pivot??123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241Defparam cumulateorders = False//pivot calculationsTimeframe(Monthly)MHigh = highMClose = closeMLow = lowMPivot = (MHigh + MLow + MClose)/3 //Monthly Pivot CalculationMR1 = MPivot*2-MLowMS1 = MPivot*2-MHighMR2 = MPivot+(MHigh-MLow)MS2 = MPivot-(MHigh-MLow)MR3 = MHigh+(2*(MPivot-MLow))MS3 = MLow-(2*(MHigh-MPivot))Timeframe(Weekly)WHigh = highWClose = closeWLow = lowWPivot = (WHigh + WLow + WClose)/3 //Weekly Pivot CalculationWR1 = WPivot*2-WLowWS1 = WPivot*2-WHighWR2 = WPivot+(WHigh-WLow)WS2 = WPivot-(WHigh-WLow)WR3 = WHigh+(2*(WPivot-WLow))WS3 = WLow-(2*(WHigh-WPivot))TImeframe(Default)If dayofweek = 1 ThenDPivot = (DHigh(2) + DLow(2) + DClose(2))/3 //Daily Pivot CalculationDR1 = DPivot*2-Dlow(2)DS1 = DPivot*2-DHigh(2)DR2 = DPivot+(DHigh(2)-DLow(2))DS2 = DPivot-(DHigh(2)-DLow(2))DR3 = DHigh(2)+(2*(DPivot-DLow(2)))DS3 = DLow(2)-(2*(DHigh(2)-DPivot))ElseDPivot = (DHigh(1) + DLow(1) + DClose(1))/3 //Daily Pivot CalculationDR1 = DPivot*2-Dlow(1)DS1 = DPivot*2-DHigh(1)DR2 = DPivot+(DHigh(1)-DLow(1))DS2 = DPivot-(DHigh(1)-DLow(1))DR3 = DHigh(1)+(2*(DPivot-DLow(1)))DS3 = DLow(1)-(2*(DHigh(1)-DPivot))EndIfIf MR1 + MS1 + MR2 + MS2 + MR3 + MS3 + Mpivot + WR1 + WS1 + WR2 + WS2 + WR3 + WS3 + Wpivot + DR1 + DS1 + DR2 + DS2 + DR3 + DS3 + Dpivot = 0 ThenEndIf//Find Pivot Above and Below CloseIf MR1 > close ThenMR1Above = MR1MR1Below = 0ElsIf MR1 < close ThenMR1Below = MR1MR1Above = 100000EndIfIf MS1 > close ThenMS1Above = MS1MS1Below = 0ElsIf MS1 < close ThenMS1Below = MS1MS1Above = 100000EndIfIf MR2 > close ThenMR2Above = MR2MR2Below = 0ElsIf MR2 < close ThenMR2Below = MR2MR2Above = 100000EndIfIf MS2 > close ThenMS2Above = MS2MS2Below = 0ElsIf MS2 < close ThenMS2Below = MS2MS2Above = 100000EndIfIf MR3 > close ThenMR3Above = MR3MR3Below = 0ElsIf MR3 < close ThenMR3Below = MR3MR3Above = 100000EndIfIf MS3 > close ThenMS3Above = MS3MS3Below = 0ElsIf MS3 < close ThenMS3Below = MS3MS3Above = 100000EndIfIf MPivot > close ThenMPivotAbove = MPivotMPivotBelow = 0ElsIf MR1 < close ThenMPivotBelow = MPivotMPivotAbove = 100000EndIfIf WR1 > close ThenWR1Above = WR1WR1Below = 0ElsIf WR1 < close ThenWR1Below = WR1WR1Above = 100000EndIfIf WS1 > close ThenWS1Above = WS1WS1Below = 0ElsIf WS1 < close ThenWS1Below = WS1WS1Above = 100000EndIfIf WR2 > close ThenWR2Above = WR2WR2Below = 0ElsIf WR2 < close ThenWR2Below = WR2WR2Above = 100000EndIfIf WS2 > close ThenWS2Above = WS2WS2Below = 0ElsIf WS2 < close ThenWS2Below = WS2WS2Above = 100000EndIfIf WR3 > close ThenWR3Above = WR3WR3Below = 0ElsIf WR3 < close ThenWR3Below = WR3WR3Above = 100000EndIfIf WS3 > close ThenWS3Above = WS3WS3Below = 0ElsIf WS3 < close ThenWS3Below = WS3WS3Above = 100000EndIfIf WPivot > close ThenWPivotAbove = WPivotWPivotBelow = 0ElsIf MR1 < close ThenWPivotBelow = WPivotWPivotAbove = 100000EndIfIf DR1 > close ThenDR1Above = DR1DR1Below = 0ElsIf DR1 < close ThenDR1Below = DR1DR1Above = 100000EndIfIf DS1 > close ThenDS1Above = DS1DS1Below = 0ElsIf DS1 < close ThenDS1Below = DS1DS1Above = 100000EndIfIf DR2 > close ThenDR2Above = DR2DR2Below = 0ElsIf DR2 < close ThenDR2Below = DR2DR2Above = 100000EndIfIf DS2 > close ThenDS2Above = DS2DS2Below = 0ElsIf DS2 < close ThenDS2Below = DS2DS2Above = 100000EndIfIf DR3 > close ThenDR3Above = DR3DR3Below = 0ElsIf DR3 < close ThenDR3Below = DR3DR3Above = 100000EndIfIf DS3 > close ThenDS3Above = DS3DS3Below = 0ElsIf DS3 < close ThenDS3Below = DS3DS3Above = 100000EndIfIf DPivot > close ThenDPivotAbove = DPivotDPivotBelow = 0ElsIf DR1 < close ThenDPivotBelow = DPivotDPivotAbove = 100000EndIfPivotAbove = min(min(min(min(min(min(min(min(min(min(min(min(min(min(min(min(min(min(min(min(MR1Above,MS1Above),MR2Above),MS2Above),MR3Above),MS3Above),MPivotAbove),WR1Above),WS1Above),WR2Above),WS2Above),WR3Above),WS3Above),WPivotAbove),DR1Above),DS1Above),DR2Above),DS2Above),DR3Above),DS3ABove),DPivotAbove)PivotBelow = max(max(max(max(max(max(max(max(max(max(max(max(max(max(max(max(max(max(max(max(MR1Below,MS1Below),MR2Below),MS2Below),MR3Below),MS3Below),MPivotBelow),WR1Below),WS1Below),WR2Below),WS2Below),WR3Below),WS3Below),WPivotBelow),DR1Below),DS1Below),DR2Below),DS2Below),DR3Below),DS3Below),DPivotBelow)Graph PivotAbove coloured(255,0,0) as "Pivot Above"Graph PivotBelow coloured(0,255,0) as "Pivot Below"Buy possize contract at market//Graph close coloured(0,0,0) as "close"//Graph MPivot coloured(255,0,0) as "MPivot"//Graph WPivot coloured(0,255,0) as "WPivot"//Graph DPivot coloured(0,0,255) as "DPivot"02/11/2019 at 9:21 AM #9100702/11/2019 at 9:23 AM #9100802/11/2019 at 9:23 AM #9100902/11/2019 at 9:27 AM #9101002/11/2019 at 9:29 AM #91012