Hi,
I'm new to libreoffice dev and I'm working on implementing some
additional financial (option pricing) functions for calc. So far I've
used
./core/scaddins/source/analysis/
locally to add my functions but I've been told scaddins is mainly for
ms excel functions and ./core/sc/ is certainly not the right place. So
I'd need some advice where to place the new functions.
Also, any opinions on naming and parameter inputs are welcome.
The functions in question return prices and greeks (partial
derivatives of the price to certain input values) of some basic
financial options, all using the classic Black-Scholes model.
OPT_BS(...): option price of a put/call option
OPT_DIGITAL(...): option price of a digital/binary option
OPT_BARRIER(...): barrier options (=put/call with barriers)
OPT_TOUCH(...): touch/no-touch options (=digitals with barriers)
...
I've modelled the names on what is implemented in Gnumeric which has a
range of opt_foo(...) functions.
As for parameters they are mainly of double type but some are of enum
type. The enums could be mapped to integers but I've a slight
preference for STRING inputs but not sure how that is viewed here with
regard to localisation? E.g. I'm currently using "p" to specify a put
and "c" for a call, and "i" for in, "o" for out, and "delta" for delta
(d/dS), "gamma" for gamma (d^2/dS^2), etc.
I've attached a code snipped of OPT_BARRIER(...) interface
implementation.
Cheers, Tino
#include "black_scholes.hxx"
using namespace sca::analysis;
bool opt_getinput_putcall(bs::types::PutCall& pc, const STRING& str) {
if(str.compareToAscii("c",1)==0) {
pc=bs::types::Call;
} else if(str.compareToAscii("p",1)==0) {
pc=bs::types::Put;
} else {
return false;
}
return true;
}
bool opt_getinput_inout(bs::types::BarrierKIO& kio, const STRING& str) {
if(str.compareToAscii("i",1)==0) {
kio=bs::types::KnockIn;
} else if(str.compareToAscii("o",1)==0) {
kio=bs::types::KnockOut;
} else {
return false;
}
return true;
}
bool opt_getinput_barrier(bs::types::BarrierActive& cont, const STRING& str) {
if(str.compareToAscii("c",1)==0) {
cont=bs::types::Continuous;
} else if(str.compareToAscii("e",1)==0) {
cont=bs::types::Maturity;
} else {
return false;
}
return true;
}
bool opt_getinput_fordom(bs::types::ForDom& fd, const STRING& str) {
if(str.compareToAscii("f",1)==0) {
fd=bs::types::Foreign;
} else if(str.compareToAscii("d",1)==0) {
fd=bs::types::Domestic;
} else {
return false;
}
return true;
}
bool opt_getinput_greek(bs::types::Greeks& greek, const ANY& anyval) {
STRING str;
if(anyval.getValueTypeClass() == ::com::sun::star::uno::TypeClass_STRING) {
anyval >>= str;
} else if(anyval.getValueTypeClass() == ::com::sun::star::uno::TypeClass_VOID) {
str="value";
} else {
return false;
}
if(str.compareToAscii("value")==0 || str.compareToAscii("price")==0 ) {
greek=bs::types::Value;
} else if(str.compareToAscii("delta")==0) {
greek=bs::types::Delta;
} else if(str.compareToAscii("gamma")==0) {
greek=bs::types::Gamma;
} else if(str.compareToAscii("theta")==0) {
greek=bs::types::Theta;
} else if(str.compareToAscii("vega")==0) {
greek=bs::types::Vega;
} else if(str.compareToAscii("volga")==0) {
greek=bs::types::Volga;
} else if(str.compareToAscii("vanna")==0) {
greek=bs::types::Vanna;
} else if(str.compareToAscii("rho")==0) {
greek=bs::types::Rho_d;
} else if(str.compareToAscii("rhof")==0) {
greek=bs::types::Rho_f;
} else {
return false;
}
return true;
}
double SAL_CALL AnalysisAddIn::getOpt_barrier( double spot, double vol,
double r, double rf, double T, double strike,
double barrier_low, double barrier_up, double rebate,
const STRING& put_call, const STRING& in_out,
const STRING& barriercont, const ANY& greekstr ) THROWDEF_RTE_IAE
{
bs::types::PutCall pc;
bs::types::BarrierKIO kio;
bs::types::BarrierActive bcont;
bs::types::Greeks greek;
// read and check input values
if( spot<=0.0 || vol<=0.0 || T<0.0 || strike<0.0 ||
!opt_getinput_putcall(pc,put_call) ||
!opt_getinput_inout(kio,in_out) ||
!opt_getinput_barrier(bcont,barriercont) ||
!opt_getinput_greek(greek,greekstr) ){
THROW_IAE;
}
double fRet=bs::barrier(spot,vol,r,rf,T,strike, barrier_low,barrier_up,
rebate,pc,kio,bcont,greek);
RETURN_FINITE( fRet );
}
Context
- opinions on additional calc functions · tino
Privacy Policy |
Impressum (Legal Info) |
Copyright information: Unless otherwise specified, all text and images
on this website are licensed under the
Creative Commons Attribution-Share Alike 3.0 License.
This does not include the source code of LibreOffice, which is
licensed under the Mozilla Public License (
MPLv2).
"LibreOffice" and "The Document Foundation" are
registered trademarks of their corresponding registered owners or are
in actual use as trademarks in one or more countries. Their respective
logos and icons are also subject to international copyright laws. Use
thereof is explained in our
trademark policy.