findContract not finding a contract

Posted By: strimp099

findContract not finding a contract - 02/23/21 10:33

Greetings,

I'm finding some interesting behavior when using contractPrint() and findContract(). My goal is to find a contract that is near the money and close to TRADE_ENTRY_DTE days from expiration.

Consider the following simple script which loads options chains, iterates through the contracts and prints the contract records to a CSV file.

Code
void run() 
{

	StartDate = 20120101;
	EndDate = 20181231;
	BarPeriod = 1440;

	// optimized parameters
	var TRADE_ENTRY_DTE = 6*7; // days to expiration to enter trade

	History = "*.t8";
	assetList("AssetsOptions");
	asset("RUT");
		
	Multiplier = 100;
	
	// get the underlying closing price
	var p = priceClose();
	
	// load today's contract chain
	if(!contractUpdate(Asset,0,CALL|PUT)) return;
	printf("\n%s Asset=%s, contractsFound=%i",strdate(YMD,ldate(ET,0)),Asset,NumContracts);
	
	int i;
	for (i=0; i < NumContracts; i++)
	{
		CONTRACT* C = &Contracts[i];
		contract(C);
		print(TO_CSV,"\nRecord=%i; Time=%s, Type=%i; Expiry=%i; Strike=%f; Bid=%f; Ask=%f; Vol=%f; Unl=%f; Delta=%f;",i,strdate(YMD,ldate(ET,0)),ContractType,ContractExpiry,ContractStrike,ContractBid,ContractAsk,ContractVol,ContractUnl,ContractVal);
	}

	CONTRACT* initCall = contractFind(CALL,TRADE_ENTRY_DTE,p,6);
	
}


In the output CSV, I filter on 2011-09-08 trading day, call option strike=700 (near the underlying) and expiration 2011-10-21 (43 days to expiration). This contract is highlighted in the screenshot 1.PNG.

When I replace the print statement with contractPrint(C, TO_WINDOW), there is a slight variation in what is being printed. For example the fVal is not printed but otherwise the data is populated which should allow me to select the contract. See screenshot 2.PNG.

When I call contractFind as such

Code
CONTRACT* initCall = contractFind(CALL,TRADE_ENTRY_DTE,p,6);


No contract is ever found even though given 6*7=42 is sufficiently close to the minimum life in days (ie 43) and the strike is within 5.10 of the strike.

Why is contractFind not finding my contract?

Attached picture 1.PNG
Attached picture 2.PNG
Posted By: strimp099

Re: findContract not finding a contract - 02/24/21 07:33

I figured out why contractPrint is printing a zero for fVal. In my data, fVal represents delta which is bound -1.0 ... 1.0. In my dataset, only 4725 records out of 9613258 have delta either -1.0 or 1.0.

The contractPrint code (found in contract.c) casts the fVal variable to an int thereby rounding down (up) to 0 when -1.0<delta<1.0 which is the vast majority of my cases.

Code
void contractPrint(CONTRACT* c,int To)
{
	if(!c) return; 
	print(To,"\n%s,%s%s%s,%i,%.4f,%.4f,%.4f,%.4f,%i,%i,%s",
		ifelse(is(TRADEMODE),(char *)c,strdate("%Y-%m-%d",c->time)),
		ifelse(c->Type&FUTURE,"Future",""),
		ifelse(c->Type&PUT,"Put",""),
		ifelse(c->Type&CALL,"Call",""),
		c->Expiry,(var)c->fStrike,(var)c->fUnl,(var)c->fAsk,(var)c->fBid,(int)c->fVal,(int)c->fVol, // <--- (int)c->fVal
		ifelse(is(TRADEMODE),strcon(c),""));
}


This of course does not solve my contractFind issue...
Posted By: strimp099

Re: findContract not finding a contract - 02/25/21 07:47

I figured it out! CALL is defined as 1, PUT is defined as 2.

A European option is defined as 1<<2.

My data source includes the exercise type, European (E) or American (A).

In my data parsing script, contract type is defined as such:

Code
O->Type = ifelse(*PC == 'P',PUT,CALL) + ifelse(*EA == 'E',EUROPEAN,0);


This means for a European call option, type is 1 + 1 * 2^2 = 5 and a European put option type is 2 + 1 * 2^2 = 6.

When redfine CALL as 5 and PUT as 6, my contract is found!
Posted By: AndrewAMD

Re: findContract not finding a contract - 02/25/21 11:53

Or you could search for PUT|EUROPEAN and CALL|EUROPEAN. Probably best not to redefine standard Zorro macros.
© 2024 lite-C Forums