Median filtering is a commonly used technique in signal processing. Typically used on signals that may contain outliers skewing the usual statistical estimators, it is usually considered too expensive to be implemented in real-time or CPU-intensive applications.
Let us define the median of N numerical values by:
- The median of a list of N values is found by sorting the input array in increasing order, and taking the middle value.
- The median of a list of N values has the property that in the list there are as many greater as smaller values than this element.
Example:
1 2 |
input values: 19 10 84 11 23 median: 19 |
Torben’s method
This method was pointed out by Torben Mogensen.
It is certainly not the fastest way of finding a median, but it has the very interesting property that it does not modify the input array when looking for the median. It becomes extremely powerful when the number of elements to consider starts to be large, and copying the input array may cause enormous overheads. For read-only input sets of several hundred megabytes in size, it is the solution of choice, also because it accesses elements sequentially and not randomly. Beware that it needs to read the array several times though: a first pass is only looking for min and max values, further passes go through the array and come out with the median.
Credits to Fast median search: an ANSI C implementation.
PRT implementation
I have translated that C implementation by N. Devillard of Torben Mogensen algorithm in PRT, obtaining this moving median indicator:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
// Settings: length = 20, closedbar = t // // Algorithm by Torben Mogensen // For more see "Fast median search: an ANSI C implementation": // http://ndevilla.free.fr/median/median/index.html // Translated by Daniele Maddaluno from torben.c implementation (Algorithm by Torben Mogensen, implementation by N. Devillard): // http://ndevilla.free.fr/median/median/src/torben.c // // 04.11.2020 // Author: Daniele Maddaluno median = undefined if barindex>length then // find series min and max price = customclose[closedbar] smin = lowest[length](price) smax = highest[length](price) while(1) do guess = (smin+smax)/2 less = 0 greater = 0 equal = 0 maxltguess = smin mingtguess = smax for i = 0 to length-1 do if (price[i]<guess) then less = less + 1 if (price[i]>maxltguess) then maxltguess = price[i] endif elsif (price[i]>guess) then greater = greater + 1 if (price[i]<mingtguess) then mingtguess = price[i] endif else equal = equal + 1 endif next if (less <= (length+1)/2 and greater <= (length+1)/2) then break elsif (less>greater) then smax = maxltguess else smin = mingtguess endif wend // Set return value if (less >= (length+1)/2) then median = maxltguess elsif(less+equal >= (length+1)/2) then median = guess else median = mingtguess endif endif return median |
Share this
No information on this site is investment advice or a solicitation to buy or sell any financial instrument. Past performance is not indicative of future results. Trading may expose you to risk of loss greater than your deposits and is only suitable for experienced investors who have sufficient financial means to bear such risk.
ProRealTime ITF files and other attachments :PRC is also on YouTube, subscribe to our channel for exclusive content and tutorials
Bonjour,
je souhaiterai savoir comment vous définissez la variable closedbar = t puisque cela me retourne une erreur.
Merci pour votre aide.
Le plus simple est de télécharger le fichier itf présent sur cette page et de l’importer dans la plateforme, vous aurez ainsi accès à toutes les fonctionnalités du code directement.
Le closedbar est simplement une variable de type boolean, et = t signifie qu’il est égal à true (c’est-à-dire checked box, 1).
Cependant, il est beaucoup plus rapide d’importer le fichier “.itf”, comme l’a suggéré Nicolas.
The closedbar is just a variable of type boolean, and = t means it is equal to true (i.e. a checked box, 1).
However, it is much faster to import the “.itf” file, as Nicolas suggested.
merci pour vos réponses et le partage de ce code. Cela fonctionne très bien.
GENIUS !
Bonjour pensez vous qu’il est possible de creer un screener par rapport à cet indicateur, que le screener avertisse dès qu’une bougie validée clôture au dessus de la torben 20 par exemple 🙂
Bien sûr, vous pouvez écrire quelque chose comme ceci dans un screener:
torben = CALL “Torben Moving Median”[20, 1](close)
filter = close crosses over torben
screener[filter]
merci Daniele vous êtes top, vous pensez qu’il est plus judicieux d’utiliser un screener de croisement de torben ou de franchissement du prix de la torben 20 ? je priorise le % de réussite au % de gain. 🙂
Bonjour Daniele j’ai une idée d’indicateur qui va vous plaire je peux vous parler ou ? discord, twitter ? mail ?
Écrivez-moi où vous préférez you can easily find me on LinkedIn
C’est payant visiblement sur linkdin je peux pas vous envoyer, discord vous avez ?