Skip to content

Instantly share code, notes, and snippets.

@botany02
Last active February 6, 2019 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save botany02/a9127384ad9015b7b28800cd7a1d93e3 to your computer and use it in GitHub Desktop.
Save botany02/a9127384ad9015b7b28800cd7a1d93e3 to your computer and use it in GitHub Desktop.
Modify All BO Stoploss - KiteNet
Public Function ModifyAllBOStoploss(ByVal ParentOrderId As String) As String
On Error GoTo ErrHandler:
'This is generic example to modify all BO stoploss at certain % from Ltp
'Modify the code to suit your requirements
'Make sure you are not calling this function in a loop
'Use static variables to restrict multiple calling of this function
Dim ModifyPct As Single
ModifyPct = 0.25 / 100 'Modify the stoploss with 0.25% from Ltp
Dim ChildOrders As String
ChildOrders = Kite.GetChildOrders(ParentOrderId)
If ChildOrders = vbNullString Then ModifyAllBOStoploss = "NullChildOrders": Exit Function
Dim ChildOrderArray() As String
ChildOrderArray = Split(ChildOrders, ",")
'Loop through all child orders
'Check for SL order type
'Check for transaction
Dim ChildOrder As Variant
For Each ChildOrder In ChildOrderArray
If ChildOrders <> vbNullString Then 'Check for Null or Empty order id
Dim OrdType As String
OrdType = Kite.GetOrderType(ChildOrder)
If OrdType = "SL" Then 'Modify only if OrderType SL
Dim Trans As String
Dim Exch As String
Dim TrdSym As String
Trans = Kite.GetOrderTrans(ChildOrder)
Exch = Kite.GetOrderExch(ChildOrder)
TrdSym = Kite.GetOrderTrdSym(ChildOrder)
Dim Ltp As Double
Ltp = Kite.GetLtp(Exch, TrdSym)
If Ltp > 0 Then 'Check we have correct Ltp
Dim NewSlPrice As Double
Dim PointsToModify As Double
PointsToModify = Ltp * ModifyPct '455 * 0.0025 = 1.14
If Trans = "BUY" Then 'SL BUY Order
NewSlPrice = Ltp + PointsToModify
Else 'SL SELL order
NewSlPrice = Ltp - PointsToModify
End If
'Modify the BO stoploss order
Kite.ModifyBOSl ChildOrder, NewSlPrice
End If
End If
End If
Next
ModifyAllBOStoploss = "Success"
Exit Function
ErrHandler:
ModifyAllBOStoploss = Err.Description
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment