Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
5 registered members (Quad, AndrewAMD, Imhotep, TipmyPip, Edgar_Herrera), 809 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
unexpected behaviour with R bridge #476536
03/08/19 10:37
03/08/19 10:37
Joined: Jan 2017
Posts: 13
N
nocide Offline OP
Newbie
nocide  Offline OP
Newbie
N

Joined: Jan 2017
Posts: 13
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?

Re: unexpected behaviour with R bridge [Re: nocide] #476537
03/08/19 10:50
03/08/19 10:50
Joined: Jul 2000
Posts: 27,977
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,977
Frankfurt
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.

Re: unexpected behaviour with R bridge [Re: jcl] #476543
03/08/19 14:22
03/08/19 14:22
Joined: Jan 2017
Posts: 13
N
nocide Offline OP
Newbie
nocide  Offline OP
Newbie
N

Joined: Jan 2017
Posts: 13
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?

Re: unexpected behaviour with R bridge [Re: nocide] #476544
03/08/19 14:28
03/08/19 14:28
Joined: Jul 2000
Posts: 27,977
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,977
Frankfurt
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.

Re: unexpected behaviour with R bridge [Re: jcl] #476546
03/08/19 14:33
03/08/19 14:33
Joined: Jan 2017
Posts: 13
N
nocide Offline OP
Newbie
nocide  Offline OP
Newbie
N

Joined: Jan 2017
Posts: 13
Ok, many thanks! That helps!


Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1