Adapted TS7 code

Discussion in 'Strategy Building' started by EliteThink, Mar 7, 2003.

  1. Greetings, I obtained the Options Complex code from TS7 which is supposed to indicate options prices. I'm having trouble getting it to work and thought others might be interested in working on it with me. Here is the code:

    It should be a simple matter of plugging in values but BjerkStensCall, near the end of the code, is not verifying. Any ideas?



    { Multiple-output function; see MULTIPLE-OUTPUT FUNCTIONS note below.

    THIS CONSOLIDATED OPTIONS FUNCTION INCLUDES THE GENERALIZED BLACK-SCHOLES MODEL FOR
    EUROPEAN/AMERICAN CALLS, EUROPEAN PUTS, AND THE GREEKS, PLUS THE BJERKSUND-STENSLAND
    MODEL FOR AMERICAN PUTS.

    THE FUNCTION INPUTS ARE AS FOLLOWS:

    MYASSETTYPE: Underlying Asset Type
    1 = NON-DIVIDEND-PAYING STOCK
    2 = DIVIDEND-PAYING STOCK
    3 = FUTURES
    4 = CURRENCIES
    DAYSLEFT: The number of calendar days left for the option. Can use the functions
    "DaysToExpiration" or "Next3rdFriday".
    STRIKEPR: Strike price for the option.
    ASSETPR: Underlying Asset Price.
    RATE100: Risk-free annual interest rate, in percent; can pass in a constant, or
    attach an appropriate index as Data 2 and pass in its price.
    YIELD100: In percent; used only if MYASSETTYPE = 2, else can pass in 0.
    FOREIGNRATE100: In percent; used only if MYASSETTYPE = 4, else can pass in 0.
    VOLTY100: Volatility of annual return, in percent, of underlying asset; can pass in a
    constant or use one of the volatility functions - ImpliedVolatility or
    VolatilityStdDev.
    PUTCALL: Set this to 2 for a put, 3 for a call (or use the keywords Put or Call ).
    EUROAMER01: Set this to 0 for European options, 1 for American. Note that this makes
    a difference in American put pricing only, all other computations remain the same.

    HEDGE PARAMETERS OR "GREEKS":
    DELTA = OPTION PRICE SENSITIVITY VS. ASSET PRICE
    GAMMA = DELTA SENSITIVITY VS. ASSET PRICE
    VEGA = OPTION PRICE SENSITIVITY VS. VOLATILITY
    THETA = OPTION PRICE SENSITIVITY VS. TIME
    RHO = OPTION PRICE SENSITIVITY VS. INTEREST RATE

    REFERENCE TEXTS:
    Black-Scholes and Beyond: Option Pricing Models, Neil A. Chriss, 1997
    Option Pricing Formulas, Espen Gaarder Haug, 1998
    }

    inputs:
    MyAssetType( numericsimple ),
    DaysLeft( numericsimple ),
    StrikePr( numericsimple ),
    AssetPr( numericsimple ),
    Rate100( numericsimple ),
    Yield100( numericsimple ),
    ForeignRate100( numericsimple ),
    Volty100( numericsimple ),
    PutCall( numericsimple ),
    EuroAmer01( numericsimple ),
    oOptPrice( numericref ),
    oDelta( numericref ),
    oGamma( numericref ),
    oVega( numericref ),
    oTheta( numericref ),
    oRho( numericref ) ;

    variables:
    Rate( 0 ),
    Volty( 0 ),
    Yield( 0 ),
    ForeignRate( 0 ),
    Carry( 0 ),
    YearsLeft( 0 ),
    YearsLeftRoot( 0 ),
    Temp1( 0 ),
    Temp2( 0 ),
    Temp3( 0 ),
    d1( 0 ),
    Z1( 0 ),
    N1( 0 ),
    d2( 0 ),
    N2( 0 ) ;

    { Note: d1, Z1, N1, d2, and N2 are the standard names used for these quantities }

    if ( MyAssetType <> 1 and MyAssetType <> 2 and MyAssetType <> 3 and MyAssetType <> 4 )
    or DaysLeft <= 0 or StrikePr <= 0 or AssetPr <= 0 or Volty100 <= 0
    or ( PutCall <> 2 and PutCall <> 3 )
    then
    begin
    oOptPrice = 0 ;
    oDelta = 0 ;
    oGamma = 0 ;
    oVega = 0 ;
    oTheta = 0 ;
    oRho = 0 ;
    OptionsComplex = -1 ;
    end
    else
    begin
    Rate = Rate100 * .01 ;
    Yield = Yield100 * .01 ;
    ForeignRate = ForeignRate100 * .01 ;

    if MyAssetType = 1 {NON-DIVIDEND-PAYING STOCK} then
    Carry = Rate
    else if MyAssetType = 2 {DIVIDEND-PAYING STOCK} then
    Carry = Rate - Yield
    else if MyAssetType = 3 {FUTURES} then
    Carry = 0
    else if MyAssetType = 4 {CURRENCIES} then
    Carry = Rate - ForeignRate ;

    Volty = Volty100 * .01 ;
    YearsLeft = DaysLeft * .002739726027 { = 1 / 365 } ;
    YearsLeftRoot = SquareRoot( YearsLeft ) ;
    Temp1 = Volty * YearsLeftRoot ;
    Temp2 = StrikePr * ExpValue( -Rate * YearsLeft ) ;
    Temp3 = ExpValue( ( Carry - Rate ) * YearsLeft ) ;
    d1 = ( Log( AssetPr / StrikePr ) + ( Carry + Power( Volty, 2 ) / 2 ) * YearsLeft )
    / Temp1 ;
    Z1 = ExpValue( -Square( d1 ) * .5 ) * .398942280407 { = 1 / SqRt2Pi } ;
    N1 = NormSCDensity( d1 ) ;
    d2 = d1 - Temp1 ;
    N2 = NormSCDensity( d2 ) ;

    if PutCall = 3 then
    begin
    oOptPrice = N1 * AssetPr * Temp3 - N2 * Temp2 ;
    oDelta = N1 * Temp3 ;
    oTheta = ( -Z1 * AssetPr * Volty * Temp3 / ( 2 * YearsLeftRoot ) - N1
    * ( Carry - Rate ) * AssetPr * Temp3 - N2 * Rate * Temp2 )
    * .002739726027 { = 1 / 365 } ;
    oRho = N2 * YearsLeft * Temp2 * .01 ;
    end
    else if PutCall = 2 then
    begin
    if EuroAmer01 = 0 then
    oOptPrice = ( N1 - 1 ) * AssetPr * Temp3 + ( 1 - N2 ) * Temp2
    else if EuroAmer01 = 1 then
    oOptPrice = BjerkStensCall( StrikePr, AssetPr, YearsLeft, Rate - Carry,
    -Carry, Volty ) ;
    oDelta = ( N1 - 1 ) * Temp3 ;
    oTheta = ( -Z1 * AssetPr * Volty * Temp3 / ( 2 * YearsLeftRoot ) + ( 1 - N1 )
    * ( Carry - Rate ) * AssetPr * Temp3 + ( 1 - N2 ) * Rate * Temp2 )
    * .002739726027 { = 1 / 365 } ;
    oRho = ( N2 - 1 ) * YearsLeft * Temp2 * .01 ;
    end ;

    oGamma = ( Z1 * Temp3 ) / ( AssetPr * Temp1 ) ;
    oVega = Z1 * AssetPr * YearsLeftRoot * Temp3 * .01 ;

    OptionsComplex = 1 ;
    end ;

    {
    MULTIPLE-OUTPUT FUNCTIONS

    A multiple-output function has two types of parameters or "inputs" - input parameters
    and input/output parameters. The values of the input parameters are passed into the
    multiple-output function, but not modified by the function. The values of the input/
    output parameters are passed into the multiple-output function, modified by it, and
    the modified values are then inherited by - or output to - the calling routine.

    The input/output parameters are often used for output purposes only, i.e., the
    incoming values are ignored. The outputs are in addition to the function return. In
    multiple-output functions, the function return is generally used to return an error
    code, though sometimes the return may simply be a dummy value.

    The input/output parameters are declared with a "ref" suffix (such as "numericref") in
    the multiple-output function's declaration statements. For further clarity, the names
    of the input/output parameters are generally prefixed with an "o" in the function as
    well as in all the routines that call the function.

    The built-in single-return WRAPPER FUNCTIONS that call the multiple-output functions
    are specialized calling routines designed to offer simplified, alternate pathways to
    the functionality of the underlying multiple-output functions. In the wrapper
    functions, the input/output parameters are declared as local variables and generally
    initialized to zero. They are passed through to the multiple-output function without
    further modification. After the call, the wrapper function picks out the single
    output of interest and assigns it as the return of the wrapper function.
    }

    { ** Copyright (c) 1991-2003 TradeStation Technologies, Inc. All rights reserved. **
    ** TradeStation reserves the right to modify or overwrite this analysis technique
    with each release. ** }