How To Calculate Williams %R using MATLAB?

by wayne.swaniawski , in category: General Help , 2 months ago

How To Calculate Williams %R using MATLAB?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by georgiana.senger , 2 months ago

@wayne.swaniawski 

Williams %R is a technical analysis indicator used to identify overbought and oversold conditions in the market. It is calculated using the following formula:


Williams %R = (Highest High - Close) / (Highest High - Lowest Low) * -100


In MATLAB, you can calculate Williams %R using the following steps:

  1. Load the historical price data of a stock or asset into a variable, such as priceData.
  2. Calculate the highest high and lowest low prices over a specified period, such as the past 14 days. You can use the functions max and min to find these values:
1
2
highestHigh = max(priceData(1:14)); % Highest high in the past 14 days
lowestLow = min(priceData(1:14)); % Lowest low in the past 14 days


  1. Calculate the Williams %R value for each day using the formula mentioned above. You can use a loop to iterate over the price data and calculate Williams %R for each day:
1
2
3
4
5
6
days = length(priceData);
WilliamsR = zeros(1, days);

for i = 1:days
    WilliamsR(i) = ((highestHigh - priceData(i)) / (highestHigh - lowestLow)) * -100;
end


  1. Plot the Williams %R values on a chart to visualize overbought and oversold conditions. You can use the plot function to create a line chart of the Williams %R values over time:
1
2
3
4
plot(1:days, WilliamsR);
xlabel('Day');
ylabel('Williams %R');
title('Williams %R Indicator');


By following these steps, you can calculate and visualize Williams %R using MATLAB for any stock or asset with historical price data.

by cathrine_goyette , 14 days ago

@wayne.swaniawski 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
% Sample historical price data (closing prices) for a stock or asset
priceData = [100, 105, 110, 108, 95, 93, 100, 98, 105, 110, 112, 115, 118, 120];

% Calculate highest high and lowest low over the past 14 days
highestHigh = max(priceData(1:14));
lowestLow = min(priceData(1:14));

% Calculate Williams %R for each day
days = length(priceData);
WilliamsR = zeros(1, days);

for i = 1:days
    WilliamsR(i) = ((highestHigh - priceData(i)) / (highestHigh - lowestLow)) * -100;
end

% Plot the Williams %R values
plot(1:days, WilliamsR);
xlabel('Day');
ylabel('Williams %R');
title('Williams %R Indicator');


You can run this MATLAB code with your actual historical price data to calculate and visualize the Williams %R indicator for any stock or asset. Make sure to adjust the length of the historical data window and customize the plot as needed based on your analysis requirements.