#include <contract.c> // needed for options
#include <r.h>
/* Test of R */
function run()
{
BarPeriod = 1440; // 1 bar is 24 hours
StartDate = 20060101;
EndDate = 20061231;
set(PRELOAD|LOGFILE|PLOTNOW);
PlotHeight1 = 1024; // height of the chart with results
PlotWidth = 1700;
assetList("AssetsIB");
assetHistory("SPY",FROM_YAHOO); // ticker
asset("SPY");
if(is(INITRUN)) {
initRQL(); // run R
dataLoad(1,"SPY_Options.t8",9);
}
// load today's contract chain
contractUpdate(Asset,1,CALL|PUT);
Multiplier = 100;
// prices
vars Price = series(price());
vars Close = series(priceClose());
vars Vol = series(Volatility(Price,20));
if(is(LOOKBACK)) return;
// determine the current underlying price (unadjusted)
var CurrentPrice = Contracts->fUnl;
// choose contract
CONTRACT* CImp = contract(PUT|ONLYW3,20,CurrentPrice);
// Implied Volatility test
var OptionPrice = contractPrice(CImp); // bid/ask average price
var ImpliedVol = contractVol(CImp,CurrentPrice,Vol[0],OptionPrice,0,0); // calculate Implied volatility
printf("#nImpliedVol %i ",ImpliedVol);
}