'Declare order object
Dim Order As SterlingLib.STIOrder
'Declare integer variable to manage loop
Dim intLoop As Integer
'Declare integer variable to hold submit order errors
Dim intSubmit As Integer
Option Explicit
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Address = "$L$2" Then
If Target.Value = "True" Then
'Stop any possible runtime errors and halting code
On Error Resume Next
Application.EnableEvents = False
'Initialize intLoop to the second row to account for the title row
intLoop = 2
'Set up Loop to cover all positions until there is a blank line
Do Until Range("A" & intLoop).Value = vbNullString
'Initialize Order object
Set Order = New SterlingLib.STIOrder
'Determine Side for the order based on column C, Sell if position is Long, Buy if position is Short
If Range("C" & intLoop).Value > 0 Then
Order.Side = "S"
End If
If Range("C" & intLoop).Value < 0 Then
Order.Side = "B"
End If
'Determine Symbol for the order based on column A
Order.Symbol = Range("A" & intLoop).Value
'Set Time in Force for the order to Day
Order.Tif = "D"
'Determine Account for the order based on column B
Order.Account = Range("B" & intLoop).Value
'Determine Quantity of the order, whether long or short, it will equal the absolute value of the position
Order.Quantity = Abs(Range("C" & intLoop).Value)
'Determine Destination for the order based on column F, the default is ARCA
If Range("F" & intLoop).Value = vbNullString Then
Order.Destination = "ARCA"
Else
Order.Destination = Range("F" & intLoop).Value
End If
'Determine Price for the order based on column E, If it is a number it will be a limit price, otherwise it will send a market order
If IsNumeric(Range("E" & intLoop).Value) Then
Order.PriceType = ptSTILmt
Order.LmtPrice = Range("E" & intLoop).Value
Else
Order.PriceType = ptSTIMkt
End If
'Send the order and capture any errors
intSubmit = Order.SubmitOrder
'Display any errors. Error code of 0 means that the order was submitted successfully. See the Sterling Trader ActiveX API Guide under the Support Documentation drop down box at http://sterlingfinancialsystems.com/support.php.
If intSubmit <> 0 Then
MsgBox ("Submit Order Error " & Str(intSubmit) & ". See the Sterling Trader ActiveX API Guide for more information.")
End If
'Destroy Order object
Set Order = Nothing
'Increment intLoop
intLoop = intLoop + 1
Loop
'Turn events back on
Application.EnableEvents = True
'Allow run time errors again
On Error GoTo 0
End If
End If
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Address = "$L$2" Then
If Target.Value = "True" Then
MsgBox ("Submit Order Error. See the Sterling Trader ActiveX API Guide for more information.")
End If
End If
End Sub