xsl-list
[Top] [All Lists]

RE: Calculating a Moving Average

2004-06-01 08:59:55
I am trying to calculate a moving average based on the below XML, 
according to a number of days parameter. Ie 20 day moving average, 
would sum the last (starting at the end 20040528) 20 days 
closing price <cl> and divide by 20.


John, I think this would work for you without having to use any
extensions.  The key was to get the 'dateFiltered' variable to only
contain the nodes where their position is greater than the (last node
minus the varDays parameter).


<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:param name="varDays" select="20"/>

<xsl:output method="html" indent="yes" version="4.0" />


<xsl:variable name="dataFiltered" select="asx/day[position() &gt;
(last() - $varDays)]" />

<xsl:template match="/">
 <html>
  <head><title>Rolling Average</title></head>
  <body>
   Average = <xsl:value-of select="sum($dataFiltered/cl) div
count($dataFiltered)"/>
   <!-- here are the details that made up the average -->
   <table border="1">
    <xsl:apply-templates select="$dataFiltered"/>
   </table>
  </body>
 </html>
</xsl:template>


<xsl:template match="day">
 <tr>
  <td><xsl:value-of select="@id"/></td>
  <td><xsl:value-of select="cl"/></td>
 </tr>
</xsl:template>

</xsl:stylesheet>



Cheers,
Jack



**********************************************************************************
This e-mail and any files transmitted with it are confidential and are intended 
solely for the use of the individual or entity to which they are addressed.  It 
is not an offer or acceptance, and it is not intended to be all or part of an 
agreement.  This communication may contain material protected by the 
attorney-client privilege.  If you are not the intended recipient or the person 
responsible for delivering the e-mail to the intended recipient, be advised 
that you have received this e-mail in error and that any review, use, 
dissemination, forwarding, printing, or copying of this e-mail is strictly 
prohibited.  If you have received this e-mail in error, please notify the 
sender immediately by return e-mail and delete this e-mail from your system.  
The sender does not accept liability for any damage caused by any virus 
transmitted by this e-mail or any errors or omissions in the contents of this 
message which arise as a result of e-mail transmission.

**********************************************************************************



<Prev in Thread] Current Thread [Next in Thread>
  • RE: Calculating a Moving Average, Jack Ratcliff <=