python - How can I change a month in a DateTime, using for loop (or better method )? -
revised question appropriate mcve:
as part of script i'm writing need have loop contains different pair of dates during each iteration, these dates first , last available stock trading dates of each month. have managed find calendar available dates in index despite research not sure how select correct dates index can used in datetime variables start
, end
. here far research has got me , continue search , build own solution post if manage find one:
from __future__ import division import numpy np import pandas pd import datetime import pandas_market_calendars mcal pandas_datareader import data web datetime import date ''' full date range: ''' startrange = datetime.date(2016, 1, 1) endrange = datetime.date(2016, 12, 31) ''' tradable dates in year: ''' nyse = mcal.get_calendar('nyse') available = nyse.valid_days(start_date='2016-01-01', end_date='2016-12-31') ''' loop needs take first , last trading date of each month: ''' dict1 = {} in available: start = datetime.date('''first available trade day of month''') end = datetime.date('''last available trade day of month''') diffdays = ((end - start).days)/365 dict1 [i] = diffdays print (dict1)
that because 1 january 2016 not trading day. check if right, try giving date 4 january 2016, following monday. if works, have more sophisticated dates ask for.
look in documentaion dm.bbgdatamanager()
. possible can ask dates available.
Comments
Post a Comment