Pegged to Market Order using VBA

Pegged to market orders are frequently used by traders and scalpers in volatile markets, because they provide the opportunity to get the best possible price when buying or selling a security. Peg orders are based on the Best Bid and Ask. The Best Bid is the highest price a trader is willing to pay for a security (or stock); while the Best Ask is the lowest price someone is willing to receive when selling a security. The difference between the Best Bid and Best Ask is known as the spread.

For example, if the trader wants to sell a stock, it would be pegged to the bid side of the order book. Peg orders allow a trader to track the best bid when they are selling a stock and the best ask when buying a security.

Sell order price = Bid price + offset amount
Buy order price = Ask price – offset amount

Applicability: Kite Connect API

Requirements:

  • Kite API Subscription
  • KiteNet installed
  • VBA Knowledge

Syntax:

PeggedToMarketOrder(ByVal OrderId As String, ByVal Offset As Single, ByVal Delay As Integer)

How it works:

This function will try to execute the Open Limit order at the best price with offset amount for two consecutive times with delay and finally place the order at MARKET at third attempt if the order is still open.

Demo:

Code:

Option Explicit

Private Common As New Common
Private Bridge As New Bridge

Private Dict_OrderPlaceTime As New Scripting.Dictionary
Private Dict_OrderModifyTime As New Scripting.Dictionary

Private Function GetDictKey(ByVal DictName As String, ByVal DictKey As String) As String
    On Error Resume Next
    GetDictKey = Common.GetDictKey(DictName, DictKey)
End Function

Private Function SetDictKey(ByVal DictName As String, ByVal DictKey As String, ByVal KeyValue As String) As Boolean
    On Error Resume Next
    SetDictKey = Common.SetDictKey(DictName, DictKey, KeyValue)
End Function

Private Function CheckDictKey(ByVal DictName As String, ByVal DictKey As String) As Boolean
    On Error Resume Next
    CheckDictKey = Common.CheckDictKey(DictName, DictKey)
End Function

Public Function PeggedToMarketOrder(ByVal OrderId As String, ByVal Offset As Single, ByVal Delay As Integer) As String
    Application.Volatile

    On Error GoTo ErrHandler:
    'Validate DictKey variables
    If OrderId = "" Then PeggedToMarketOrder = "NULLORDERID": Exit Function
    If (Delay < 3 Or Delay > 300) Then PeggedToMarketOrder = "NULLDELAY": Exit Function
    If Bridge.GetConnectedClient = "" Then PeggedToMarketOrder = "NULLBRIDGE": Exit Function

    Dim PegStatus As String
    Dim Exch As String
    Dim TrdSym As String
    Dim Trans As String

    Dim OrderModifyTime As Date
    Dim TimeElasped As Integer

    Dim BestPrice As Double
    Dim OrderStatus As String
    Dim OrdType As String
    Dim IsAsync As Boolean

    If Not CheckDictKey("P2M", OrderId) Then    'First Call
        Exch = Bridge.GetOrderExch(OrderId, True)
        TrdSym = Bridge.GetOrderTrdSym(OrderId, True)
        Trans = Bridge.GetOrderTrans(OrderId, True)

        SetDictKey "P2M", OrderId, "1"
        SetDictKey "P2M-STATUS", OrderId, ""
        SetDictKey "P2M-EXCH", OrderId, Exch
        SetDictKey "P2M-TRDSYM", OrderId, TrdSym
        SetDictKey "P2M-TRANS", OrderId, Trans

        Dict_OrderModifyTime.Add OrderId, Now()

        PeggedToMarketOrder = "WAIT"
        Exit Function
    End If

    PegStatus = GetDictKey("P2M", OrderId)
    If PegStatus = "4" Then PeggedToMarketOrder = GetDictKey("P2M-STATUS", OrderId): Exit Function
    
    OrderStatus = Bridge.GetOrderStatus(OrderId, True)
    If (OrderStatus = "COMPLETE" Or OrderStatus = "CANCELLED" Or OrderStatus = "REJECTED") Then    'Check
        SetDictKey "P2M", OrderId, "4"
        SetDictKey "P2M-STATUS", OrderId, OrderStatus
        PeggedToMarketOrder = OrderStatus
        Exit Function
    End If

    Exch = GetDictKey("P2M-EXCH", OrderId)
    TrdSym = GetDictKey("P2M-TRDSYM", OrderId)
    Trans = GetDictKey("P2M-TRANS", OrderId)
    OrdType = "LIMIT"
    IsAsync = True

    OrderModifyTime = Dict_OrderModifyTime(OrderId)
    TimeElasped = DateDiff("s", OrderModifyTime, Now())

    If Trans = "BUY" Then    'Check
        BestPrice = Bridge.GetBestAsk(Exch, TrdSym) + Offset
    Else
        BestPrice = Bridge.GetBestBid(Exch, TrdSym) - Offset
    End If

    If PegStatus = "1" Then GoTo ModifyOne:
    If PegStatus = "2" Then GoTo ModifyTwo:
    If PegStatus = "3" Then GoTo ModifyThree:

ModifyOne:
    If TimeElasped < Delay Then PeggedToMarketOrder = "WAIT1": Exit Function
    'Modify Order
    Bridge.ModifyRegularOrderBridge OrderId, OrdType, , BestPrice, 0, IsAsync, 0
    'Set Status
    SetDictKey "P2M", OrderId, "2"
    Dict_OrderModifyTime.Item(OrderId) = Now()
    PeggedToMarketOrder = "MODIFIED1"
    Exit Function
ModifyTwo:
    If TimeElasped < Delay Then PeggedToMarketOrder = "WAIT2": Exit Function
    'Modify Order
    Bridge.ModifyRegularOrderBridge OrderId, OrdType, , BestPrice, 0, IsAsync, 0
    'Set Status
    SetDictKey "P2M", OrderId, "3"
    Dict_OrderModifyTime.Item(OrderId) = Now()
    PeggedToMarketOrder = "MODIFIED2"
    Exit Function
ModifyThree:
    If TimeElasped < Delay Then PeggedToMarketOrder = "WAIT3": Exit Function
    'Modify Order to Market
    Bridge.ModifyRegularOrderBridge OrderId, "MARKET", , BestPrice, 0, IsAsync, 0    'Check
    'Set Final Status
    SetDictKey "P2M-STATUS", OrderId, "PEGGED"
    SetDictKey "P2M", OrderId, "4"
    Dict_OrderModifyTime.Item(OrderId) = Now()
    PeggedToMarketOrder = "MODIFIED3"
    Exit Function
ErrHandler:
    PeggedToMarketOrder = "ERROR"
End Function

Was this article helpful?

Related Articles

Leave A Comment?