- Flip array up to down using 'flipupd' function: https://www.mathworks.com/help/matlab/ref/flipud.html
- Find graphics objects with specific properties: https://www.mathworks.com/help/matlab/ref/findobj.html
- Candlestick chart using 'candle' function: https://www.mathworks.com/help/finance/candle.html
Updated solution to red and green candles for candle function?
11 views (last 30 days)
Show older comments
I'm attempting to plot the candle stick chart with the following code:
% Plot the candlestick chart
data = [currentTimetable.open, currentTimetable.high, currentTimetable.low, currentTimetable.close];
figure(1);
h = candle(data);
for i = 1:length(h)
if data(i,4) >= data(i,1)
h(i).Color = 'green'; % green
else
h(i).Color = 'red'; % red
end
end
ax = gca;
ax.Color = 'w';
title('Candlestick chart for ESMini')
hold on;
xlabel('Time');
ylabel('Price');
grid on;
This it the error I receive:
Unrecognized property 'Color' for class 'matlab.graphics.primitive.Patch'.
h(i).Color = 'red'; % red
^^^^^^^^^^^^^^^^^^^^^^
I've tried to use the property 'FaceColor' instead of 'Color', but then I receive the error:
Unrecognized property 'FaceColor' for class 'matlab.graphics.chart.primitive.Line'.
p(i).FaceColor = [1 0 0]; % red
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Is there a way to accomplish this without writing a modified candle function?
Thanks!
0 Comments
Answers (1)
Rahul
on 23 Jun 2025
I understand that you are trying to customize candlestick colors in MATLAB, based on whether the 'close' price is above or below the 'open' price, and you're encountering an error when trying to set the 'Color' or 'FaceColor' properties on the object returned by the 'candle' function.
This issue can occur because the 'candle' function returns a single 'CandlestickChart' object, rather than an array of individual elements. As a result, trying to index into this object, using 'h(i)', and modifying properties like 'Color' or 'FaceColor' leads to errors.
To color the candlestick bodies conditionally, you can use the 'findobj' function to access the underlying 'Patch' objects that represent the candle bodies, and then set their color based on your specific data.
In the original 'for' loop, MATLAB attemps to try and index into 'h' and set its color, but since 'h' is a single object, this throws an error. Here is how you can modify your existing MATLAB script to achieve the same:
patchHandles = findobj(gca, 'Type', 'Patch');
patchHandles = flipud(patchHandles); % Ensure correct ordering
for i = 1:length(patchHandles)
if data(i,4) >= data(i,1)
patchHandles(i).FaceColor = [0 0.7 0]; % green
else
patchHandles(i).FaceColor = [0.85 0 0]; % red
end
end
The above-mentioned approach should allow the plot to color each candlestick body appropriately without needing to modify the behavior of the built-in 'candle' function.
To know more about the usage of various functions mentioned in the previous code snippet, you can refer to the following documentation links:
Hope this helped!
0 Comments
See Also
Categories
Find more on View and Analyze Simulation Results in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!