Last active
July 17, 2018 10:26
-
-
Save botany02/8b1a687e4c309a50bb34510dca157073 to your computer and use it in GitHub Desktop.
Function to check crossover of Price (Excel VBA)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Option Explicit | |
''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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment