<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Forex Software — Smoothed Moving Average]]></title>
	<link rel="self" href="https://forexsb.com/forum/feed/atom/topic/2887/" />
	<updated>2011-12-28T15:13:06Z</updated>
	<generator>PunBB</generator>
	<id>https://forexsb.com/forum/topic/2887/smoothed-moving-average/</id>
		<entry>
			<title type="html"><![CDATA[Re: Smoothed Moving Average]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/12024/#p12024" />
			<content type="html"><![CDATA[<div class="codebox"><pre><code>/// &lt;summary&gt;
        /// Calculates a Moving Average
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;iPeriod&quot;&gt;Period&lt;/param&gt;
        /// &lt;param name=&quot;iShift&quot;&gt;Shift&lt;/param&gt;
        /// &lt;param name=&quot;maMethod&quot;&gt;Method of calculation&lt;/param&gt;
        /// &lt;param name=&quot;afSource&quot;&gt;The array of source data&lt;/param&gt;
        /// &lt;returns&gt;the Moving Average&lt;/returns&gt;
        protected static double[] MovingAverage(int iPeriod, int iShift, MAMethod maMethod, double[] adSource)
        {
            int      iBar;
            double   sum;
            double[] adTarget = new double[Bars];
 
            if (iPeriod &lt;= 1 &amp;&amp; iShift == 0)
            {   // There is no smoothing
                return adSource;
            }
 
            if (iPeriod &gt; Bars || iPeriod + iShift &lt;= 0 || iPeriod + iShift &gt; Bars)
            {   // Error in the parameters
                return null;
            }
 
            for (iBar = 0; iBar &lt; iPeriod + iShift - 1; iBar++)
            {
                adTarget[iBar] = 0;
            }
 
            for (iBar = 0, sum = 0; iBar &lt; iPeriod; iBar++)
            {
                sum += adSource[iBar];
            }
 
            adTarget[iPeriod + iShift - 1] = sum / iPeriod;
 
            // Simple Moving Average
            if (maMethod == MAMethod.Simple)
            {   
                for (iBar = iPeriod; iBar &lt; Math.Min(Bars, Bars - iShift); iBar++)
                {
                    adTarget[iBar + iShift] = adTarget[iBar + iShift - 1] + adSource[iBar] / iPeriod - adSource[iBar - iPeriod] / iPeriod;
                }
            }
 
            // Exponential Moving Average
            else if (maMethod == MAMethod.Exponential)
            {   
                double pr = 2d / (iPeriod + 1);
 
                for (iBar = iPeriod; iBar &lt; Math.Min(Bars, Bars - iShift); iBar++)
                {
                    adTarget[iBar + iShift] = adSource[iBar] * pr + adTarget[iBar + iShift - 1] * (1 - pr);
                }
            }
 
            // Weighted Moving Average
            else if (maMethod == MAMethod.Weighted)
            {
                double dWeight = iPeriod * (iPeriod + 1) / 2d;
 
                for (iBar = iPeriod; iBar &lt; Math.Min(Bars, Bars - iShift); iBar++)
                {
                    sum = 0;
                    for (int i = 0; i &lt; iPeriod; i++)
                    {
                        sum += adSource[iBar - i] * (iPeriod - i);
                    }
 
                    adTarget[iBar + iShift] = sum / dWeight;
                }
            }
 
            // Smoothed Moving Average
            else if (maMethod == MAMethod.Smoothed)
            {
                for (iBar = iPeriod; iBar &lt; Math.Min(Bars, Bars - iShift); iBar++)
                {
                    adTarget[iBar + iShift] = (adTarget[iBar + iShift - 1] * (iPeriod - 1) + adSource[iBar]) / iPeriod;
                }
            }
 
            for (iBar = Bars + iShift; iBar &lt; Bars; iBar++)
            {
                adTarget[iBar] = 0;
            }
 
            return adTarget;
        }</code></pre></div>]]></content>
			<author>
				<name><![CDATA[footon]]></name>
				<uri>https://forexsb.com/forum/user/1242/</uri>
			</author>
			<updated>2011-12-28T15:13:06Z</updated>
			<id>https://forexsb.com/forum/post/12024/#p12024</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Smoothed Moving Average]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/12023/#p12023" />
			<content type="html"><![CDATA[<p>I need to now how to calculate a Smoothed Moving Average for a data tester Im making on a spreadsheet.</p><p>How does FSB calculate the Smoothed MA Method</p>]]></content>
			<author>
				<name><![CDATA[mixmanmatt]]></name>
				<uri>https://forexsb.com/forum/user/5015/</uri>
			</author>
			<updated>2011-12-28T15:09:47Z</updated>
			<id>https://forexsb.com/forum/post/12023/#p12023</id>
		</entry>
</feed>
