%% Example for Stock and Option Prices

%% Stock prices
% We look at IBM stock prices for July to October 2016.
%
% Compute the log returns from the daily closing prices.

[so,sc] = get_stock_plot('IBM',[2016 7],[]); % from July 1 2016 to now
                                            % sc are closing prices

r = log(sc(2:end)./sc(1:end-1));            % daily log returns
figure(2); stem(r,'r.'); grid on
title('daily log returns')

%% Option prices
% Get the current IBM option prices and plot them:

data = getOptionPrices('IBM')
option_plot(data)

%% Call option prices with maturity June 2017
% We get option data with 3 expiration dates. For the 2nd 
% expiration date: make a table and a plot of call option price vs. strike

S0 = data.stockprice   % current stock price 
option = data.opt(2);  % option prices for 2nd expiration date
option.expir           % expiration date
K = option.cstrike;    % vector of strike values for call option
V = option.cprice;     % vector of call option prices
[K',V']                % table of call option prices
plot(K,V,'.-',K,max(S0-K,0),'r--')
xlabel('strike K')
legend('call option price','payoff for current stock price S_0')

%% Find implied volatility 
% For the strike K0=150 use the call option price to find the implied volatility sigma.
%
% We obtain an implied volatility sigma=16.4%.

T = years(option.expir-data.time) % time between expir. of option and time of data in years
K0 = 150;
i = find(K==K0)         % find i with K(i)=150
V0 = V(i)               % corresponding option price
rc = 0;                 % use interest rate rc=0
f = @(sigma) BlackScholes(S0,K0,rc,sigma,T)-V0; 
sigma = fzero(f,0.3)    % find implied volatility sigma

