Anyone write a Visual Basic program to help their trading?

Discussion in 'Trading Software' started by monty21, Mar 26, 2009.

  1. richardm

    richardm

    What sucks it that there are a bunch of bs dependencies in every platform out there - - visual basic and / or VBA can at least get rid of the dependencies.
    Wealthlab was bought by TDAmeritrade and was already somewhat locked down by those dorks with dotNet, so there goes that code.
    The modulusfe dot com stuff looks good, but there is a big chunk of cash to get all of the source - - if anyone has some, then modify it accordingly and show us to help accellerate the progress here.
    Otherwise, I was thinking we could use the code inside of a platform that has visual basic (not dotNet) or visual-basic-like scripting involved. The only one that I have found so far that lets you at least use visual basic to script is Visual Trader, so here is some of their flavor in the next couple of posts...
     
    #21     Sep 5, 2009
  2. richardm

    richardm

    almost forgot - - visual basic like scripting in tradersstudio as well, but I have not taken the time to try it yet since it does not interface with live data yet...
    So here is a quick example from traders studio to show why I am excited that visual basic can drop-kick any of the dependencies out there.

    Sub PrintPerfectProfit (TickValue As Integer)

    Dim i As Integer
    Dim PerfectProfit As BarArray
    Dim PerfectWinnings As BarArray

    'Range is a global bararray
    Print "Perfect Profit for the day"," ", FormatDateTime(D), " ", Range

    For i = FirstBar To LastBar Step 1
    Next

    PerfectProfit = PerfectProfit + Range
    PerfectWinnings = PerfectProfit * TickValue

    Print "Running Total"," ",PerfectProfit
    Print "PnL"," ","$","",PerfectWinnings
    End Sub
     
    #22     Sep 5, 2009
  3. richardm

    richardm

    Dim Period As Double
    Dim Num As Double
    Dim Den As Double
    Dim i As Integer
    Dim PreviousValue As Double
    Dim PresentBar As Double
    Dim value As Double
    Const Data As Long = 0

    Option Explicit
    Public APP As OscUserApp
    Implements Indicator

    Public Sub Indicator_OnInitCalculate()
    With APP
    Num = 0
    Den = 0
    i = 0
    PreviousValue = 0
    PresentBar = Period - 1
    value = 0
    .StartBar = Period
    End With
    End Sub

    Public Sub Indicator_OnCalculateBar(ByVal Bar As Long)
    With APP
    If .CurrentBar <> PresentBar Then
    Num = 0
    Den = 0
    For i = 1 To Period
    Num = Num + (1 + i) * (.High(i) + .Low(i) / 2)
    Den = Den + (.High(i) + .Low(i) / 2)
    Next i
    If Den <> 0 Then
    value = -Num / Den
    End If
    .SetIndicatorValue value
    .SetIndicatorValue PreviousValue, 2
    PreviousValue = value
    PresentBar = .CurrentBar
    End If
    End With
    End Sub

    Public Sub Indicator_OnSetParameters(ParamArray ParamList() As Variant)
    Period = ParamList(1)
    End Sub

    Public Sub Indicator_OnCalculateRange(ByVal StartBar As Long, ByVal FinalBar As Long)
    Dim i As Long
    i = APP.StartBar
    If StartBar > i Then
    i = StartBar
    End If
    While Not APP.ShouldTerminate And i <= FinalBar
    APP.CurrentBar = i
    Indicator_OnCalculateBar i
    i = i + 1
    Wend
    End Sub

    Private Sub OscUserAppInstance_OnConnection(ByVal Application As OscUserApp, ByVal MTDllInst As Object, Custom() As Variant)
    Set APP = Application
    End Sub
     
    #23     Sep 5, 2009
  4. richardm

    richardm

    Dim Range As Double
    Dim a As Double
    Dim c1 As Double
    Dim c2 As Double
    Dim c3 As Double
    Dim c4 As Double
    Dim res As Double
    Dim E1Period As Double
    Dim E2Period As Double
    Dim E3Period As Double
    Dim E4Period As Double
    Dim E5Period As Double
    Dim E6Period As Double
    Dim E1PriceSource As Price
    Dim E2PriceSource As Price
    Dim E3PriceSource As Price
    Dim E4PriceSource As Price
    Dim E5PriceSource As Price
    Dim E6PriceSource As Price
    Const Data As Long = 0
    Dim E1 As Long
    Dim E2 As Long
    Dim E3 As Long
    Dim E4 As Long
    Dim E5 As Long
    Dim E6 As Long

    Option Explicit
    Public APP As OscUserApp
    Implements Indicator
    Public Sub Indicator_OnInitCalculate()
    With APP

    c1 = 0
    c2 = 0
    c3 = 0
    c4 = 0
    res = 0
    E1Period = Range
    E2Period = Range
    E3Period = Range
    E4Period = Range
    E5Period = Range
    E6Period = Range
    E1PriceSource = 0
    E2PriceSource = 0
    E3PriceSource = 0
    E4PriceSource = 0
    E5PriceSource = 0
    E6PriceSource = 0
    E1 = .GetIndicatorIdentifier(AvExponential, Data, E1Period, E1PriceSource)
    E2 = .GetIndicatorIdentifier(AvExponential, E1, E2Period, E2PriceSource)
    E3 = .GetIndicatorIdentifier(AvExponential, E2, E3Period, E3PriceSource)
    E4 = .GetIndicatorIdentifier(AvExponential, E3, E4Period, E4PriceSource)
    E5 = .GetIndicatorIdentifier(AvExponential, E4, E5Period, E5PriceSource)
    E6 = .GetIndicatorIdentifier(AvExponential, E5, E6Period, E6PriceSource)
    .StartBar = 0
    End With
    End Sub

    Public Sub Indicator_OnCalculateBar(ByVal Bar As Long)
    With APP
    c1 = -(a * a * a)
    c2 = 3 * a * a + 3 * a * a * a
    c3 = -6 * a * a - 3 * a - 3 * a * a * a
    c4 = 1 + 3 * a + a * a * a + 3 * a * a
    res = c1 * .GetIndicatorValue(E6) + c2 * .GetIndicatorValue(E5) + c3 * .GetIndicatorValue(E4) + c4 * .GetIndicatorValue(E3)
    .SetIndicatorValue res
    End With
    End Sub

    Public Sub Indicator_OnSetParameters(ParamArray ParamList() As Variant)
    Range = ParamList(1)
    a = ParamList(2)
    End Sub

    Public Sub Indicator_OnCalculateRange(ByVal StartBar As Long, ByVal FinalBar As Long)
    Dim i As Long
    i = APP.StartBar
    If StartBar > i Then
    i = StartBar
    End If
    While Not APP.ShouldTerminate And i <= FinalBar
    APP.CurrentBar = i
    Indicator_OnCalculateBar i
    i = i + 1
    Wend
    End Sub

    Private Sub OscUserAppInstance_OnConnection(ByVal Application As OscUserApp, ByVal MTDllInst As Object, Custom() As Variant)
    Set APP = Application
    End Sub
     
    #24     Sep 5, 2009
  5. richardm

    richardm

    Dim TicksNumber As Long
    Dim OnChangeClose As String
    Dim LastBar As Double
    Dim X As Double
    Dim Ticks() As Double
    Dim LastClose As Double
    Dim SymbolCode As String

    Option Explicit
    Public APP As OscUserApp
    Implements Indicator
    Public Sub Indicator_OnInitCalculate()
    With APP

    ReDim Preserve Ticks(TicksNumber) As Double

    If SymbolCode <> Mid(.GetSymbolInfo(SbiCode, Data, Date), 7, 3) Then
    For X = 0 To TicksNumber
    Ticks(X) = NullValue
    Next X
    SymbolCode = Mid(.GetSymbolInfo(SbiCode, Data, Date), 7, 3)
    End If
    End With
    End Sub

    Public Sub Indicator_OnCalculateBar(ByVal Bar As Long)
    With APP

    If .CurrentBar = LastBar Then
    If OnChangeClose = "No" Then
    For X = TicksNumber To 1 Step -1
    Ticks(X) = Ticks(X - 1)
    Next X
    Ticks(0) = .Close
    For X = 0 To TicksNumber
    If Ticks(X) <> 0 And X < .CurrentBar Then
    .SetIndicatorValue Ticks(X), 1, X
    End If
    Next X
    Else
    If .Close <> LastClose Then
    For X = TicksNumber To 1 Step -1
    Ticks(X) = Ticks(X - 1)
    Next X
    Ticks(0) = .Close
    For X = 0 To TicksNumber
    If Ticks(X) <> 0 And X < .CurrentBar Then
    .SetIndicatorValue Ticks(X), 1, X
    End If
    Next X
    End If
    End If
    End If
    LastClose = .Close

    End With
    End Sub

    Public Sub Indicator_OnSetParameters(ParamArray ParamList() As Variant)
    TicksNumber = ParamList(1)
    OnChangeClose = ParamList(2)
    End Sub

    Public Sub Indicator_OnCalculateRange(ByVal StartBar As Long, ByVal FinalBar As Long)
    LastBar = FinalBar
    Dim i As Long
    i = APP.StartBar
    If StartBar > i Then
    i = StartBar
    End If
    While Not APP.ShouldTerminate And i <= FinalBar
    APP.CurrentBar = i
    Indicator_OnCalculateBar i
    i = i + 1
    Wend
    End Sub

    Private Sub OscUserAppInstance_OnConnection(ByVal Application As OscUserApp, ByVal MTDllInst As Object, Custom() As Variant)
    Set APP = Application
    End Sub
     
    #25     Sep 5, 2009
  6. richardm

    richardm

    Dim X As Double
    Dim Z As Double
    Const Data As Long = 0

    Option Explicit
    Public APP As DrwUserApp
    Implements Study
    Public Sub Study_OnInitCalculate()
    With APP
    X = 0
    Z = 0
    .StartBar = 0
    End With
    End Sub

    Public Sub Study_OnCalculateBar(ByVal Bar As Long)
    With APP
    X = 0
    While .Close(X) > .Close(X + 1)
    X = X + 1
    Wend
    Z = 0
    While .Close(Z) < .Close(Z + 1)
    Z = Z + 1
    Wend
    If X >= Z Then
    .PaintBar .Open, .High, .Low, .Close, RGB(0, 0, 255), 0, X - 1, 0
    Else
    .PaintBar .Open, .High, .Low, .Close, RGB(255, 0, 0), 0, Z - 1, 0
    End If
    End With
    End Sub

    Public Sub Study_OnSetParameters(ParamArray ParamList() As Variant)
    End Sub

    Public Sub Study_OnCalculateRange(ByVal StartBar As Long, ByVal FinalBar As Long)
    Dim i As Long
    i = APP.StartBar
    If StartBar > i Then
    i = StartBar
    End If
    While Not APP.ShouldTerminate And i <= FinalBar
    APP.CurrentBar = i
    Study_OnCalculateBar i
    i = i + 1
    Wend
    End Sub

    Private Sub DrwUserAppInstance_OnConnection(ByVal Application As DrwUserApp, ByVal MTDllInst As Object, Custom() As Variant)
    Set APP = Application
    End Sub
     
    #26     Sep 5, 2009
  7. richardm

    richardm

    'tick charts vol

    Dim TicksNumber As Long
    Dim OnChangeClose As String
    Dim LastBar As Double
    Dim X As Double
    Dim Ticks() As Double
    Dim LastClose As Double
    Dim SymbolCode As String
    Dim LastVolume As Double
    Dim LastCurrentBar As Double

    Option Explicit
    Public APP As OscUserApp
    Implements Indicator

    Public Sub Indicator_OnInitCalculate()
    With APP
    ReDim Ticks(TicksNumber) As Double

    If SymbolCode <> Mid(.GetSymbolInfo(SbiCode, Data, Date), 7, 3) Then
    For X = 0 To TicksNumber
    Ticks(X) = NullValue
    Next X
    SymbolCode = Mid(.GetSymbolInfo(SbiCode, Data, Date), 7, 3)
    End If
    End With
    End Sub

    Public Sub Indicator_OnCalculateBar(ByVal Bar As Long)
    With APP
    If .CurrentBar > LastCurrentBar Then
    LastVolume = 0
    End If
    If .CurrentBar = LastBar Then
    If OnChangeClose = "No" Then
    For X = TicksNumber To 1 Step -1
    Ticks(X) = Ticks(X - 1)
    Next X
    Ticks(0) = .Volume - LastVolume
    LastVolume = .Volume
    For X = 0 To TicksNumber
    If Ticks(X) <> 0 And X < .CurrentBar Then
    .SetIndicatorValue Ticks(X), 1, X
    End If
    Next X
    Else
    If .Close <> LastClose Then
    For X = TicksNumber To 1 Step -1
    Ticks(X) = Ticks(X - 1)
    Next X
    Ticks(0) = .Volume - LastVolume
    LastVolume = .Volume
    For X = 0 To TicksNumber
    If Ticks(X) <> 0 And X < .CurrentBar Then
    .SetIndicatorValue Ticks(X), 1, X
    End If
    Next X
    End If
    End If
    End If
    LastClose = .Close
    LastCurrentBar = .CurrentBar
    End With
    End Sub

    Public Sub Indicator_OnSetParameters(ParamArray ParamList() As Variant)
    TicksNumber = ParamList(1)
    OnChangeClose = ParamList(2)
    End Sub

    Public Sub Indicator_OnCalculateRange(ByVal StartBar As Long, ByVal FinalBar As Long)
    LastBar = FinalBar
    Dim i As Long
    i = APP.StartBar
    If StartBar > i Then
    i = StartBar
    End If
    While Not APP.ShouldTerminate And i <= FinalBar
    APP.CurrentBar = i
    Indicator_OnCalculateBar i
    i = i + 1
    Wend
    End Sub

    Private Sub OscUserAppInstance_OnConnection(ByVal Application As OscUserApp, ByVal MTDllInst As Object, Custom() As Variant)
    Set APP = Application
    End Sub
     
    #27     Sep 5, 2009
  8. This is just what I was talking about:
    Traders Studio IS A PLATFORM.
    The fact that it uses VBA is just a choice.
    Tradestation's Easy Language would use about 1/2 the amount of code.
    So VBA, even with Traders Studio is NOT the easiest-to-program platform.
     
    #28     Sep 5, 2009
  9. richardm

    richardm

    'Trend Tracking Indicator

    Dim Period As Integer
    Option Explicit
    Option Base 1
    Dim Z As Integer
    Dim UpPivotValue As Double
    Dim UpPivot As Double
    Dim UpPivotAverage As Double
    Dim DownPivotAverage As Double
    Dim DownPivot As Double
    Dim DownPivotValue As Double
    Dim LateralBars As Integer
    Dim PivotNumber As Integer
    Dim UpFind As Integer
    Dim DownFind As Integer
    Dim UpPivotAverageReserve As Double
    Dim Avg() As Double
    Dim CommonAvg As Double
    Dim AvgCounter As Double
    Dim DefinitiveAvg As Double
    Dim Inidicator_Value As Double
    Dim nPeriod As Integer
    Dim Control_Time As Double
    Dim Control_Date As Double
    Dim FinalControl As Boolean
    Dim Msg, Style, Title, Help, Ctxt, Response, MyString
    Dim MovilAvg As DataIdentifier

    Public APP As OscUserApp
    Implements Indicator

    Public Sub Indicator_OnInitCalculate()
    With APP
    FinalControl = True
    .StartBar = Period * 20 + 1
    nPeriod = Period * 6
    PivotNumber = Period - 1
    LateralBars = Period
    DefinitiveAvg = 0
    Inidicator_Value = 0
    CommonAvg = 0
    Control_Time = 0
    Control_Date = 0
    MovilAvg = .GetIndicatorIdentifier(AvSimple, Data, 3, PriceClose)
    ReDim Avg(nPeriod)
    AvgCounter = 1

    ' Controls if the value introduced in Period is within the allowed rank
    If Period <= 1 Or Period > 10 Then
    Msg = "It has introduced a value for Period not between 1 and 10, do you want to continue calculating this indicator?"
    ' Define message.
    Style = vbOKCancel + vbCritical + vbDefaultButton2 ' Define buttons.
    Title = "Error of Parameter" ' Define title.
    Ctxt = 1000 ' Define topic
    ' context.
    ' Display message.
    Response = MsgBox(Msg, Style, Title, Help, Ctxt)
    If Response = vbCancel Then
    FinalControl = False
    End If
    End If
    End With
    End Sub

    Public Sub Indicator_OnCalculateBar(ByVal Bar As Long)
    With APP
    UpPivotAverage = 0
    DownPivotAverage = 0
    UpFind = 0
    DownFind = 0
    For Z = 1 To PivotNumber
    DownPivot = .GetSwingLow(Data, Z, PriceLow, LateralBars, Period * 20)
    UpPivot = .GetSwingHigh(Data, Z, PriceHigh, LateralBars, Period * 20)
    If UpPivot <> NullValue Then
    UpPivotAverage = UpPivotAverage + UpPivot
    UpFind = UpFind + 1
    End If
    If DownPivot <> NullValue Then
    DownPivotAverage = DownPivotAverage + DownPivot
    DownFind = DownFind + 1
    End If
    Next Z
    UpPivotAverage = UpPivotAverage / UpFind
    DownPivotAverage = DownPivotAverage / DownFind

    If .High > UpPivotAverage Then
    CommonAvg = DownPivotAverage
    End If

    If .Low < DownPivotAverage Then
    CommonAvg = UpPivotAverage
    End If
    If .CurrentBar = .StartBar Then
    For Z = 1 To nPeriod
    Avg(Z) = CommonAvg
    Next Z
    End If
    If .Time > Control_Time Or .Date > Control_Date Then
    If AvgCounter <= nPeriod Then
    Avg(AvgCounter) = CommonAvg
    AvgCounter = AvgCounter + 1
    Else
    AvgCounter = 1
    Avg(AvgCounter) = CommonAvg
    AvgCounter = AvgCounter + 1

    End If
    Control_Time = .Time
    Control_Date = .Date
    End If

    DefinitiveAvg = 0
    For Z = 1 To nPeriod
    DefinitiveAvg = DefinitiveAvg + Avg(Z)
    Next Z

    DefinitiveAvg = DefinitiveAvg / nPeriod

    Inidicator_Value = .GetIndicatorValue(MovilAvg) - DefinitiveAvg

    .SetIndicatorValue Inidicator_Value, 1
    .SetIndicatorValue 0, 2
    End With
    End Sub

    Public Sub Indicator_OnSetParameters(ParamArray ParamList() As Variant)
    Period = ParamList(1)
    End Sub

    Public Sub Indicator_OnCalculateRange(ByVal StartBar As Long, ByVal FinalBar As Long)
    Dim i As Long
    i = APP.StartBar
    If StartBar > i Then
    i = StartBar
    End If
    While Not APP.ShouldTerminate And i <= FinalBar And FinalControl
    APP.CurrentBar = i
    Indicator_OnCalculateBar i
    i = i + 1
    Wend
    End Sub

    Private Sub OscUserAppInstance_OnConnection(ByVal Application As OscUserApp, ByVal MTDllInst As Object, Custom() As Variant)
    Set APP = Application
    End Sub
     
    #29     Sep 5, 2009
  10. richardm

    richardm

    Visual Basic for Applications and Visual Basic (not dotNet) is less of a hassle - - whatever platform, just less proprietary BS...
    Tradestation does not even let you do intrabar arrow indicators...
     
    #30     Sep 5, 2009