Daniele Maddaluno

Torben Moving Median

Category: Indicators By: Daniele Maddaluno Created: November 9, 2020, 5:13 PM
November 9, 2020, 5:13 PM
Indicators
13 Comments
Torben Moving Median

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:

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:

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

Download
Filename: TorbenMovingMedian.itf
Downloads: 644
Daniele Maddaluno
Daniele Maddaluno Master
Currently debugging life, so my bio is on hold. Check back after the next commit for an update.
Author’s Profile

Comments

AK27
5 years ago
#

C'est payant visiblement sur linkdin je peux pas vous envoyer, discord vous avez ?

Daniele Maddaluno
5 years ago
#

Daniele Maddaluno
5 years ago
#

AK27
5 years ago
#

Bonjour Daniele j'ai une idée d'indicateur qui va vous plaire je peux vous parler ou ? discord, twitter ? mail ?

Daniele Maddaluno
5 years ago
#

Écrivez-moi où vous préférez you can easily find me on LinkedIn

AK27
5 years ago
#

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 :)

AK27
5 years ago
#

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. :)

Daniele Maddaluno
5 years ago
#

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]

Bruno Carnazzi
5 years ago
#

GENIUS !

yanntorp
6 years ago
#

merci pour vos réponses et le partage de ce code. Cela fonctionne très bien.

yanntorp
6 years ago
#

Bonjour, je souhaiterai savoir comment vous définissez la variable closedbar = t puisque cela me retourne une erreur. Merci pour votre aide.

Daniele Maddaluno
6 years ago
#

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.

Nicolas
6 years ago
#

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.

ProRealCode ProRealCode
Loading...