The Option-Graph Project Option-Graph.com

The Code Behind the Buttons

     At this point we have the option calculator interface and the code that can calculate an option price.

     To make our calculator actually do something, we need to add the code behind the buttons, so that when the user pushes a button, the calculation is made and the results are displayed on the interface.

     At the same time, we will also add some functions that create option legs, set startup parameters, and perform various checks.

     In your Visual Basic project, click on the tab that says "Form1.vb". You should see the code that we added previously for testing the names, that starts "Sub TestNames()" and ends "End Sub". Select that entire Sub and press your delete key to get rid of it. It serves no purpose in the completed calculator.

      If there is already a line that starts "Private Sub Form1_Load", get rid of that and the "End Sub" below it also. It will be replaced with the code you are pasting.

     Now, copy all the code below and paste it to a blank line between "Public Class Form1" and "End Class".

Friend leg1 as EquityOption

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As Date = startupdate()
Leg1ExpirationDate.Value = dt
Messages.Text = "Reminder: Double-Click any number to change it quickly"
Messages.ForeColor = Color.Blue
End Sub

Private Sub FindButtonsHandler(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_FindPrice.Click, B_FindIV.Click
createlegs()
If checkdates() = True Then
If sender.name = "B_FindPrice" Then
setprices()
If Dividends.Value > 0 Then
Messages.Text = "Option Prices on Dividend Paying stocks are approximate"
Else
Messages.Text = "Option Price calculated from IV"
End If
Else
setvolatility()
If Dividends.Value > 0 Then
Messages.Text = "Option Prices on Dividend Paying stocks are approximate"
Else
Messages.Text = "IV calculated from Option Price"
End If
End If
setvalues()
Messages.ForeColor = Color.DarkGreen
Else
setzeros()
Messages.ForeColor = Color.Red
Messages.Text = "Expiration date is on or before the Entry date"
End If
End Sub

Private Sub ChangeExp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Leg1ExpPrev.Click, Leg1ExpNext.Click
Dim direction As Integer = 1
If sender.name.contains("Prev") Then
direction = -1
End If
Leg1ExpirationDate.Value = changeexpiration(Leg1ExpirationDate.Value, direction)
End Sub

Private Sub createlegs()
'each option leg belongs to the EquityOption class
leg1 = New EquityOption(Leg1Type.Text, Leg1Contracts.Value, Leg1EntryDate.Value, Leg1StockPrice.Value, Leg1StrikePrice.Value, Leg1ExpirationDate.Value, Leg1OptionPrice.Value, Leg1IV.Value, True, InterestRate.Value, Dividends.Value)
End Sub

Private Function checkdates() As Boolean
Return leg1.expirationafterentry
End Function

Function startupdate() As Date
Dim dt As Date = changeexpiration(Now.Date, 0)
If DateDiff(DateInterval.Day, Now.Date, dt) > 0 Then
Return dt
Else
Return changeexpiration(Now.Date, 1)
End If
End Function

Private Sub setprices()
Leg1OptionPrice.Value = leg1.price
End Sub

Private Sub setvolatility()
Leg1IV.Value = leg1.volatility
End Sub

Private Sub setvalues()
Leg1Value.Text = leg1.totalvalue(Leg1OptionPrice.Value)
Leg1Value.BackColor = leg1.colorize
End Sub

Private Sub setzeros()
Leg1Value.Text = FormatCurrency(0)
Leg1Value.BackColor = Color.Gray
End Sub

Save your project.

     

Home Page Downloads
Previous Step: The EquityOption Class
Next Step: Final Checks and Testing

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