Skip to content

Commit 48f6b33

Browse files
Merge pull request #120 from matteozanettii/mz
Add Bollinger Bands support to getYahoo function
2 parents 7c4a804 + ba37cc1 commit 48f6b33

1 file changed

Lines changed: 103 additions & 9 deletions

File tree

toolbox/utilities/getYahoo.m

Lines changed: 103 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@
114114
% possible values are:
115115
% 'candle' = candlestick chart (default option);
116116
% 'line' = close price only;
117-
% 'ma' = close price and moving averages.
117+
% 'ma' = close price and moving averages;
118+
% 'boll' = close price and Bollinger bands.
118119
% In this case, all suboptions associated with the selected
119120
% top panel mode remain at their default values.
120121
%
@@ -130,6 +131,10 @@
130131
% showMACrossovers=show moving average crossover markers
131132
% (default is true).
132133
%
134+
% The suboptions inside 'boll' are:
135+
% bollWindow = Window for the moving average (default 20).
136+
% bollNumStd = Number of standard deviations (default 2).
137+
%
133138
% The default values for 'line' are:
134139
% no additional suboptions.
135140
%
@@ -234,7 +239,7 @@
234239
% out.Message = message describing the result.
235240
% out.class = 'getYahoo'.
236241
%
237-
% See also: getTickers, getFundamentals, rsindex, candle, movavg
242+
% See also: getTickers, getFundamentals, rsindex, candle, movavg, bollinger
238243
%
239244
% References:
240245
%
@@ -409,6 +414,15 @@
409414
out = getYahoo('G.MI','topPanelMode',s);
410415
%}
411416

417+
%{
418+
% Define the Bollinger settings structure in the top panel.
419+
s = struct;
420+
s.Name = 'boll';
421+
s.bollWindow = 20; % Moving average window (default 20)
422+
s.bollNumStd = 2; % Number of standard deviations (default 2)
423+
out = getYahoo('G.MI', 'topPanelMode', s);
424+
%}
425+
412426
%{
413427
% RSI custom in the bottom panel.
414428
s = struct;
@@ -581,6 +595,11 @@
581595

582596
rocLen = 12;
583597

598+
% Default Bollinger
599+
bollWindow = 20;
600+
bollNumStd = 2;
601+
602+
584603
% Parse topPanelMode.
585604
% If topPanelMode is a char vector or string scalar, all suboptions remain
586605
% at their default values.
@@ -612,6 +631,9 @@
612631
case 'line'
613632
allowedTopFields = {'Name'};
614633

634+
case 'boll'
635+
allowedTopFields = {'Name','bollWindow','bollNumStd'};
636+
615637
otherwise
616638
error('FSDA:getYahoo:WngInp', ...
617639
'Unknown value for topPanelMode.Name: %s', topName);
@@ -643,6 +665,11 @@
643665
if isfield(STop,'widthFactor'), widthFactor = STop.widthFactor; end
644666
if isfield(STop,'upColor'), upColor = STop.upColor; end
645667
if isfield(STop,'downColor'), downColor = STop.downColor; end
668+
669+
case 'boll'
670+
if isfield(STop,'bollWindow'), bollWindow = STop.bollWindow; end
671+
if isfield(STop,'bollNumStd'), bollNumStd = STop.bollNumStd; end
672+
646673
end
647674
end
648675

@@ -973,6 +1000,7 @@
9731000
'maFast',maFast, ...
9741001
'maMid',maMid, ...
9751002
'maSlow',maSlow);
1003+
9761004
out(ii).Success = true;
9771005
out(ii).Message = "OK";
9781006

@@ -984,26 +1012,30 @@
9841012
bottomPanelMode, topRSI, lowRSI, topStoch, lowStoch, ...
9851013
topWilliams, lowWilliams, ...
9861014
rsiVals, stochK, stochD, macdLine, macdSignal, macdHist, ...
987-
williamsR, rocVals, maFast, maMid, maSlow,layoutHeights)
1015+
williamsR, rocVals, maFast, maMid, maSlow, ...
1016+
bollWindow, bollNumStd, layoutHeights)
9881017
end
9891018
end
9901019

9911020
if showPanelHelp
9921021
showPanelExplanationCommand(topPanelMode, bottomPanelMode, ...
9931022
maFastLen, maMidLen, maSlowLen, ...
9941023
rsiLen, stochLen, stochSmooth, macdFastLen, macdSlowLen, macdSigLen, rocLen, wrLen, ...
995-
topRSI, lowRSI, topStoch, lowStoch, topWilliams, lowWilliams);
1024+
topRSI, lowRSI, topStoch, lowStoch, topWilliams, lowWilliams, ...
1025+
bollWindow, bollNumStd);
9961026
end
9971027

9981028
end
1029+
9991030
function localPlotYahoo(TT, tickerNow, LastPeriod, intervalThis, ...
10001031
widthFactor, removeGaps, breakAtSession, nTicks, ...
10011032
topPanelMode, maFastLen, maMidLen, maSlowLen, showMACrossovers, ...
10021033
upColor, downColor, ...
10031034
bottomPanelMode, topRSI, lowRSI, topStoch, lowStoch, ...
10041035
topWilliams, lowWilliams, ...
10051036
rsiVals, stochK, stochD, macdLine, macdSignal, macdHist, ...
1006-
williamsR, rocVals, maFast, maMid, maSlow, layoutHeights)
1037+
williamsR, rocVals, maFast, maMid, maSlow, ...
1038+
bollWindow, bollNumStd, layoutHeights)
10071039

10081040
layoutHeights = layoutHeights(:)';
10091041
if numel(layoutHeights) ~= 3 || any(~isfinite(layoutHeights)) || any(layoutHeights <= 0)
@@ -1152,11 +1184,68 @@ function localPlotYahoo(TT, tickerNow, LastPeriod, intervalThis, ...
11521184
'HandleVisibility','off');
11531185
end
11541186

1187+
case 'boll'
1188+
1189+
[bollMid, bollUpper, bollLower] = bollinger(TT.Close, 'WindowSize', bollWindow, 'NumStd', bollNumStd);
1190+
1191+
1192+
xvec = x(:);
1193+
yvec = TT.Close(:);
1194+
bbMid = bollMid(:);
1195+
bbUp = bollUpper(:);
1196+
bbLow = bollLower(:);
1197+
1198+
% Set axis
1199+
hold(ax1,'on'); grid(ax1,'on');
1200+
1201+
% Up/Down colored segments: successive differences
1202+
d = [0; diff(yvec)];
1203+
upIdx = d >= 0;
1204+
dnIdx = d < 0;
1205+
1206+
% Sequential up/down color plotting
1207+
if any(upIdx)
1208+
plot(ax1, xvec(upIdx), yvec(upIdx), '-', 'Color', [0 0.6 0], 'LineWidth', 1.4);
1209+
end
1210+
if any(dnIdx)
1211+
plot(ax1, xvec(dnIdx), yvec(dnIdx), '-', 'Color', [0.85 0.15 0.15], 'LineWidth', 1.4);
1212+
end
1213+
1214+
% SMA and bands
1215+
plot(ax1, xvec, bbMid, '-','Color',[0 0.4470 0.7410],'LineWidth',1);
1216+
plot(ax1, xvec, bbUp, '--','Color',[0.3 0.3 0.3],'LineWidth',0.9);
1217+
plot(ax1, xvec, bbLow, '--','Color',[0.3 0.3 0.3],'LineWidth',0.9);
1218+
1219+
% Fill band (with NaN check)
1220+
validBands = ~isnan(bbUp) & ~isnan(bbLow);
1221+
xFill = xvec(validBands);
1222+
yUpFill = bbUp(validBands);
1223+
yLowFill = bbLow(validBands);
1224+
1225+
xpatch = [xFill; flipud(xFill)];
1226+
ypatch = [yUpFill; flipud(yLowFill)];
1227+
1228+
hPatch = patch('XData', xpatch, 'YData', ypatch, ...
1229+
'FaceColor',[0.6 0.6 0.6], 'FaceAlpha', 0.15, ...
1230+
'EdgeColor','none', 'Parent', ax1);
1231+
uistack(hPatch, 'bottom');
1232+
1233+
% Marker on the last point
1234+
plot(ax1, xvec(end), yvec(end), 'o', 'MarkerFaceColor',[0 0.4470 0.7410], 'MarkerEdgeColor','k');
1235+
1236+
% Legend
1237+
legend(ax1, {'Range','Price up','Price Down','SMA','Bands'}, 'Location', 'best');
1238+
11551239
otherwise
11561240
error('FSDA:getYahoo:WngInp','Unknown topPanelMode.');
11571241
end
11581242

1159-
ylim(ax1,'tight');
1243+
% ylim implemetation
1244+
if strcmp(lower(topPanelMode), 'boll')
1245+
ylim(ax1, 'padded');
1246+
else
1247+
ylim(ax1, 'tight');
1248+
end
11601249

11611250
%% Middle panel: volume
11621251
ax2 = axes('Position',[leftMargin y2 availW h2]);
@@ -1351,7 +1440,7 @@ function localPlotYahoo(TT, tickerNow, LastPeriod, intervalThis, ...
13511440
ax2.XTickLabel = [];
13521441
end
13531442

1354-
xlim(ax1,'tight');
1443+
xlim(ax1, 'tight');
13551444

13561445
%% Last labels
13571446
switch lower(bottomPanelMode)
@@ -1552,7 +1641,8 @@ function addBottomIndicatorRows(hObj, bottomPanelMode, ...
15521641
function showPanelExplanationCommand(topPanelMode, bottomPanelMode, ...
15531642
maFastLen, maMidLen, maSlowLen, ...
15541643
rsiLen, stochLen, stochSmooth, macdFastLen, macdSlowLen, macdSigLen, rocLen, wrLen, ...
1555-
topRSI, lowRSI, topStoch, lowStoch, topWilliams, lowWilliams)
1644+
topRSI, lowRSI, topStoch, lowStoch, topWilliams, lowWilliams, ...
1645+
bollWindow, bollNumStd)
15561646

15571647
fprintf('\n');
15581648
fprintf('============================================================\n');
@@ -1574,6 +1664,10 @@ function showPanelExplanationCommand(topPanelMode, bottomPanelMode, ...
15741664
fprintf(' - Fast MA = %d\n', maFastLen);
15751665
fprintf(' - Medium MA= %d\n', maMidLen);
15761666
fprintf(' - Slow MA = %d\n', maSlowLen);
1667+
case 'boll'
1668+
fprintf('Mode: Close + Bollinger Bands\n');
1669+
fprintf(' - Window length = %d\n', bollWindow);
1670+
fprintf(' - Number of Std = %.1f\n', bollNumStd);
15771671
end
15781672

15791673
fprintf('\nBOTTOM PANEL\n');
@@ -1654,4 +1748,4 @@ function showPanelExplanationCommand(topPanelMode, bottomPanelMode, ...
16541748
intervalOut = priority{idxAllowed};
16551749
end
16561750

1657-
%FScategory:UTI-FIN
1751+
%FScategory:UTI-FIN

0 commit comments

Comments
 (0)