I finished a simple script for Delta calculation. Feel free to use it:
#include <contract.c> // needed for options
function run()
{
BarPeriod = 1440; // 1 bar is 24 hours
BarZone = ET; // SPY is in New York
BarOffset = 15*60 + 50; // set the bar end to 15:50, 10 minutes before the market close
StartDate = 20161131;
EndDate = 20161231;
EndWeek = 52100;
LookBack = 256; // 256 = 1 year
set(PRELOAD+LOGFILE);
assetList("AssetsOnlyFew");
//assetHistory("SPY",FROM_YAHOO);
asset("SPY");
if(is(INITRUN)) {
initRQL(); // run R
dataLoad(1,"SPY_Options.t8",9);
}
if(is(LOOKBACK)) return;
// load today's contract chain
int N = contractUpdate(Asset,1,CALL|PUT);
Multiplier = 100;
if(!N) {
printf("#nNo contracts at bar %i",Bar);
return;
}
// determine the current underlying price (unadjusted)
var CurrentPrice = ifelse(is(TRADEMODE),priceClose(),Contracts->fUnl);
// Select contract and calculate delta and implied volatility
CONTRACT* CImp = contract(PUT|ONLYW3,1,CurrentPrice-2);
contractPrint(CImp,TO_LOG); // print contract values to the log file
var OptionPrice = contractPrice(CImp); // bid/ask average price
var HistVolAnnual = VolatilityOV(20);
var Delta = 0;
var ImpliedVol = contractVol(CImp,CurrentPrice,HistVolAnnual,OptionPrice,0,0); // calculate Implied volatility
print(TO_LOG,"nImpliedVol %g",ImpliedVol);
// contractVal (CONTRACT*, var Price, var HistVol, var Dividend, var RiskFree, var* Delta, var* Gamma, var* Vega, var* Theta, var* Rho): var
var ThValue = contractVal (CImp,CurrentPrice,HistVolAnnual,0.02,0.02,&Delta);
print(TO_LOG,"nDelta = %g",Delta);
print(TO_LOG,"nTheoretical value (contractVal) = %g",ThValue);
}
I would like to compare the outputs with some other source(s), so I can be sure that the calculation is correct.
I didn't bother to use the exact dividend and risk free rate for this first test. I believe that it's not that important (I use approximate fixed values).
I have ThinkOrSwim free account (it's probably the nicest platform for options trading and they have historical data with Greeks) and I am trying to compare my values with this source. If you know about any other source, I'll be happy to try it.
Bid and Ask prices are exactly the same from both data sources (I have historical data from IVolatility), so there shouldn't be any problem there. There might be some problem with historical volatility, because I can see that ThinkOrSwim doesn't have exactly the same underlying prices as I can see in Zorro log file. However, the differences are very small, like 219.57 (ThinkOrSwim) and 219.73 (Zorro).
It looks that the calculated implied volatility is (most of the time) almost exactly the same like in the ThinkOrSwim platform. Only Deltas are a bit too different in my opinion and I am thinking what might be the reason.
The question is, if it means that I need to improve my script. Maybe that ThinkOrSwim has some specific Delta calculation and I shouldn't use it for comparison at all.
Few examples:
Zorro log file:
2016-12-01,Put,20161216,217.5000,219.7300,1.8700,1.8400,9576
ImpliedVol 0.159051
Delta = -0.310674
ThinkOrSwim:
ImpliedVol 15.33
Delta -0.37
Zorro log file:
2016-12-06,Put,20161216,219.5000,221.5900,1.0500,1.0200,12594
ImpliedVol 0.129566
Delta = -0.283869
ThinkOrSwim:
ImpliedVol 12.82
Delta -0.32
Zorro log file:
2016-12-13,Put,20161216,226.0000,227.8600,0.8800,0.8400,12904
ImpliedVol 0.196424
Delta = -0.243288
ThinkOrSwim:
ImpliedVol 16.83
Delta -0.32
Zorro log file:
2016-12-15,Put,20161216,225.0000,226.8700,0.3600,0.3500,43434
ImpliedVol 0.217107
Delta = -0.130342
ThinkOrSwim:
ImpliedVol 15.23
Delta -0.24
Zorro log file:
2016-07-01,Put,20160715,208.0000,210.0300,1.1600,1.1500,73349
ImpliedVol 0.122174
Delta = -0.426875
ThinkOrSwim:
ImpliedVol 11.79
Delta -0.34
Zorro log file:
2016-07-22,Put,20160819,215.0000,217.0800,1.6700,1.6400,104783
ImpliedVol 0.106583
Delta = -0.394118
ThinkOrSwim:
ImpliedVol 10.76
Delta -0.36
Zorro log file:
2016-08-17,Put,20160819,216.5000,218.4000,0.2000,0.1900,25813
ImpliedVol 0.126256
Delta = -0.156243
ThinkOrSwim:
ImpliedVol 10.33
Delta -0.18