Bump version to 6.0-36
[LibreOffice.git] / scaddins / source / pricing / pricing.cxx
blobdd18d1f4597062e5fc2c3cdf4ab26c76faf69c91
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <iostream>
33 #include <algorithm>
34 #include <rtl/math.hxx>
35 #include <rtl/ustrbuf.hxx>
36 #include <unotools/resmgr.hxx>
38 using namespace ::com::sun::star;
39 using namespace sca::pricing;
42 #define ADDIN_SERVICE "com.sun.star.sheet.AddIn"
43 #define MY_SERVICE "com.sun.star.sheet.addin.PricingFunctions"
44 #define MY_IMPLNAME "com.sun.star.sheet.addin.PricingFunctionsImpl"
46 #define UNIQUE false // function name does not exist in Calc
48 #define STDPAR false // all parameters are described
50 #define FUNCDATA( FuncName, CompName, ParamCount, Category, Double, IntPar ) \
51 { "get" #FuncName, PRICING_FUNCNAME_##FuncName, PRICING_FUNCDESC_##FuncName, CompName, ParamCount, Category, Double, IntPar }
53 const ScaFuncDataBase pFuncDataArr[] =
55 FUNCDATA(OptBarrier, "OPT_BARRIER", 13, ScaCategory::Finance, UNIQUE, STDPAR),
56 FUNCDATA(OptTouch, "OPT_TOUCH", 11, ScaCategory::Finance, UNIQUE, STDPAR),
57 FUNCDATA(OptProbHit, "OPT_PROB_HIT", 6, ScaCategory::Finance, UNIQUE, STDPAR),
58 FUNCDATA(OptProbInMoney, "OPT_PROB_INMONEY", 8, ScaCategory::Finance, UNIQUE, STDPAR)
61 #undef FUNCDATA
63 ScaFuncData::ScaFuncData( const ScaFuncDataBase& rBaseData ) :
64 aIntName( OUString::createFromAscii( rBaseData.pIntName ) ),
65 pUINameID( rBaseData.pUINameID ),
66 pDescrID( rBaseData.pDescrID ),
67 nParamCount( rBaseData.nParamCount ),
68 eCat( rBaseData.eCat ),
69 bDouble( rBaseData.bDouble ),
70 bWithOpt( rBaseData.bWithOpt )
72 aCompList.push_back(OUString::createFromAscii(rBaseData.pCompName));
75 ScaFuncData::~ScaFuncData()
79 sal_uInt16 ScaFuncData::GetStrIndex( sal_uInt16 nParam ) const
81 if( !bWithOpt )
82 nParam++;
83 return (nParam > nParamCount) ? (nParamCount * 2) : (nParam * 2);
86 void sca::pricing::InitScaFuncDataList(ScaFuncDataList& rList)
88 for (const auto & nIndex : pFuncDataArr)
89 rList.push_back(ScaFuncData(nIndex));
92 // entry points for service registration / instantiation
93 uno::Reference< uno::XInterface > SAL_CALL ScaPricingAddIn_CreateInstance(
94 const uno::Reference< lang::XMultiServiceFactory >& )
96 return static_cast<cppu::OWeakObject*>(new ScaPricingAddIn());
99 extern "C" {
101 SAL_DLLPUBLIC_EXPORT void * SAL_CALL pricing_component_getFactory(
102 const sal_Char * pImplName, void * pServiceManager, void * /*pRegistryKey*/ )
104 void* pRet = nullptr;
106 if ( pServiceManager &&
107 OUString::createFromAscii( pImplName ) == ScaPricingAddIn::getImplementationName_Static() )
109 uno::Reference< lang::XSingleServiceFactory > xFactory( cppu::createOneInstanceFactory(
110 static_cast< lang::XMultiServiceFactory* >( pServiceManager ),
111 ScaPricingAddIn::getImplementationName_Static(),
112 ScaPricingAddIn_CreateInstance,
113 ScaPricingAddIn::getSupportedServiceNames_Static() ) );
115 if (xFactory.is())
117 xFactory->acquire();
118 pRet = xFactory.get();
122 return pRet;
125 } // extern C
127 // "normal" service implementation
128 ScaPricingAddIn::ScaPricingAddIn() :
129 pDefLocales( nullptr ),
130 pFuncDataList( nullptr )
134 ScaPricingAddIn::~ScaPricingAddIn()
136 delete pFuncDataList;
137 delete[] pDefLocales;
140 static const sal_Char* pLang[] = { "de", "en" };
141 static const sal_Char* pCoun[] = { "DE", "US" };
142 static const sal_uInt32 nNumOfLoc = SAL_N_ELEMENTS( pLang );
144 void ScaPricingAddIn::InitDefLocales()
146 pDefLocales = new lang::Locale[ nNumOfLoc ];
148 for( sal_uInt32 nIndex = 0; nIndex < nNumOfLoc; nIndex++ )
150 pDefLocales[ nIndex ].Language = OUString::createFromAscii( pLang[ nIndex ] );
151 pDefLocales[ nIndex ].Country = OUString::createFromAscii( pCoun[ nIndex ] );
155 const lang::Locale& ScaPricingAddIn::GetLocale( sal_uInt32 nIndex )
157 if( !pDefLocales )
158 InitDefLocales();
160 return (nIndex < sizeof( pLang )) ? pDefLocales[ nIndex ] : aFuncLoc;
163 void ScaPricingAddIn::InitData()
165 aResLocale = Translate::Create("sca", LanguageTag(aFuncLoc));
166 delete pFuncDataList;
168 pFuncDataList = new ScaFuncDataList;
169 InitScaFuncDataList(*pFuncDataList);
171 if( pDefLocales )
173 delete pDefLocales;
174 pDefLocales = nullptr;
178 OUString ScaPricingAddIn::GetFuncDescrStr(const char** pResId, sal_uInt16 nStrIndex)
180 return ScaResId(pResId[nStrIndex - 1]);
183 OUString ScaPricingAddIn::getImplementationName_Static()
185 return OUString( MY_IMPLNAME );
188 uno::Sequence< OUString > ScaPricingAddIn::getSupportedServiceNames_Static()
190 uno::Sequence< OUString > aRet( 2 );
191 OUString* pArray = aRet.getArray();
192 pArray[0] = ADDIN_SERVICE;
193 pArray[1] = MY_SERVICE;
194 return aRet;
197 // XServiceName
198 OUString SAL_CALL ScaPricingAddIn::getServiceName()
200 // name of specific AddIn service
201 return OUString( MY_SERVICE );
204 // XServiceInfo
205 OUString SAL_CALL ScaPricingAddIn::getImplementationName()
207 return getImplementationName_Static();
210 sal_Bool SAL_CALL ScaPricingAddIn::supportsService( const OUString& aServiceName )
212 return cppu::supportsService(this, aServiceName);
215 uno::Sequence< OUString > SAL_CALL ScaPricingAddIn::getSupportedServiceNames()
217 return getSupportedServiceNames_Static();
220 // XLocalizable
221 void SAL_CALL ScaPricingAddIn::setLocale( const lang::Locale& eLocale )
223 aFuncLoc = eLocale;
224 InitData(); // change of locale invalidates resources!
227 lang::Locale SAL_CALL ScaPricingAddIn::getLocale()
229 return aFuncLoc;
232 // function descriptions start here
233 // XAddIn
234 OUString SAL_CALL ScaPricingAddIn::getProgrammaticFuntionName( const OUString& )
236 // not used by calc
237 // (but should be implemented for other uses of the AddIn service)
238 return OUString();
241 OUString SAL_CALL ScaPricingAddIn::getDisplayFunctionName( const OUString& aProgrammaticName )
243 OUString aRet;
245 auto fDataIt = std::find_if(pFuncDataList->begin(), pFuncDataList->end(),
246 FindScaFuncData( aProgrammaticName ) );
247 if (fDataIt != pFuncDataList->end() )
249 aRet = ScaResId(fDataIt->GetUINameID());
250 if( fDataIt->IsDouble() )
251 aRet += "_ADD";
253 else
255 aRet = "UNKNOWNFUNC_";
256 aRet += aProgrammaticName;
259 return aRet;
262 OUString SAL_CALL ScaPricingAddIn::getFunctionDescription( const OUString& aProgrammaticName )
264 OUString aRet;
266 auto fDataIt = std::find_if( pFuncDataList->begin(), pFuncDataList->end(),
267 FindScaFuncData( aProgrammaticName ) );
268 if( fDataIt != pFuncDataList->end() )
269 aRet = GetFuncDescrStr( fDataIt->GetDescrID(), 1 );
271 return aRet;
274 OUString SAL_CALL ScaPricingAddIn::getDisplayArgumentName(
275 const OUString& aProgrammaticName, sal_Int32 nArgument )
277 OUString aRet;
279 auto fDataIt = std::find_if( pFuncDataList->begin(), pFuncDataList->end(),
280 FindScaFuncData( aProgrammaticName ) );
281 if( fDataIt != pFuncDataList->end() && (nArgument <= 0xFFFF) )
283 sal_uInt16 nStr = fDataIt->GetStrIndex( static_cast< sal_uInt16 >( nArgument ) );
284 if( nStr )
285 aRet = GetFuncDescrStr( fDataIt->GetDescrID(), nStr );
286 else
287 aRet = "internal";
290 return aRet;
293 OUString SAL_CALL ScaPricingAddIn::getArgumentDescription(
294 const OUString& aProgrammaticName, sal_Int32 nArgument )
296 OUString aRet;
298 auto fDataIt = std::find_if( pFuncDataList->begin(), pFuncDataList->end(),
299 FindScaFuncData( aProgrammaticName ) );
300 if( fDataIt != pFuncDataList->end() && (nArgument <= 0xFFFF) )
302 sal_uInt16 nStr = fDataIt->GetStrIndex( static_cast< sal_uInt16 >( nArgument ) );
303 if( nStr )
304 aRet = GetFuncDescrStr( fDataIt->GetDescrID(), nStr + 1 );
305 else
306 aRet = "for internal use only";
309 return aRet;
312 OUString SAL_CALL ScaPricingAddIn::getProgrammaticCategoryName(
313 const OUString& aProgrammaticName )
315 OUString aRet;
317 auto fDataIt = std::find_if( pFuncDataList->begin(), pFuncDataList->end(),
318 FindScaFuncData( aProgrammaticName ) );
319 if( fDataIt != pFuncDataList->end() )
321 switch( fDataIt->GetCategory() )
323 case ScaCategory::DateTime: aRet = "Date&Time"; break;
324 case ScaCategory::Text: aRet = "Text"; break;
325 case ScaCategory::Finance: aRet = "Financial"; break;
326 case ScaCategory::Inf: aRet = "Information"; break;
327 case ScaCategory::Math: aRet = "Mathematical"; break;
328 case ScaCategory::Tech: aRet = "Technical"; break;
332 if( aRet.isEmpty() )
333 aRet = "Add-In";
334 return aRet;
337 OUString SAL_CALL ScaPricingAddIn::getDisplayCategoryName(
338 const OUString& aProgrammaticName )
340 return getProgrammaticCategoryName( aProgrammaticName );
343 // XCompatibilityNames
344 uno::Sequence< sheet::LocalizedName > SAL_CALL ScaPricingAddIn::getCompatibilityNames(
345 const OUString& aProgrammaticName )
347 auto fDataIt = std::find_if( pFuncDataList->begin(), pFuncDataList->end(),
348 FindScaFuncData( aProgrammaticName ) );
349 if( fDataIt == pFuncDataList->end() )
350 return uno::Sequence< sheet::LocalizedName >( 0 );
352 const std::vector<OUString>& rStrList = fDataIt->GetCompNameList();
353 sal_uInt32 nCount = rStrList.size();
355 uno::Sequence< sheet::LocalizedName > aRet( nCount );
356 sheet::LocalizedName* pArray = aRet.getArray();
358 for( sal_uInt32 nIndex = 0; nIndex < nCount; nIndex++ )
359 pArray[ nIndex ] = sheet::LocalizedName( GetLocale( nIndex ), rStrList[nIndex] );
361 return aRet;
364 // actual function implementation starts here
365 // auxiliary input handling functions
366 namespace {
368 bool getinput_putcall(bs::types::PutCall& pc, const OUString& str) {
369 if(str.startsWith("c")) {
370 pc=bs::types::Call;
371 } else if(str.startsWith("p")) {
372 pc=bs::types::Put;
373 } else {
374 return false;
376 return true;
379 bool getinput_putcall(bs::types::PutCall& pc, const uno::Any& anyval) {
380 OUString str;
381 if(anyval.getValueTypeClass() == uno::TypeClass_STRING) {
382 anyval >>= str;
383 } else if(anyval.getValueTypeClass() == uno::TypeClass_VOID) {
384 str="c"; // call as default
385 } else {
386 return false;
388 return getinput_putcall(pc, str);
391 bool getinput_strike(double& strike, const uno::Any& anyval) {
392 if(anyval.getValueTypeClass() == uno::TypeClass_DOUBLE) {
393 anyval >>= strike;
394 } else if(anyval.getValueTypeClass() == uno::TypeClass_VOID) {
395 strike=-1.0; // -1 as default (means not set)
396 } else {
397 return false;
399 return true;
402 bool getinput_inout(bs::types::BarrierKIO& kio, const OUString& str) {
403 if(str.startsWith("i")) {
404 kio=bs::types::KnockIn;
405 } else if(str.startsWith("o")) {
406 kio=bs::types::KnockOut;
407 } else {
408 return false;
410 return true;
413 bool getinput_barrier(bs::types::BarrierActive& cont, const OUString& str) {
414 if(str.startsWith("c")) {
415 cont=bs::types::Continuous;
416 } else if(str.startsWith("e")) {
417 cont=bs::types::Maturity;
418 } else {
419 return false;
421 return true;
424 bool getinput_fordom(bs::types::ForDom& fd, const OUString& str) {
425 if(str.startsWith("f")) {
426 fd=bs::types::Foreign;
427 } else if(str.startsWith("d")) {
428 fd=bs::types::Domestic;
429 } else {
430 return false;
432 return true;
435 bool getinput_greek(bs::types::Greeks& greek, const uno::Any& anyval) {
436 OUString str;
437 if(anyval.getValueTypeClass() == uno::TypeClass_STRING) {
438 anyval >>= str;
439 } else if(anyval.getValueTypeClass() == uno::TypeClass_VOID) {
440 str="value";
441 } else {
442 return false;
445 if(str == "value" || str == "price" || str == "v" || str == "p") {
446 greek=bs::types::Value;
447 } else if(str == "delta" || str == "d") {
448 greek=bs::types::Delta;
449 } else if(str == "gamma" || str == "g") {
450 greek=bs::types::Gamma;
451 } else if(str == "theta" || str == "t") {
452 greek=bs::types::Theta;
453 } else if(str == "vega" || str == "e") {
454 greek=bs::types::Vega;
455 } else if(str == "volga" || str == "o") {
456 greek=bs::types::Volga;
457 } else if(str == "vanna" || str == "a") {
458 greek=bs::types::Vanna;
459 } else if(str == "rho" || str == "r") {
460 greek=bs::types::Rho_d;
461 } else if(str == "rhof" || str == "f") {
462 greek=bs::types::Rho_f;
463 } else {
464 return false;
466 return true;
469 } // namespace for auxiliary functions
471 // OPT_BARRIER(...)
472 double SAL_CALL ScaPricingAddIn::getOptBarrier( double spot, double vol,
473 double r, double rf, double T, double strike,
474 double barrier_low, double barrier_up, double rebate,
475 const OUString& put_call, const OUString& in_out,
476 const OUString& barriercont, const uno::Any& greekstr )
478 bs::types::PutCall pc;
479 bs::types::BarrierKIO kio;
480 bs::types::BarrierActive bcont;
481 bs::types::Greeks greek;
482 // read and check input values
483 if( spot<=0.0 || vol<=0.0 || T<0.0 || strike<0.0 ||
484 !getinput_putcall(pc,put_call) ||
485 !getinput_inout(kio,in_out) ||
486 !getinput_barrier(bcont,barriercont) ||
487 !getinput_greek(greek,greekstr) ){
488 throw lang::IllegalArgumentException();
491 double fRet=bs::barrier(spot,vol,r,rf,T,strike, barrier_low,barrier_up,
492 rebate,pc,kio,bcont,greek);
494 RETURN_FINITE( fRet );
497 // OPT_TOUCH(...)
498 double SAL_CALL ScaPricingAddIn::getOptTouch( double spot, double vol,
499 double r, double rf, double T,
500 double barrier_low, double barrier_up,
501 const OUString& for_dom, const OUString& in_out,
502 const OUString& barriercont, const uno::Any& greekstr )
504 bs::types::ForDom fd;
505 bs::types::BarrierKIO kio;
506 bs::types::BarrierActive bcont;
507 bs::types::Greeks greek;
508 // read and check input values
509 if( spot<=0.0 || vol<=0.0 || T<0.0 ||
510 !getinput_fordom(fd,for_dom) ||
511 !getinput_inout(kio,in_out) ||
512 !getinput_barrier(bcont,barriercont) ||
513 !getinput_greek(greek,greekstr) ){
514 throw lang::IllegalArgumentException();
517 double fRet=bs::touch(spot,vol,r,rf,T,barrier_low,barrier_up,
518 fd,kio,bcont,greek);
520 RETURN_FINITE( fRet );
523 // OPT_PRB_HIT(...)
524 double SAL_CALL ScaPricingAddIn::getOptProbHit( double spot, double vol,
525 double mu, double T,
526 double barrier_low, double barrier_up )
528 // read and check input values
529 if( spot<=0.0 || vol<=0.0 || T<0.0 ) {
530 throw lang::IllegalArgumentException();
533 double fRet=bs::prob_hit(spot,vol,mu,T,barrier_low,barrier_up);
535 RETURN_FINITE( fRet );
538 // OPT_PROB_INMONEY(...)
539 double SAL_CALL ScaPricingAddIn::getOptProbInMoney( double spot, double vol,
540 double mu, double T,
541 double barrier_low, double barrier_up,
542 const uno::Any& strikeval, const uno::Any& put_call )
544 bs::types::PutCall pc=bs::types::Call;
545 double K = 0;
547 // read and check input values
548 if( spot<=0.0 || vol<=0.0 || T<0.0 ||
549 !getinput_putcall(pc,put_call) ||
550 !getinput_strike(K,strikeval) ) {
551 throw lang::IllegalArgumentException();
554 double fRet=bs::prob_in_money(spot,vol,mu,T,K,barrier_low,barrier_up,pc);
556 RETURN_FINITE( fRet );
559 OUString ScaPricingAddIn::ScaResId(const char* pResId)
561 return Translate::get(pResId, aResLocale);
564 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */