Results 1 to 5 of 5

Thread: A Simple Moving Average Algorithm

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    A Simple Moving Average Algorithm

    I am looking for a way to find the moving average for customers over a 30 day period. I am new to the concept of moving averages so I started with a Google search and found a lot of good information. However I was not able to find any sample VB code to help get me started. I did find this C# sample on Code Project but my attempts at conversion have not been successfull.

    Does anybody have an existing VB class they would like to share or do you know of a sample that I could use to build my own?

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: A Simple Moving Average Algorithm

    You mean this?
    Code:
    Imports System
    Imports System.Collections.Generic
    Imports System.Text
    
    Namespace AveragerTest
        Public Class BruteForce
            Private samples() As Single
            Private idx As Integer
    
            Public ReadOnly Property Average() As Single
                Get
                    Dim total As Single = 0.0!
    
                    For i As Integer = 0I To samples.Length - 1I
                        total += samples(i)
                    Next i
    
                    Return total / samples.Length
                End Get
            End Property
    
            Public Sub New(ByVal numSamples As Integer)
                If numSamples <= 0I Then
                    Throw New ArgumentOutOfRangeException("numSamples can't be negative or 0.")
                Else
                    ReDim samples(numSamples - 1I)
                    idx = 0I
                End If
            End Sub
    
            Public Sub AddSample(ByVal val As Single)
                samples(idx) = val
               
                If System.Threading.Interlocked.Increment(idx) = samples.Length Then
                    idx = 0I
                End If
            End Sub
        End Class
    End Namespace
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: A Simple Moving Average Algorithm

    I tried converting the whole thing (with the bells and whistles) that is farther down the page with a "web based" converter and it didn't convert correctly for me. I did get the same section that you were able to convert but I don't think that is everything I need to get the correct moving averages.

    I am sure that it is something I don't understand about the original code but I am just not sure what it is. Any ideas?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: A Simple Moving Average Algorithm

    1. Try Instant VB for converting C# code to VB.

    2. Do you understand how to calculate a moving average on paper? If so then you should be able to implement it in VB without any examples, or at least make an attempt.

    3. VB code is very similar to C# code so you should be able to convert the vast majority of what's in that Code Project article. If you were to tell us what specific parts you're having issues with then we could help you with those and you're done.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: A Simple Moving Average Algorithm

    this should give you an idea

    Code:
        Private Sub MovingAVG()
            Dim rndm As New Random 'used to generate random data
            Dim someData(999) As Byte 'place to store random data
            rndm.NextBytes(someData) 'generate 1000 values randomly
            Const maDuration As Integer = 30 'length of moving average
            Dim ma As New List(Of Integer), theAvg As Integer 'storage for values and average
            For x As Integer = 0 To someData.Length - 1 'iterate through random data
                ma.Insert(0, someData(x)) 'insert into list newest will be at index 0
                theAvg = 0 'calculate the average
                For y = 0 To ma.Count - 1
                    theAvg += ma(y) 'add up the values
                Next
                theAvg = theAvg \ ma.Count 'divide by the count
                'Debug.WriteLine(theAvg.ToString)
                If ma.Count = maDuration Then 'do we have a durations worth of data?
                    ma.RemoveAt(ma.Count - 1) 'remove the oldest
                End If
            Next
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width