cancel
Showing results for 
Search instead for 
Did you mean: 

exponential moving average function for Q

Wind
New Contributor
I have found the ema function from the kxforums.com. But there issome error. Maybe because of the difference between K and Q. Cananybody help change the function for Q?/ the original function for exponential moving average[code:1]\ema:{a:2%x+1;b:1-a;c:(+/x#y)%x;((x-1)#0n),c{(a*y)+b*x}\x _ y}[/code:1]I have changed it to the following format, but the "/" maybe shouldalso be replaced:ema:{[x;y] a:2%x+1;b:1-a;c:(+/x#y)%x;((x-1)#0n),c{(a*y)+b*x}\x _ y};Thanks.Wind
6 REPLIES 6

niall_dalton
New Contributor
X-Mailer: Apple Mail (2.930.3)Untested, but something like ema:{[x;y]b:1-a:2%x+1;c:(sum x#y)%x; ((x-1)#0n),c,c{[a;b;c;d](a*d)+b*c}[a;b]\x _y} should provide a starting point.On Apr 9, 2009, at 8:10 PM, Wind wrote:>> I have found the ema function from the kxforums.com. But there is> some error. Maybe because of the difference between K and Q. Can> anybody help change the function for Q?>> / the original function for exponential moving average> [code:1]\ema:{a:2%x+1;b:1-a;c:(+/x#y)%x;((x-1)#0n),c{(a*y)+b*x}\x _ y}> [/code:1]>> I have changed it to the following format, but the "/" maybe should> also be replaced:> ema:{[x;y] a:2%x+1;b:1-a;c:(+/x#y)%x;((x-1)#0n),c{(a*y)+b*x}\x _ y};>> Thanks.>> Wind> >

I copied Nial's ema function directly. It works well and is exactlywhat I need.Thanks Niall and Aaron.On Apr 10, 11:46�am, Niall Dalton wrote:> Untested, but something like ema:{[x;y]b:1-a:2%x+1;c:(sum x#y)%x;> ((x-1)#0n),c,c{[a;b;c;d](a*d)+b*c}[a;b]\x _y} should provide a �> starting point.>> On Apr 9, 2009, at 8:10 PM, Wind wrote:>>>> > I have found the ema function from the kxforums.com. � But there is> > some error. � Maybe because of the difference between K and Q. �Can> > anybody help change the function for Q?>> > / the original function for exponential moving average> > [code:1]\ema:{a:2%x+1;b:1-a;c:(+/x#y)%x;((x-1)#0n),c{(a*y)+b*x}\x _ y}> > [/code:1]>> > I have changed it to the following format, but the "/" maybe should> > also be replaced:> > ema:{[x;y] a:2%x+1;b:1-a;c:(+/x#y)%x;((x-1)#0n),c{(a*y)+b*x}\x _ y};>> > Thanks.>> > Wind

Attila
New Contributor
Date: Fri, 10 Apr 2009 09:30:34 +0100Message-ID: <1855e77f0904100130m1183e050nbeef3b19b4a31d5a@mail.gmail.com>Subject: Re: [personal kdb+] Re: exponential moving average function for QFrom: Attila Vrabecz To: personal-kdbplus@googlegroups.comm:{((x-1)#0n),i,{z+x*y}\[i:avg x#y;1-a;(x _y)*a:2%1+x]}is a bit fasterq)x:1000000?10fq)\t ema[100;x]750q)\t m[100;x]531 AttilaOn Fri, Apr 10, 2009 at 6:58 AM, Wind wrote:>> I copied Nial's ema function directly. �It works well and is exactly> what I need.> Thanks Niall and Aaron.>>> On Apr 10, 11:46�am, Niall Dalton wrote:>> Untested, but something like ema:{[x;y]b:1-a:2%x+1;c:(sum x#y)%x;>> ((x-1)#0n),c,c{[a;b;c;d](a*d)+b*c}[a;b]\x _y} should provide a>> starting point.>>>> On Apr 9, 2009, at 8:10 PM, Wind wrote:>>>>>>>> > I have found the ema function from the kxforums.com. � But there is>> > some error. � Maybe because of the difference between K and Q. �Can>> > anybody help change the function for Q?>>>> > / the original function for exponential moving average>> > [code:1]\ema:{a:2%x+1;b:1-a;c:(+/x#y)%x;((x-1)#0n),c{(a*y)+b*x}\x _ y}>> > [/code:1]>>>> > I have changed it to the following format, but the "/" maybe should>> > also be replaced:>> > ema:{[x;y] a:2%x+1;b:1-a;c:(+/x#y)%x;((x-1)#0n),c{(a*y)+b*x}\x _ y};>>>> > Thanks.>>>> > Wind> >>

heydi
New Contributor
if just want the first one as intial value, one can� use
ewma:{{y+x*z-y}[x:2%1+x]\[y]}
Xi

Wind
New Contributor
I tried Xi and Attila's methods. All are perfect q codes.Thanks again.On Apr 10, 11:26�pm, Xi Chen wrote:> if just want the first one as intial value, one can �use> ewma:{{y+x*z-y}[x:2%1+x]\[y]}> Xi>> On Fri, Apr 10, 2009 at 9:30 AM, Attila Vrabecz wrote:>>>> > m:{((x-1)#0n),i,{z+x*y}\[i:avg x#y;1-a;(x _y)*a:2%1+x]}> > is a bit faster>> > q)x:1000000?10f> > q)\t ema[100;x]> > 750> > q)\t m[100;x]> > 531> > �Attila>> > On Fri, Apr 10, 2009 at 6:58 AM, Wind wrote:>> > > I copied Nial's ema function directly. �It works well and is exactly> > > what I need.> > > Thanks Niall and Aaron.>> > > On Apr 10, 11:46 am, Niall Dalton wrote:> > >> Untested, but something like ema:{[x;y]b:1-a:2%x+1;c:(sum x#y)%x;> > >> ((x-1)#0n),c,c{[a;b;c;d](a*d)+b*c}[a;b]\x _y} should provide a> > >> starting point.>> > >> On Apr 9, 2009, at 8:10 PM, Wind wrote:>> > >> > I have found the ema function from the kxforums.com. � But there is> > >> > some error. � Maybe because of the difference between K and Q. �Can> > >> > anybody help change the function for Q?>> > >> > / the original function for exponential moving average> > >> > [code:1]\ema:{a:2%x+1;b:1-a;c:(+/x#y)%x;((x-1)#0n),c{(a*y)+b*x}\x _ y}> > >> > [/code:1]>> > >> > I have changed it to the following format, but the "/" maybe should> > >> > also be replaced:> > >> > ema:{[x;y] a:2%x+1;b:1-a;c:(+/x#y)%x;((x-1)#0n),c{(a*y)+b*x}\x _ y};>> > >> > Thanks.>> > >> > Wind

Aaron_Davies
New Contributor
X-Mailer: Apple Mail (2.930.3)On Apr 10, 2009, at 11:10 AM, Wind wrote:> ema:{a:2%x+1;b:1-a;c:(+/x#y)%x;((x-1)#0n),c{(a*y)+b*x}\x _ y}here's a direct port from to k4:k)ema:{a:2%x+1;b:1-a;c:(+/x#y)%x;((x-1)#0n),c{[a;b;x;y](a*y)+b*x}[a;b] \x _ y}k3 must have made locals available to nested lambdas