MATLAB: How to make the highlow plot function display profits in green and losses in red

MATLAB

How can I make the 'highlow' function plot profits in green, and losses in red?

Best Answer

This can be done in the following way.
% Load sample dataset - already exists in R2018a
load SimulatedStock.mat
% Get the closing values
closing = TMW_TB{:,4};
% Get the differences in closing values from each day
% This represents the net value - as it is the closing of one day
% subtracted from the value of the next day
% closing(1) is appeneded to the front to
% make diff_vals have the same size as closing
diff_vals = diff([closing(1);closing]);
% Positive values of diff_vals indicate a profit,
profits = diff_vals >= 0;
% Set date range to view in the graph - here, view the first 25 entries
range = 1:25;
% Use profits to filter TMW so that we only look at the profits
profit_table = TMW(profits(range),:);
% Plot the profits in green
highlow(profit_table,'g');
% Next get the loss values, which are whatever are not profits
% Repeat the same process to plot the losses, this time in red
losses_table = TMW(~profits(range),:);
hold on
highlow(losses_table,'r');