QuickStart Samples

# Variable Transformations QuickStart Sample (Visual Basic)

Illustrates how to perform a range of transformations on statistical data in Visual Basic.

C# code F# code IronPython code Back to QuickStart Samples

Option Infer On Imports System.Data Imports System.Data.OleDb Imports Extreme.DataAnalysis Imports Extreme.Mathematics Imports Extreme.Statistics Imports Extreme.Statistics.TimeSeriesAnalysis Namespace Extreme.Numerics.QuickStart.VB ' Illustrates various kinds of transformations of numerical variables ' by showing how to compute several financial indicators. Module VariableTransforms Sub Main() ' We use a TimeSeriesCollection to load the data. Dim seriesTable As DataTable = LoadTimeSeriesData() Dim timeSeries = DataFrame.FromDataTable(Of DateTime)(seriesTable, "date") Dim open = timeSeries("Open").As(Of Double) Dim close = timeSeries("Close").As(Of Double) Dim high = timeSeries("High").As(Of Double) Dim low = timeSeries("Low").As(Of Double) Dim volume = timeSeries("Volume").As(Of Double) ' ' Arithmetic operations ' ' The NumericalVariable class defines the standard ' arithmetic operators. Operands can be either ' numerical variables or constants. ' The Typical Price (TP) is the average of the day's high, low and close: Dim TP = (high + low + close) / 3.0 ' Exponentiation is available through the ElementwisePow method, ' with special cases for square roots and reciprocals: Dim inverseVolume = volume.Reciprocal ' ' Simple transformations ' ' The Transforms property of a numerical variable gives access ' to a large number of transformations. ' The GetLag method returns a variable whose observations ' are moved ahead by the specified amount: Dim close1 = close.Lag(1) ' You can get cumulative sums and products: Dim cumVolume = volume.CumulativeSum ' ' Indicators of change ' ' You can get the absolute change, percent change, ' or (exponential) growth rate of a variable. The optional ' parameter is the number of periods to go back. ' The default is 1. Dim closeChange = close.Change(10) ' You can extrapolate the change to a longer number of periods. ' The additional argument is the number of large periods. Dim monthyChange = close.ExtrapolatedChange(10, 20) ' ' Moving averages ' ' You can get simple, exponential, and weighted moving averages. Dim MA20 = close.MovingAverage(20) ' Weighted moving averages can use either a fixed array or vector ' to specify the weight. The weights are automatically normalized. Dim weights As Double() = {1.0, 2.0, 3.0} Dim WMA3 = close.WeightedMovingAverage(weights) ' You can also specify another variable for the weights. ' In this case, the corresponding observations are used. ' For example, to obtain the volume weighted average ' of the close price over a 14 day period, you can write: Dim VWA14 = close.WeightedMovingAverage(14, volume) ' Other statistics, such as maximum, minimum and standard ' deviation are also available. ' ' Misc. transforms ' ' The Box-Cox transform is often used to reduce the effects ' of non-normality of a variable. It takes one parameter, ' which must be between 0 and 1. Dim bcVolume = volume.BoxCoxTransform(0.4) ' ' Creating more complicated indicators ' ' All these transformations can be combined to create ' more complicated transformations. We give some examples ' of common Technical Analysis indicators. ' The Accumulation Distribution is a leading indicator of price movements. ' It is used in many other indicators. ' The formula uses only arithmetic operations: Dim AD = Vector.ElementwiseMultiply( Vector.ElementwiseDivide(close - open, high - low), volume) ' The Chaikin oscillator is used to monitor the flow of money into ' and out of a market. It is the difference between a 3 day and a 10 day ' moving average of the Accumulation Distribution. ' We use the GetExponentialMovingAverage method for this purpose. Dim CO = AD.ExponentialMovingAverage(3) - AD.ExponentialMovingAverage(10) ' Bollinger bands provide an envelope around the price that indicates ' whether the current price level is relatively high or low. ' It uses a 20 day simple average as a central line: Dim TPMA20 = TP.MovingAverage(20) ' The actual bands are at 2 standard deviations (over the same period) ' from the central line. We have to pass the moving average ' over the same period as the second parameter. Dim SD20 = TP.MovingStandardDeviation(20, TPMA20) Dim BOLU = MA20 + 2 * SD20 Dim BOLD = MA20 - 2 * SD20 ' The Relative Strength Index is an index that compares ' the average price gain to the average loss. ' The GetPositiveToNegativeIndex method performs this ' calculation in one operation. The first argument is the period. ' The second argument is the variable that determines ' if an observation counts towards the plus or the minus side. Dim change = close.Change(1) Dim RSI = change.PositiveToNegativeIndex(14, change) ' Finally, let's print some of our results: Dim index As Integer = timeSeries.RowIndex.Lookup(New DateTime(2002, 9, 17)) Console.WriteLine("Data for September 17, 2002:") Console.WriteLine("Acumulation Distribution (in millions): {0:F2}", AD(index) / 1000000) Console.WriteLine("Chaikin Oscillator (in millions): {0:F2}", CO(index) / 1000000) Console.WriteLine("Bollinger Band (Upper): {0:F2}", BOLU(index)) Console.WriteLine("Bollinger Band (Central): {0:F2}", TPMA20(index)) Console.WriteLine("Bollinger Band (Lower): {0:F2}", BOLD(index)) Console.WriteLine("Relative Strength Index: {0:F2}", RSI(index)) Console.WriteLine("Press Enter key to continue.") Console.ReadLine() End Sub Private Function LoadTimeSeriesData() As DataTable Dim filename As String = "..\..\..\Data\MicrosoftStock.xls" Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""" Dim cnn As OleDbConnection = Nothing Dim ds As DataSet = New DataSet Try cnn = New OleDbConnection(connectionString) cnn.Open() Dim adapter As OleDbDataAdapter = New OleDbDataAdapter("Select * from [MicrosoftStock$]", cnn) adapter.Fill(ds) Catch ex As OleDbException Console.WriteLine(ex.InnerException) Finally If Not (cnn Is Nothing) Then cnn.Close() End If end try Return ds.Tables(0) End Function End Module End Namespace

Copyright Â© 2003-2021, Extreme Optimization. All rights reserved.

*Extreme Optimization,* *Complexity made simple*, *M#*, and *M Sharp* are trademarks of ExoAnalytics Inc.

*Microsoft*, *Visual C#, Visual Basic, Visual Studio*, *Visual Studio.NET*, and the *Optimized for Visual Studio* logo

are registered trademarks of Microsoft Corporation.