Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
5 registered members (TipmyPip, AndrewAMD, Quad, aliswee, degenerate_762), 970 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 2 of 2 1 2
Re: LowPass filter formula [Re: deweymcg] #410542
11/05/12 08:20
11/05/12 08:20
Joined: Jul 2000
Posts: 27,977
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,977
Frankfurt
That looks not like a 2-pole lowpass filter, but like a Butterworth filter.

However it illustrates good the coding overhead of MQL4, even though it's also based on C. A 3rd order Butterworth filter in lite-C looks like this:

Code:
var Butterworth(var *Data,int Cutoff)
{
	var a = exp(-PI / Cutoff);
	var b = 2*a*cos(1.738*PI / Cutoff);
	var c = a*a;
	var c1 = b + c;
	var c2 = -(c + b*c);
	var c3 = c*c;
	var c0 = 1 - c1 - c2 - c3;

	var* Filt = series(*Data,4);
	return Filt[0] = c0*Data[0] + c1*Filt[1] + c2*Filt[2] + c3*Filt[3];
}


Re: LowPass filter formula [Re: jcl] #410767
11/08/12 14:37
11/08/12 14:37
Joined: Nov 2012
Posts: 19
Texas, US
D
deweymcg Offline
Newbie
deweymcg  Offline
Newbie
D

Joined: Nov 2012
Posts: 19
Texas, US
George on Steve Hopwood's forum was kind enough to supply this for Metatrader:

#property copyright "Copyright © 2012, George Heitman"
#property link "http://www.stevehopwoodforex.com"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Black

//---- input parameters
extern int Per=20;
extern int Price=PRICE_CLOSE;

//---- buffers
double LP[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
//---- additional buffers are used for counting.
IndicatorBuffers(1);
SetIndexBuffer(0,LP);

//---- indicator lines
SetIndexStyle(0,DRAW_LINE);

//---- name for DataWindow label
short_name=StringConcatenate("LowPass Filter (",Per,")");
IndicatorShortName(short_name);
SetIndexLabel(0,short_name);
//----
SetIndexDrawBegin(0,Per);
//----
return(0);
}

int start() {
int i,limit;

int counted_bars=IndicatorCounted();
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;

double a = 2.0/(Per+1.0);
double a2 = a*a;
double p1, p2, p3;

for(i=limit; i>=0; i--) {

switch(Price)
{
case PRICE_CLOSE: p1 = Close[i]; p2 = Close[i+1];p3 = Close[i+2];break;
case PRICE_OPEN: p1 = Open[i]; p2 = Open[i+1];p3 = Open[i+2];break;
case PRICE_HIGH: p1 = High[i]; p2 = High[i+1];p3 = High[i+2];break;
case PRICE_LOW: p1 = Low[i]; p2 = Low[i+1];p3 = Low[i+2];break;
case PRICE_MEDIAN: p1 = (High[i]+Low[i])/2; p2 = (High[i+1]+Low[i+1])/2;p3 = (High[i+2]+Low[i+2])/2;break;
case PRICE_TYPICAL: p1 = (High[i]+Low[i]+Close[i])/3; p2 = (High[i+1]+Low[i+1]+Close[i+1])/3;p3 = (High[i+2]+Low[i+2]+Close[i+2])/3;break;
case PRICE_WEIGHTED: p1 = (High[i]+Low[i]+2*Close[i])/4; p2 = (High[i+1]+Low[i+1]+2*Close[i+1])/4;p3 = (High[i+2]+Low[i+2]+2*Close[i+2])/4;break;
default: p1 = Close[i]; p2 = Close[i+1];p3 = Close[i+2];
}

LP[i] = (a-0.25*a2)*p1
+ 0.5*a2*p2
- (a-0.75*a2)*p3
+ 2*(1.0-a)*LP[i+1]
- (1.0-a)*(1.0-a)*LP[i+2];
}

return(0);
}
//+------------------------------------------------------------------+

Re: LowPass filter formula [Re: deweymcg] #459734
06/02/16 08:06
06/02/16 08:06
Joined: May 2016
Posts: 16
USA
J
JerryS Offline
Newbie
JerryS  Offline
Newbie
J

Joined: May 2016
Posts: 16
USA
From: http://www.financial-hacker.com/trend-delusion-or-reality/

var LowPass(var *Data,int Period)
{
var* LP = series(Data[0]);
var a = 2.0/(1+Period);
return LP[0] = (a-0.25*a*a)*Data[0]
+ 0.5*a*a*Data[1]
- (a-0.75*a*a)*Data[2]
+ 2*(1.-a)*LP[1]
- (1.-a)*(1.-a)*LP[2];
}

I'm having trouble actually finding this function in the files that came with Zorro though... and I'm not sure what the Data is (i'm new to Zorro--is it ever defined in the code or it's just a variable you can replace with anything?)

Last edited by JerryS; 06/02/16 08:17.
Re: LowPass filter formula [Re: JerryS] #459736
06/02/16 09:03
06/02/16 09:03
Joined: May 2016
Posts: 16
USA
J
JerryS Offline
Newbie
JerryS  Offline
Newbie
J

Joined: May 2016
Posts: 16
USA
It doesn't even fully make sense to me how LP is defined...it includes earlier versions of itself (LP[2] and LP[1]) in its own definition.... Can it handle that?

Re: LowPass filter formula [Re: JerryS] #459737
06/02/16 10:45
06/02/16 10:45
Joined: Jul 2000
Posts: 27,977
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,977
Frankfurt
You bet.

Re: LowPass filter formula [Re: jcl] #459749
06/02/16 17:36
06/02/16 17:36
Joined: May 2016
Posts: 16
USA
J
JerryS Offline
Newbie
JerryS  Offline
Newbie
J

Joined: May 2016
Posts: 16
USA
If LP=series(Data[0]), then doesn't LP[1]=Data[1]? and LP[2]=Data[2]?

Could you please explain:

return LP[0] = (a-0.25*a*a)*Data[0]
+ 0.5*a*a*Data[1]
- (a-0.75*a*a)*Data[2]
+ 2*(1.-a)*LP[1]
- (1.-a)*(1.-a)*LP[2]

I'm just working with what I learned in the manual. If there's something else I need to read to be able to understand this language please let me know.

Re: LowPass filter formula [Re: JerryS] #459758
06/03/16 03:06
06/03/16 03:06
Joined: May 2016
Posts: 16
USA
J
JerryS Offline
Newbie
JerryS  Offline
Newbie
J

Joined: May 2016
Posts: 16
USA
I've now read the relevant chapter of Ehlers' text, and arrived at the following equation for a 2-pole filter (Gaussian), as supposedly written in EasyLanguage:

y = a*a*x + 2*(1-a)*y[1]-(1-a)*(1-a)*y[2]

Which seems much simpler but still has the problem of referring back to earlier versions of the same variable to define a variable (which is completely circular)... ***ALSO, it never in the chapter defines what "x" is supposed to be. (Kind of like in the Zorro code it doesn't define what "Data" is.***

If you are referring to earlier versions of a variable to run into this logical problem: what is y[2]? y[2] = a*a*x + 2*(1-a)*y[3]-(1-a)*(1-a)*y[4] ??? And what is y[4]???? It just goes on forever never truly defining what exactly it is...

When you return LP[0] = something... what are you doing? Why not just return LP[0]? How do you make it equal to something other than series(Data[0])? Should the result just be "false"? I know it can't be since the function is supposed to return a variable...

This is why we need a chat room--because it seems like it will take years to even understand one of the basic functions of the manual this way. According to wikipedia circular references are useless. I tend to believe that it works better than simple moving averages and can easily test it, but I prefer to halfway understand the indicators I'm using, and would expect anyone who takes this seriously to do the same. Hopefully someone out there can explain...

Re: LowPass filter formula [Re: JerryS] #459760
06/03/16 08:17
06/03/16 08:17
Joined: Jul 2000
Posts: 27,977
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,977
Frankfurt
Asking such questions in a forum is better than in a chat room: Other users might have the same issue and can then read the answer here, while it would be lost in a chat.

The "x" in the above formula is the input to the filter, here normally the current price.

"return LP[0] = ..." assigns a value to LP[0] and returns that value at the same time.

And recursive definitions are a frequent element in math as well as in programming. You will find them everywhere in mathematical formulas and in program code. Y[1] is simply the previous value of Y.

Re: LowPass filter formula [Re: jcl] #459766
06/03/16 13:01
06/03/16 13:01
Joined: Jun 2013
Posts: 1,609
D
DdlV Offline
Serious User
DdlV  Offline
Serious User
D

Joined: Jun 2013
Posts: 1,609
Hi JerryS. No idea what your computer/science/math background is, but I'll try to help. To expand on what jcl said, recursive is not the same as circular. Circular means the calculation is in some way using itself. Recursive means a prior value is being used. The classic example is factorial: n! = n * (n-1)!.

In the case of Zorro, this is done with series() (for simplicity to start think of them as time series). Perhaps the missing element is that on the first bar the value of y[n] with n>0 is 0 - only y[0] has a value. But on the next bar y[1] does also (the value of y[0] at the end of the previous bar), and so on. In this way the series is populated. If you add debug prints to your code, you can see this happening (as well as find other issues laugh ).

HTH.

Re: LowPass filter formula [Re: DdlV] #459855
06/08/16 22:27
06/08/16 22:27
Joined: Apr 2014
Posts: 482
Sydney, Australia
B
boatman Offline
Senior Member
boatman  Offline
Senior Member
B

Joined: Apr 2014
Posts: 482
Sydney, Australia
Also search in the manual for "UnstablePeriod" for a description of how Zorro handles calculations that are influenced by an infinite number of past values.

Page 2 of 2 1 2

Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1