Jump to content

Black-Scholes promotion for Autoit


Recommended Posts

I need help and some input please. Im an accountant and for the past few years new rules have require us to calculate a notional expense value for stock options granted by public companies to staff and others.

The accepted method to calculate these values is the Black-Scholes method (heavy duty mathematical formula). I now need to do these calculations and need a Black-Scholes calculator. You can buy them on the web, some sites have pay as you go and some are free but they wont give away the formulas. Then I found a website of a professor and author who specializes in option pricing http://www.answers.com/topic/espen-haug and he is giving away the code for a basic BScalculator (meets accountants needs) and he provides an example with an excel download. The site also provides the code in about 30 programming languages none of which are Autoit. So as defender of the faith I undertook (to myself) what I thought was a piece of cake converting from excel to Autoit.

Some languages have and some dont have a function that excel has called normsdist().

Autoit does not have this statistical function but I did eventually work it out following the C++ math example. http://www.espenhaug.com/black_scholes.html

He asks that you use his variable names and I did my best to stick to his format and I tried to take the opportunity to subtly show off Autoit

Im going to improve this code with proper names and condense it a bit and stick on a Gui and give it away to accountants. I will post it on the appropriate forum when complete (just the kind of program everyone wants). At first he was reluctant to post the Autoit script because he was afraid that Autoit might be mathematical inconsistent (and without logic). I explained Autoit was written in C++ and gave him the link and told him it is an incredible scripting language and he has to post it. I assured him and promised him that my script works and its fast and I will send him a compiled Gui next week. He just emailed me thanks I will be happy to put it on my webpage - can you write a few more lines about this language (that I can put on page)?

Finally heres my question Im not the guy to write this stuff - so can someone who articulates well, please look at what the others have written about their respective languages and put together a two line promotion for Autoit , without overdoing it. Pretty please help someone in the marketing department, if any.

BTW here is the code he will be posting (I sent it in text and a pdf picture of the Scite code.). I know how to tighten up some formula but I left it to look like the others. All suggestions welcome but more important is the little blurb from the marketing department please. Thanks.

;BlackScholes stock option function
Func _BlackScholes()
    Local $CallPutFlag, $S, $w, $T, $r, $v,$d1 = (Log($S / $w) + ($r + $v ^ 2 / 2) * $T) / ($v * Sqrt($T)), $d2 = $d1 - $v * Sqrt($T)
    If $CallPutFlag = "c" Then
        Local $callval = ($S * _CND($d1) - $w * Exp(-$r * $T) * _CND($d2))
    ElseIf $CallPutFlag = "p" Then
        Local $putval = ($w * Exp(-$r * $T) * _CND(-$d2) - $S * _CND(-$d1))
    EndIf
EndFunc   ;==>_BlackScholes

;The cumulative normal distribution function
Func _CND($w)
    Const $a1 = 0.31938153, $a2 = -0.356563782, $a3 = 1.781477937, $a4 = -1.821255978, $a5 = 1.330274429, $Pi = 3.14159265
    $w1 = $w
    $L = Abs($w)
    $K = 1 / (1 + 0.2316419 * $L)
    $w = 1 - 1 / Sqrt(2 * $Pi) * Exp(-$L * $L / 2) * ($a1 * $K + $a2 * $K ^ 2 + $a3 * $K ^ 3 + $a4 * $K ^ 4 + $a5 * $K ^ 5)

    If $w1 < 0 Then
        $w = 1 - $w
    EndIf
    Return $w
EndFunc   ;==>_CND
Link to comment
Share on other sites

Ummm... how do you pass data to this _BlackScholes() function? You are declaring a bunch of Local variables, and I don't see any reference to any Global external variables, so where does the inputs to the function come from?

:whistle:

Edit: Typos.

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

AutoIt is a freeware Windows automation language which is particularly adept at manipulation of GUI windows and controls. AutoIt scripts can be 'compiled' with a run-time interpreter that allows them to run on most Windows platforms without requiring software installation.

Written and actively maintained by Jonathan Bennett and a group of volunteer developers, AutoIt is continually extended with “User Defined Functions” or UDFs written by the thousands of active members of its user forums. For more information, visit the AutoIt web sit at: http://www.autoitscript.com.

Visit these user forums and you will soon realize that the best looking member of those forums by far, if not one of the particularly smart ones, is a rakishly handsome flightless Antarctic water fowl...

I didn't have time to fill out the third paragraph. I'll let you do that...

:whistle:

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Ummm... how do pass data to this _BlackScholes() function? You are declaring a bunch of Local variables, and I don't see any reference to any Global external variables, so where does the input to the function come from?

:whistle:

Hi PsaltyDS go to http://www.espenhaug.com/black_scholes.html

The variables are listed at the top. Here is a working copy. See result is the same as excel (you need to download to see.) Or try C++ I believe youll get same result.

Please help with the blurb - don't get hung up on the script it works - after hours of battling. A nice two line promotion please.

HotKeySet("^`", "_BlackScholes")

While 1
    Sleep(100)
WEnd

;BlackScholes function
Func _BlackScholes()
    Local $CallPutFlag = "c", $S = 60, $w = 65, $T = .25, $r = .08, $v = .3, $d1 = (Log($S / $w) + ($r + $v ^ 2 / 2) * $T) / ($v * Sqrt($T)), $d2 = $d1 - $v * Sqrt($T)
    If $CallPutFlag = "c" Then
        Local $callval = ($S * _CND($d1) - $w * Exp(-$r * $T) * _CND($d2))
        MsgBox(0, "Call", $callval)
    ElseIf $CallPutFlag = "p" Then
        Local $putval = ($w * Exp(-$r * $T) * _CND(-$d2) - $S * _CND(-$d1))
        MsgBox(0, "Put", $putval)
    EndIf
    Exit
EndFunc   ;==>_BlackScholes

;The cumulative normal distribution function
Func _CND($w)
    $w1 = $w
    ;Local $L, $K
    Const $a1 = 0.31938153, $a2 = -0.356563782, $a3 = 1.781477937, $a4 = -1.821255978, $a5 = 1.330274429, $Pi = 3.14159265
    $L = Abs($w)
    $K = 1 / (1 + 0.2316419 * $L)
    $w = 1 - 1 / Sqrt(2 * $Pi) * Exp(-$L * $L / 2) * ($a1 * $K + $a2 * $K ^ 2 + $a3 * $K ^ 3 + $a4 * $K ^ 4 + $a5 * $K ^ 5)

    If $w1 < 0 Then
        $w = 1 - $w
    EndIf
    Return $w
EndFunc   ;==>_CND
Link to comment
Share on other sites

the best looking member of those forums by far, if not one of the particularly smart ones, is a rakishly handsome flightless Antarctic water fowl...

Why thank you for the compliments :whistle::lmao:

Link to comment
Share on other sites

Follow the link on the first post of This Topic.

This guy pretty much says it all.


Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Link to comment
Share on other sites

I see that you had hard coded the values in the function.

Here is a very simple gui to get you started on.

There's no error checking though.

I was bored! :whistle:

#include <GuiConstants.au3>

GuiCreate("MyGUI", 392, 316,-1, -1 , BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))

$S_I = GuiCtrlCreateInput("", 120, 20, 120, 20)
GuiCtrlCreateLabel("Stock Price", 20, 20, 100, 20)
$W_I = GuiCtrlCreateInput("", 120, 50, 120, 20)
GuiCtrlCreateLabel("Strike Price", 20, 50, 100, 20)
$T_I = GuiCtrlCreateInput("", 120, 80, 120, 20)
GuiCtrlCreateLabel("Years to Maturity", 20, 80, 100, 20)
$R_I = GuiCtrlCreateInput("", 120, 110, 120, 20)
GuiCtrlCreateLabel("Risk Free Rate %", 20, 110, 100, 20)
$V_I = GuiCtrlCreateInput("", 120, 140, 120, 20)
GuiCtrlCreateLabel("Volatility %", 20, 140, 100, 20)

$callRadio = GuiCtrlCreateRadio("Call",170,180,100,20)
GuiCtrlSetState(-1,$Gui_Checked)
$putRadio = GuiCtrlCreateRadio("Put",170,200,100,20)

$calc = GuiCtrlCreateButton("Calculate",20,180,70,30)
$clear = GuiCtrlCreateButton("Clear",20,230,70,30)

$result = GUICtrlCreateInput("",20,280,316,20)
GuiSetState()
While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case $Msg = $clear
        GUiCtrlSetData($S_I,"")
        GUiCtrlSetData($W_I,"")
        GUiCtrlSetData($T_I,"")
        GUiCtrlSetData($R_I,"")
        GUiCtrlSetData($V_I,"")
        GUiCtrlSetData($Result,"")
    Case $msg = $calc
        If BitAnd(GuiCtrlRead($callRadio),$Gui_Checked) then 
            $ret = _BlackScholes( Number(GuiCtrlRead($S_I)),Number(GuiCtrlRead($W_I)),Number(GuiCtrlRead($T_I)),Number(GuiCtrlRead($R_I)),Number(GuiCtrlRead($V_I)),"c" )
        Else
            $ret = _BlackScholes( Number(GuiCtrlRead($S_I)),Number(GuiCtrlRead($W_I)),Number(GuiCtrlRead($T_I)),Number(GuiCtrlRead($R_I)),Number(GuiCtrlRead($V_I)),"p" )
        EndIf
        
        GuiCtrlSetData($Result,$ret)
    Case Else
    ;;;
    EndSelect
WEnd
Exit

;Original hard coded values $S = 60, $w = 65, $T = .25, $r = .08, $v = .3,

;BlackScholes function
Func _BlackScholes($S,$W,$T,$R,$V,$CallPutFlag)
    
    $R = $R /100
    $V = $V /100
    
    Local $d1 = (Log($S / $w) + ($r + $v ^ 2 / 2) * $T) / ($v * Sqrt($T)), $d2 = $d1 - $v * Sqrt($T)
    If $CallPutFlag = "c" Then
        Local $callval = ($S * _CND($d1) - $w * Exp(-$r * $T) * _CND($d2))
       Return $callval
    ElseIf $CallPutFlag = "p" Then
        Local $putval = ($w * Exp(-$r * $T) * _CND(-$d2) - $S * _CND(-$d1))
        Return $putval
    EndIf
   ;Exit
EndFunc  ;==>_BlackScholes

;The cumulative normal distribution function
Func _CND($w)
    $w1 = $w
   ;Local $L, $K
    Const $a1 = 0.31938153, $a2 = -0.356563782, $a3 = 1.781477937, $a4 = -1.821255978, $a5 = 1.330274429, $Pi = 3.14159265
    $L = Abs($w)
    $K = 1 / (1 + 0.2316419 * $L)
    $w = 1 - 1 / Sqrt(2 * $Pi) * Exp(-$L * $L / 2) * ($a1 * $K + $a2 * $K ^ 2 + $a3 * $K ^ 3 + $a4 * $K ^ 4 + $a5 * $K ^ 5)

    If $w1 < 0 Then
        $w = 1 - $w
    EndIf
    Return $w
EndFunc  ;==>_CND
Link to comment
Share on other sites

I see that you had hard coded the values in the function.

Here is a very simple gui to get you started on.

There's no error checking though.

I was bored! :whistle:

@BigDod - thanks for that, I sent it on for him to read -great article.

@ChrisL

Thanks for this great starter Gui. Its so simple when someone shows you how.

I already started it yesterday with Koda and want to see where that takes me, learning as I go, when I get time.

I'm going to use your radio button idea instead of a combo box (as shown on their Java Applet).

One click instead of two wins every time.

I will also be substituting with accounting labels for easier understanding.

I compiled your script, as is, and sent it to him explaining it’s a quick and dirty draft from a bored Autoit member and I promised a fancier one coming soon.

This is a basic BlackScholes calculator acceptable for accounting valuations.

It does come with a list of assumptions including that no dividends will be paid during the option period.

Each new attribute introduced obviously affects the option value and the math and the function and the Gui.

Okay thanks again everyone. Glad I am able to give Autoit a plug and a cumulative normal distribution function - equivalent to excel normsdist() or CND in other languages.

July 23, 2007 edit - Okay he has it up and running and we're listed number one and appended at the bottom.

http://www.espenhaug.com/black_scholes.html

Edited by 1905russell
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...