Multiple assets with options

Posted By: GreenBoat

Multiple assets with options - 06/14/17 12:20

I am trying to use multiple assets for options trading.

I can't make it work. I would like to ask, if it's even possible?

It looks that I can't use the function contractUpdate with multiple assets?

My script is working fine, when I use it with one asset. When I add one more, there are errors like "Invalid contract parameters" or "Invalid contract".
Posted By: GreenBoat

Re: Multiple assets with options - 06/14/17 12:48

The script is as simple as possible:
Code:
void run() {
	set(PRELOAD|LOGFILE);
	BarPeriod = 1440;
	BarZone = ET;
	BarOffset = 15*60;
	StartDate = 20130101;
	EndDate = 20151231;
	PlotWidth = 1000;
	PlotHeight1 = 400;
	Multiplier = 100;

	assetList("Assets");

	if(is(FIRSTINITRUN)) {
		asset("SPY");
		dataLoad(1,"SPY_Options.t8",9);
		asset("YHOO");
		dataLoad(2,"YHOO_OptionsSim.t8",9);
	}

  	string assetName;
  	while (assetName = loop(Assets)) {
		asset(assetName);
		if (assetName == "SPY")	contractUpdate(assetName,1,CALL|PUT);
		else if (assetName == "YHOO")	contractUpdate(assetName,2,CALL|PUT);
		var CurrentPrice = ifelse(is(TRADEMODE),priceClose(),Contracts->fUnl); // the current underlying price (unadjusted)
		CONTRACT* c = contract(PUT|ONLYW3,30,CurrentPrice);
		if(contractDays(c) == 31 /*&& assetName == "YHOO"*/) enterShort();
  	}
  	
}

Posted By: jcl

Re: Multiple assets with options - 06/14/17 16:45

You can have as many assets as you want. The selected asset does not matter for contractUpdate, since you give the underlying name explicitely in the call. This is because the underlying name is sometimes different to the asset name.

Check how many contracts are in the chain and if the selected contract really exists.
Posted By: GreenBoat

Re: Multiple assets with options - 06/16/17 14:43

The script is working! Your answer helped me, thank you.

Another thing is, that I was getting "Crash in the script error", only once, at the first day after the lookback period.

After debugging, I found that:
- the error is there only when I use SPY data (from IVolatility)
- the error is not there when I use artificial historical data generated by financial-hacker script
- the error is caused by Contracts->fUnl
- the error is there when the code looks like this:
Code:
while (assetName = loop(Assets)) {
		if (assetName == "SPY") contractUpdate(assetName,1,CALL|PUT);
		else if (assetName == "YHOO") contractUpdate(assetName,2,CALL|PUT);
		asset(assetName);
		var CurrentPrice = ifelse(is(TRADEMODE),priceClose(),Contracts->fUnl);
......


when I select the asset before contractUpdate, there is no Crash in the script. My code without errors is:
Code:
while (assetName = loop(Assets)) {
		asset(assetName);
		if (assetName == "SPY") contractUpdate(assetName,1,CALL|PUT);
		else if (assetName == "YHOO") contractUpdate(assetName,2,CALL|PUT);
		var CurrentPrice = ifelse(is(TRADEMODE),priceClose(),Contracts->fUnl);

Posted By: jcl

Re: Multiple assets with options - 06/16/17 15:15

This code crashes when "Contracts" is zero. Which happens on all assets except SPY and YHOO. So don't use the Contracts pointer except when contractUpdate was called before and didn't fail.
Posted By: GreenBoat

Re: Multiple assets with options - 06/16/17 15:50

I forget to mention, that I have only two assets in the assets file:

Name,Price,Spread,RollLong,RollShort,PIP,PIPCost,MarginCost,Leverage,LotAmount,Commission,Symbol
SPY,215,0.02,0,0,0.01,0.01,0,4,1,0.02,
YHOO,215,0.02,0,0,0.01,0.01,0,4,1,0.02,

The second version of the code doesn't crash. Only the first version of the code causes crash, which is the version where asset(assetName) is after contractUpdate. And it crashes only once, one day after lookback period.
Posted By: jcl

Re: Multiple assets with options - 06/19/17 07:55

I've just checked it, and found that what I said above is wrong - the underlying must be in fact selected with asset() before and not after contractUpdate(). This will be added to the manual. Reason is that the option chain is individually stored per asset. So the second version of your script is the right one. When only two assets are in the assets file, it won't crash.
Posted By: GreenBoat

Re: Multiple assets with options - 06/19/17 07:59

Glad to hear that laugh
© 2024 lite-C Forums