Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / scaddins / source / pricing / pricing.cxx
blob1b698fd0ec49e73e1db8bb08c6db7d5dc00f90f5
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 <com/sun/star/lang/XSingleServiceFactory.hpp>
33 #include <iostream>
34 #include <algorithm>
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)
62 #undef FUNCDATA
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
82 if( !bWithOpt )
83 nParam++;
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());
100 extern "C" {
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() ) );
116 if (xFactory.is())
118 xFactory->acquire();
119 pRet = xFactory.get();
123 return pRet;
126 } // extern C
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 )
154 if( !pDefLocales )
155 InitDefLocales();
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);
165 pDefLocales.reset();
168 OUString ScaPricingAddIn::GetFuncDescrStr(const char** pResId, sal_uInt16 nStrIndex)
170 return ScaResId(pResId[nStrIndex - 1]);
173 OUString ScaPricingAddIn::getImplementationName_Static()
175 return MY_IMPLNAME;
178 uno::Sequence< OUString > ScaPricingAddIn::getSupportedServiceNames_Static()
180 return { ADDIN_SERVICE, MY_SERVICE };
183 // XServiceName
184 OUString SAL_CALL ScaPricingAddIn::getServiceName()
186 // name of specific AddIn service
187 return MY_SERVICE;
190 // XServiceInfo
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();
206 // XLocalizable
207 void SAL_CALL ScaPricingAddIn::setLocale( const lang::Locale& eLocale )
209 aFuncLoc = eLocale;
210 InitData(); // change of locale invalidates resources!
213 lang::Locale SAL_CALL ScaPricingAddIn::getLocale()
215 return aFuncLoc;
218 // function descriptions start here
219 // XAddIn
220 OUString SAL_CALL ScaPricingAddIn::getProgrammaticFuntionName( const OUString& )
222 // not used by calc
223 // (but should be implemented for other uses of the AddIn service)
224 return OUString();
227 OUString SAL_CALL ScaPricingAddIn::getDisplayFunctionName( const OUString& aProgrammaticName )
229 OUString aRet;
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() )
237 aRet += "_ADD";
239 else
241 aRet = "UNKNOWNFUNC_" + aProgrammaticName;
244 return aRet;
247 OUString SAL_CALL ScaPricingAddIn::getFunctionDescription( const OUString& aProgrammaticName )
249 OUString aRet;
251 auto fDataIt = std::find_if( pFuncDataList->begin(), pFuncDataList->end(),
252 FindScaFuncData( aProgrammaticName ) );
253 if( fDataIt != pFuncDataList->end() )
254 aRet = GetFuncDescrStr( fDataIt->GetDescrID(), 1 );
256 return aRet;
259 OUString SAL_CALL ScaPricingAddIn::getDisplayArgumentName(
260 const OUString& aProgrammaticName, sal_Int32 nArgument )
262 OUString aRet;
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 ) );
269 if( nStr )
270 aRet = GetFuncDescrStr( fDataIt->GetDescrID(), nStr );
271 else
272 aRet = "internal";
275 return aRet;
278 OUString SAL_CALL ScaPricingAddIn::getArgumentDescription(
279 const OUString& aProgrammaticName, sal_Int32 nArgument )
281 OUString aRet;
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 ) );
288 if( nStr )
289 aRet = GetFuncDescrStr( fDataIt->GetDescrID(), nStr + 1 );
290 else
291 aRet = "for internal use only";
294 return aRet;
297 OUString SAL_CALL ScaPricingAddIn::getProgrammaticCategoryName(
298 const OUString& aProgrammaticName )
300 OUString aRet;
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;
317 if( aRet.isEmpty() )
318 aRet = "Add-In";
319 return aRet;
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] );
346 return aRet;
349 // actual function implementation starts here
350 // auxiliary input handling functions
351 namespace {
353 bool getinput_putcall(bs::types::PutCall& pc, const OUString& str) {
354 if(str.startsWith("c")) {
355 pc=bs::types::Call;
356 } else if(str.startsWith("p")) {
357 pc=bs::types::Put;
358 } else {
359 return false;
361 return true;
364 bool getinput_putcall(bs::types::PutCall& pc, const uno::Any& anyval) {
365 OUString str;
366 if(anyval.getValueTypeClass() == uno::TypeClass_STRING) {
367 anyval >>= str;
368 } else if(anyval.getValueTypeClass() == uno::TypeClass_VOID) {
369 str="c"; // call as default
370 } else {
371 return false;
373 return getinput_putcall(pc, str);
376 bool getinput_strike(double& strike, const uno::Any& anyval) {
377 if(anyval.getValueTypeClass() == uno::TypeClass_DOUBLE) {
378 anyval >>= strike;
379 } else if(anyval.getValueTypeClass() == uno::TypeClass_VOID) {
380 strike=-1.0; // -1 as default (means not set)
381 } else {
382 return false;
384 return true;
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;
392 } else {
393 return false;
395 return true;
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;
403 } else {
404 return false;
406 return true;
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;
414 } else {
415 return false;
417 return true;
420 bool getinput_greek(bs::types::Greeks& greek, const uno::Any& anyval) {
421 OUString str;
422 if(anyval.getValueTypeClass() == uno::TypeClass_STRING) {
423 anyval >>= str;
424 } else if(anyval.getValueTypeClass() == uno::TypeClass_VOID) {
425 str="value";
426 } else {
427 return false;
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;
448 } else {
449 return false;
451 return true;
454 } // namespace for auxiliary functions
456 // OPT_BARRIER(...)
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 );
482 // OPT_TOUCH(...)
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,
503 fd,kio,bcont,greek);
505 RETURN_FINITE( fRet );
508 // OPT_PRB_HIT(...)
509 double SAL_CALL ScaPricingAddIn::getOptProbHit( double spot, double vol,
510 double mu, double T,
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,
525 double mu, double T,
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;
530 double K = 0;
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: */