1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 // pricing functions add in
22 // all of the UNO add-in technical details have been copied from
23 // ../datefunc/datefunc.cxx
25 #include "pricing.hxx"
26 #include "black_scholes.hxx"
27 #include <pricing.hrc>
28 #include <strings.hrc>
30 #include <cppuhelper/factory.hxx>
31 #include <cppuhelper/supportsservice.hxx>
32 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
35 #include <rtl/math.hxx>
36 #include <unotools/resmgr.hxx>
37 #include <i18nlangtag/languagetag.hxx>
39 using namespace ::com::sun::star
;
40 using namespace sca::pricing
;
43 #define ADDIN_SERVICE "com.sun.star.sheet.AddIn"
44 #define MY_SERVICE "com.sun.star.sheet.addin.PricingFunctions"
45 #define MY_IMPLNAME "com.sun.star.sheet.addin.PricingFunctionsImpl"
47 #define UNIQUE false // function name does not exist in Calc
49 #define STDPAR false // all parameters are described
51 #define FUNCDATA( FuncName, CompName, ParamCount, Category, Double, IntPar ) \
52 { "get" #FuncName, PRICING_FUNCNAME_##FuncName, PRICING_FUNCDESC_##FuncName, CompName, ParamCount, Category, Double, IntPar }
54 const ScaFuncDataBase pFuncDataArr
[] =
56 FUNCDATA(OptBarrier
, "OPT_BARRIER", 13, ScaCategory::Finance
, UNIQUE
, STDPAR
),
57 FUNCDATA(OptTouch
, "OPT_TOUCH", 11, ScaCategory::Finance
, UNIQUE
, STDPAR
),
58 FUNCDATA(OptProbHit
, "OPT_PROB_HIT", 6, ScaCategory::Finance
, UNIQUE
, STDPAR
),
59 FUNCDATA(OptProbInMoney
, "OPT_PROB_INMONEY", 8, ScaCategory::Finance
, UNIQUE
, STDPAR
)
64 ScaFuncData::ScaFuncData( const ScaFuncDataBase
& rBaseData
) :
65 aIntName( OUString::createFromAscii( rBaseData
.pIntName
) ),
66 pUINameID( rBaseData
.pUINameID
),
67 pDescrID( rBaseData
.pDescrID
),
68 nParamCount( rBaseData
.nParamCount
),
69 eCat( rBaseData
.eCat
),
70 bDouble( rBaseData
.bDouble
),
71 bWithOpt( rBaseData
.bWithOpt
)
73 aCompList
.push_back(OUString::createFromAscii(rBaseData
.pCompName
));
76 ScaFuncData::~ScaFuncData()
80 sal_uInt16
ScaFuncData::GetStrIndex( sal_uInt16 nParam
) const
84 return (nParam
> nParamCount
) ? (nParamCount
* 2) : (nParam
* 2);
87 void sca::pricing::InitScaFuncDataList(ScaFuncDataList
& rList
)
89 for (const auto & nIndex
: pFuncDataArr
)
90 rList
.push_back(ScaFuncData(nIndex
));
93 // entry points for service registration / instantiation
94 static uno::Reference
< uno::XInterface
> ScaPricingAddIn_CreateInstance(
95 const uno::Reference
< lang::XMultiServiceFactory
>& )
97 return static_cast<cppu::OWeakObject
*>(new ScaPricingAddIn());
102 SAL_DLLPUBLIC_EXPORT
void * pricing_component_getFactory(
103 const sal_Char
* pImplName
, void * pServiceManager
, void * /*pRegistryKey*/ )
105 void* pRet
= nullptr;
107 if ( pServiceManager
&&
108 OUString::createFromAscii( pImplName
) == ScaPricingAddIn::getImplementationName_Static() )
110 uno::Reference
< lang::XSingleServiceFactory
> xFactory( cppu::createOneInstanceFactory(
111 static_cast< lang::XMultiServiceFactory
* >( pServiceManager
),
112 ScaPricingAddIn::getImplementationName_Static(),
113 ScaPricingAddIn_CreateInstance
,
114 ScaPricingAddIn::getSupportedServiceNames_Static() ) );
119 pRet
= xFactory
.get();
128 // "normal" service implementation
129 ScaPricingAddIn::ScaPricingAddIn()
133 ScaPricingAddIn::~ScaPricingAddIn()
137 static const sal_Char
* pLang
[] = { "de", "en" };
138 static const sal_Char
* pCoun
[] = { "DE", "US" };
139 static const sal_uInt32 nNumOfLoc
= SAL_N_ELEMENTS( pLang
);
141 void ScaPricingAddIn::InitDefLocales()
143 pDefLocales
.reset( new lang::Locale
[ nNumOfLoc
] );
145 for( sal_uInt32 nIndex
= 0; nIndex
< nNumOfLoc
; nIndex
++ )
147 pDefLocales
[ nIndex
].Language
= OUString::createFromAscii( pLang
[ nIndex
] );
148 pDefLocales
[ nIndex
].Country
= OUString::createFromAscii( pCoun
[ nIndex
] );
152 const lang::Locale
& ScaPricingAddIn::GetLocale( sal_uInt32 nIndex
)
157 return (nIndex
< sizeof( pLang
)) ? pDefLocales
[ nIndex
] : aFuncLoc
;
160 void ScaPricingAddIn::InitData()
162 aResLocale
= Translate::Create("sca", LanguageTag(aFuncLoc
));
163 pFuncDataList
.reset(new ScaFuncDataList
);
164 InitScaFuncDataList(*pFuncDataList
);
168 OUString
ScaPricingAddIn::GetFuncDescrStr(const char** pResId
, sal_uInt16 nStrIndex
)
170 return ScaResId(pResId
[nStrIndex
- 1]);
173 OUString
ScaPricingAddIn::getImplementationName_Static()
178 uno::Sequence
< OUString
> ScaPricingAddIn::getSupportedServiceNames_Static()
180 return { ADDIN_SERVICE
, MY_SERVICE
};
184 OUString SAL_CALL
ScaPricingAddIn::getServiceName()
186 // name of specific AddIn service
191 OUString SAL_CALL
ScaPricingAddIn::getImplementationName()
193 return getImplementationName_Static();
196 sal_Bool SAL_CALL
ScaPricingAddIn::supportsService( const OUString
& aServiceName
)
198 return cppu::supportsService(this, aServiceName
);
201 uno::Sequence
< OUString
> SAL_CALL
ScaPricingAddIn::getSupportedServiceNames()
203 return getSupportedServiceNames_Static();
207 void SAL_CALL
ScaPricingAddIn::setLocale( const lang::Locale
& eLocale
)
210 InitData(); // change of locale invalidates resources!
213 lang::Locale SAL_CALL
ScaPricingAddIn::getLocale()
218 // function descriptions start here
220 OUString SAL_CALL
ScaPricingAddIn::getProgrammaticFuntionName( const OUString
& )
223 // (but should be implemented for other uses of the AddIn service)
227 OUString SAL_CALL
ScaPricingAddIn::getDisplayFunctionName( const OUString
& aProgrammaticName
)
231 auto fDataIt
= std::find_if(pFuncDataList
->begin(), pFuncDataList
->end(),
232 FindScaFuncData( aProgrammaticName
) );
233 if (fDataIt
!= pFuncDataList
->end() )
235 aRet
= ScaResId(fDataIt
->GetUINameID());
236 if( fDataIt
->IsDouble() )
241 aRet
= "UNKNOWNFUNC_" + aProgrammaticName
;
247 OUString SAL_CALL
ScaPricingAddIn::getFunctionDescription( const OUString
& aProgrammaticName
)
251 auto fDataIt
= std::find_if( pFuncDataList
->begin(), pFuncDataList
->end(),
252 FindScaFuncData( aProgrammaticName
) );
253 if( fDataIt
!= pFuncDataList
->end() )
254 aRet
= GetFuncDescrStr( fDataIt
->GetDescrID(), 1 );
259 OUString SAL_CALL
ScaPricingAddIn::getDisplayArgumentName(
260 const OUString
& aProgrammaticName
, sal_Int32 nArgument
)
264 auto fDataIt
= std::find_if( pFuncDataList
->begin(), pFuncDataList
->end(),
265 FindScaFuncData( aProgrammaticName
) );
266 if( fDataIt
!= pFuncDataList
->end() && (nArgument
<= 0xFFFF) )
268 sal_uInt16 nStr
= fDataIt
->GetStrIndex( static_cast< sal_uInt16
>( nArgument
) );
270 aRet
= GetFuncDescrStr( fDataIt
->GetDescrID(), nStr
);
278 OUString SAL_CALL
ScaPricingAddIn::getArgumentDescription(
279 const OUString
& aProgrammaticName
, sal_Int32 nArgument
)
283 auto fDataIt
= std::find_if( pFuncDataList
->begin(), pFuncDataList
->end(),
284 FindScaFuncData( aProgrammaticName
) );
285 if( fDataIt
!= pFuncDataList
->end() && (nArgument
<= 0xFFFF) )
287 sal_uInt16 nStr
= fDataIt
->GetStrIndex( static_cast< sal_uInt16
>( nArgument
) );
289 aRet
= GetFuncDescrStr( fDataIt
->GetDescrID(), nStr
+ 1 );
291 aRet
= "for internal use only";
297 OUString SAL_CALL
ScaPricingAddIn::getProgrammaticCategoryName(
298 const OUString
& aProgrammaticName
)
302 auto fDataIt
= std::find_if( pFuncDataList
->begin(), pFuncDataList
->end(),
303 FindScaFuncData( aProgrammaticName
) );
304 if( fDataIt
!= pFuncDataList
->end() )
306 switch( fDataIt
->GetCategory() )
308 case ScaCategory::DateTime
: aRet
= "Date&Time"; break;
309 case ScaCategory::Text
: aRet
= "Text"; break;
310 case ScaCategory::Finance
: aRet
= "Financial"; break;
311 case ScaCategory::Inf
: aRet
= "Information"; break;
312 case ScaCategory::Math
: aRet
= "Mathematical"; break;
313 case ScaCategory::Tech
: aRet
= "Technical"; break;
322 OUString SAL_CALL
ScaPricingAddIn::getDisplayCategoryName(
323 const OUString
& aProgrammaticName
)
325 return getProgrammaticCategoryName( aProgrammaticName
);
328 // XCompatibilityNames
329 uno::Sequence
< sheet::LocalizedName
> SAL_CALL
ScaPricingAddIn::getCompatibilityNames(
330 const OUString
& aProgrammaticName
)
332 auto fDataIt
= std::find_if( pFuncDataList
->begin(), pFuncDataList
->end(),
333 FindScaFuncData( aProgrammaticName
) );
334 if( fDataIt
== pFuncDataList
->end() )
335 return uno::Sequence
< sheet::LocalizedName
>( 0 );
337 const std::vector
<OUString
>& rStrList
= fDataIt
->GetCompNameList();
338 sal_uInt32 nCount
= rStrList
.size();
340 uno::Sequence
< sheet::LocalizedName
> aRet( nCount
);
341 sheet::LocalizedName
* pArray
= aRet
.getArray();
343 for( sal_uInt32 nIndex
= 0; nIndex
< nCount
; nIndex
++ )
344 pArray
[ nIndex
] = sheet::LocalizedName( GetLocale( nIndex
), rStrList
[nIndex
] );
349 // actual function implementation starts here
350 // auxiliary input handling functions
353 bool getinput_putcall(bs::types::PutCall
& pc
, const OUString
& str
) {
354 if(str
.startsWith("c")) {
356 } else if(str
.startsWith("p")) {
364 bool getinput_putcall(bs::types::PutCall
& pc
, const uno::Any
& anyval
) {
366 if(anyval
.getValueTypeClass() == uno::TypeClass_STRING
) {
368 } else if(anyval
.getValueTypeClass() == uno::TypeClass_VOID
) {
369 str
="c"; // call as default
373 return getinput_putcall(pc
, str
);
376 bool getinput_strike(double& strike
, const uno::Any
& anyval
) {
377 if(anyval
.getValueTypeClass() == uno::TypeClass_DOUBLE
) {
379 } else if(anyval
.getValueTypeClass() == uno::TypeClass_VOID
) {
380 strike
=-1.0; // -1 as default (means not set)
387 bool getinput_inout(bs::types::BarrierKIO
& kio
, const OUString
& str
) {
388 if(str
.startsWith("i")) {
389 kio
=bs::types::KnockIn
;
390 } else if(str
.startsWith("o")) {
391 kio
=bs::types::KnockOut
;
398 bool getinput_barrier(bs::types::BarrierActive
& cont
, const OUString
& str
) {
399 if(str
.startsWith("c")) {
400 cont
=bs::types::Continuous
;
401 } else if(str
.startsWith("e")) {
402 cont
=bs::types::Maturity
;
409 bool getinput_fordom(bs::types::ForDom
& fd
, const OUString
& str
) {
410 if(str
.startsWith("f")) {
411 fd
=bs::types::Foreign
;
412 } else if(str
.startsWith("d")) {
413 fd
=bs::types::Domestic
;
420 bool getinput_greek(bs::types::Greeks
& greek
, const uno::Any
& anyval
) {
422 if(anyval
.getValueTypeClass() == uno::TypeClass_STRING
) {
424 } else if(anyval
.getValueTypeClass() == uno::TypeClass_VOID
) {
430 if(str
== "value" || str
== "price" || str
== "v" || str
== "p") {
431 greek
=bs::types::Value
;
432 } else if(str
== "delta" || str
== "d") {
433 greek
=bs::types::Delta
;
434 } else if(str
== "gamma" || str
== "g") {
435 greek
=bs::types::Gamma
;
436 } else if(str
== "theta" || str
== "t") {
437 greek
=bs::types::Theta
;
438 } else if(str
== "vega" || str
== "e") {
439 greek
=bs::types::Vega
;
440 } else if(str
== "volga" || str
== "o") {
441 greek
=bs::types::Volga
;
442 } else if(str
== "vanna" || str
== "a") {
443 greek
=bs::types::Vanna
;
444 } else if(str
== "rho" || str
== "r") {
445 greek
=bs::types::Rho_d
;
446 } else if(str
== "rhof" || str
== "f") {
447 greek
=bs::types::Rho_f
;
454 } // namespace for auxiliary functions
457 double SAL_CALL
ScaPricingAddIn::getOptBarrier( double spot
, double vol
,
458 double r
, double rf
, double T
, double strike
,
459 double barrier_low
, double barrier_up
, double rebate
,
460 const OUString
& put_call
, const OUString
& in_out
,
461 const OUString
& barriercont
, const uno::Any
& greekstr
)
463 bs::types::PutCall pc
;
464 bs::types::BarrierKIO kio
;
465 bs::types::BarrierActive bcont
;
466 bs::types::Greeks greek
;
467 // read and check input values
468 if( spot
<=0.0 || vol
<=0.0 || T
<0.0 || strike
<0.0 ||
469 !getinput_putcall(pc
,put_call
) ||
470 !getinput_inout(kio
,in_out
) ||
471 !getinput_barrier(bcont
,barriercont
) ||
472 !getinput_greek(greek
,greekstr
) ){
473 throw lang::IllegalArgumentException();
476 double fRet
=bs::barrier(spot
,vol
,r
,rf
,T
,strike
, barrier_low
,barrier_up
,
477 rebate
,pc
,kio
,bcont
,greek
);
479 RETURN_FINITE( fRet
);
483 double SAL_CALL
ScaPricingAddIn::getOptTouch( double spot
, double vol
,
484 double r
, double rf
, double T
,
485 double barrier_low
, double barrier_up
,
486 const OUString
& for_dom
, const OUString
& in_out
,
487 const OUString
& barriercont
, const uno::Any
& greekstr
)
489 bs::types::ForDom fd
;
490 bs::types::BarrierKIO kio
;
491 bs::types::BarrierActive bcont
;
492 bs::types::Greeks greek
;
493 // read and check input values
494 if( spot
<=0.0 || vol
<=0.0 || T
<0.0 ||
495 !getinput_fordom(fd
,for_dom
) ||
496 !getinput_inout(kio
,in_out
) ||
497 !getinput_barrier(bcont
,barriercont
) ||
498 !getinput_greek(greek
,greekstr
) ){
499 throw lang::IllegalArgumentException();
502 double fRet
=bs::touch(spot
,vol
,r
,rf
,T
,barrier_low
,barrier_up
,
505 RETURN_FINITE( fRet
);
509 double SAL_CALL
ScaPricingAddIn::getOptProbHit( double spot
, double vol
,
511 double barrier_low
, double barrier_up
)
513 // read and check input values
514 if( spot
<=0.0 || vol
<=0.0 || T
<0.0 ) {
515 throw lang::IllegalArgumentException();
518 double fRet
=bs::prob_hit(spot
,vol
,mu
,T
,barrier_low
,barrier_up
);
520 RETURN_FINITE( fRet
);
523 // OPT_PROB_INMONEY(...)
524 double SAL_CALL
ScaPricingAddIn::getOptProbInMoney( double spot
, double vol
,
526 double barrier_low
, double barrier_up
,
527 const uno::Any
& strikeval
, const uno::Any
& put_call
)
529 bs::types::PutCall pc
=bs::types::Call
;
532 // read and check input values
533 if( spot
<=0.0 || vol
<=0.0 || T
<0.0 ||
534 !getinput_putcall(pc
,put_call
) ||
535 !getinput_strike(K
,strikeval
) ) {
536 throw lang::IllegalArgumentException();
539 double fRet
=bs::prob_in_money(spot
,vol
,mu
,T
,K
,barrier_low
,barrier_up
,pc
);
541 RETURN_FINITE( fRet
);
544 OUString
ScaPricingAddIn::ScaResId(const char* pResId
)
546 return Translate::get(pResId
, aResLocale
);
549 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */