Black & Scholes

Nyptop

Active Member
Licensed User
Longtime User
I have this code (bare in mind that S= Stock price, X=Strike price, T=Years to maturity, r= Risk-free rate, v=Volatility):

Sub button5_click As Button
'// The Black and Scholes (1973) Stock option formula

S = edittext1.Text
X = edittext2.Text
T = edittext4.Text
r = edittext3.Text
v = edittext5.Text


d1 = (Logarithm(10,(S / X)) + (r + v ^ 2 / 2) * T) / (v * Sqrt(T))
d2 = d1 - v * Sqrt(T)
BlackScholes = S * CND(d1) - X * Exp(-r * T) * CND(d2)


'// The cumulative normal distribution function



Const a1 = 0.31938153: Const a2 = -0.356563782: Const a3 = 1.781477937:
Const a4 = -1.821255978: Const a5 = 1.330274429

L = Abs(X)
K = 1 / (1 + 0.2316419 * L)
CND = 1 - 1 / Sqrt(2 * 3.14159) * Exp(-L ^ 2 / 2) * (a1 * K + a2 * K ^ 2 + a3 * K ^ 3 + a4 * K ^ 4 + a5 * K ^ 5)

If X < 0 Then
CND = 1 - CND
End If
End Sub




Is the keyword 'power' a natural logarithm. Is there anything else I'm doing wrong?
 

magalt

Member
Licensed User
Longtime User
Hi Nyptop.
If the keyword 'Logarithm' is a 'natural logarithm' then you try to change

d1 = (Logarithm(10,(S / X)) + (r + v ^ 2 / 2) * T) / (v * Sqrt(T))

with

d1 = (power(ce,(S / X)) + (r + v ^ 2 / 2) * T) / (v * Sqrt(T))

Marco
 
Upvote 0

Nyptop

Active Member
Licensed User
Longtime User
thanks, but I'm getting the error 'input string was not in correct format'. How do I go about sorting this?
 
Upvote 0

Nyptop

Active Member
Licensed User
Longtime User
Also, I want to raise the natural number to a power. is there a function specifically suited to this? If not I would still like to know how to do powers so as that I can just add the natural no. in manually. thanks again,
Nyptop
 
Upvote 0

magalt

Member
Licensed User
Longtime User
I'm sorry. Last formula was wrong. Try it:

d1 = (Logarithm(S / X, cE) + (r + Power(v, 2) * T)) / (v * Sqrt(T))

Marco
 
Upvote 0

Nyptop

Active Member
Licensed User
Longtime User
My line that used to look like:

CND = 1 - 1 / Sqrt(2 * 3.14159) * Exp(-L ^ 2 / 2) * (a1 * K + a2 * K ^ 2 + a3 * K ^ 3 + a4 * K ^ 4 + a5 * K ^ 5)

Now is:

CND = 1 - 1 / Sqrt(2 * 3.14159) * Power(cE,(Power(-L , 2) / 2)) * (a1 * K + a2 * Power(K , 2) + a3 * Power(K , 3) + a4 * Power(K , 4) + a5 * Power(K , 5)

However I now have the error 'Invalid number of parentheses'. Any suggestions?
 
Upvote 0

Nyptop

Active Member
Licensed User
Longtime User
Thanks Again. However, when I test it out on the emulator I have a different problem. I calculate the value and all I get most of the time is either 'infinity' or 'nan'. I have attached the file if anyone would like to take a look.
 

Attachments

  • OptionCalculator.zip
    11.6 KB · Views: 220
Upvote 0

klaus

Expert
Licensed User
Longtime User
The 'infinity' comes from here:
CND = 1 - 1 / Sqrt(2 * 3.14159) * Power(cE,(Power(-L , 2) / 2)) * (a1 * K + a2 * Power(K , 2) + a3 * Power(K , 3) + a4 * Power(K , 4) + a5 * Power(K , 5))
must be:
CND = 1 - 1 / Sqrt(2 * 3.14159) * Power(cE,(-Power(L , 2) / 2)) * (a1 * K + a2 * Power(K , 2) + a3 * Power(K , 3) + a4 * Power(K , 4) + a5 * Power(K , 5))
The '-' sign is at the wrong place.

The NaN can come from here:
BlackScholes = S * CND*(d1) - X * Power(cE, -r * T) * CND*(v * Sqrt(T-d1))
if d1 > T then Sqrt of a negative number raises the NaN error.

Best regards.
 
Upvote 0

magalt

Member
Licensed User
Longtime User
Thanks Again. However, when I test it out on the emulator I have a different problem. I calculate the value and all I get most of the time is either 'infinity' or 'nan'. I have attached the file if anyone would like to take a look.

Hi Nyptop.
Could you provide me the sample data and expected results?

S= Stock price = ???
X=Strike price = ???
T=Years to maturity = ???
r= Risk-free rate = ???
v=Volatility = ???

Call value = !!!
Put value = !!!

Marco
 
Upvote 0

Nyptop

Active Member
Licensed User
Longtime User
There is a serious problem with answers not being as they should. Here is the code in Visual Basic if you think it would be of any help:

'// The Black and Scholes (1973) Stock option formula
Public Function BlackScholes(CallPutFlag As String, S As Double, X _
As Double, T As Double, r As Double, v As Double) As Double

Dim d1 As Double, d2 As Double

d1 = (Log(S / X) + (r + v ^ 2 / 2) * T) / (v * Sqr(T))
d2 = d1 - v * Sqr(T)
If CallPutFlag = "c" Then
BlackScholes = S * CND(d1) - X * Exp(-r * T) * CND(d2)
ElseIf CallPutFlag = "p" Then
BlackScholes = X * Exp(-r * T) * CND(-d2) - S * CND(-d1)
End If
End Function

'// The cumulative normal distribution function
Public Function CND(X As Double) As Double

Dim L As Double, K As Double
Const a1 = 0.31938153: Const a2 = -0.356563782: Const a3 = 1.781477937:
Const a4 = -1.821255978: Const a5 = 1.330274429

L = Abs(X)
K = 1 / (1 + 0.2316419 * L)
CND = 1 - 1 / Sqr(2 * Application.Pi()) * Exp(-L ^ 2 / 2) * (a1 * K + a2 * K ^ 2 + a3 * K ^ 3 + a4 * K ^ 4 + a5 * K ^ 5)

If X < 0 Then
CND = 1 - CND
End If
End Function
 
Upvote 0

magalt

Member
Licensed User
Longtime User


The problem was on the function call.
Try this code.

Marco
(I'm sorry for my bad English)
 
Upvote 0

Nyptop

Active Member
Licensed User
Longtime User
I have read the summary of scroll views by klaus and was wondering what is the best scrollview to use for this particular app?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
. . . wondering what is the best scrollview to use for this particular app?
There are no different ScrollViews nor is there a best ScrollView. The principle, in all examples, consits in filling a ScrollView with different views dending on what should be displayed.
For your application, it depends on what you want to display.

If it is for the input screen, this example ScrollView example with a Panel higher than the screen is the closest to your requirement.

Having a look at your program, I have following suggestions:
- Using a same panel in the same activity, or one other activity, for both calculations instead of two, the data are the same.
- Why not give both results at the same time, same data and most of the equations are the same ?

Best regards.
 
Upvote 0

Nyptop

Active Member
Licensed User
Longtime User
Thanks, Will do Klaus. One problem still. the program pauses when I load the the scroll view layout. I think I'm doing pretty much everything right? What might a problem be?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…