The Option-Graph Project Option-Graph.com

The EquityOption Class

   A Visual Basic "Class" is a way to keep all information about a type of object together, and also a way to return whatever information about an object you need.

     For instance, if you had a program that needed information about a lot of rectangles, to do it without using a class you would enter the height and width of each rectangle, and every time you needed to know the area of a rectangle you would need to do a calculation like "rectangle1 area = rectangle1 height x rectangle1 width".

     But by using a class, you can put functions you need inside the class, such as "area = height x width". Then every time you need the area of a rectangle, you can just refer to the function in the class, such as by using "rectangle1.area".

     This saves a lot of repetitive coding and calculations. In our project, an "option" is a type of object we need to store and retrieve a lot of details about, such as "long or short", "put or call", price, strike, etc. Making a class for options will therefore make our code cleaner, easier to use, and easier to understand.

     Using a class may seem like a little overkill right now, since our calculator project only uses one option "leg". But we will be building on our calculator later by adding three more option legs, and putting the details about options in a class will save a lot of repetitive coding.

     We have built the class for you. All you need to do is copy and paste. But first, you need to make a special place to put it.

     In your Visual Basic project, look to the upper right of your screen and find the "Solution Explorer" window. You should see the name of the project, "Option Calculator", in bold. Right-click on "Option Calculator", click on "Add", and then "Class".

     A window will open and suggest the name "Class1.vb" at the bottom. Change the name to "EquityOption.vb" and click the "Add" button.

     A new code space will open, with "Public Class EquityOption" at the top, and "End Class" at the bottom.

     Now, simply copy all of the code below, and paste it to the space between "Public Class EquityOption" and "End Class".

Dim otype As String
Dim numc As Integer
Dim entry, exp As Date
Dim stock, strike, oprice, ivol, intrate, div As Single
Dim include As Boolean

Public Sub New(ByVal optiontype As String, ByVal numcontracts As Integer, ByVal entrydate As Date, ByVal stockprice As Single, ByVal strikeprice As Single, ByVal expiration As Date, ByVal price As Single, ByVal iv As Single, ByVal included As Boolean, ByVal interestrate As Single, ByVal dividends As Single)
otype = optiontype
numc = numcontracts
entry = entrydate .Date
stock = stockprice
strike = strikeprice
exp = expiration.Date
oprice = Math.Round(price, 2)
ivol = Math.Round(iv / 100, 2)
include = included
intrate = interestrate
div = dividends
End Sub

Function price() As Single
oprice = optionvalue(stock, strike, timeleft(entry, exp), intrate, ivol, div, otype)
If oprice < 0.01 Then
Return 0.01
Else
Return Math.Round(oprice, 2)
End If
End Function

Function volatility() As Single
ivol = findIV(stock, strike, timeleft(entry, exp), intrate, div, oprice, otype)
If ivol < 0.01 Then
Return 0.01
Else
Return Math.Round(ivol, 2)
End If
End Function

Function totalvalue(ByVal displayprice As Decimal) As String
If include = True Then
Return FormatCurrency(valuesign() * displayprice * 100 * numc)
Else
Return FormatCurrency(0)
End If
End Function

Function colorize() As Color
If include = True Then
If otype.Contains("Long") Then
Return Color.Pink
Else
Return Color.LightGreen
End If
Else
Return Color.Gray
End If
End Function

Function expirationafterentry() As Boolean
'need to check all legs included or not, otherwise could get errors when figuring price
If DateDiff(DateInterval.Day, entry, exp) > 0 Then
Return True
Else
Return False
End If
End Function

Function valuesign() As Integer
If otype.Contains("Long") Then
Return 1
Else
Return -1
End If
End Function

Save your project.

     

Home Page Downloads
Previous Step: The FindIV Function
Next Step: The Code Behind the Buttons

Copyright 2017 option-info.com Privacy Policy

Questions, corrections, suggestions, comments to: this contact

Want to learn options, find strategies? Visit www.option-info.com