The Option-Graph Project Option-Graph.com

Basic Option Graph Step 9

Add and Replace Code

There is one line of code in the EquityOption Class that needs to be changed. Find the code as in the picture below, and change the ".01" to "0".

We have new code to add to three "code behind" areas - OptionsCalc.vb, EquityOption.vb, and Form1.vb.

The code for OptionsCalc.vb and EquityOption.vb will be ADDED to what was already in the Option Calculator Project.

The code for Form1.vb REPLACES everything that is currently there, which should just be "Public Class Form1", the TestNames() sub, and "End Class" if you have been following the instructions.

In these instructions, we won't go into much detail about what the code is doing. If you are interested in programming, you can study it all you want and/or e-mail questions to this contact
and we will either answer directly, or through an entry in the FAQS for this project. If you are more interested in using Basic Option Graph and not learning programming, you don't really need to know about the programming details.

First, click on the tab that says "OptionsCalc.vb" and ADD the following code to a blank line after the last "End Sub" but before the line that says "End Class".

Function middate(ByVal dte1 As Date, ByVal dte2 As Date) As Date
Dim diff As Integer = DateDiff(DateInterval.Day, dte1, dte2)
If diff < 2 Then
Return dte1
Else
Return DateAdd(DateInterval.Day, CInt(diff / 2), dte1)
End If
End Function
Function earliestincludedexpiration(ByVal dte1 As Date, ByVal dte2 As Date, ByVal dte3 As Date, ByVal dte4 As Date) As Date
Dim expdates(3) As Date
expdates(0) = dte1
expdates(1) = dte2
expdates(2) = dte3
expdates(3) = dte4
Array.Sort(expdates)
Return expdates(0).Date
End Function
Function latestincludedentry(ByVal dte1 As Date, ByVal dte2 As Date, ByVal dte3 As Date, ByVal dte4 As Date) As Date
Dim entdates(3) As Date
entdates(0) = dte1
entdates(1) = dte2
entdates(2) = dte3
entdates(3) = dte4
Array.Sort(entdates)
Return entdates(3).Date
End Function
Function averageincludedstrike(ByVal s1 As Single, ByVal s2 As Single, ByVal s3 As Single, ByVal s4 As Single, ByVal s5 As Single) As Single
'returns average included strikeprice rounded to nearest .50
Dim strikes(4) As Single
strikes(0) = s1
strikes(1) = s2
strikes(2) = s3
strikes(3) = s4
strikes(4) = s5
Dim count As Integer
Dim sum As Single
For i As Integer = 0 To 4
sum += strikes(i)
If strikes(i) <> 0 Then count += 1
Next
If sum = 0 Then
Return 0
End If
Return Math.Ceiling((sum / count) * 2) / 2
End Function

Next, click on the tab that says "EquityOption.vb" and ADD the following code to a blank line after the last "End Sub" but before the line that says "End Class".

Function expdate() As Date
If include = True Then
Return exp
Else
Return #12/31/9000#
End If
End Function
Function entdate() As Date
If include = True Then
Return entry
Else
Return #1/1/1900#
End If
End Function
Function strikeprc() As Single
If include = True Then
Return strike
Else
Return 0
End If
End Function
Function pchart(ByVal sp As Single) As Single
If include = True Then
Return optionvalue(sp, strike, timeleft(entry, exp), intrate, ivol, div, otype)
Else
Return 0
End If
End Function
Function vchart(ByVal sp As Single) As Single
If include = True Then
Return pchart(sp) * valuesign() * numc * 100
Else
Return 0
End If
End Function

Now, click on the tab that says "Form1.vb", copy all of the code below, and replace EVERYTHING that is already in the code behind, INCLUDING "Public Class Form1" and "End Class".

Imports System.Windows.Forms.DataVisualization.Charting
Public Class Form1
Dim checkdate As Date
Dim checkprice As Single
Friend leg1, leg2, leg3, leg4, leg1check, leg2check, leg3check, leg4check, leg1exp, leg2exp, leg3exp, leg4exp As EquityOption
Friend stockleg1 As EquityShares
Friend lastentry, earlyexp As Date
Friend entrydrcr, startprice As Single
Friend range As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
setstartupposition()
setupchart()
Messages.Text = "Reminder: Double-Click any number to change it quickly"
Messages.ForeColor = Color.Blue
B_FindPrice.PerformClick()
End Sub
Private Sub FindButtonsHandler(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_FindPrice.Click, B_FindIV.Click
createlegs()
setcheckprice()
If checkprice = 0 Then
Chart1.Visible = False
setzeros()
Messages.ForeColor = Color.Red
Messages.Text = "No Legs are Included or all Legs are 0.00"
Exit Sub
End If
resetcheckdate()
If checkdates() = True Then
Chart1.Visible = True
If sender.name = "B_FindPrice" Then
setprices()
createlegs()
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()
createlegs()
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()
makechart()
Messages.ForeColor = Color.DarkGreen
Else
Chart1.Visible = False
setzeros()
Messages.ForeColor = Color.Red
Messages.Text = "An 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, Leg2ExpPrev.Click, Leg2ExpNext.Click, Leg3ExpPrev.Click, Leg3ExpNext.Click, Leg4ExpPrev.Click, Leg4ExpNext.Click
Dim direction As Integer = 1
If sender.name.contains("Prev") Then
direction = -1
End If
Select Case Mid(sender.name, 1, 4)
Case "Leg1"
Leg1ExpirationDate.Value = changeexpiration(Leg1ExpirationDate.Value, direction)
Case "Leg2"
Leg2ExpirationDate.Value = changeexpiration(Leg2ExpirationDate.Value, direction)
Case "Leg3"
Leg3ExpirationDate.Value = changeexpiration(Leg3ExpirationDate.Value, direction)
Case "Leg4"
Leg4ExpirationDate.Value = changeexpiration(Leg4ExpirationDate.Value, direction)
End Select
End Sub
Private Sub createlegs()
leg1 = New EquityOption(Leg1Type.Text, Leg1Contracts.Value, Leg1EntryDate.Value, Leg1StockPrice.Value, Leg1StrikePrice.Value, Leg1ExpirationDate.Value, Leg1OptionPrice.Value, Leg1IV.Value, Leg1Include.Checked, InterestRate.Value, Dividends.Value)
leg2 = New EquityOption(Leg2Type.Text, Leg2Contracts.Value, Leg2EntryDate.Value, Leg2StockPrice.Value, Leg2StrikePrice.Value, Leg2ExpirationDate.Value, Leg2OptionPrice.Value, Leg2IV.Value, Leg2Include.Checked, InterestRate.Value, Dividends.Value)
leg3 = New EquityOption(Leg3Type.Text, Leg3Contracts.Value, Leg3EntryDate.Value, Leg3StockPrice.Value, Leg3StrikePrice.Value, Leg3ExpirationDate.Value, Leg3OptionPrice.Value, Leg3IV.Value, Leg3Include.Checked, InterestRate.Value, Dividends.Value)
leg4 = New EquityOption(Leg4Type.Text, Leg4Contracts.Value, Leg4EntryDate.Value, Leg4StockPrice.Value, Leg4StrikePrice.Value, Leg4ExpirationDate.Value, Leg4OptionPrice.Value, Leg4IV.Value, Leg4Include.Checked, InterestRate.Value, Dividends.Value)

leg1check = New EquityOption(Leg1Type.Text, Leg1Contracts.Value, checkdate, checkprice, Leg1StrikePrice.Value, Leg1ExpirationDate.Value, Leg1OptionPrice.Value, Leg1CheckIV.Value, Leg1Include.Checked, InterestRate.Value, Dividends.Value)
leg2check = New EquityOption(Leg2Type.Text, Leg2Contracts.Value, checkdate, checkprice, Leg2StrikePrice.Value, Leg2ExpirationDate.Value, Leg2OptionPrice.Value, Leg2CheckIV.Value, Leg2Include.Checked, InterestRate.Value, Dividends.Value)
leg3check = New EquityOption(Leg3Type.Text, Leg3Contracts.Value, checkdate, checkprice, Leg3StrikePrice.Value, Leg3ExpirationDate.Value, Leg3OptionPrice.Value, Leg3CheckIV.Value, Leg3Include.Checked, InterestRate.Value, Dividends.Value)
leg4check = New EquityOption(Leg4Type.Text, Leg4Contracts.Value, checkdate, checkprice, Leg4StrikePrice.Value, Leg4ExpirationDate.Value, Leg4OptionPrice.Value, Leg4CheckIV.Value, Leg4Include.Checked, InterestRate.Value, Dividends.Value)

lastentry = latestincludedentry(leg1.entdate, leg2.entdate, leg3.entdate, leg4.entdate)
earlyexp = earliestincludedexpiration(leg1.expdate, leg2.expdate, leg3.expdate, leg4.expdate)

leg1exp = New EquityOption(Leg1Type.Text, Leg1Contracts.Value, earlyexp, checkprice, Leg1StrikePrice.Value, Leg1ExpirationDate.Value, Leg1OptionPrice.Value, Leg1CheckIV.Value, Leg1Include.Checked, InterestRate.Value, Dividends.Value)
leg2exp = New EquityOption(Leg2Type.Text, Leg2Contracts.Value, earlyexp, checkprice, Leg2StrikePrice.Value, Leg2ExpirationDate.Value, Leg2OptionPrice.Value, Leg2CheckIV.Value, Leg2Include.Checked, InterestRate.Value, Dividends.Value)
leg3exp = New EquityOption(Leg3Type.Text, Leg3Contracts.Value, earlyexp, checkprice, Leg3StrikePrice.Value, Leg3ExpirationDate.Value, Leg3OptionPrice.Value, Leg3CheckIV.Value, Leg3Include.Checked, InterestRate.Value, Dividends.Value)
leg4exp = New EquityOption(Leg4Type.Text, Leg4Contracts.Value, earlyexp, checkprice, Leg4StrikePrice.Value, Leg4ExpirationDate.Value, Leg4OptionPrice.Value, Leg4CheckIV.Value, Leg4Include.Checked, InterestRate.Value, Dividends.Value)

stockleg1 = New EquityShares(StockLegType.Text, StockLegShares.Value, StockLegStockPrice.Value, StockLegInclude.Checked)
End Sub
Private Sub setupchart()
Chart1.Series(0).Enabled = False

With Chart1.ChartAreas("ChartArea1")
.AxisY.IsStartedFromZero = False
.AxisX.Title = "Stock Price"
.AxisY.Title = "Position Gain or Loss"
.AxisX.TitleFont = New Font("Arial", 12, FontStyle.Bold)
.AxisY.TitleFont = New Font("Arial", 12, FontStyle.Bold)
End With

Chart1.Series.Add("Entry Date")
With Chart1.Series("Entry Date")
.Color = Color.Blue
.ChartType = SeriesChartType.Spline
.BorderWidth = 2
End With

Chart1.Series.Add("Check Date")
With Chart1.Series("Check Date")
.Color = Color.Green
.ChartType = SeriesChartType.Spline
.BorderWidth = 2
End With

Chart1.Series.Add("Exp Date")
With Chart1.Series("Exp Date")
.Color = Color.Red
.ChartType = SeriesChartType.Line
.BorderWidth = 2
End With

Chart1.Series.Add("Minimum")
With Chart1.Series("Minimum")
.Color = Color.Purple
.ChartType = SeriesChartType.Line
.BorderDashStyle = ChartDashStyle.Dash
.BorderWidth = 1
End With

Chart1.Series.Add("Maximum")
With Chart1.Series("Maximum")
.Color = Color.Blue
.ChartType = SeriesChartType.Line
.BorderDashStyle = ChartDashStyle.Dash
.BorderWidth = 1
End With

Chart1.Series.Add("Breakeven1")
With Chart1.Series("Breakeven1")
.Color = Color.Brown
.ChartType = SeriesChartType.Line
.BorderDashStyle = ChartDashStyle.Dash
.BorderWidth = 2
End With

Chart1.Series.Add("Breakeven2")
With Chart1.Series("Breakeven2")
.Color = Color.Brown
.ChartType = SeriesChartType.Line
.BorderDashStyle = ChartDashStyle.Dash
.BorderWidth = 2
End With

End Sub
Private Sub makechart()
range = 10
startprice = checkprice - (range / 2)
startprice = Math.Round(startprice, 0)
If startprice < 0 Then startprice = 0
Dim entrytotal, checktotal, exptotal As Single

For Each cs As Series In Chart1.Series
cs.Points.Clear()
Next

Chart1.ChartAreas("ChartArea1").AxisX.Minimum = startprice
Chart1.ChartAreas("ChartArea1").AxisX.Interval = range / 20

Dim spi As Single
If range = 10 Then
spi = 0.01
Else
spi = 0.05
End If
For s As Single = startprice To startprice + range Step spi
entrytotal = leg1.vchart(s) + leg2.vchart(s) + leg3.vchart(s) + leg4.vchart(s) + stockleg1.vchart(s) - entrydrcr
Chart1.Series("Entry Date").Points.AddXY(s, entrytotal)

checktotal = leg1check.vchart(s) + leg2check.vchart(s) + leg3check.vchart(s) + leg4check.vchart(s) + stockleg1.vchart(s) - entrydrcr
Chart1.Series("Check Date").Points.AddXY(s, checktotal)

exptotal = leg1exp.vchart(s) + leg2exp.vchart(s) + leg3exp.vchart(s) + leg4exp.vchart(s) + stockleg1.vchart(s) - entrydrcr
Chart1.Series("Exp Date").Points.AddXY(s, exptotal)
Next

'find stock prices for breakevens
Dim be1, be2 As Single
Dim be1set, be2set As Boolean

For s As Single = startprice To startprice + range Step 0.01
If levelcross(s, 0) = True Then
be1 = s + 0.01
be1set = True
Exit For
End If
Next

For s As Single = startprice + range To startprice Step -0.01
If levelcross(s, 0) = True Then
be2 = s - 0.01
be2set = True
Exit For
End If
Next

If Math.Abs(be2 - be1) < 0.5 Then
be2set = False
End If

Dim minv As Single = Chart1.Series("Exp Date").Points.FindMinByValue().YValues(0)
Dim maxv As Single = Chart1.Series("Exp Date").Points.FindMaxByValue().YValues(0)

For s As Single = startprice To startprice + range Step 0.25
Chart1.Series("Minimum").Points.AddXY(Math.Round(s, 2), minv)
Chart1.Series("Maximum").Points.AddXY(Math.Round(s, 2), maxv)
Next

If be1set = True Then
Chart1.Series("Breakeven1").Points.AddXY(Math.Round(be1, 2), minv)
Chart1.Series("Breakeven1").Points.AddXY(Math.Round(be1, 2), maxv)
End If
If be2set = True Then
Chart1.Series("Breakeven2").Points.AddXY(Math.Round(be2, 2), minv)
Chart1.Series("Breakeven2").Points.AddXY(Math.Round(be2, 2), maxv)
End If

Chart1.Series("Entry Date").LegendText = "Entry Date: " & DateDiff(DateInterval.Day, lastentry.Date, earlyexp.Date) & " days left"
Chart1.Series("Check Date").LegendText = "Check Date: " & DateDiff(DateInterval.Day, checkdate.Date, earlyexp.Date) & " days left"
Chart1.Series("Exp Date").LegendText = "Expiration: 0 days left"
Chart1.Series("Minimum").LegendText = "Minimum = " & FormatCurrency(Math.Round(minv, 0))
Chart1.Series("Maximum").LegendText = "Maximum = " & FormatCurrency(Math.Round(maxv, 0))

If be1set = True Then
Chart1.Series("Breakeven1").IsVisibleInLegend = True
Chart1.Series("Breakeven1").LegendText = "Breakeven1 = " & FormatCurrency(be1)
Else
Chart1.Series("Breakeven1").IsVisibleInLegend = False
End If

If be2set = True Then
Chart1.Series("Breakeven2").IsVisibleInLegend = True
Chart1.Series("Breakeven2").LegendText = "Breakeven2 = " & FormatCurrency(be2)
Else
Chart1.Series("Breakeven2").IsVisibleInLegend = False
End If
End Sub
Private Function checkdates() As Boolean
Return leg1.expirationafterentry And leg2.expirationafterentry And leg3.expirationafterentry And leg4.expirationafterentry
End Function
Private Sub resetcheckdate()
Dim lastentry As Date = latestincludedentry(leg1.entdate, leg2.entdate, leg3.entdate, leg4.entdate)
Dim firstexp As Date = earliestincludedexpiration(leg1.expdate, leg2.expdate, leg3.expdate, leg4.expdate)
checkdate = middate(lastentry, firstexp)
End Sub
Function startupdate() As Date
Return changeexpiration(Now.Date, 2)
End Function
Private Sub setprices()
Leg1OptionPrice.Value = leg1.price
Leg2OptionPrice.Value = leg2.price
Leg3OptionPrice.Value = leg3.price
Leg4OptionPrice.Value = leg4.price
End Sub
Private Sub setvolatility()
Leg1IV.Value = leg1.volatility
Leg2IV.Value = leg2.volatility
Leg3IV.Value = leg3.volatility
Leg4IV.Value = leg4.volatility
End Sub
Private Sub setvalues()
Leg1Value.Text = leg1.totalvalue(Leg1OptionPrice.Value)
Leg1Value.BackColor = leg1.colorize
Leg2Value.Text = leg2.totalvalue(Leg2OptionPrice.Value)
Leg2Value.BackColor = leg2.colorize
Leg3Value.Text = leg3.totalvalue(Leg3OptionPrice.Value)
Leg3Value.BackColor = leg3.colorize
Leg4Value.Text = leg4.totalvalue(Leg4OptionPrice.Value)
Leg4Value.BackColor = leg4.colorize
StockLegValue.Text = stockleg1.totalvalue(StockLegStockPrice.Value)
StockLegValue.BackColor = stockleg1.colorize

entrydrcr = CSng(Leg1Value.Text) + CSng(Leg2Value.Text) + CSng(Leg3Value.Text) + CSng(Leg4Value.Text) + CSng(StockLegValue.Text)
EntryValue.Text = FormatCurrency(entrydrcr)
If entrydrcr > 0 Then
EntryValue.BackColor = Color.Pink
Else
EntryValue.BackColor = Color.LightGreen
End If
End Sub
Private Sub setzeros()
Leg1Value.Text = FormatCurrency(0)
Leg1Value.BackColor = Color.Gray
Leg2Value.Text = FormatCurrency(0)
Leg2Value.BackColor = Color.Gray
Leg3Value.Text = FormatCurrency(0)
Leg3Value.BackColor = Color.Gray
Leg4Value.Text = FormatCurrency(0)
Leg4Value.BackColor = Color.Gray
StockLegValue.Text = FormatCurrency(0)
StockLegValue.BackColor = Color.Gray
EntryValue.Text = FormatCurrency(0)
EntryValue.BackColor = Color.Gray
End Sub
Private Sub contractssame()
Dim cv As Integer = Leg1Contracts.Value
Leg2Contracts.Value = cv
Leg3Contracts.Value = cv
Leg4Contracts.Value = cv
StockLegShares.Value = cv * 100
End Sub
Private Sub entrysame()
Dim ev As Date = Leg1EntryDate.Value
Leg2EntryDate.Value = ev
Leg3EntryDate.Value = ev
Leg4EntryDate.Value = ev
End Sub
Private Sub stocksame()
Dim sv As Single = Leg1StockPrice.Value
Leg2StockPrice.Value = sv
Leg3StockPrice.Value = sv
Leg4StockPrice.Value = sv
StockLegStockPrice.Value = sv
End Sub
Private Sub strikesame()
Dim sv As Single = Leg1StrikePrice.Value
Leg2StrikePrice.Value = sv
Leg3StrikePrice.Value = sv
Leg4StrikePrice.Value = sv
End Sub
Private Sub expirationsame()
Dim ev As Date = Leg1ExpirationDate.Value
Leg2ExpirationDate.Value = ev
Leg3ExpirationDate.Value = ev
Leg4ExpirationDate.Value = ev
End Sub
Private Sub ivsame()
Dim sv As Single = Leg1IV.Value
Leg2IV.Value = sv
Leg3IV.Value = sv
Leg4IV.Value = sv
End Sub
Private Sub checkivsame()
Dim sv As Single = Leg1CheckIV.Value
Leg2CheckIV.Value = sv
Leg3CheckIV.Value = sv
Leg4CheckIV.Value = sv
End Sub
Private Sub B_CopyLeg1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_CopyLeg1.Click
contractssame()
entrysame()
stocksame()
strikesame()
expirationsame()
ivsame()
checkivsame()
End Sub
Private Sub Leg1IV_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Leg1IV.ValueChanged
Leg1CheckIV.Value = Leg1IV.Value
End Sub
Private Function levelcross(ByVal s As Single, ByVal level As Single) As Boolean
Dim s1 As Single = s + 0.01
Dim s2 As Single = s - 0.01
Dim test1 As Single = leg1exp.vchart(s1) + leg2exp.vchart(s1) + leg3exp.vchart(s1) + leg4exp.vchart(s1) + stockleg1.vchart(s1) - entrydrcr
Dim test2 As Single = leg1exp.vchart(s2) + leg2exp.vchart(s2) + leg3exp.vchart(s2) + leg4exp.vchart(s2) + stockleg1.vchart(s2) - entrydrcr

If test1 > level AndAlso test2 < level Then Return True
If test1 < level AndAlso test2 > level Then Return True

Return False
End Function
Private Sub Chart1_Customize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Chart1.Customize
For Each cl As CustomLabel In Chart1.ChartAreas(0).AxisY.CustomLabels
If CDbl(cl.Text) < 0 Then
cl.ForeColor = Color.Red
Else
cl.ForeColor = Color.Green
End If
Next
End Sub
Sub setcheckprice()
checkprice = averageincludedstrike(leg1.strikeprc, leg2.strikeprc, leg3.strikeprc, leg4.strikeprc, stockleg1.pchart)
End Sub
Sub setstartupposition()
Dim dt As Date = startupdate()
Leg1ExpirationDate.Value = dt
expirationsame()

Leg1Include.Checked = True
checkdate = middate(Now.Date, dt)

Leg1Type.SelectedIndex = 0
Leg1StockPrice.Value = 22
Leg1StrikePrice.Value = 22
Leg1IV.Value = 30
Leg1CheckIV.Value = 30

B_CopyLeg1.PerformClick()

Leg2Include.Checked = True
Leg2Type.SelectedIndex = 2
Leg2StrikePrice.Value = 24
End Sub
End Class

 

Save your Project.

 

Home Page Downloads
Previous Step: Add the EquityShares Class
Next Step: Test the Project

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