%Iain to do Template

%Purpose:
% Create dummy data or load in historical data
% Calculate statistics for data
% Create an sample option (can be a spread)
% Value the option throughout its life
% Decide when to sell and when to keep

clear, clc, close all

%% Create dummy data or load in historical data

%Use Iain's loader

source = 3;
%1 for csv reader
%2 for sample data (saved)
%3 generate data

num_sims = 30;
num_data_points = 100;

switch source
    case 1
        [header, dates, data] = csvreader;
        prices = data(:,1);
    case 2
        load sampledata;
        prices = data(:,1);
    case 3
        prices = zeros(num_sims,num_data_points);
        for i = 1:num_sims
            prices(i,:) = brownian(num_data_points);
        end
        %modify brownian.m to change generation settings.
        
end
clear source;

movement = diff(prices);
N = length(prices);

[Cash,Rule] = lsm(prices);

Rule

r = 1.05;

[i,j] = ind2sub(size(Rule),find(Rule));
g = sortrows([i,j],1);
[del1,firstones,del2] = unique(g(:,1),'first');
firstones = g(firstones,2);

%Calculate cash flows

present_vals = zeros(num_sims,1);

for i = 1:size(firstones,1)
    Rule(i,firstones(i)+1:end) = 0;
    Cash(i,firstones(i)+1:end) = 0;
    
    present_vals(i) = Cash(i,firstones(i)) / (r)^(num_data_points/365);
    
end

option_value = mean(present_vals);















