Achieving IsCrossAbove & IsCrossBelow Functionality

VenkateshV
Hi,
On GetBuyTrigger and GetShorttrigger methods, we use IsCrossAbove & IsCrossBelow to check whether the LTP crosses from below the entry price.

I want to use this similar functionality (IsCrossAbove & IsCrossBelow) to check a particular custom dynamic variable crosses from above or below certain threshold levels.

Is there a standard kitenet function/method available? or otherwise appreciate if could you suggest a VBA method to achieve it
  • HowUTrade
    @VenkateshV

    You need to do it in VBA as there is no such function in KiteNet.
    Below is the example code in VBA to get started.


    '''Created By HowUTrade
    '''https://howutrade.in
    '''[email protected]

    'Add Reference to 'Microsoft Scripting Runtime'
    'VBA Editor --> Menu --> Tools --> References
    'We declare two Dictionaries to hold values

    Public Dict_IsCrossFromBelow As New Scripting.Dictionary
    Public Dict_IsCrossFromAbove As New Scripting.Dictionary

    Public Function IsCrossFromBelow(ByVal DictKey As String, ByVal Level As Double, ByVal Price As Double) As Boolean
    On Error GoTo ErrHandler:
    'Parameters
    'DictKey : A unique key to store values Ex: Trade Symbol
    'Level : The price level to check
    'Price : Dynamic value
    'Check for Invalid Parameters
    If DictKey = "" Or Level = 0 Or Price = 0 Then IsCrossFromBelow = False: Exit Function
    'Add Dictionary Key if doesn't exists
    If Not Dict_IsCrossFromBelow.Exists(DictKey) Then
    Dict_IsCrossFromBelow.Add DictKey, Price
    End If
    'Retrieve the last price
    Dim LastPrice As Double
    LastPrice = Dict_IsCrossFromBelow.Item(DictKey)
    'Store the latest price as last price
    Dict_IsCrossFromBelow.Item(DictKey) = Price
    If LastPrice < Level And Price >= Level Then IsCrossFromBelow = True: Exit Function
    IsCrossFromBelow = False
    Exit Function
    ErrHandler:
    IsCrossFromBelow = False
    End Function

    Public Function IsCrossFromAbove(ByVal DictKey As String, ByVal Level As Double, ByVal Price As Double) As Boolean
    On Error GoTo ErrHandler:
    'Parameters
    'DictKey : A unique key to store values Ex: Trade Symbol
    'Level : The price level to check
    'Price : Dynamic value
    'Check for Invalid Parameters
    If DictKey = "" Or Level = 0 Or Price = 0 Then IsCrossFromAbove = False: Exit Function
    'Add Dictionary Key if doesn't exists
    If Not Dict_IsCrossFromAbove.Exists(DictKey) Then
    Dict_IsCrossFromAbove.Add DictKey, Price
    End If
    'Retrieve the last price
    Dim LastPrice As Double
    LastPrice = Dict_IsCrossFromAbove.Item(DictKey)
    'Store the latest price as last price
    Dict_IsCrossFromAbove.Item(DictKey) = Price
    If LastPrice > Level And Price
  • HowUTrade
    Hi Venkat,

    You need to do it in VBA
    Below is the example code to get started.



    '''Created By HowUTrade
    '''https://howutrade.in
    '''[email protected]

    'Add Reference to 'Microsoft Scripting Runtime'
    'VBA Editor --> Menu --> Tools --> References
    'We declare two Dictionaries to hold values

    Public Dict_IsCrossFromBelow As New Scripting.Dictionary
    Public Dict_IsCrossFromAbove As New Scripting.Dictionary

    Public Function IsCrossFromBelow(ByVal DictKey As String, ByVal Level As Double, ByVal Price As Double) As Boolean
    On Error GoTo ErrHandler:

    'Parameters
    'DictKey : A unique key to store values Ex: Trade Symbol
    'Level : The price level to check
    'Price : Dynamic value

    'Check for Invalid Parameters
    If DictKey = "" Or Level = 0 Or Price = 0 Then IsCrossFromBelow = False: Exit Function

    'Add Dictionary Key if doesn't exists
    If Not Dict_IsCrossFromBelow.Exists(DictKey) Then
    Dict_IsCrossFromBelow.Add DictKey, Price
    End If

    'Retrieve the last price
    Dim LastPrice As Double
    LastPrice = Dict_IsCrossFromBelow.Item(DictKey)

    'Store the latest price as last price
    Dict_IsCrossFromBelow.Item(DictKey) = Price

    If LastPrice < Level And Price >= Level Then IsCrossFromBelow = True: Exit Function

    IsCrossFromBelow = False
    Exit Function
    ErrHandler:
    IsCrossFromBelow = False
    End Function

    Public Function IsCrossFromAbove(ByVal DictKey As String, ByVal Level As Double, ByVal Price As Double) As Boolean
    On Error GoTo ErrHandler:

    'Parameters
    'DictKey : A unique key to store values Ex: Trade Symbol
    'Level : The price level to check
    'Price : Dynamic value

    'Check for Invalid Parameters
    If DictKey = "" Or Level = 0 Or Price = 0 Then IsCrossFromAbove = False: Exit Function

    'Add Dictionary Key if doesn't exists
    If Not Dict_IsCrossFromAbove.Exists(DictKey) Then
    Dict_IsCrossFromAbove.Add DictKey, Price
    End If

    'Retrieve the last price
    Dim LastPrice As Double
    LastPrice = Dict_IsCrossFromAbove.Item(DictKey)

    'Store the latest price as last price
    Dict_IsCrossFromAbove.Item(DictKey) = Price

    If LastPrice > Level And Price <= Level Then IsCrossFromAbove = True: Exit Function

    IsCrossFromAbove = False
    Exit Function
    ErrHandler:
    IsCrossFromAbove = False
    End Function
  • VenkateshV
    VenkateshV edited July 2018
    Hi Sir,
    I tried this and works perfectly what i wanted!!
    Many thanks for your amazing codes...

    However i just see a caveat here..
    no doubt, it detects the crossover from below or above...however i see a limitation of not getting latched once price crossed over the level and if price is further rising/falling after that ...let me explain with my observation test on Iscrossfrombelow function.

    Level = 50 Price = 40 , ISCrossfrombelow = FALSE
    Level = 50 Price = 50.5 , ISCrossfrombelow = TRUE (here this works perfectly)
    Level = 50 Price = 51 , ISCrossfrombelow = FALSE (Here i expect the value should get latched to True, however its not happening, same with ISCrossfromAbove as well)
    Since the value is not latching, i see a limitation using for strategy.

    Pls advise.
  • HowUTrade
    @VenkateshV

    That's how it works. Whenever it crosses, it will return true.
    If you want that to happen only once,

    We just need to check whether the lastprice is crossed the level or not.

    Here is the code
    Public Function IsCrossFromBelow(ByVal DictKey As String, ByVal Level As Double, ByVal Price As Double) As Boolean
    On Error GoTo ErrHandler:

    'Parameters
    'DictKey : A unique key to store values Ex: Trade Symbol
    'Level : The price level to check
    'Price : Dynamic value

    'Check for Invalid Parameters
    If DictKey = "" Or Level = 0 Or Price = 0 Then IsCrossFromBelow = False: Exit Function

    'Add Dictionary Key if doesn't exists
    If Not Dict_IsCrossFromBelow.Exists(DictKey) Then
    Dict_IsCrossFromBelow.Add DictKey, Price
    End If

    'Retrieve the last price
    Dim LastPrice As Double
    LastPrice = Dict_IsCrossFromBelow.Item(DictKey)

    'Exit if already crossed. Cross will happen only once.
    If LastPrice >= Level Then IsCrossFromBelow = True: Exit Function

    'Store the latest price as last price
    Dict_IsCrossFromBelow.Item(DictKey) = Price

    If LastPrice < Level And Price >= Level Then IsCrossFromBelow = True: Exit Function

    IsCrossFromBelow = False
    Exit Function
    ErrHandler:
    IsCrossFromBelow = False
    End Function
  • VenkateshV
    Dear Sir,
    This works fantabulous and exactly what i wanted.
    Many thanks..
Sign In or Register to comment.