For a deeper look into our Eikon Data API, look into:

Overview |  Quickstart |  Documentation |  Downloads |  Tutorials |  Articles

question

Upvotes
Accepted
1 0 1 2

How to ignore days with no data while looping over a certain date range?

Hi,


I am trying to retrieve the daily stock prices from a certain stock. Below you can see my code. The problem is that on a certain day like weekend or holiday I am receiving the following error message which makes sense "NSUG.DE: No data available for the requested date range". Is there a possibility that I can ignore those days and my code does not run into an error?


#date modfication
start_date = datetime(2020, 1, 1, tzinfo=timezone.utc)
end_date = datetime(2020, 7, 10, tzinfo=timezone.utc)
date_range = pd.date_range(start=start_date, end=end_date, freq='D')
#RIC, fields, time to receive stock prices
rics = ['NSUG.DE']
fields = ['OPEN','HIGH','LOW','CLOSE','VOLUME']
for date in date_range:
    sdate = str(date)[0:10] + 'T07:00:00'
    edate = str(date)[0:10] + 'T22:01:00'
    df = ek.get_timeseries(rics=rics,
                           fields=fields,
                           start_date=sdate,
                           end_date=edate,
                           interval='minute')

    print(df)

    #safe to csv
    
    df.to_csv('test' + str(date)[0:10] + '.csv')
eikoneikon-data-apiworkspaceworkspace-data-apirefinitiv-dataplatform-eikonpythontime-series
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

1 Answer

Upvotes
Accepted
7.6k 15 6 9

@victor.johannes.holl

You might need to add a try-catch block to skip the error so it can continue log or print the data for the available date like below snippet of codes.

#suppress warning
import logging 
logger = logging.getLogger('pyeikon') 
logger.setLevel(logging.CRITICAL)

#date modfication
start_date = datetime(2020, 1, 1, tzinfo=timezone.utc)
end_date = datetime(2020, 7, 10, tzinfo=timezone.utc)
date_range = pd.date_range(start=start_date, end=end_date, freq='D')
#RIC, fields, time to receive stock prices
rics = ['NSUG.DE']
fields = ['OPEN','HIGH','LOW','CLOSE','VOLUME']
for date in date_range:
    try:
        sdate = str(date)[0:10] + 'T07:00:00'
        edate = str(date)[0:10] + 'T22:01:00'
        df = ek.get_timeseries(rics=rics,
                               fields=fields,
                               start_date=sdate,
                               end_date=edate,
                               interval='minute').dropna()

        print(df)

        #safe to csv

        df.to_csv('test' + str(date)[0:10] + '.csv')
    except ek.EikonError as err:
        print('Ignore Error Code:{0} Message:{1}'.format(err.code,err.message))

It will show the result like the following sample and still write the data to .csv file as you wish.

Ignore Error Code:-1 Message:NSUG.DE: No data available for the requested date range | 
NSUG.DE               OPEN   HIGH    LOW  CLOSE  VOLUME
Date                                                   
2020-01-02 08:03:00  800.0  800.0  800.0  800.0    22.0
2020-01-02 08:08:00  808.0  808.0  808.0  808.0     5.0
2020-01-02 11:12:00  804.0  804.0  804.0  804.0     2.0
2020-01-02 12:01:00  802.0  802.0  802.0  802.0     2.0
2020-01-02 12:02:00  800.0  800.0  800.0  800.0     1.0
2020-01-02 12:21:00  800.0  800.0  800.0  800.0    26.0
2020-01-02 15:16:00  816.0  816.0  816.0  816.0     4.0
2020-01-02 16:37:00  810.0  810.0  810.0  810.0     0.0
NSUG.DE               OPEN   HIGH    LOW  CLOSE  VOLUME
Date                                                   
2020-01-03 10:36:00  812.0  812.0  812.0  812.0    10.0
2020-01-03 11:48:00  802.0  802.0  802.0  802.0     2.0
2020-01-03 11:49:00  800.0  800.0  800.0  800.0    71.0
2020-01-03 12:32:00  800.0  800.0  800.0  800.0    30.0
2020-01-03 13:29:00  810.0  810.0  810.0  810.0    12.0
2020-01-03 16:23:00  810.0  810.0  810.0  810.0     4.0
2020-01-03 16:36:00  806.0  806.0  806.0  806.0     0.0



icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

thank you very much for your help. I have not thought about it like that.

Click below to post an Idea Post Idea