unexpected behaviour with R bridge

Posted By: nocide

unexpected behaviour with R bridge - 03/08/19 10:37

Consider the following code, where I try to calculate value and volatility of options from the generated
SPY option chain.

Code:
// Zorro v1.9.6.4
#include <contract.c>

#define DTE      (42)
#define DIVIDEND (0.02)

function run()
{
	set(LOGFILE);
	BarPeriod = 1440; // 1 day bar
	LookBack  = 20;
	StartDate = 20171018;
	EndDate   = 20171022;

	assetList("AssetsIB");
	asset("SPY");
	
	// load todays' contract chain
	contractUpdate("SPYa", 0, CALL);
	
	if (is(LOOKBACK)) return;
	
	var Price    = priceClose();
	var RiskFree = yield() / 100;
	var HistVol  = VolatilityOV(20);
	
	// get first contract of today's bar
	CONTRACT* c = Contracts;
	var currVal = contractVal(c, Price, HistVol, DIVIDEND, RiskFree);
	//var currVol = contractVol(c, Price, HistVol, currVal, DIVIDEND, RiskFree);
	printf("<value=%.f>",currVal);
	//printf("<value=%.f> <volatility=%.f>",currVal, currVol);
}



I get following output:
[27: Wed 17-10-18 15:40] (255.72)<value=80>
[28: Thu 17-10-19 15:40] (255.79)<value=80>
[29: Fri 17-10-20 15:40] (257.11)<value=78>
[30: Mon 17-10-23 15:40] (256.10)<value=79>

and when I add contractVol,
Code:
var currVal = contractVal(c, Price, HistVol, DIVIDEND, RiskFree, 0, 0, 0, 0, 0);
var currVol = contractVol(c, Price, HistVol, currVal, DIVIDEND, RiskFree);
//printf("<value=%.f>",currVal);
printf("<value=%.f> <volatility=%.f>",currVal, currVol);



I get following output:
[27: Wed 17-10-18 15:40] (255.72)<value=80> <volatility=0>
[28: Thu 17-10-19 15:40] (255.79)<value=80> <volatility=0>
[29: Fri 17-10-20 15:40] (257.11)<value=0> <volatility=0>
[30: Mon 17-10-23 15:40] (256.10)<value=0> <volatility=0>

Why I get the zero's and why the different result, when I just add a new function call against the R bridge?
Posted By: jcl

Re: unexpected behaviour with R bridge - 03/08/19 10:50

The current RQuantLib version tends to crash when a function is called with bad parameters, f.i. negative volatility or a value that does not fit to strike and spot price. After a crash, all RQuantLib functions return 0.

Print out the parameters that you pass to contractVol. Try the same parameters in a R session. You'll then get an error message that might tell what the problem was.
Posted By: nocide

Re: unexpected behaviour with R bridge - 03/08/19 14:22

Ok, I've tracked down to this values and got following error:

> Type <- 'put'
> Strike <- 335
> Expiry <- 0.4
> Price <- 257
> Value <- 78.7
> Volatility<- 0.03
> Dividend <- 0.02
> RiskFree <- 0.01
> Vol <- AmericanOptionImpliedVolatility(Type,Value,Price,Strike,Dividend,RiskFree,Expiry,Volatility)
Fehler in americanOptionImpliedVolatilityEngine(type, value, underlying, :
root not bracketed: f[1e-007,4] -> [1.047432e-002,1.953335e+002]


Since Price+Value (257+78,7) is bigger than Strike (335),
this code in contract.c doesn't prevent the crash (and should probably be improved):
Code:
if((c->Type&PUT) && Price+Value <= Strike) return 0.;




So it seems, the value is to small for this contract?
Which part should be improved, to prevent this sort of problem?
Posted By: jcl

Re: unexpected behaviour with R bridge - 03/08/19 14:28

Avoid extreme strike distances. That's all advice that I have. It is not possible to improve the crash prevention without affecting valid values, since there is no simple rule that tells when the function crashes and when not. The more unusual the values, the more likely the crash. I hope future updates to RQuantlib will overcome this problem.
Posted By: nocide

Re: unexpected behaviour with R bridge - 03/08/19 14:33

Ok, many thanks! That helps!
© 2024 lite-C Forums