## Re: [xsl] Calculating a Moving Average

 Subject: Re: [xsl] Calculating a Moving Average From: Mukul Gandhi Date: Sun, 30 May 2004 01:35:12 -0700 (PDT)
Hi John,

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:xalan="http://xml.apache.org/xalan";>
<xsl:output method="text" version="1.0"
encoding="UTF-8" indent="yes"/>

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

<xsl:template match="/asx">
<xsl:variable name="rtf">
<xsl:for-each select="day[(last() - position())
&lt; \$varDays]">
<cl>
<xsl:value-of select="cl"/>
</cl>
</xsl:for-each>
</xsl:variable>

Moving Average = <xsl:value-of
select="sum(xalan:nodeset(\$rtf)/cl) div \$varDays"/>

</xsl:template>

</xsl:stylesheet>

Regards,
Mukul

--- John Reid <John.Reid@xxxxxxxxxxxxxxx> wrote:
> G'day,
>
> 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.
>
> <xsl:param name="varDays" select="0" />
> <xsl:variable name="varTotalMA"
> select="sum(day[(last() - \$varDays) and
> last()]/cl)" />
>
> <xsl:variable name="varMA" select="\$varTotalMA /
> \$varDays" />
>
> Any help appreciated
>
> John
>
> 	<asx id="AAC" coy="AUSTRALIAN AGRICULTURAL COMPANY
> LIMITED."
> sector="Food Beverage &amp; Tobacco" asxNo="ASX300"
> currHi="122" currOp="121"
> currCl="120" currVol="113728" currDate="20040528">
> 		<day id="20040329" weekno="1">
> 			<op>116</op>
> 			<hi>116</hi>
> 			<lo>114</lo>
> 			<cl>114</cl>
> 			<vol>73200</vol>
> 		</day>
> 		<day id="20040330" weekno="1">
> 			<op>114</op>
> 			<hi>115</hi>
> 			<lo>114</lo>
> 			<cl>115</cl>
> 			<vol>73745</vol>
> 		</day>
> 		<day id="20040331" weekno="1">
> 			<op>115</op>
> 			<hi>115</hi>
> 			<lo>113</lo>
> 			<cl>113</cl>
> 			<vol>262756</vol>
> 		</day>
> 		<day id="20040401" weekno="1">
> 			<op>114</op>
> 			<hi>114</hi>
> 			<lo>113</lo>
> 			<cl>114</cl>
> 			<vol>95200</vol>
> 		</day>
> 		<day id="20040402" weekno="1">
> 			<op>114</op>
> 			<hi>114</hi>
> 			<lo>113</lo>
> 			<cl>113</cl>
> 			<vol>198992</vol>
> 		</day>
> 		<day id="20040405" weekno="2">
> 			<op>113</op>
> 			<hi>116</hi>
> 			<lo>113</lo>
> 			<cl>116</cl>
> 			<vol>643913</vol>
> 		</day>
> 		<day id="20040406" weekno="2">
> 			<op>116</op>
> 			<hi>116</hi>
> 			<lo>115</lo>
> 			<cl>116</cl>
> 			<vol>421525</vol>
> 		</day>
> 		<day id="20040407" weekno="2">
> 			<op>116</op>
> 			<hi>116</hi>
> 			<lo>115</lo>
> 			<cl>116</cl>
> 			<vol>351438</vol>
> 		</day>
> 		<day id="20040408" weekno="2">
> 			<op>116</op>
> 			<hi>116</hi>
> 			<lo>114</lo>
> 			<cl>116</cl>
> 			<vol>154881</vol>
> 		</day>
> 		<day id="20040409" weekno="2">
> 			<op>116</op>
> 			<hi>116</hi>
> 			<lo>116</lo>
> 			<cl>116</cl>
> 			<vol>0</vol>
> 		</day>
> 		<day id="20040413" weekno="3">
> 			<op>116</op>
> 			<hi>117</hi>
> 			<lo>115</lo>
> 			<cl>115</cl>
> 			<vol>127715</vol>
> 		</day>
> 		<day id="20040413" weekno="3">
> 			<op>116</op>
> 			<hi>117</hi>
> 			<lo>115</lo>
> 			<cl>115</cl>
> 			<vol>127715</vol>
> 		</day>
> 		<day id="20040414" weekno="3">
> 			<op>115</op>
> 			<hi>116</hi>
> 			<lo>115</lo>
> 			<cl>116</cl>
> 			<vol>133444</vol>
> 		</day>
> 		<day id="20040415" weekno="3">
> 			<op>114</op>
> 			<hi>115</hi>
> 			<lo>113</lo>
> 			<cl>114</cl>
> 			<vol>218466</vol>
> 		</day>
> 		<day id="20040416" weekno="3">
> 			<op>114</op>
> 			<hi>114</hi>
> 			<lo>113</lo>
> 			<cl>114</cl>
> 			<vol>151423</vol>
> 		</day>
> 		<day id="20040419" weekno="4">
> 			<op>113</op>
> 			<hi>114</hi>
> 			<lo>113</lo>
> 			<cl>113</cl>
> 			<vol>79202</vol>
> 		</day>
> 		<day id="20040420" weekno="4">
> 			<op>114</op>
> 			<hi>115</hi>
> 			<lo>113</lo>
> 			<cl>115</cl>
> 			<vol>151972</vol>
> 		</day>
> 		<day id="20040421" weekno="4">
> 			<op>114</op>
> 			<hi>114</hi>
> 			<lo>113</lo>
> 			<cl>113</cl>
> 			<vol>125633</vol>
> 		</day>
> 		<day id="20040422" weekno="4">
> 			<op>114</op>
> 			<hi>119</hi>
> 			<lo>113</lo>
> 			<cl>118</cl>
> 			<vol>744055</vol>
> 		</day>
> 		<day id="20040423" weekno="4">
> 			<op>118</op>
> 			<hi>119</hi>
> 			<lo>118</lo>
> 			<cl>118</cl>
> 			<vol>513601</vol>
> 		</day>
> 		<day id="20040426" weekno="5">
> 			<op>118</op>
> 			<hi>118</hi>
> 			<lo>117</lo>
> 			<cl>117</cl>
> 			<vol>198288</vol>
> 		</day>
> 		<day id="20040427" weekno="5">
> 			<op>118</op>
> 			<hi>118</hi>
> 			<lo>117</lo>
> 			<cl>117</cl>
> 			<vol>191337</vol>
> 		</day>
> 		<day id="20040428" weekno="5">
> 			<op>118</op>
> 			<hi>120</hi>
> 			<lo>118</lo>
> 			<cl>118</cl>
> 			<vol>323800</vol>
> 		</day>
> 		<day id="20040429" weekno="5">
> 			<op>118</op>
> 			<hi>119</hi>
> 			<lo>117</lo>
> 			<cl>119</cl>
> 			<vol>260638</vol>
> 		</day>
> 		<day id="20040430" weekno="5">
> 			<op>119</op>
> 			<hi>119</hi>
> 			<lo>117</lo>
> 			<cl>118</cl>
> 			<vol>163400</vol>
> 		</day>
> 		<day id="20040503" weekno="6">
> 			<op>119</op>
> 			<hi>119</hi>
> 			<lo>117</lo>
> 			<cl>118</cl>
> 			<vol>123565</vol>
> 		</day>
> 		<day id="20040504" weekno="6">
> 			<op>117</op>
> 			<hi>120</hi>
> 			<lo>117</lo>
> 			<cl>120</cl>
> 			<vol>470247</vol>
> 		</day>
> 		<day id="20040505" weekno="6">
> 			<op>120</op>
> 			<hi>124</hi>
> 			<lo>120</lo>
> 			<cl>124</cl>
> 			<vol>1646033</vol>
> 		</day>
> 		<day id="20040506" weekno="6">
> 			<op>124</op>
> 			<hi>124</hi>
> 			<lo>122</lo>
> 			<cl>122</cl>
> 			<vol>191631</vol>
> 		</day>
> 		<day id="20040507" weekno="6">
> 			<op>122</op>
> 			<hi>123</hi>
> 			<lo>122</lo>
> 			<cl>122</cl>
> 			<vol>179778</vol>
> 		</day>
> 		<day id="20040510" weekno="7">
> 			<op>122</op>
> 			<hi>122</hi>
> 			<lo>120</lo>
> 			<cl>121</cl>
> 			<vol>474624</vol>
> 		</day>
> 		<day id="20040511" weekno="7">
> 			<op>121</op>
> 			<hi>122</hi>
> 			<lo>121</lo>
> 			<cl>122</cl>
> 			<vol>148610</vol>
> 		</day>
> 		<day id="20040512" weekno="7">
> 			<op>122</op>
> 			<hi>123</hi>
> 			<lo>122</lo>
> 			<cl>122</cl>
> 			<vol>84962</vol>
> 		</day>
> 		<day id="20040513" weekno="7">
> 			<op>121</op>
> 			<hi>121</hi>
> 			<lo>119</lo>
> 			<cl>119</cl>
> 			<vol>153598</vol>
> 		</day>
> 		<day id="20040514" weekno="7">
> 			<op>119</op>
> 			<hi>120</hi>
> 			<lo>118</lo>
> 			<cl>120</cl>
> 			<vol>58598</vol>
> 		</day>
> 		<day id="20040517" weekno="8">
> 			<op>119</op>
> 			<hi>119</hi>
> 			<lo>118</lo>
> 			<cl>119</cl>
> 			<vol>145304</vol>
> 		</day>
> 		<day id="20040518" weekno="8">
> 			<op>118</op>
> 			<hi>120</hi>
> 			<lo>117</lo>
> 			<cl>120</cl>
> 			<vol>425565</vol>
> 		</day>
> 		<day id="20040519" weekno="8">
> 			<op>120</op>
> 			<hi>121</hi>
> 			<lo>120</lo>
> 			<cl>120</cl>
> 			<vol>91171</vol>
> 		</day>
> 		<day id="20040520" weekno="8">
> 			<op>121</op>
> 			<hi>121</hi>
> 			<lo>120</lo>
> 			<cl>121</cl>
> 			<vol>218439</vol>
> 		</day>
> 		<day id="20040521" weekno="8">
> 			<op>121</op>
> 			<hi>121</hi>
> 			<lo>120</lo>
> 			<cl>120</cl>
> 			<vol>80331</vol>
> 		</day>
> 	</asx>

__________________________________
Do you Yahoo!?
Friends.  Fun.  Try the all-new Yahoo! Messenger.
http://messenger.yahoo.com/