getYahoo

getYahoo downloads financial time series from Yahoo Finance and optionally plots them using a customizable three-panel layout

Syntax

Description

getTickers, getYahoo and getFundamentals can be used jointly to build a complete workflow: from the selection of representative market tickers, to the retrieval and dynamic interactive plot of their price time series, and finally to the extraction of their fundamental financial information.

This function allows the user to choose the visualization shown in the top panel and the technical indicator shown in the bottom panel.

If options topPanelMode and bottomPanelMode are supplied as a character vector or string scalar, the selected visualization/statistic uses its default internal settings. If instead one of these options is supplied as a scalar struct, then it is possible to customize the suboptions associated with the selected visualization/statistic.

For background on financial data and market analysis, see:

Yahoo Finance API documentation https://finance.yahoo.com/

example

out =getYahoo(ticker) Single ticker with default options.

example

out =getYahoo(ticker, Name, Value) Two tickers, no plots.

Examples

expand all

  • Two tickers, no plots.
  • out=getYahoo({'G.MI','ENEL.MI'},'plots',false);

    Related Examples

    expand all

  • Daily data for one year with MACD in the bottom panel.
  • out=getYahoo("ENEL.MI",'interval','1d','bottomPanelMode','macd');

  • Close price with moving averages in the top panel.
  • Bottom panel hosts the RSI index.

    out=getYahoo("G.MI",'topPanelMode','ma','bottomPanelMode','rsi');

  • Multiple tickers.
  • ticker passed as a cell array of characters.

    ticker={'G.MI','ENEL.MI','ISP.MI'};
    out = getYahoo(ticker,'plots',false,'msg',false);

  • Auto interval fix.
  • out = getYahoo('ENEL.MI','LastPeriod','1y','interval','1m','plots',false);
    % Note that with 'autoFixInterval',false the previous examples fails
    % out = getYahoo('ENEL.MI','LastPeriod','1y','interval','1m','autoFixInterval',false,'plots',false);

  • Top panel occupies about half of the available plotting area.
  • out=getYahoo('G.MI','layoutHeights',[2 1 1]);

  • Strong emphasis on the price panel with RSI in the bottom panel.
  • out=getYahoo('ENEL.MI','topPanelMode','candle', ...
    'bottomPanelMode','rsi','layoutHeights',[3 1 1]);

  • Balanced layout with slightly smaller bottom indicator panel.
  • out=getYahoo('G.MI','topPanelMode','ma', ...
    'bottomPanelMode','macd','layoutHeights',[1.5 1 0.7]);

  • Intraday data with real time axis and larger top panel.
  • out=getYahoo('G.MI','LastPeriod','5d','interval','15m', ...
    'removeGaps',false,'layoutHeights',[2 1 1]);

  • Monthly data over many years with Williams R and custom panel heights.
  • out=getYahoo('ENEL.MI','LastPeriod','10y','interval','1mo', ...
    'bottomPanelMode','williamsr','layoutHeights',[2.5 1 1]);

  • Intraday with real time axis.
  • out = getYahoo('G.MI','LastPeriod','5d','interval','15m','removeGaps',false);

  • Intraday compressed.
  • out = getYahoo('G.MI','LastPeriod','5d','interval','15m','removeGaps',true,'breakAtSession',false);

  • More ticks on the x axis.
  • out = getYahoo('G.MI','LastPeriod','3mo','interval','1d','nTicks',12);

  • Long horizon of 10 years interval 1mo.
  • out = getYahoo('ENEL.MI','LastPeriod','10y','interval','1mo');

  • Options LastPeriod, with ytd.
  • out = getYahoo('ENEL.MI','LastPeriod','ytd','interval','1d');

  • Max history.
  • out = getYahoo('G.MI','LastPeriod','max','interval','1wk','plots',false);
    Processing ticker 1 of 1: G.MI
    Requested time interval is: 1wk for ticker G.MI
    Downloaded time interval is: 3mo for ticker G.MI
    

  • With help.
  • out = getYahoo('G.MI','showPanelHelp',true);
    Requested pair range=1y, interval=1m is not supported.
    Using interval=1d instead.
    
    Processing ticker 1 of 1: G.MI
    
    ============================================================
    EXPLANATION OF THE SELECTED PANELS
    ============================================================
    
    TOP PANEL
    ------------------------------------------------------------
    Mode: Candlestick chart
      - Open, High, Low, Close for each bar.
    
    BOTTOM PANEL
    ------------------------------------------------------------
    Indicator: Stochastic Oscillator
      - Lookback length = 14
      - Smoothing for %D = 3
      - Upper threshold = 70
      - Lower threshold = 30
    
    ============================================================
    
    
    Click here for the graphical output of this example (link to Ro.S.A. website)

  • Inspect output structure array.
  • tickers=["G.MI","ENEL.MI"];
    out = getYahoo(tickers,'plots',false);
    disp(tickers(1))
    disp(out(1))
    disp(tickers(2))
    disp(out(2))

  • Line chart in the top panel.
  • out = getYahoo('ENEL.MI','topPanelMode','line','bottomPanelMode','rsi');

  • Line Chart with personalized moving averages in the top panel and crossovers.
  • s = struct;
    s.Name = 'ma';
    s.maFastLen = 5;
    s.maMidLen  = 20;
    s.maSlowLen = 50;
    s.showMACrossovers = true;
    out = getYahoo('ENEL.MI','topPanelMode',s);
    Requested pair range=1y, interval=1m is not supported.
    Using interval=1d instead.
    
    Processing ticker 1 of 1: ENEL.MI
    
    Click here for the graphical output of this example (link to Ro.S.A. website)

  • Line Chart with moving averages in the top panel but no crossovers.
  • s = struct;
    s.Name = 'ma';
    s.showMACrossovers = false;
    out = getYahoo('ENEL.MI','topPanelMode',s);

  • Custom colors for the candles and the bars associated with the quantities.
  • s = struct;
    s.Name = 'candle';
    s.upColor   = [0 0.4 0.8];
    s.downColor = [0.8 0.4 0];
    out = getYahoo('G.MI','topPanelMode',s);

  • Wider candles in the top panel.
  • s = struct;
    s.Name = 'candle';
    s.widthFactor = 2;
    out = getYahoo('G.MI','topPanelMode',s);

  • RSI custom in the bottom panel.
  • s = struct;
    s.Name = 'rsi';
    s.rsiLen = 10;
    s.topRSI = 70;
    s.lowRSI = 30;
    out = getYahoo('G.MI','bottomPanelMode',s);

  • Stochastic custom in the bottom panel.
  • s = struct;
    s.Name = 'stoch';
    s.stochLen    = 10;
    s.stochSmooth = 5;
    out = getYahoo('G.MI','bottomPanelMode',s);

  • MACD custom in the bottom panel.
  • s = struct;
    s.Name = 'macd';
    s.macdFastLen = 8;
    s.macdSlowLen = 17;
    out = getYahoo('ENEL.MI','bottomPanelMode',s);

  • Williams R custom in the bottom panel.
  • s = struct;
    s.Name = 'williamsr';
    s.wrLen = 100;
    out = getYahoo('G.MI','bottomPanelMode',s);

  • ROC custom in the bottom panel.
  • s = struct;
    s.Name = 'roc';
    s.rocLen = 6;
    out = getYahoo('G.MI','bottomPanelMode',s);

  • Compare indicators in the bottom panel.
  • getYahoo('G.MI','bottomPanelMode','rsi');
    getYahoo('G.MI','bottomPanelMode','macd');

  • Intraday 1d with 1m.
  • out = getYahoo('ENEL.MI','LastPeriod','1d','interval','1m');

  • Full options stress test with personalized top and bottom panels.
  • stop = struct;
    stop.Name = 'ma';
    stop.maFastLen = 7;
    stop.maMidLen  = 21;
    stop.maSlowLen = 50;
    stop.showMACrossovers = true;
    sbottom = struct;
    sbottom.Name = 'rsi';
    sbottom.rsiLen = 14;
    sbottom.topRSI = 80;
    sbottom.lowRSI = 20;
    out = getYahoo('G.MI','LastPeriod','3mo','interval','1d', ...
    'topPanelMode',stop,'bottomPanelMode',sbottom);

  • Use output.
  • ticker='G.MI';
    out = getYahoo(ticker,'plots',false);
    TT = out.TT;
    plot(TT.t,TT.Close);
    title(ticker)

  • Combined use of getTickers, getYahoo and getFundamentals.
  • T = getTickers('market','London','Source','dynamic','nStocks',15);
    disp(T)
    % Retrieve price data
    out = getYahoo(T.ticker(2:end));
    % Retrieve valuation metrics
    F = getFundamentals(T.ticker(2:end),'Fields','valuation');
    disp(F)

    Input Arguments

    expand all

    ticker — Ticker symbol(s). Character, string or cell array of char.

    It can be:

    - a character scalar, for example 'G.MI' - a string scalar or string array, for example "G.MI" or ["G.MI" "ENEL.MI"] - a cell array of character vectors, for example {'G.MI','ENEL.MI'} The function downloads OHLCV data from Yahoo Finance for each supplied ticker and returns the results inside a structure array.

    Example - 'G.MI'

    Data Types: char | string | cell

    Name-Value Pair Arguments

    Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

    Example: 'showPanelHelp',true , 'autoFixInterval',false , 'LastPeriod','6mo' , 'interval','1d' , 'layoutHeights',[2 1 1] , 'removeGaps',false , 'breakAtSession',false , 'nTicks',10 , 'topPanelMode','ma' , 'bottomPanelMode','macd' , 'plots',false , 'msg',false

    showPanelHelp —Display textual explanation in Command Window.boolean.

    Default is false.

    Example: 'showPanelHelp',true

    Data Types: logical

    autoFixInterval —Replace invalid range/interval combinations automatically.boolean.

    Default is true.

    Example: 'autoFixInterval',false

    Data Types: logical

    LastPeriod —Period to analyze.character | string scalar.

    Possible values are:

    '1d' '5d' '1mo' '3mo' '6mo' '1y' '2y' '5y' '10y' 'ytd' 'max' Default is '1y'.

    Example: 'LastPeriod','6mo'

    Data Types: char | string

    interval —Sampling interval requested to Yahoo Finance.character | string scalar.

    Possible values are:

    '1m','2m','5m','15m','30m','60m','90m','1h','1d','5d', '1wk','1mo','3mo' Default is '1m'. If the pair LastPeriod/interval is not admissible, the interval is replaced automatically if option autoFixInterval=true.

    Example: 'interval','1d'

    Data Types: char | string

    layoutHeights —Relative heights of the three panels.numeric vector.

    Vector with three positive elements controlling the relative heights of:

    1) top panel = price / candles / moving averages;

    2) middle panel= volume;

    3) bottom panel= technical indicator.

    The actual heights are obtained by normalizing the vector so that its elements sum to 1.

    Default is [1 1 1], which gives the same height to the three panels.

    For example:

    - [2 1 1] makes the top panel occupy about half of the plotting area and the other two panels about one quarter each.

    - [3 1 1] gives even more emphasis to the top panel.

    - [1.5 1 0.7] gives moderate emphasis to the top panel and reduces the space of the bottom panel.

    Example: 'layoutHeights',[2 1 1]

    Data Types: double

    removeGaps —Remove night or weekend gaps in x-axis.boolean.

    If true, x-axis is based on the progressive index of the time series.

    If false, x-axis is based on actual datetime.

    Default is true.

    Example: 'removeGaps',false

    Data Types: logical

    breakAtSession —Add separators between sessions.boolean.

    Used only when removeGaps=true.

    Default is true.

    Example: 'breakAtSession',false

    Data Types: logical

    nTicks —Number of ticks shown on x-axis when removeGaps=true.positive integer scalar.

    Default is 8.

    Example: 'nTicks',10

    Data Types: double

    topPanelMode —Type of plot shown in the top panel.character, string scalar | scalar struct.

    If topPanelMode is a character vector or string scalar, possible values are:

    'candle' = candlestick chart (default option);

    'line' = close price only;

    'ma' = close price and moving averages.

    In this case, all suboptions associated with the selected top panel mode remain at their default values.

    The suboptions inside 'candle' are:

    widthFactor = Candle width scaling factor (default 1.5).

    upColor = Color for up candles and bars (default [0 0.7 0]).

    downColor = Color for down candles and bars (default [0.85 0 0]).

    The suboptions inside 'ma' are:

    maFastLen =Fast moving average length (default 10).

    maMidLen =Medium moving average length (default 30).

    maSlowLen =Show moving average length (default 60).

    showMACrossovers=show moving average crossover markers (default is true).

    The default values for 'line' are:

    no additional suboptions.

    If topPanelMode is a scalar struct, field Name specifies the selected visualization and the remaining fields control only the suboptions associated with that visualization.

    For example, the call out=getYahoo('G.MI','topPanelMode',topPanelMode);

    is admissible where topPanelMode is a struct with the following fields

    Value Description
    Name

    'ma';

    maFastLen

    15;

    maMidLen

    25;

    maSlowLen

    50;

    showMACrossovers

    true;

    Example: 'topPanelMode','ma'

    Data Types: char | string | struct

    bottomPanelMode —Type of indicator shown in the bottom panel.character, string scalar | scalar struct.

    If bottomPanelMode is a character vector or string scalar, possible values are:

    'rsi' = Relative Strength Index;

    'stoch' = Stochastic oscillator (default option);

    'macd' = MACD;

    'williamsr' = Williams %R;

    'roc' = Rate of Change.

    In this case, all suboptions associated with the selected bottom panel mode remain at their default values.

    The suboptions inside 'stoch' are:

    topStoch = Upper stochastic threshold (default 70).

    lowStoch = Lower stochastic threshold (default 30).

    stochLen = Stochastic lookback length (default 14).

    stochSmooth= Smoothing length for %D (default 3).

    The suboptions inside 'rsi' are:

    topRSI = Upper RSI threshold (default 80).

    lowRSI = Lower RSI threshold (default 20).

    rsiLen = RSI window length (default 14).

    The suboptions inside 'williamsr' are:

    topWilliams = Upper Williams %R threshold (default -30).

    lowWilliams = Lower Williams %R threshold (default -70).

    wrLen = Williams %R lookback length (default 14).

    The suboptions inside 'macd' are:

    macdFastLen = Fast EMA length (default 12).

    macdSlowLen = Slow EMA length (default 26).

    macdSigLen = Signal EMA length (default 9).

    The suboptions inside 'roc' are:

    rocLen = Rate of Change lag (default 12).

    If bottomPanelMode is a scalar struct, field Name specifies the selected indicator and the remaining fields control only the suboptions associated with that indicator.

    For example, the call out=getYahoo('G.MI','bottomPanelMode',bottomPanelMode);

    is admissible where bottomPanelMode is a struct with the following fields

    Value Description
    Name

    'rsi';

    rsiLen

    10;

    topRSI

    70;

    lowRSI

    30;

    Example: 'bottomPanelMode','macd'

    Data Types: char | string | struct

    plots —Produce plots.boolean.

    If true (default) the three-panel figure is created for each ticker. If false only the data are downloaded and stored in the output structure.

    Example: 'plots',false

    Data Types: logical

    msg —Display progress messages.boolean.

    If true (default), progress messages and warnings are shown.

    Example: 'msg',false

    Data Types: logical

    Output Arguments

    expand all

    out — description Structure

    Structure array containing the following fields

    Value Description
    Ticker

    ticker symbol.

    LastPeriod

    requested period.

    intervalRequested

    requested interval after validation.

    intervalActual

    actual data granularity returned by Yahoo.

    TimeZone

    exchange timezone.

    TT

    timetable with variables Open, High, Low, Close, Volume.

    Indicators

    structure containing RSI, StochK, StochD, MACD, MACDSignal, MACDHist, WilliamsR, ROC, maFast, maMid and maSlow.

    Success

    true if download and processing succeeded.

    Message

    message describing the result.

    class

    'getYahoo'.

    References

    Damodaran, A. (2012), "Investment Valuation: Tools and Techniques for Determining the Value of Any Asset", 3rd Edition, Wiley.

    Cochrane, J. H. (2023), "Asset Pricing, Revised Edition", Princeton University Press, Princeton.

    Koller, T., Goedhart, M., and Wessels, D. (2020), "Valuation: Measuring and Managing the Value of Companies, 7th Edition", Wiley, Hoboken.

    This page has been automatically generated by our routine publishFS