function today_result = drawWilliamsR(hist_date, hist_open, hist_close, hist_low, hist_high, hist_vol)

dates = hist_date;
today_result = 0;

data = indicators([hist_high,hist_low,hist_close],'william',4)

will = fints(dates, data)

%just draw Williams %R.
p2 = plot(will,'r')
title('Williams %R')
legend(p2, 'Williams %R')
set(p2,'LineWidth', 2)
hold on
plot(will.dates, 90*ones(1, length(will)),...  
'color', [0.5 0 0], 'linewidth', 2)
plot(will.dates, 10*ones(1, length(will)),... 
'color', [0 0.5 0], 'linewidth', 2)

NumTicks = 18;
L = get(gca,'XLim');
set(gca,'XTick',linspace(L(1),L(2),NumTicks))
% set(get(gca,'XLabel'),'Rotation',270)
dateaxis('x', 2)


%----Will
filename = 'Willtestdata.xlsx';
filenameSim = 'WillSimulation.xlsx';
fileID = fopen('WilliamsBudget.txt','w');

low_limit = 10;
high_limit = 90;

flag = {};
flag{1} = 'calm';
flag{2} = 'calm';

low = hist_low
high = hist_high
open = hist_open
close = hist_close

fee = 0.2;

inTrade = 0;
inTradeOld = 0;

inTradeSell = 0;
inTradeBuy = 0;
typeOfTrade = 'calm';

dayClose = 0;
dayOpen = 0;

sell = 0;
oldSell = 0;
oldBuy = 1000000;
buy = 1000000;
stop = -1;

startBudget = 100000;
budget = 100000;
trend = 'no trend';

for i = 4:length(data)    
    last_value = data(i-1)
    pre_last_value = data(i - 2)
    pre_pre_last_value = data(i - 3)
   
    today = dates{i}
    
        % EMA13 Trend
%        if(datenum(today) < datenum('02-Mar-2014'))
%            trend = 'decreasing';
%        elseif(datenum(today) < datenum('18-May-2014'))
%            trend = 'increasing';
%        elseif(datenum(today) < datenum('26-Oct-2014'))
%            trend = 'decreasing';
%        elseif(datenum(today) < datenum('04-Jan-2015'))
%            trend = 'increasing';
%        elseif(datenum(today) < datenum('11-Jan-2015'))
%            trend = 'decreasing';
%        elseif(datenum(today) < datenum('18-Jan-2015'))
%            trend = 'increasing';
%        elseif(datenum(today) < datenum('08-Feb-2015'))
%            trend = 'decreasing';
%        elseif(datenum(today) < datenum('22-Feb-2015'))
%            trend = 'increasing';
%        elseif(datenum(today) < datenum('01-Mar-2015'))
%            trend = 'decreasing';
%        elseif(datenum(today) < datenum('08-Mar-2015'))
%            trend = 'increasing';
%        elseif(datenum(today) < datenum('29-Mar-2015'))
%            trend = 'decreasing';
%        elseif(datenum(today) < datenum('05-Apr-2015'))
%            trend = 'increasing';
%        elseif(datenum(today) < datenum('28-Jun-2015'))
%            trend = 'decreasing';
%        elseif(datenum(today) < datenum('26-Jul-2015'))
%            trend = 'increasing';
%        else
%            trend = 'decreasing';
%        end    


%     %Directional system
%      if(datenum(today) < datenum('06-Apr-2014'))
%          trend = 'decreasing';
%      elseif(datenum(today) < datenum('13-Apr-2014'))
%          trend = 'no ternd';
%      elseif(datenum(today) < datenum('18-May-2014'))
%          trend = 'increasing';
%      elseif(datenum(today) < datenum('25-May-2014'))
%          trend = 'no ternd';
%      elseif(datenum(today) < datenum('16-Nov-2014'))
%          trend = 'decreasing';
%      elseif(datenum(today) < datenum('23-Nov-2014'))
%          trend = 'no trend';
%      elseif(datenum(today) < datenum('30-Nov-2014'))
%          trend = 'increasing';
%      elseif(datenum(today) < datenum('14-Dec-2014'))
%          trend = 'no trend';
%      elseif(datenum(today) < datenum('04-Jan-2015'))
%          trend = 'increasing';
%      elseif(datenum(today) < datenum('11-Jan-2015'))
%          trend = 'no trend';
%      elseif(datenum(today) < datenum('28-Jun-2015'))
%          trend = 'decreasing';
%      elseif(datenum(today) < datenum('05-Jul-2015'))
%          trend = 'no trend';
%      elseif(datenum(today) < datenum('02-Aug-2015'))
%          trend = 'increasing';
%      elseif(datenum(today) < datenum('09-Aug-2015'))
%          trend = 'no trend';
%      else
%          trend = 'decreasing';
%      end

%MACD
%       if(datenum(today) < datenum('12-Jan-2014'))
%           trend = 'decreasing';
%       elseif(datenum(today) < datenum('19-Jan-2014'))
%           trend = 'increasing';
%       elseif(datenum(today) < datenum('02-Feb-2014'))
%           trend = 'decreasing';
%       elseif(datenum(today) < datenum('20-Apr-2014'))
%           trend = 'increasing';
%       elseif(datenum(today) < datenum('18-May-2015'))
%           trend = 'increasing';
%       elseif(datenum(today) < datenum('01-Jun-2014'))
%           trend = 'decreasing';
%       elseif(datenum(today) < datenum('12-Oct-2014'))
%           trend = 'decreasing';
%       elseif(datenum(today) < datenum('18-Jan-2015'))
%           trend = 'increasing';
%       elseif(datenum(today) < datenum('08-Feb-2015'))
%           trend = 'decreasing';
%       elseif(datenum(today) < datenum('08-Mar-2015'))
%           trend = 'increasing';
%       elseif(datenum(today) < datenum('29-Mar-2015'))
%           trend = 'decreasing';
%       elseif(datenum(today) < datenum('05-Apr-2015'))
%           trend = 'increasing';
%       elseif(datenum(today) < datenum('28-Jun-2015'))
%           trend = 'decreasing';
%       elseif(datenum(today) < datenum('26-Jul-2015'))
%           trend = 'increasing'; 
%       elseif(datenum(today) < datenum('02-Aug-2015'))
%           trend = 'decreasing';     
%       else
%           trend = 'decreasing';
%       end

% EMA13 - Out of sample 1.9.2015 - 1.5.2016
      if(datenum(today) < datenum('13-Sep-2015'))
          trend = 'decreasing';
      elseif(datenum(today) < datenum('20-Sep-2015'))
          trend = 'increasing';
      elseif(datenum(today) < datenum('27-Sep-2015'))
          trend = 'decreasing';
      elseif(datenum(today) < datenum('18-Oct-2015'))
          trend = 'increasing';
      elseif(datenum(today) < datenum('01-Nov-2015'))
          trend = 'decreasing';
      elseif(datenum(today) < datenum('08-Nov-2015'))
          trend = 'increasing';
      elseif(datenum(today) < datenum('06-Dec-2015'))
          trend = 'decreasing';
      elseif(datenum(today) < datenum('13-Dec-2015'))
          trend = 'increasing';
      elseif(datenum(today) < datenum('31-Jan-2016'))
          trend = 'decreasing';
      elseif(datenum(today) < datenum('07-Feb-2016'))
          trend = 'increasing';
      elseif(datenum(today) < datenum('27-Mar-2016'))
          trend = 'decreasing';
      elseif(datenum(today) < datenum('03-Apr-2016'))
          trend = 'increasing';
      elseif(datenum(today) < datenum('17-Apr-2016'))
          trend = 'decreasing';
      elseif(datenum(today) < datenum('24-Apr-2016'))
          trend = 'increasing';
      else
          trend = 'decreasing';
      end

    
    %Will. prekroci horni hranici, nasledne pod ni spadne a nasledne uz nad ni nedokaze vylezt - Medvedi divergence, velmi silny signal k prodeji
    if (pre_pre_last_value > high_limit && pre_last_value < high_limit && last_value <= high_limit)
        flag{i-1} = '!!! Bear divergence - sell !!!'     
    %Will. spadne pod dolni hranici, nasledne nad ni vyleze a nasledne uz %pod ni nespadne -  divergence, velmi silny signal k prodeji
    elseif(pre_pre_last_value < low_limit && pre_last_value > low_limit && last_value >= low_limit)
            flag{i-1} = '!!! Bull divergence - buy !!!'
    %prekoupeni - Will je nad horni hranici - prodavat
    elseif(last_value > high_limit) 
        flag{i-1} = 'Overbought - sell'
    %preprodani - Will je pod spodni hranici - nakupovat
    elseif(last_value < low_limit)
            flag{i-1} = 'Oversold - buy'
    else flag{i-1} = 'calm'
    end
 
 A = {'day', today, 'pre_pre_last_value', pre_pre_last_value, 'pre_last_value' , pre_last_value, 'last_value', last_value, 'flag', flag{i-1}}
 sheet = 1;
 xlswrite(filename,A,sheet,strcat('A',num2str(i)));
 
 % simulace obchodovani
 % hist_date, hist_open, hist_close, hist_low, hist_high, hist_vol
 %pokud indikator radi nakupovat, nastavim nakupni prikaz
 % na high + 1
 if((strcmp(flag{i-1}, '!!! Bull divergence - buy !!!') == 1) || (strcmp(flag{i-1}, 'Oversold - buy') == 1)) && (inTrade == 0)
     buy = high(i-1) + 1
     sell = 0
     typeOfTrade = 'BUY'
     stop = 1000000
 elseif((strcmp(flag{i-1}, '!!! Bear divergence - sell !!!') == 1) || (strcmp(flag{i-1}, 'Overbought - sell') == 1)) && (inTrade == 0)
 % pokud indikator radi prodavat, nastavim prodejni prikaz
 % na low - 1
     sell = low(i-1) - 1
     buy = 1000000
     typeOfTrade = 'SELL'
     stop = 0
 % else
   %  typeOfTrade = 'calm'
 end
 
 % dlouha pozice - nakup
 %vystup z dlouhe pozice - pokud dnes ceny uzavrou pod urovni stop loss, tak se obchod automaticky uzavre  
 if(inTradeBuy == 1) && ((close(i) <= stop) || (open(i) <= stop) || i == length(data))
     inTradeOld = inTrade
     inTradeBuy = 0
     inTrade = 0
     typeOfTrade = 'calm'
     dayClose = today
     budget = budget + ((stop - buy) * 50);
     sell = 0
     buy = 10000000
% vstup do dlouhe pozice - kdyz behem dne ceny protnou prikaz buy (vzrostou na hodnotu buy), tak automaticky vstoupim do obchodu   
 elseif((low(i) >= buy) || (high(i) > buy)) && (inTrade == 0) && (strcmp(trend, 'increasing') == 1) && (strcmp(typeOfTrade, 'BUY') == 1);
    inTradeOld = inTrade
    inTrade = 1;
    inTradeBuy = 1;
    dayOpen = today;
    %budget = budget - buy
    budget = budget - fee
    stop = (min(low(i), low(i-1))) -1
    oldBuy = buy    
 %setrvani v dlouhe pozici a posunuti stop lossu    
 elseif(inTradeBuy == 1)
    stop = low(i-1);
    oldBuy = buy
    inTradeOld = inTrade
 else
 % cekani na vstup do dlouhe pozice    
    oldBuy = buy 
    buy = high(i) + 1;
    %inTradeOld = inTrade
 end
 
 % kratka pozice (prodej)
 %vystup z kratke pozice - pokud dnes ceny uzavrou nad urovni stop loss, tak se obchod automaticky uzavre
 if(inTradeSell == 1) && ((close(i) >= stop) || (open(i) >= stop) || i == length(data))
     inTradeOld = inTrade
     inTradeSell = 0;
     inTrade = 0;
     typeOfTrade = 'calm';
     dayClose = today;
     budget = budget + ((sell - stop) * 50);
     sell = 0;
     buy = 10000000; 
 % vstup do kratke pozice - kdyz behem dne ceny protnou prikaz sell (klesnou na hodnotu sell), tak automaticky vstoupim do obchodu
  elseif((low(i) <= sell) || (high(i) < sell)) && (inTrade == 0) && (strcmp(trend, 'decreasing') == 1) && (strcmp(typeOfTrade, 'SELL') == 1);
     inTradeOld = inTrade
     inTrade = 1;
     inTradeSell = 1;
     dayOpen = today;
     %budget = budget + sell
     budget = budget - fee
     stop = (max(high(i), high(i-1))) + 1;
     oldSell = sell
 % setrvani v kratke pozici    
 elseif(inTradeSell == 1)
     stop = high(i-1)
     oldSell = sell
     inTradeOld = inTrade
 % cekani na vstup do kratke pozice    
 else
     oldSell = sell
     sell = low(i) - 1;
     %inTradeOld = inTrade
 end
 
 if(inTradeOld == 1) && (inTrade == 0)
    nbytes = fprintf(fileID,'day - %s, typeOfTrade - %s, budget - %f\n', today, typeOfTrade, budget);
    inTradeOld = inTrade;
 end    
 
 M = {'today', today, 'trend', trend, 'yesterdayFlag', flag{i-1}, 'dayOpen', dayOpen, 'dayClose', dayClose, 'inTradeOld', inTradeOld, 'inTrade', inTrade, 'typeOfTrade', typeOfTrade, 'inTradeSell', inTradeSell, 'inTradeBuy', inTradeBuy, 'budget', budget, 'budgetDiff', budget - startBudget, 'hist_low', hist_low(i), 'hist_high', hist_high(i), 'oldBuy', oldBuy, 'buy', buy, 'oldSell', oldSell, 'sell', sell, 'stop', stop, 'hist_open', hist_open(i), 'hist_close', hist_close(i)};
     sheet = 1;
     xlswrite(filenameSim,M,sheet,strcat('A',num2str(i)));
end

fclose(fileID);