How to identify the closest pivot points level to current price

Viewing 15 posts - 1 through 15 (of 24 total)
  • Author
    Posts
  • #90989 quote
    juanj
    Participant
    Master

    Good Day PRC Community

    I have a question which I hope some of the other experienced coders (@Nicolas @Vonasi @RobertoGozzi) here can help me with:

    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.

    Question:

    How do I determine the level (variable name) directly above the current closing price as well as directly below?

    #90990 quote
    robertogozzi
    Moderator
    Master

    Since arrays are not supported, you’ll have to use IF…ELSIF… ENDIF to test each time all the 10 variables.

    Thers’s no shorter workaround.

    #90991 quote
    juanj
    Participant
    Master

    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 it

    #90992 quote
    juanj
    Participant
    Master

    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.

    #90994 quote
    juanj
    Participant
    Master

    Coding this without arrays is going to tricky but I want to believe there is a way without writing a 21×21=441 condition nested if statement

    #90996 quote
    juanj
    Participant
    Master

    My brain is breaking over this, every solution keep going back to needing arrays

    #90997 quote
    robertogozzi
    Moderator
    Master

    No workarounds, sorry.

    We must wait till end of March to discover any new feature in version 11, but I guess arrays are not going to be supported, yet.

    #90998 quote
    Vonasi
    Moderator
    Master

    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.

    once above1 = 100000000
    once above2 = 100000000
    once above3 = 100000000
    once below1 = 0
    once below2 = 0
    once 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 value
    if a1 < 100000000 and a1 > close then 
    above1 = a1 - close
    endif
    
    if a1 > 0 and a1 < close then 
    below1 = close - a1
    endif
    
    //2nd value
    if a2 < 100000000 and a2 > close then 
    above2 = a2 - close
    endif
    
    if a2 > 0 and a2 < close then 
    below2 = close - a2
    endif
    
    //3nd value
    if a3 < 100000000 and a3 > close then 
    above3 = a3 - close
    endif
    
    if a3 > 0 and a3 < close then 
    below3 = close - a3
    endif
    
    ///repeat the above until all 10 options are covered!
    
    nearestabove = min(min(above1,above2),above3) //expand to 10
    nearestbelow = min(min(below1,below2),below3) //expand to 10
    
    lb = close - nearestbelow
    ha = 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!

    #91000 quote
    Vonasi
    Moderator
    Master

    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:

    newlevel = (value returned by your code for finding new support or resistance levels)
    
    if a3 <> newlevel then
    a1 = a2
    a2 = a3
    a3 = newlevel
    endif
    
    // and then onto the code posted above.
    #91006 quote
    juanj
    Participant
    Master

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

    Defparam cumulateorders = False
    
    //pivot calculations
    
    Timeframe(Monthly)
    
    MHigh = high[1]
    MClose = close[1]
    MLow = low[1]
    
    MPivot = (MHigh + MLow + MClose)/3 //Monthly Pivot Calculation
    
    MR1 = MPivot*2-MLow
    MS1 = MPivot*2-MHigh
    MR2 = MPivot+(MHigh-MLow)
    MS2 = MPivot-(MHigh-MLow)
    MR3 = MHigh+(2*(MPivot-MLow))
    MS3 = MLow-(2*(MHigh-MPivot))
    
    Timeframe(Weekly)
    
    WHigh = high[1]
    WClose = close[1]
    WLow = low[1]
    
    WPivot = (WHigh + WLow + WClose)/3 //Weekly Pivot Calculation
    
    WR1 = WPivot*2-WLow
    WS1 = WPivot*2-WHigh
    WR2 = WPivot+(WHigh-WLow)
    WS2 = WPivot-(WHigh-WLow)
    WR3 = WHigh+(2*(WPivot-WLow))
    WS3 = WLow-(2*(WHigh-WPivot))
    
    TImeframe(Default)
    
    If dayofweek = 1 Then
    DPivot = (DHigh(2) + DLow(2) + DClose(2))/3 //Daily Pivot Calculation
    
    DR1 = 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))
    Else
    DPivot = (DHigh(1) + DLow(1) + DClose(1))/3 //Daily Pivot Calculation
    
    DR1 = 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))
    EndIf
    
    If MR1 + MS1 + MR2 + MS2 + MR3 + MS3 + Mpivot + WR1 + WS1 + WR2 + WS2 + WR3 + WS3 + Wpivot + DR1 + DS1 + DR2 + DS2 + DR3 + DS3 + Dpivot = 0 Then
    EndIf
    
    //Find Pivot Above and Below Close
    
    If MR1 > close Then
    MR1Above = MR1
    MR1Below = 0
    ElsIf MR1 < close Then
    MR1Below = MR1
    MR1Above = 100000
    EndIf
    
    If MS1 > close Then
    MS1Above = MS1
    MS1Below = 0
    ElsIf MS1 < close Then
    MS1Below = MS1
    MS1Above = 100000
    EndIf
    
    If MR2 > close Then
    MR2Above = MR2
    MR2Below = 0
    ElsIf MR2 < close Then
    MR2Below = MR2
    MR2Above = 100000
    EndIf
    
    If MS2 > close Then
    MS2Above = MS2
    MS2Below = 0
    ElsIf MS2 < close Then
    MS2Below = MS2
    MS2Above = 100000
    EndIf
    
    If MR3 > close Then
    MR3Above = MR3
    MR3Below = 0
    ElsIf MR3 < close Then
    MR3Below = MR3
    MR3Above = 100000
    EndIf
    
    If MS3 > close Then
    MS3Above = MS3
    MS3Below = 0
    ElsIf MS3 < close Then
    MS3Below = MS3
    MS3Above = 100000
    EndIf
    
    If MPivot > close Then
    MPivotAbove = MPivot
    MPivotBelow = 0
    ElsIf MR1 < close Then
    MPivotBelow = MPivot
    MPivotAbove = 100000
    EndIf
    
    If WR1 > close Then
    WR1Above = WR1
    WR1Below = 0
    ElsIf WR1 < close Then
    WR1Below = WR1
    WR1Above = 100000
    EndIf
    
    If WS1 > close Then
    WS1Above = WS1
    WS1Below = 0
    ElsIf WS1 < close Then
    WS1Below = WS1
    WS1Above = 100000
    EndIf
    
    If WR2 > close Then
    WR2Above = WR2
    WR2Below = 0
    ElsIf WR2 < close Then
    WR2Below = WR2
    WR2Above = 100000
    EndIf
    
    If WS2 > close Then
    WS2Above = WS2
    WS2Below = 0
    ElsIf WS2 < close Then
    WS2Below = WS2
    WS2Above = 100000
    EndIf
    
    If WR3 > close Then
    WR3Above = WR3
    WR3Below = 0
    ElsIf WR3 < close Then
    WR3Below = WR3
    WR3Above = 100000
    EndIf
    
    If WS3 > close Then
    WS3Above = WS3
    WS3Below = 0
    ElsIf WS3 < close Then
    WS3Below = WS3
    WS3Above = 100000
    EndIf
    
    If WPivot > close Then
    WPivotAbove = WPivot
    WPivotBelow = 0
    ElsIf MR1 < close Then
    WPivotBelow = WPivot
    WPivotAbove = 100000
    EndIf
    
    If DR1 > close Then
    DR1Above = DR1
    DR1Below = 0
    ElsIf DR1 < close Then
    DR1Below = DR1
    DR1Above = 100000
    EndIf
    
    If DS1 > close Then
    DS1Above = DS1
    DS1Below = 0
    ElsIf DS1 < close Then
    DS1Below = DS1
    DS1Above = 100000
    EndIf
    
    If DR2 > close Then
    DR2Above = DR2
    DR2Below = 0
    ElsIf DR2 < close Then
    DR2Below = DR2
    DR2Above = 100000
    EndIf
    
    If DS2 > close Then
    DS2Above = DS2
    DS2Below = 0
    ElsIf DS2 < close Then
    DS2Below = DS2
    DS2Above = 100000
    EndIf
    
    If DR3 > close Then
    DR3Above = DR3
    DR3Below = 0
    ElsIf DR3 < close Then
    DR3Below = DR3
    DR3Above = 100000
    EndIf
    
    If DS3 > close Then
    DS3Above = DS3
    DS3Below = 0
    ElsIf DS3 < close Then
    DS3Below = DS3
    DS3Above = 100000
    EndIf
    
    If DPivot > close Then
    DPivotAbove = DPivot
    DPivotBelow = 0
    ElsIf DR1 < close Then
    DPivotBelow = DPivot
    DPivotAbove = 100000
    EndIf
    
    PivotAbove = 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"
    #91007 quote
    Vonasi
    Moderator
    Master

    I’ve only just glanced at the code but can see straight away that line 231 is wrong. They should all be MIN and not MAX as we are looking for the smallest distance  below price and not the biggest.

    #91008 quote
    juanj
    Participant
    Master

    @Nicolas @Vonasi I suspect my above-mentioned problem is possibly due to the way MTF updates values? If you graph close in relation to ‘Pivot Above’ and ‘Pivot Below’ you can clearly see there is a problem. Either that or the nested Max statement is failing.

    #91009 quote
    Vonasi
    Moderator
    Master

    Also I notice that when you store the values of ABOVE and BELOW you store the actual price whereas in my code I stored the difference between price and the level.

    #91010 quote
    Vonasi
    Moderator
    Master

    Sorry – only one cup of tea so far so not thinking straight! I can see that you have written your code differently to mine. I have to go out now but will try to look at it more closely when I get back.

    #91012 quote
    juanj
    Participant
    Master

    Since the pivot level was still a value I didn’t think it a problem to just use the straight level instead of the difference between the close and price. But will change it and see

Viewing 15 posts - 1 through 15 (of 24 total)
  • You must be logged in to reply to this topic.

How to identify the closest pivot points level to current price


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
juanj @juanj Participant
Summary

This topic contains 23 replies,
has 5 voices, and was last updated by GraHal
7 years ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 02/10/2019
Status: Active
Attachments: No files
Logo Logo
Loading...