MEDIAN function coding

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #62594 quote
    DAK
    Participant
    Average

    Hi everyone,

    I am working on an indicator trying to return the Median function instead of the classic Average one.

    For example the Median value of the 5 latest price records such as : Median (Price, 5)

    I haven’t seen any function of this type in ProRealTime or on the site’s topics so I tried to code it but can’t manage to get a correct result.

    Any help would be greatly appreciated. Thanks !

    #62605 quote
    Nicolas
    Keymaster
    Master

    Please confirm this definition for a Median:

    Median :    which is the middle number of a group of numbers; that is, half the numbers have values that are greater than the median, and half the numbers have values that are less than the median. For example, the median of 2, 3, 3, 5, 7, and 10 is 4.

    #62608 quote
    DAK
    Participant
    Average

    Yes Nicolas I confirm this definition.

    Pretty hard to code…

    #62610 quote
    Nicolas
    Keymaster
    Master

    Yes pretty hard without Arrays. Should be possible with a nested loop (first rough idea that come in mind).. let me think of it.

    #62643 quote
    Despair
    Blocked
    Master

    I solved this once with 2 nested for-loops.

    #62652 quote
    Nicolas
    Keymaster
    Master

    @Despair

    Would save me time if you could share your code snippet with us. Thank you in advance.

    #62656 quote
    Despair
    Blocked
    Master

    I will of course do so but I can’t find it so far. :-S I will search again…

    #62659 quote
    Despair
    Blocked
    Master

    Found it 🙂

    once Median=0
    if barindex>length then
      FOR X = 0 TO length-1
      M = close[X]    //this example takes the median of the last 5 closes
      SmallPart = 0
      LargePart = 0
      FOR Y = 0 TO length-1
       IF close[Y] < M THEN
        SmallPart = SmallPart + 1
       ELSIF close[Y] > M THEN
        LargePart = LargePart + 1
       ENDIF
      IF LargePart = SmallPart AND Y = length-1 THEN
       Median = M
       BREAK
      ENDIF
     NEXT
    NEXT
    endif
    Return Median as "Median"
    
    DAK and Nicolas thanked this post
    #62670 quote
    DAK
    Participant
    Average

    It works very well thank you guys for quick and helpful response !

    #173127 quote
    IG_CFD_Trader
    Participant
    Average

    Hi Nicolas,

    Have you worked out the Median function using arrays?

    Tnx in advance.

    #173148 quote
    robertogozzi
    Moderator
    Master

    Actually I don’t think arrays are of much help in this case, because CLOSE is an array by itself (close[0], close[1], etc…). In any case you will have to always scan LENGTH bars (or elements) to get the MEDIAN.

    #173150 quote
    IG_CFD_Trader
    Participant
    Average

    What is the data set is not CLOSE?

    #173152 quote
    robertogozzi
    Moderator
    Master

    Whatever tha data set, it is historicized (thus becoming an array).

    Be it MedianPrice, TypicalPrice, xClose (Heikin-Ashi close) or any expression assigned to a variable.

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

MEDIAN function coding


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
DAK @dak Participant
Summary

This topic contains 12 replies,
has 2 voices, and was last updated by robertogozzi
4 years, 7 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 02/13/2018
Status: Active
Attachments: No files
Logo Logo
Loading...