Gamestudio Links
Zorro Links
Newest Posts
No connection to MT4
by JeyKey II. 10/29/26 09:34
Zorro plugin for Hyperliquid.xyz
by opm. 10/28/26 13:50
Icon "OP"
by JeyKey II. 10/27/26 07:03
ZorroGPT
by TipmyPip. 10/26/26 08:09
Chat Room for Zorro Programmers
by Haitao_Ren. 10/25/26 03:14
Trading Journey
by Haitao_Ren. 10/25/26 03:14
AUM Magazine
Latest Screens
Dorifto samurai
Shadow 2
Rocker`s Revenge
Stug 3 Stormartillery
Who's Online Now
2 registered members (TipmyPip, JeyKey II), 12,626 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
Haitao_Ren, JimSimons, julienp, julienper, KelleyAbbey
19247 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: 28,145
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 28,145
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: 28,145
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 28,145
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 | 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