Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (AndrewAMD, VoroneTZ), 1,258 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Trading option Contract from runtime #479615
04/12/20 03:42
04/12/20 03:42
Joined: Mar 2020
Posts: 41
J
jbhunter Offline OP
Newbie
jbhunter  Offline OP
Newbie
J

Joined: Mar 2020
Posts: 41
With all of the recent options discussion and inspiration from the recent financial hacker blogs I have decided I need to learn more about options and backtesting them.
https://financial-hacker.com/algorithmic-options-trading/


I am trying to test some theories which led me to trying to calculate a CONTRACT on the fly. I have tried this similar to the first series of jcl's blog posts that describe how to load a dataSet with artificial option data from historic prices and contractVal().

Thinking that the CONTRACT needs to stay in memory I have just created a dataSet in INITRUN with some number larger then the number of contracts I plan on trading. Then I add sequentially to the dataSet with the last line in the below code. The contractPrint appears to be loading the struct correctly.

I am getting an error "Error 111: Crash in TMF at Bar ###". It happens on the following bar. Any thoughts on why this is happening? Any suggestions on a different method to calculate options in runtime?

Code
if(is(INITRUN))
{
    c = (CONTRACT*)dataNew(1,num,9);
}


then inside run() i use something similar to

Code

	vol = VolatilityOV(20);
	Price = series(price());
	barDate = ymd(wdate(0));
	//printf("\n BarDate=%i",barDate);
	
	printf("\n\n Entering option barDate=%i   BarNum=%i",barDate,Bar);
	
	c->time = dmy(barDate);
	c->fUnl = Price[0];
	c->fStrike = Price[0] +10;
	c->Type = CALL;
	c->Expiry = ymd(c->time+6*7);
	c->fBid = contractVal(c,Price[0],vol,Dividend,Interest);
	c->fAsk = (1.+BidAskSpread/100)*c->fBid + BidAskSpreadMin;
	contractPrint(c,TO_WINDOW);

	var prem = c->fBid*Lots*Multiplier;
	Commission = 0.65/Multiplier;
	MarginCost = contractMargin(c,2); //2==INDEX
	printf("\n contractMargin=%.2f",contractMargin(c,2));
	printf("\n ContractPrice=%.2f",contractPrice(c));
	printf("\n ContractIntrinsic=%.2f",contractIntrinsic(c,c->fUnl));
	printf("\n Contract=%i",c);
	printf("\nMarginCost=%.2f",MarginCost*100);

	enterShort(c);
	c++;
	



Last edited by jbhunter; 04/12/20 03:54.
Re: Trading option Contract from runtime [Re: jbhunter] #479616
04/12/20 04:13
04/12/20 04:13
Joined: Mar 2020
Posts: 41
J
jbhunter Offline OP
Newbie
jbhunter  Offline OP
Newbie
J

Joined: Mar 2020
Posts: 41
Already figured this one out.

I adapted the code from when I was entering a trade with a combo. Maybe the first arg in enterShort was setting a TMF parameter.

This seems to be working... But I am some other problems to work through.

if(combo(
c,-Lots,
0,0,
0,0,
0,0))
{
enterLong(comboLeg(1));
}

Re: Trading option Contract from runtime [Re: jbhunter] #479630
04/12/20 22:58
04/12/20 22:58
Joined: Mar 2020
Posts: 41
J
jbhunter Offline OP
Newbie
jbhunter  Offline OP
Newbie
J

Joined: Mar 2020
Posts: 41
This does not seem to be working out. I am not sure the CONTRACT is correctly selected for a trade and maybe missing some other required logic such as described in the help files for contract()

contract (int Type, int Expiry, var Strike): CONTRACT*
Selects the option or future contract from the current option chain that matches exactly the given Type, Expiry date, and Strike value. Returns a CONTRACT pointer to the found contract, or 0 when no such contract was found. If a contract is selected, enterLong and enterShort will buy or write (sell short) the contract instead of the underlying asset. The function sets up ContractStrike, ContractExpiry and other contract variables, and sets ContractRow to the row of the selected contract in the historical dataset or in the options chain. Calling asset(), contractUpdate(), or contract(0,0,0) deselects the contract and allows again trading with the underlying.

From the log files the trade does not look like it is updating the underlying price. In addition the option should have expired OTM with the profit of the premium. Not sure where +55.99 is coming from.


Code
[SPY::SP12304] Write 1 Put 20120217 134.0 100@7.05 Unl 127.53 at 15:40:00
...
[164: Fri 12-03-02 15:40] 10056 0 1/0  (137.33)
[SPY::SP12304] Expired ITM 1 Put 20120217 134.0 100@127.53: +55.99


Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1