Historical closing price of stocks using API

Hemant
Hemant Newcomer

I have a dynamic set of stocks (RICs) for which I want to get the closing price data from say last 6 years on daily weekly and monthly level in USD. How Do I do that using LSEG APIs.

Answers

  • Jirapongse
    Jirapongse ✭✭✭✭✭

    @Hemant

    Thank you for reaching out to us.

    You can use the get_history method in the LSEG Data Library for Python to retrieve historical data.

    For example:

    df = ld.get_history(
        universe = ["IBM.N","MSFT.O","GOOG.O"],   
        start= "2024-01-01",
        end = "2025-03-01",
        interval="daily")
    
    df
    

    If you don't specify the fields parameter, it will return all available fields. Otherwise, you can specify the required fields in fields parameter, such as TRDPRC_1.

    df = ld.get_history(
        universe = ["IBM.N","MSFT.O","GOOG.O"],
        fields = ["TRDPRC_1"],
        start= "2024-01-01",
        end = "2025-03-01",
        interval="daily")
    
    df
    
    image.png

    You can change the start, end, and interval parameters and use the help command to list all parameters.

    help(ld.get_history)
    

    The examples are also available on GitHub.

  • Hemant
    Hemant Newcomer

    Thanks for the response @jirapongse. How do I make sure these closing prices are in USD?

  • Jirapongse
    Jirapongse ✭✭✭✭✭

    @Hemant

    I forgot about the currency. In this case, you can use the TR.ClosePrice field.

    df = ld.get_history(
        universe = ["IBM.N","MSFT.O","GOOG.O","PTT.BK"],
        fields = ["TR.ClosePrice(Curn=USD)"],
        start= "2024-01-01",
        end = "2025-03-01",
        interval="daily")
    
    df
    
  • Hemant
    Hemant Newcomer

    Thanks again @Jirapongse .

    We are not there yet.
    The following 2 are the excel formulas that I am using so far and want to now use APIs to get these data points.
    ———————————-——
    1) DSGRID(MASTER_LIST!$A3:$B3000,"1000*X(RI)~U$/MSACWF$(RI)","2022-01-01","","D","CustomHeader=true;CustHeaderDatatypes=RIC;RowHeader=true;ColHeader=true;DispSeriesDescription=false;YearlyTSFormat=false;QuarterlyTSFormat=false;Sym=RIC;MonthlyTSFormat=True","")


    2) @DSGRID(MASTER_LIST!$A3:$B3000,"X(RI)~U$","2022-01-01","","D","CustomHeader=true;CustHeaderDatatypes=RIC;RowHeader=true;ColHeader=true;DispSeriesDescription=false;YearlyTSFormat=false;QuarterlyTSFormat=false;Sym=RIC;MonthlyTSFormat=False","")
    ———————————-——

    With respect to the above two
    a) 1000*X(RI)~U$/MSACWF$(RI) : Here I believe we are trying to get a relative price of the constituents (in master_list a3-b3000) against an index.

    b) X(RI)~U$ : Here the absolute prices for the same instruments.

    This is exactly what I am looking for.

    My thought was

    df = ld.get_history(    universe = ["IBM.N","MSFT.O","GOOG.O","PTT.BK"],    fields = ["TR.ClosePrice(Curn=USD)"],    start= "2024-01-01",    end = "2025-03-01",    interval="daily")df
    

    For example will yield the answer atleast for the point I mentioned above in 2nd, but It wasnt really matching up.

    Can we try to convert 1) and 2) i.e. mentioned excel formulas into python code?

  • Jirapongse
    Jirapongse ✭✭✭✭✭

    @Hemant

    DSGRID is a Datastream product. You can use the Datastream Web Service to retrieve data.

    The DatastreamPy Python library is available on Pypi.

    The code looks like this:

    df = ds.get_data(tickers="@GOOGL,U:IBM", 
                     fields = [
                         "1000*X(RI)~U$/MSACWF$(RI)",
                         "X(RI)~U$"],
                     kind=1,
                     freq='D',
                     start="2022-01-01")
    df
    
    image.png
  • Hemant
    Hemant Newcomer

    I imagine that pricing data that you shared works on the mnemonics as the inputs, Correct? I remember in eikon there was a direct way to convert them from one symbol type to another, couldnt really find a way to convert RICs to Mnemonics in LSEG.data. Any workaround for that?

    I also tried to work with :
    response = symbol_conversion.Definition(
    symbols=["MSFT.O", "AAPL.O", "GOOG.O", "IBM.N"]
    ).get_data()

    response.data.df


    This doesnt rwally give mnemonics.




    2) For an entirely different problem,
    I am interested in getting the following data :
    (

    RIC', 'NAME', 'SECD', 'WTIDX', 'ISOCUR', 'MSCNSN', 'MSCNIGN', 'MSCNIN', 'MSCNSIN', 'MSCTRYN', 'WC06028'

    )

    I am able to get it on the list level as dictated by "LMSACWF0424", I want to replicate the same on stock (RIC) level
    df=ds.get_data(tickers='LMSACWF0424', fields=('RIC', 'NAME', 'SECD', 'WTIDX', 'ISOCUR', 'MSCNSN', 'MSCNIGN', 'MSCNIN', 'MSCNSIN', 'MSCTRYN', 'WC06028'), kind=0)

    Please suggest the best ways to do that.

  • Jirapongse
    Jirapongse ✭✭✭✭✭

    It also works with RICs by enclosing a RIC with the <…>. For example:

    df = ds.get_data(tickers="<MSFT.O>,<AAPL.O>,<GOOG.O>,<IBM.N>", 
                     fields = [
                         "1000*X(RI)~U$/MSACWF$(RI)",
                         "X(RI)~U$"],
                     kind=1,
                     freq='D',
                     start="2022-01-01")
    df