Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : //------------------------------------------------------------------
21 : //
22 : // pricing functions add in
23 : //
24 : // all of the UNO add-in technical details have been copied from
25 : // ../datefunc/datefunc.cxx
26 : //
27 : //------------------------------------------------------------------
28 :
29 : #include "pricing.hxx"
30 : #include "black_scholes.hxx"
31 : #include "pricing.hrc"
32 : #include <cppuhelper/factory.hxx>
33 : #include <osl/diagnose.h>
34 : #include <rtl/ustrbuf.hxx>
35 : #include <rtl/math.hxx>
36 : #include <tools/resmgr.hxx>
37 : #include <tools/rcid.h>
38 :
39 : #include <iostream>
40 :
41 : using namespace ::com::sun::star;
42 : using namespace sca::pricing;
43 :
44 :
45 : //------------------------------------------------------------------
46 :
47 : #define ADDIN_SERVICE "com.sun.star.sheet.AddIn"
48 : #define MY_SERVICE "com.sun.star.sheet.addin.PricingFunctions"
49 : #define MY_IMPLNAME "com.sun.star.sheet.addin.PricingFunctionsImpl"
50 :
51 : //------------------------------------------------------------------
52 :
53 : #define STR_FROM_ANSI( s ) OUString( s, strlen( s ), RTL_TEXTENCODING_MS_1252 )
54 :
55 : //------------------------------------------------------------------
56 :
57 : const sal_uInt32 ScaList::nStartSize = 16;
58 : const sal_uInt32 ScaList::nIncrSize = 16;
59 :
60 20 : ScaList::ScaList() :
61 20 : pData( new void*[ nStartSize ] ),
62 : nSize( nStartSize ),
63 : nCount( 0 ),
64 40 : nCurr( 0 )
65 : {
66 20 : }
67 :
68 20 : ScaList::~ScaList()
69 : {
70 20 : delete[] pData;
71 20 : }
72 :
73 0 : void ScaList::_Grow()
74 : {
75 0 : nSize += nIncrSize;
76 :
77 0 : void** pNewData = new void*[ nSize ];
78 0 : memcpy( pNewData, pData, nCount * sizeof( void* ) );
79 :
80 0 : delete[] pData;
81 0 : pData = pNewData;
82 0 : }
83 :
84 : //------------------------------------------------------------------
85 :
86 32 : ScaStringList::~ScaStringList()
87 : {
88 32 : for( OUString* pStr = First(); pStr; pStr = Next() )
89 16 : delete pStr;
90 16 : }
91 :
92 : //------------------------------------------------------------------
93 :
94 1024 : ScaResId::ScaResId( sal_uInt16 nId, ResMgr& rResMgr ) :
95 1024 : ResId( nId, rResMgr )
96 : {
97 1024 : }
98 :
99 :
100 : //------------------------------------------------------------------
101 :
102 : #define UNIQUE sal_False // function name does not exist in Calc
103 :
104 : #define STDPAR sal_False // all parameters are described
105 :
106 : #define FUNCDATA( FuncName, ParamCount, Category, Double, IntPar ) \
107 : { "get" #FuncName, PRICING_FUNCNAME_##FuncName, PRICING_FUNCDESC_##FuncName, PRICING_DEFFUNCNAME_##FuncName, ParamCount, Category, Double, IntPar }
108 :
109 : const ScaFuncDataBase pFuncDataArr[] =
110 : {
111 : FUNCDATA( OptBarrier, 13, ScaCat_Finance, UNIQUE, STDPAR),
112 : FUNCDATA( OptTouch, 11, ScaCat_Finance, UNIQUE, STDPAR),
113 : FUNCDATA( OptProbHit, 6, ScaCat_Finance, UNIQUE, STDPAR),
114 : FUNCDATA( OptProbInMoney, 8, ScaCat_Finance, UNIQUE, STDPAR)
115 : };
116 :
117 : #undef FUNCDATA
118 :
119 :
120 : //------------------------------------------------------------------
121 :
122 16 : ScaFuncData::ScaFuncData( const ScaFuncDataBase& rBaseData, ResMgr& rResMgr ) :
123 : aIntName( OUString::createFromAscii( rBaseData.pIntName ) ),
124 : nUINameID( rBaseData.nUINameID ),
125 : nDescrID( rBaseData.nDescrID ),
126 : nCompListID( rBaseData.nCompListID ),
127 : nParamCount( rBaseData.nParamCount ),
128 : eCat( rBaseData.eCat ),
129 : bDouble( rBaseData.bDouble ),
130 16 : bWithOpt( rBaseData.bWithOpt )
131 : {
132 16 : ScaResStringArrLoader aArrLoader( RID_PRICING_DEFFUNCTION_NAMES, nCompListID, rResMgr );
133 16 : const ResStringArray& rArr = aArrLoader.GetStringArray();
134 :
135 32 : for( sal_uInt16 nIndex = 0; nIndex < rArr.Count(); nIndex++ )
136 32 : aCompList.Append( rArr.GetString( nIndex ) );
137 16 : }
138 :
139 32 : ScaFuncData::~ScaFuncData()
140 : {
141 32 : }
142 :
143 304 : sal_uInt16 ScaFuncData::GetStrIndex( sal_uInt16 nParam ) const
144 : {
145 304 : if( !bWithOpt )
146 304 : nParam++;
147 304 : return (nParam > nParamCount) ? (nParamCount * 2) : (nParam * 2);
148 : }
149 :
150 :
151 : //------------------------------------------------------------------
152 :
153 4 : ScaFuncDataList::ScaFuncDataList( ResMgr& rResMgr ) :
154 4 : nLast( 0xFFFFFFFF )
155 : {
156 20 : for( sal_uInt16 nIndex = 0; nIndex < SAL_N_ELEMENTS(pFuncDataArr); nIndex++ )
157 16 : Append( new ScaFuncData( pFuncDataArr[ nIndex ], rResMgr ) );
158 4 : }
159 :
160 12 : ScaFuncDataList::~ScaFuncDataList()
161 : {
162 20 : for( ScaFuncData* pFData = First(); pFData; pFData = Next() )
163 16 : delete pFData;
164 8 : }
165 :
166 356 : const ScaFuncData* ScaFuncDataList::Get( const OUString& rProgrammaticName ) const
167 : {
168 356 : if( aLastName == rProgrammaticName ){
169 336 : return Get( nLast );
170 : }
171 :
172 50 : for( sal_uInt32 nIndex = 0; nIndex < Count(); nIndex++ )
173 : {
174 50 : const ScaFuncData* pCurr = Get( nIndex );
175 50 : if( pCurr->Is( rProgrammaticName ) )
176 : {
177 20 : const_cast< ScaFuncDataList* >( this )->aLastName = rProgrammaticName;
178 20 : const_cast< ScaFuncDataList* >( this )->nLast = nIndex;
179 20 : return pCurr;
180 : }
181 : }
182 0 : return NULL;
183 : }
184 :
185 :
186 : //------------------------------------------------------------------
187 :
188 320 : ScaFuncRes::ScaFuncRes( ResId& rResId, ResMgr& rResMgr, sal_uInt16 nIndex, OUString& rRet ) :
189 320 : Resource( rResId )
190 : {
191 320 : rRet = String( ScaResId( nIndex, rResMgr ) );
192 320 : FreeResource();
193 320 : }
194 :
195 :
196 : //------------------------------------------------------------------
197 : //
198 : // entry points for service registration / instantiation
199 : //
200 : //------------------------------------------------------------------
201 :
202 4 : uno::Reference< uno::XInterface > SAL_CALL ScaPricingAddIn_CreateInstance(
203 : const uno::Reference< lang::XMultiServiceFactory >& )
204 : {
205 4 : static uno::Reference< uno::XInterface > xInst = (cppu::OWeakObject*) new ScaPricingAddIn();
206 4 : return xInst;
207 : }
208 :
209 :
210 : //------------------------------------------------------------------------
211 :
212 : extern "C" {
213 :
214 4 : SAL_DLLPUBLIC_EXPORT void * SAL_CALL pricing_component_getFactory(
215 : const sal_Char * pImplName, void * pServiceManager, void * /*pRegistryKey*/ )
216 : {
217 4 : void* pRet = 0;
218 :
219 20 : if ( pServiceManager &&
220 20 : OUString::createFromAscii( pImplName ) == ScaPricingAddIn::getImplementationName_Static() )
221 : {
222 : uno::Reference< lang::XSingleServiceFactory > xFactory( cppu::createOneInstanceFactory(
223 : reinterpret_cast< lang::XMultiServiceFactory* >( pServiceManager ),
224 : ScaPricingAddIn::getImplementationName_Static(),
225 : ScaPricingAddIn_CreateInstance,
226 4 : ScaPricingAddIn::getSupportedServiceNames_Static() ) );
227 :
228 4 : if (xFactory.is())
229 : {
230 4 : xFactory->acquire();
231 4 : pRet = xFactory.get();
232 4 : }
233 : }
234 :
235 4 : return pRet;
236 : }
237 :
238 : } // extern C
239 :
240 : //------------------------------------------------------------------------
241 : //
242 : // "normal" service implementation
243 : //
244 : //------------------------------------------------------------------------
245 :
246 4 : ScaPricingAddIn::ScaPricingAddIn() :
247 : pDefLocales( NULL ),
248 : pResMgr( NULL ),
249 4 : pFuncDataList( NULL )
250 : {
251 4 : }
252 :
253 12 : ScaPricingAddIn::~ScaPricingAddIn()
254 : {
255 4 : if( pFuncDataList )
256 4 : delete pFuncDataList;
257 4 : if( pDefLocales )
258 1 : delete[] pDefLocales;
259 :
260 : // pResMgr already deleted (_all_ resource managers are deleted _before_ this dtor is called)
261 8 : }
262 :
263 : static const sal_Char* pLang[] = { "de", "en" };
264 : static const sal_Char* pCoun[] = { "DE", "US" };
265 : static const sal_uInt32 nNumOfLoc = SAL_N_ELEMENTS( pLang );
266 :
267 1 : void ScaPricingAddIn::InitDefLocales()
268 : {
269 1 : pDefLocales = new lang::Locale[ nNumOfLoc ];
270 :
271 3 : for( sal_uInt32 nIndex = 0; nIndex < nNumOfLoc; nIndex++ )
272 : {
273 2 : pDefLocales[ nIndex ].Language = OUString::createFromAscii( pLang[ nIndex ] );
274 2 : pDefLocales[ nIndex ].Country = OUString::createFromAscii( pCoun[ nIndex ] );
275 : }
276 1 : }
277 :
278 4 : const lang::Locale& ScaPricingAddIn::GetLocale( sal_uInt32 nIndex )
279 : {
280 4 : if( !pDefLocales )
281 1 : InitDefLocales();
282 :
283 4 : return (nIndex < sizeof( pLang )) ? pDefLocales[ nIndex ] : aFuncLoc;
284 : }
285 :
286 976 : ResMgr& ScaPricingAddIn::GetResMgr() throw( uno::RuntimeException )
287 : {
288 976 : if( !pResMgr )
289 : {
290 0 : InitData(); // try to get resource manager
291 0 : if( !pResMgr )
292 0 : throw uno::RuntimeException();
293 : }
294 976 : return *pResMgr;
295 : }
296 :
297 4 : void ScaPricingAddIn::InitData()
298 : {
299 :
300 4 : if( pResMgr )
301 0 : delete pResMgr;
302 :
303 4 : OString aModName( "pricing" );
304 4 : pResMgr = ResMgr::CreateResMgr( aModName.getStr(), LanguageTag( aFuncLoc) );
305 :
306 4 : if( pFuncDataList )
307 0 : delete pFuncDataList;
308 :
309 4 : pFuncDataList = pResMgr ? new ScaFuncDataList( *pResMgr ) : NULL;
310 :
311 4 : if( pDefLocales )
312 : {
313 0 : delete pDefLocales;
314 0 : pDefLocales = NULL;
315 4 : }
316 4 : }
317 :
318 16 : OUString ScaPricingAddIn::GetDisplFuncStr( sal_uInt16 nResId ) throw( uno::RuntimeException )
319 : {
320 16 : return ScaResStringLoader( RID_PRICING_FUNCTION_NAMES, nResId, GetResMgr() ).GetString();
321 : }
322 :
323 320 : OUString ScaPricingAddIn::GetFuncDescrStr( sal_uInt16 nResId, sal_uInt16 nStrIndex ) throw( uno::RuntimeException )
324 : {
325 320 : OUString aRet;
326 :
327 640 : ScaResPublisher aResPubl( ScaResId( RID_PRICING_FUNCTION_DESCRIPTIONS, GetResMgr() ) );
328 320 : ScaResId aResId( nResId, GetResMgr() );
329 320 : aResId.SetRT( RSC_RESOURCE );
330 :
331 320 : if( aResPubl.IsAvailableRes( aResId ) )
332 320 : ScaFuncRes aSubRes( aResId, GetResMgr(), nStrIndex, aRet );
333 :
334 320 : aResPubl.FreeResource();
335 640 : return aRet;
336 : }
337 :
338 :
339 : //------------------------------------------------------------------------
340 :
341 8 : OUString ScaPricingAddIn::getImplementationName_Static()
342 : {
343 8 : return OUString( MY_IMPLNAME );
344 : }
345 :
346 4 : uno::Sequence< OUString > ScaPricingAddIn::getSupportedServiceNames_Static()
347 : {
348 4 : uno::Sequence< OUString > aRet( 2 );
349 4 : OUString* pArray = aRet.getArray();
350 4 : pArray[0] = OUString( ADDIN_SERVICE );
351 4 : pArray[1] = OUString( MY_SERVICE );
352 4 : return aRet;
353 : }
354 :
355 : // XServiceName
356 :
357 8 : OUString SAL_CALL ScaPricingAddIn::getServiceName() throw( uno::RuntimeException )
358 : {
359 : // name of specific AddIn service
360 8 : return OUString( MY_SERVICE );
361 : }
362 :
363 : // XServiceInfo
364 :
365 0 : OUString SAL_CALL ScaPricingAddIn::getImplementationName() throw( uno::RuntimeException )
366 : {
367 0 : return getImplementationName_Static();
368 : }
369 :
370 0 : sal_Bool SAL_CALL ScaPricingAddIn::supportsService( const OUString& aServiceName ) throw( uno::RuntimeException )
371 : {
372 0 : return aServiceName == ADDIN_SERVICE || aServiceName == MY_SERVICE;
373 : }
374 :
375 0 : uno::Sequence< OUString > SAL_CALL ScaPricingAddIn::getSupportedServiceNames() throw( uno::RuntimeException )
376 : {
377 0 : return getSupportedServiceNames_Static();
378 : }
379 :
380 : // XLocalizable
381 :
382 4 : void SAL_CALL ScaPricingAddIn::setLocale( const lang::Locale& eLocale ) throw( uno::RuntimeException )
383 : {
384 4 : aFuncLoc = eLocale;
385 4 : InitData(); // change of locale invalidates resources!
386 4 : }
387 :
388 0 : lang::Locale SAL_CALL ScaPricingAddIn::getLocale() throw( uno::RuntimeException )
389 : {
390 0 : return aFuncLoc;
391 : }
392 :
393 : //------------------------------------------------------------------
394 : //
395 : // function descriptions start here
396 : //
397 : //------------------------------------------------------------------
398 :
399 : // XAddIn
400 0 : OUString SAL_CALL ScaPricingAddIn::getProgrammaticFuntionName( const OUString& ) throw( uno::RuntimeException )
401 : {
402 : // not used by calc
403 : // (but should be implemented for other uses of the AddIn service)
404 0 : return OUString();
405 : }
406 :
407 16 : OUString SAL_CALL ScaPricingAddIn::getDisplayFunctionName( const OUString& aProgrammaticName ) throw( uno::RuntimeException )
408 : {
409 16 : OUString aRet;
410 :
411 16 : const ScaFuncData* pFData = pFuncDataList->Get( aProgrammaticName );
412 16 : if( pFData )
413 : {
414 16 : aRet = GetDisplFuncStr( pFData->GetUINameID() );
415 16 : if( pFData->IsDouble() )
416 0 : aRet += STR_FROM_ANSI( "_ADD" );
417 : }
418 : else
419 : {
420 0 : aRet = STR_FROM_ANSI( "UNKNOWNFUNC_" );
421 0 : aRet += aProgrammaticName;
422 : }
423 :
424 16 : return aRet;
425 : }
426 :
427 16 : OUString SAL_CALL ScaPricingAddIn::getFunctionDescription( const OUString& aProgrammaticName ) throw( uno::RuntimeException )
428 : {
429 16 : OUString aRet;
430 :
431 16 : const ScaFuncData* pFData = pFuncDataList->Get( aProgrammaticName );
432 16 : if( pFData )
433 16 : aRet = GetFuncDescrStr( pFData->GetDescrID(), 1 );
434 :
435 16 : return aRet;
436 : }
437 :
438 152 : OUString SAL_CALL ScaPricingAddIn::getDisplayArgumentName(
439 : const OUString& aProgrammaticName, sal_Int32 nArgument ) throw( uno::RuntimeException )
440 : {
441 152 : OUString aRet;
442 :
443 152 : const ScaFuncData* pFData = pFuncDataList->Get( aProgrammaticName );
444 152 : if( pFData && (nArgument <= 0xFFFF) )
445 : {
446 152 : sal_uInt16 nStr = pFData->GetStrIndex( static_cast< sal_uInt16 >( nArgument ) );
447 152 : if( nStr )
448 152 : aRet = GetFuncDescrStr( pFData->GetDescrID(), nStr );
449 : else
450 0 : aRet = STR_FROM_ANSI( "internal" );
451 : }
452 :
453 152 : return aRet;
454 : }
455 :
456 152 : OUString SAL_CALL ScaPricingAddIn::getArgumentDescription(
457 : const OUString& aProgrammaticName, sal_Int32 nArgument ) throw( uno::RuntimeException )
458 : {
459 152 : OUString aRet;
460 :
461 152 : const ScaFuncData* pFData = pFuncDataList->Get( aProgrammaticName );
462 152 : if( pFData && (nArgument <= 0xFFFF) )
463 : {
464 152 : sal_uInt16 nStr = pFData->GetStrIndex( static_cast< sal_uInt16 >( nArgument ) );
465 152 : if( nStr )
466 152 : aRet = GetFuncDescrStr( pFData->GetDescrID(), nStr + 1 );
467 : else
468 0 : aRet = STR_FROM_ANSI( "for internal use only" );
469 : }
470 :
471 152 : return aRet;
472 : }
473 :
474 16 : OUString SAL_CALL ScaPricingAddIn::getProgrammaticCategoryName(
475 : const OUString& aProgrammaticName ) throw( uno::RuntimeException )
476 : {
477 16 : OUString aRet;
478 :
479 16 : const ScaFuncData* pFData = pFuncDataList->Get( aProgrammaticName );
480 16 : if( pFData )
481 : {
482 16 : switch( pFData->GetCategory() )
483 : {
484 0 : case ScaCat_DateTime: aRet = STR_FROM_ANSI( "Date&Time" ); break;
485 0 : case ScaCat_Text: aRet = STR_FROM_ANSI( "Text" ); break;
486 16 : case ScaCat_Finance: aRet = STR_FROM_ANSI( "Financial" ); break;
487 0 : case ScaCat_Inf: aRet = STR_FROM_ANSI( "Information" ); break;
488 0 : case ScaCat_Math: aRet = STR_FROM_ANSI( "Mathematical" ); break;
489 0 : case ScaCat_Tech: aRet = STR_FROM_ANSI( "Technical" ); break;
490 : default: // to prevent compiler warnings
491 0 : break;
492 : }
493 : }
494 :
495 16 : if( aRet.isEmpty() )
496 0 : aRet = STR_FROM_ANSI( "Add-In" );
497 16 : return aRet;
498 : }
499 :
500 0 : OUString SAL_CALL ScaPricingAddIn::getDisplayCategoryName(
501 : const OUString& aProgrammaticName ) throw( uno::RuntimeException )
502 : {
503 0 : return getProgrammaticCategoryName( aProgrammaticName );
504 : }
505 :
506 :
507 : // XCompatibilityNames
508 :
509 4 : uno::Sequence< sheet::LocalizedName > SAL_CALL ScaPricingAddIn::getCompatibilityNames(
510 : const OUString& aProgrammaticName ) throw( uno::RuntimeException )
511 : {
512 4 : const ScaFuncData* pFData = pFuncDataList->Get( aProgrammaticName );
513 4 : if( !pFData )
514 0 : return uno::Sequence< sheet::LocalizedName >( 0 );
515 :
516 4 : const ScaStringList& rStrList = pFData->GetCompNameList();
517 4 : sal_uInt32 nCount = rStrList.Count();
518 :
519 4 : uno::Sequence< sheet::LocalizedName > aRet( nCount );
520 4 : sheet::LocalizedName* pArray = aRet.getArray();
521 :
522 8 : for( sal_uInt32 nIndex = 0; nIndex < nCount; nIndex++ )
523 4 : pArray[ nIndex ] = sheet::LocalizedName( GetLocale( nIndex ), *rStrList.Get( nIndex ) );
524 :
525 4 : return aRet;
526 : }
527 :
528 :
529 :
530 : // ---------------------------------------------------------------------
531 : // ---------------------------------------------------------------------
532 : // actual function implementation starts here
533 : //
534 :
535 : // auxillary input handling functions
536 : namespace {
537 :
538 0 : bool getinput_putcall(bs::types::PutCall& pc, const OUString& str) {
539 0 : if(str.startsWith("c")) {
540 0 : pc=bs::types::Call;
541 0 : } else if(str.startsWith("p")) {
542 0 : pc=bs::types::Put;
543 : } else {
544 0 : return false;
545 : }
546 0 : return true;
547 : }
548 :
549 0 : bool getinput_putcall(bs::types::PutCall& pc, const uno::Any& anyval) {
550 0 : OUString str;
551 0 : if(anyval.getValueTypeClass() == uno::TypeClass_STRING) {
552 0 : anyval >>= str;
553 0 : } else if(anyval.getValueTypeClass() == uno::TypeClass_VOID) {
554 0 : str="c"; // call as default
555 : } else {
556 0 : return false;
557 : }
558 0 : return getinput_putcall(pc, str);
559 : }
560 :
561 0 : bool getinput_strike(double& strike, const uno::Any& anyval) {
562 0 : if(anyval.getValueTypeClass() == uno::TypeClass_DOUBLE) {
563 0 : anyval >>= strike;
564 0 : } else if(anyval.getValueTypeClass() == uno::TypeClass_VOID) {
565 0 : strike=-1.0; // -1 as default (means not set)
566 : } else {
567 0 : return false;
568 : }
569 0 : return true;
570 : }
571 :
572 0 : bool getinput_inout(bs::types::BarrierKIO& kio, const OUString& str) {
573 0 : if(str.startsWith("i")) {
574 0 : kio=bs::types::KnockIn;
575 0 : } else if(str.startsWith("o")) {
576 0 : kio=bs::types::KnockOut;
577 : } else {
578 0 : return false;
579 : }
580 0 : return true;
581 : }
582 :
583 0 : bool getinput_barrier(bs::types::BarrierActive& cont, const OUString& str) {
584 0 : if(str.startsWith("c")) {
585 0 : cont=bs::types::Continuous;
586 0 : } else if(str.startsWith("e")) {
587 0 : cont=bs::types::Maturity;
588 : } else {
589 0 : return false;
590 : }
591 0 : return true;
592 : }
593 :
594 0 : bool getinput_fordom(bs::types::ForDom& fd, const OUString& str) {
595 0 : if(str.startsWith("f")) {
596 0 : fd=bs::types::Foreign;
597 0 : } else if(str.startsWith("d")) {
598 0 : fd=bs::types::Domestic;
599 : } else {
600 0 : return false;
601 : }
602 0 : return true;
603 : }
604 :
605 0 : bool getinput_greek(bs::types::Greeks& greek, const uno::Any& anyval) {
606 0 : OUString str;
607 0 : if(anyval.getValueTypeClass() == uno::TypeClass_STRING) {
608 0 : anyval >>= str;
609 0 : } else if(anyval.getValueTypeClass() == uno::TypeClass_VOID) {
610 0 : str="value";
611 : } else {
612 0 : return false;
613 : }
614 :
615 0 : if(str.compareToAscii("value")==0 || str.compareToAscii("price")==0 ||
616 0 : str.compareToAscii("v")==0 || str.compareToAscii("p")==0 ) {
617 0 : greek=bs::types::Value;
618 0 : } else if(str.compareToAscii("delta")==0||str.compareToAscii("d")==0) {
619 0 : greek=bs::types::Delta;
620 0 : } else if(str.compareToAscii("gamma")==0||str.compareToAscii("g")==0) {
621 0 : greek=bs::types::Gamma;
622 0 : } else if(str.compareToAscii("theta")==0||str.compareToAscii("t")==0) {
623 0 : greek=bs::types::Theta;
624 0 : } else if(str.compareToAscii("vega")==0||str.compareToAscii("e")==0) {
625 0 : greek=bs::types::Vega;
626 0 : } else if(str.compareToAscii("volga")==0||str.compareToAscii("o")==0) {
627 0 : greek=bs::types::Volga;
628 0 : } else if(str.compareToAscii("vanna")==0||str.compareToAscii("a")==0) {
629 0 : greek=bs::types::Vanna;
630 0 : } else if(str.compareToAscii("rho")==0||str.compareToAscii("r")==0) {
631 0 : greek=bs::types::Rho_d;
632 0 : } else if(str.compareToAscii("rhof")==0||str.compareToAscii("f")==0) {
633 0 : greek=bs::types::Rho_f;
634 : } else {
635 0 : return false;
636 : }
637 0 : return true;
638 : }
639 :
640 : } // namespace for auxillary functions
641 :
642 :
643 : // OPT_BARRIER(...)
644 0 : double SAL_CALL ScaPricingAddIn::getOptBarrier( double spot, double vol,
645 : double r, double rf, double T, double strike,
646 : double barrier_low, double barrier_up, double rebate,
647 : const OUString& put_call, const OUString& in_out,
648 : const OUString& barriercont, const uno::Any& greekstr ) throw( uno::RuntimeException, lang::IllegalArgumentException )
649 : {
650 : bs::types::PutCall pc;
651 : bs::types::BarrierKIO kio;
652 : bs::types::BarrierActive bcont;
653 : bs::types::Greeks greek;
654 : // read and check input values
655 0 : if( spot<=0.0 || vol<=0.0 || T<0.0 || strike<0.0 ||
656 0 : !getinput_putcall(pc,put_call) ||
657 0 : !getinput_inout(kio,in_out) ||
658 0 : !getinput_barrier(bcont,barriercont) ||
659 0 : !getinput_greek(greek,greekstr) ){
660 0 : throw lang::IllegalArgumentException();
661 : }
662 :
663 : double fRet=bs::barrier(spot,vol,r,rf,T,strike, barrier_low,barrier_up,
664 0 : rebate,pc,kio,bcont,greek);
665 :
666 0 : RETURN_FINITE( fRet );
667 : }
668 :
669 : // OPT_TOUCH(...)
670 0 : double SAL_CALL ScaPricingAddIn::getOptTouch( double spot, double vol,
671 : double r, double rf, double T,
672 : double barrier_low, double barrier_up,
673 : const OUString& for_dom, const OUString& in_out,
674 : const OUString& barriercont, const uno::Any& greekstr ) throw( uno::RuntimeException, lang::IllegalArgumentException )
675 : {
676 : bs::types::ForDom fd;
677 : bs::types::BarrierKIO kio;
678 : bs::types::BarrierActive bcont;
679 : bs::types::Greeks greek;
680 : // read and check input values
681 0 : if( spot<=0.0 || vol<=0.0 || T<0.0 ||
682 0 : !getinput_fordom(fd,for_dom) ||
683 0 : !getinput_inout(kio,in_out) ||
684 0 : !getinput_barrier(bcont,barriercont) ||
685 0 : !getinput_greek(greek,greekstr) ){
686 0 : throw lang::IllegalArgumentException();
687 : }
688 :
689 : double fRet=bs::touch(spot,vol,r,rf,T,barrier_low,barrier_up,
690 0 : fd,kio,bcont,greek);
691 :
692 0 : RETURN_FINITE( fRet );
693 : }
694 :
695 : // OPT_PRB_HIT(...)
696 0 : double SAL_CALL ScaPricingAddIn::getOptProbHit( double spot, double vol,
697 : double mu, double T,
698 : double barrier_low, double barrier_up ) throw( uno::RuntimeException, lang::IllegalArgumentException )
699 : {
700 : // read and check input values
701 0 : if( spot<=0.0 || vol<=0.0 || T<0.0 ) {
702 0 : throw lang::IllegalArgumentException();
703 : }
704 :
705 0 : double fRet=bs::prob_hit(spot,vol,mu,T,barrier_low,barrier_up);
706 :
707 0 : RETURN_FINITE( fRet );
708 : }
709 :
710 : // OPT_PROB_INMONEY(...)
711 0 : double SAL_CALL ScaPricingAddIn::getOptProbInMoney( double spot, double vol,
712 : double mu, double T,
713 : double barrier_low, double barrier_up,
714 : const uno::Any& strikeval, const uno::Any& put_call ) throw( uno::RuntimeException, lang::IllegalArgumentException )
715 : {
716 0 : bs::types::PutCall pc=bs::types::Call;
717 : double K;
718 :
719 : // read and check input values
720 0 : if( spot<=0.0 || vol<=0.0 || T<0.0 ||
721 0 : !getinput_putcall(pc,put_call) ||
722 0 : !getinput_strike(K,strikeval) ) {
723 0 : throw lang::IllegalArgumentException();
724 : }
725 :
726 0 : double fRet=bs::prob_in_money(spot,vol,mu,T,K,barrier_low,barrier_up,pc);
727 :
728 0 : RETURN_FINITE( fRet );
729 12 : }
730 :
731 :
732 :
733 : //------------------------------------------------------------------
734 :
735 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|