title: My C# Weighted Moving Average result is different than TradingView's, how does TradingView calculate WMA?
body: <p>Finance charts on <a href="https://www.tradingview.com" rel="nofollow noreferrer">TradingView</a> provide a <a href="https://www.tradingview.com/wiki/Moving_Average#Weighted_Moving_Average_.28WMA.29" rel="nofollow noreferrer">weighted moving average</a> <strong>(WMA)</strong> value based on a series of past stock prices.</p>
  
  <p>I've written a C# method (shown below) to calculate these WMA values so they are exactly the same as the results on TradingView, given the same stock price data.</p>
  
  <p>After testing my script, I <em>believe</em> I am calculating the WMA correctly, and have verified my results on <a href="https://www.rapidtables.com/calc/math/weighted-average-calculator.html" rel="nofollow noreferrer">another online WMA calculator</a>.</p>
  
  <p>My issue is that my results are slightly different than TradingView's results, usually differing at the third decimal place of precision on each WMA value.  </p>
  
  <p>To give you a real-life example, here's a set of 10 closing prices for Apple from April 1st, 2020:</p>
  
  <ol>
  <li>3:00 PM - $241.30</li>
  <li>2:59 PM - $241.01</li>
  <li>2:58 PM - $241.04</li>
  <li>2:57 PM - $241.01</li>
  <li>2:56 PM - $241.00</li>
  <li>2:55 PM - $241.15</li>
  <li>2:54 PM - $241.47</li>
  <li>2:53 PM - $241.35</li>
  <li>2:52 PM - $241.61</li>
  <li>2:51 PM - $241.57</li>
  </ol>
  
  <p>They come into my script through a "Prices" object (which can contain more price values than actually are needed, thus the extra 'startIndex' &amp; 'howManyPeriods' parameters):</p>
  
  <pre><code> public static decimal WeightedMovingAverage(Prices prices, int startIndex = 0, decimal howManyPeriods = -1)
      {
          // Get the correct number of periods
          if (howManyPeriods &lt; 1) howManyPeriods = prices.Count - startIndex;
  
          // Loop through calculations for the WMA
          decimal numerator = 0, denominator = 0;
          for (int i = 0; i &lt; howManyPeriods; i++)
          {
              Price price = prices[i + startIndex];
              numerator += price.Value * (howManyPeriods - i);
              denominator += i + 1;
          }
          return numerator / denominator;
      }
  </code></pre>
  
  <p>In this example, these 10 values produce the WMA result <strong>241.16272727</strong>.</p>
  
  <p>TradingView's <strong>wma</strong> Pine function returns <strong>241.16324727</strong></p>
  
  <p>That's a small difference of <strong>0.00052</strong>
  ...but this is a big problem for me because I use this result for other math operations and the difference becomes magnified and thus unusable.</p>
  
  <p>I cannot find an explanation for the difference, and this has brought me to a grinding halt.</p>
  
  <p>I doubt it's a rounding error in Pine since their <a href="https://www.tradingview.com/pine-script-docs/en/v4/language/Type_system.html#float" rel="nofollow noreferrer">user manual</a> specifies:</p>
  
  <blockquote>
    <p>The internal precision of floats in Pine is 1e-10</p>
  </blockquote>
  
  <p>The only <a href="https://www.tradingview.com/wiki/Moving_Average#Weighted_Moving_Average_.28WMA.29" rel="nofollow noreferrer">documentation</a> I've found from TradingView or Pine on exactly how they calculate their WMA seems to match my script perfectly, so I'm out of ideas and am looking for help.  Hopefully I'm just missing something obvious!</p>
  
  <p>My best guess is that Pine has an alternative WMA equation, but I haven't been able to find it.</p>
  
  <p>Any help or ideas would be greatly appreciated!</p>