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

victor.johannes.holl
Newcomer
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')
Tagged:
0
Best Answer
-
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.00
Answers
-
thank you very much for your help. I have not thought about it like that.
0
Categories
- All Categories
- 3 Polls
- 6 AHS
- 36 Alpha
- 166 App Studio
- 6 Block Chain
- 4 Bot Platform
- 18 Connected Risk APIs
- 47 Data Fusion
- 34 Data Model Discovery
- 685 Datastream
- 1.4K DSS
- 615 Eikon COM
- 5.2K Eikon Data APIs
- 10 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- 3 Trading API
- 2.9K Elektron
- 1.4K EMA
- 252 ETA
- 556 WebSocket API
- 38 FX Venues
- 14 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 23 Messenger Bot
- 3 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 60 Open Calais
- 275 Open PermID
- 44 Entity Search
- 2 Org ID
- 1 PAM
- PAM - Logging
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 22 RDMS
- 1.9K Refinitiv Data Platform
- 652 Refinitiv Data Platform Libraries
- 4 LSEG Due Diligence
- LSEG Due Diligence Portal API
- 4 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.2K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 12 World-Check Customer Risk Screener
- 1K World-Check One
- 46 World-Check One Zero Footprint
- 45 Side by Side Integration API
- 2 Test Space
- 3 Thomson One Smart
- 10 TR Knowledge Graph
- 151 Transactions
- 143 REDI API
- 1.8K TREP APIs
- 4 CAT
- 27 DACS Station
- 121 Open DACS
- 1.1K RFA
- 104 UPA
- 193 TREP Infrastructure
- 228 TRKD
- 917 TRTH
- 5 Velocity Analytics
- 9 Wealth Management Web Services
- 90 Workspace SDK
- 11 Element Framework
- 5 Grid
- 18 World-Check Data File
- 1 Yield Book Analytics
- 46 中文论坛