Re: [personal kdb+] Moving weigthed average in q

To Q1:

something like this ?

(::)t: date xasc ([]sym:raze (10#)each A`B;date:raze 2#enlist 2012.01.01+til 10;close:20?20.0)
w:0.2 0.3 0.5

update ma:flip[`float$prev[count[w]-1;] close ] mmu w by sym from t

kim

Am 06.09.2012 20:03, schrieb andrasbme:

Hi All,

I feel that this should be easy, but I just can’t figure it out, even after checking the k code implementing msum and mavg. So the question: how to implement weighted moving average in q?
Question 1.
q) t:(sym:(10#A),10#B;date:raze 2#enlist 2012.01.01+til 10;close:20?20.0)
q) w:0.2 0.3 0.5

How to insert a new column to t, which contains the weighted average price of the last 3 days for each sym with weights w?

Question 2. (I think this one is harder)
w:{.94 xexp x}

How to insert a new column to t, which contains the weighted average price of all days of the last month (or some other predetermined time window) where the weight for each price depends on how old (in days) the price is. 
E.g.
sym date         close

A   2012.01.01 4.90757
A   2012.01.02 4.924
A   2012.01.05 4.849193
The new value for row 3 should be:
(w[0]*4.849193 + w[3]*4.924 + w[4]*4.90757) / sum w[0],w[3],w[4]
where 3 is the time between 2012.01.02 and 2012.01.05 in days. Of course, we do not have a price for all the dates (i.e. markets are closed on weekends) so this is not the same question as question 1.

Thanks in advance for any help.
Best,
Andras


You received this message because you are subscribed to the Google Groups “Kdb+ Personal Developers” group.
To view this discussion on the web visit https://groups.google.com/d/msg/personal-kdbplus/-/AaNGsN50wg4J.
To post to this group, send email to personal-kdbplus@googlegroups.com.
To unsubscribe from this group, send email to personal-kdbplus+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/personal-kdbplus?hl=en.

Thanks, kim!

  1. szeptember 6., csütörtök 22:41:04 UTC+2 id?pontban kuentang a következ?t írta:

To Q1:

something like this ?

(::)t: date xasc ([]sym:raze (10#)each A`B;date:raze 2#enlist 2012.01.01+til 10;close:20?20.0)
w:0.2 0.3 0.5

update ma:flip[`float$prev[count[w]-1;] close ] mmu w by sym from t

kim

Am 06.09.2012 20:03, schrieb andrasbme:

Hi All,

I feel that this should be easy, but I just can’t figure it out, even after checking the k code implementing msum and mavg. So the question: how to implement weighted moving average in q?
Question 1.
q) t:(sym:(10#A),10#B;date:raze 2#enlist 2012.01.01+til 10;close:20?20.0)
q) w:0.2 0.3 0.5

How to insert a new column to t, which contains the weighted average price of the last 3 days for each sym with weights w?

Question 2. (I think this one is harder)
w:{.94 xexp x}

How to insert a new column to t, which contains the weighted average price of all days of the last month (or some other predetermined time window) where the weight for each price depends on how old (in days) the price is.
E.g.
sym date close
---------------------
A 2012.01.01 4.90757
A 2012.01.02 4.924
A 2012.01.05 4.849193
The new value for row 3 should be:
(w[0]*4.849193 + w[3]*4.924 + w[4]*4.90757) / sum w[0],w[3],w[4]
where 3 is the time between 2012.01.02 and 2012.01.05 in days. Of course, we do not have a price for all the dates (i.e. markets are closed on weekends) so this is not the same question as question 1.

Thanks in advance for any help.
Best,
Andras




You received this message because you are subscribed to the Google Groups “Kdb+ Personal Developers” group.
To view this discussion on the web visit https://groups.google.com/d/msg/personal-kdbplus/-/AaNGsN50wg4J.
To post to this group, send email to personal…@googlegroups.com.
To unsubscribe from this group, send email to personal-kdbpl…@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/personal-kdbplus?hl=en.