Profile Picture

Convert SMASample1.cs to SMASample1.vb

Posted By willy 7 Years Ago
Posted Monday November 21 2011
Here is the visual basic code I generatet.

#Region "Imports statements"
Imports System
Imports System.Drawing
Imports System.Collections.Generic
Imports System.Text

Imports RightEdge.Common
Imports RightEdge.Indicators
#End Region

Namespace awSampleIndicators

'   Use the Indicator Attribute to provide information such as the
'   name and description of your indicator. If you do not include
'   this attribute, your indicator will not show up in the indicator
'   list.
'   The Id attribute needs to be set to a unique code that will identify
'   your indicator. A GUID is a good candidate.
'   Indicators must be marked Serializable in order to be used in
'   trading systems.
'   Use the SeriesInputAttribute to specify how many inputs your
'   indicator uses.
Author:="my name", _
CompanyName:="Company", _
Description:="Sample Simple Moving Average Indicator #1", _
GroupName:="Samples", _
HelpText:="Computes the moving average of the input.", _
Id:="{20b8d55d-72c7-4199-8f5d-432a165f7510}", _
Name:="SMASample1")> _
Public Class SMASample1
Inherits SeriesCalculatorBaseWithValues
Private _periods As Integer

'   You can add ConstructorArgumentAttributes to your constructor to specify
'   the name and default value of the argument.
Public Sub New(ByVal periods As Integer)
If periods <= 0 Then
Throw New ArgumentException("Periods must be greater than zero")
End If
_periods = periods
End Sub

'   This method is where you calculate the indicator value for a given bar.
'   The index parameter to this method tells you which value you need to
'   calculate.
'   You can access the first input series with inputs[0], the second
'   input series with inputs[1], etc.
Protected Overrides Function CalcNewValue(ByVal index As Integer) As Double
If index < _periods - 1 Then
Return Double.NaN
End If

Dim sum As Double = 0.0
For i As Integer = index - _periods + 1 To index
sum += inputs(0)(i) <------------------------------- this is the point where the error occured

Return sum / _periods
End Function

Protected Overrides Sub Reset()
'   This method is called when the series needs to be recalculated.
'   If you save any state between calls to CalcNewValue(), you should
'   reset that state in this method.

'   For this indicator, we don't need to do anything in this method.
End Sub
End Class
End Namespace

Then I got the following error message:

Fehler   1   Die Schnittstelle "RightEdge.Common.ISeries" kann nicht indiziert werden, da keine Standardeigenschaft vorhanden ist.   D:\VisualBasic\ClassLibrary1\ClassLibrary1\SMASampleIndicator_01.vb   60   24   ClassLibrary1

Ok I have to translate it for you.

Error 1 The "RightEdge.Common.ISeries" interface can not be indexed, because no standard property is available.

I need some help here to go ahead.
Posted Saturday November 26 2011

I solved the problem already.

The correxct code must be like this in vb.

sum += inputs(0).LookBack(i)

Posted Monday December 19 2011
Hi all,

for all of you who are beginners like me and want some examples in visual basic I added the samples I converted into visual basic here for your convenience.

Some further information.
Sample1 and Sample2 are not working when you use them in a quick chart, I don't know why. Maybe Daniel can give some further hints on that.
But all of them are working fine when you use them in system code.


Attachments (122 views, 10.00 KB)

Edited: Monday December 19 2011 by willy
Posted Monday December 19 2011
Thanks for sharing this, willy.
Posted Thursday December 29 2011
Hi all,

have made some minor modifications to sample 1 and 2.
Now they are working on a quick chart as well as in a system.


SMASample1.vb (193 views, 4.00 KB)
SMASample2.vb (211 views, 5.00 KB)
Posted Thursday December 29 2011
Hi Daniel,

I translated your SpearmanRanksCorrelation into VB.

Using this indicator on a quick chart it seems to work fine but when using it in a system it always return NaN.

I learned that if we use an indicator in a system the index value in the CalcNewValue function is always 0 right?

The following code snipped shows the beginning of the CalcNewValue function of the SpearmanRanksCorrelation indicator you have posted here

Protected Overrides Function CalcNewValue(ByVal index As Integer) As Double

Dim ranks As New List(Of RankData)

For i As Integer = index - _periods + 1 To index
If i < 0 Then
Return Double.NaN
End If
Dim data As New RankData()
data.S1 = inputs(0).LookBack(i)
data.S2 = inputs(1).LookBack(i)
If Double.IsNaN(data.S1) OrElse Double.IsNaN(data.S2) Then
Return Double.NaN
End If


If index is 0 then i will always be negative and NaN will be returned always. Is this your intention?


SpearmanRanksCorrelation.vb (211 views, 5.00 KB)

Edited: Friday December 30 2011 by willy
Posted Thursday December 29 2011
The code for the SpearmanRanksCorrelation is very old, and since it was written RightEdge has changed the way indicators work.  Originally, series (such as indicators) in RightEdge were indexed where 0 was the oldest item and the newest item had the highest index.  We changed it so that now you specify a LookBack value where 0 is the current bar's value, 1 is the previous bar's, etc.  This makes a lot of code a lot simpler: ie now you just write SMA.Current or SMA.LookBack(1) instead of SMA[SMA.Count- 1] or SMA[SMA.Count - 2].

Anyway, so the C# code you started with wouldn't have worked with the current version of RightEdge anyway.  The index passed to CalcNewValue is now a lookback value.  So basically the direction of all the indexing has changed.

I've attached an updated version of the C# code that *should* work with the current version of RightEdge (though I haven't tested it beyond making sure it compiles).


SpearmanRanksCorrelation.cs (235 views, 5.00 KB)
Posted Friday December 30 2011
Hi Daniel,

thanks a lot sharing this code with us.
Now the indicator is working perfectly when using it on a quick chart as well as using it in a system.

From my point of view it is the best way to learn a new system when playing around with some code examples.

I posted the converted VB code for all other users who also like to play with some code.


SpearmanRanksCorrelation.vb (186 views, 5.00 KB)

Similar Topics

Reading This Topic

2005-2018 © RightEdge Systems