KiteXL Repeat Order Restriction - In place even after SL/Target hit + Not able to get Order ID

arjunmurali1993
Hi @botany02

I've used the below code that you had provided in this forum (modified for BO of course) to restrict multiple order placements in KiteXL. However, my strategy involves staying in the trade till the SL or Target is achieved and so there won't be any "Sell" order placements from KiteXL. So this prevents KiteXL from reentering that particular stock till the end of the day, even if the required criteria gets satisfied again.

Public Function PlaceRegularOrder(Exch As String, TrdSym As String, Trans As String, OrdType As String, Qty As Integer, _
ProdType As String, Optional LmtPrice As Double = 0, Optional TrgPrice As Double = 0, Optional val As String = "DAY") As String
On Error GoTo ErrHandler:

If Kite Is Nothing Then
PlaceRegularOrder = "User Not Logged-In"
Exit Function
End If

Dim LastOrder As String
If Not Dict_PlaceRG.Exists(UCase(TrdSym)) Then
Dict_PlaceRG.Add UCase(TrdSym), UCase(Trans)
LastOrder = ""
Else
LastOrder = Dict_PlaceRG(UCase(TrdSym))
End If

If LastOrder = UCase(Trans) Then
PlaceRegularOrder = "Duplicate Order"
Exit Function
End If

Dict_PlaceRG.Item(UCase(TrdSym)) = UCase(Trans)

PlaceRegularOrder = Kite.PlaceRegularOrder(Exch, TrdSym, Trans, OrdType, Qty, _
ProdType, LmtPrice, TrgPrice, val)

Exit Function
ErrHandler:
PlaceRegularOrder = Err.Description
Exit Function
End Function
Is there any way to check open positions from Kite and then use that to prevent repeat orders?

Another problem I am facing is that, because the price keeps changing every second, the cell with the order placement function immediately changes to 'Duplicate Order' after the order gets executed. So I am unable to get the order ID for the executed order and use that for modifying stop loss, etc.

I am not an expert in VBA or MS Scripting and would be really grateful for any kind of help with these two issues.
  • arjunmurali1993
    Hi @botany02

    1) I figured out how to get the last executed Cover order ID using =getOrderID and then finding the last 15 digits using =Right function to get the last order(I think this will be the Order ID of the auto generated Stop Loss Order) and then finding the second last order ID using =mid function (I think this will be the executed parent cover order ID). Then, using these two order IDs I can build my exit strategy using the ExitCO function.

    Is this logic correct? (the part where I assume the last order will be the SL order ID and second last order will be the parent Cover Order ID)


    2) Also, I learnt about the MS Scripting Dictionary in VBA and it seems pretty straightforward to use. I also studied the logic you had used to restrict repeated orders in Regular Order. Unfortunately, the same logic doesn't work for CO. So I was thinking of a possible logic as below:

    ->Store last two Orders in two adjacent cells as per the above point 1
    ->Use the =getOrderStatus function on the next cell to see the last order (the auto-generated SL order) status
    ->Modify the PlaceCO function as follows:

    Public Function PlaceCO(Exch As String, TrdSym As String, Trans As String, Qty As Integer, _
    stoploss_Price As Double, PrevOrderStatus As String) As String
    'Added PrevOrderStatus parameter to pass the cell with the SL Order Status
    On Error GoTo ErrHandler:

    If Kite Is Nothing Then
    PlaceCO = "User Not Logged-In"
    Exit Function
    End If

    If Not PrevOrderStatus = "OrderId is Null or Invalid" OR "COMPLETE" OR "CANCELLED"
    PlaceCO = "Duplicate Order"
    Exit Function
    Else
    PlaceCO = Kite.PlaceCO(Exch, TrdSym, Trans, Qty, _
    stoploss_Price)
    End If

    Exit Function
    ErrHandler:
    PlaceCO = Err.Description
    Exit Function
    End Function

    Is this logic correct? Please advice na

    Regards,
    Arjun Murali
  • botany02
    @arjunmurali1993
    The logic is correct..

    But Point 2 may not work. If You used vba to write Order Id to cell Values & using that value
    in getOrderStatus, then the Function will not be recalculated by Excel as no parameter is changing or dynamic.You need to pass at-least some param whose value is changing, so that excel will recalculate the formula.Ex:- add ltp as additional dummy parameter.

  • arjunmurali1993
    Hi @botany02

    I tried passing LTP as a dummpy parameter na, and it seemed to be working fine when I was manually testing.

    But during market hours, the buy/sell order gets repeatedly executed before the getorderIds gets updated. This causes Multiple entries as well as the ExitCO to fail and the whole system doesn't work as intended.

    So I have reverted back to placing a Bracket Order with a trailing SL as the exit na. For now, I've added a button to trigger a macro that manually resets the Dictionary value to " " after an SL/Target hit so as to enable it to re enter a trade on that scrip. Till I can think up an automated solution for this, I'll stick to this setup na. It seems to be working well.

    If you get any idea for a logic to make this automated, do let me know na.

    Thank you so much for the timely help :smile:

    Regards,
    Arjun Murali
Sign In or Register to comment.