0

Revised question with appropriate MCVE:

As part of a script I'm writing I need to have a loop that contains a different pair of dates during each iteration, these dates are the first and last available stock trading dates of each month. I have managed to find a calendar with the available dates in an index however despite my research I am not sure how to select the correct dates from this index so that they can be used in the DateTime variables start and end. Here is as far as my research has got me and I will continue to search for and build my own solution which I will post if I manage to find one:

from __future__ import division
import numpy as np
import pandas as pd
import datetime
import pandas_market_calendars as mcal
from pandas_datareader import data as web
from datetime import date
'''
Full date range:
'''
startrange = datetime.date(2016, 1, 1)
endrange = datetime.date(2016, 12, 31)
'''
Tradable dates in the year:
'''
nyse = mcal.get_calendar('NYSE')
available = nyse.valid_days(start_date='2016-01-01', end_date='2016-12-31')
'''
The loop that needs to take first and last trading date of each month:
'''
dict1 = {}
for i in available:
    start = datetime.date('''first available trade day of the month''')
    end = datetime.date('''last available trade day of the month''')
    diffdays = ((end - start).days)/365
    dict1 [i] = diffdays
    print (dict1)
  • 1
    See [Get Last Day of the Month in Python](https://stackoverflow.com/questions/42950/get-last-day-of-the-month-in-python). For your main error, you don't give enough information and it's difficult to reproduce your problem. See how to create a [mcve] – Thierry Lathuille Jul 28 '17 at 08:38
  • The Problem is that my code has to fetch data from Bloomberg which requires a subscription. But I can't be the only person on stackoverflow who uses this... right? – Louis Falconer-Flavin Jul 28 '17 at 08:44
  • Negative, Sir. You can always **create a `def proxyBloomberg(...):`** code, that will serve the data as if were Bloomberg called directly. Just pre-load your data ( you out of any question can do it ) **and provide that proxy altogether with relevant data in your MCVE.** That simple, MCVE is not The MCVE, if it lacks a completeness ( including a set of self-contained data to re-run the MCVE-code ... by definition ... ). – user3666197 Jul 28 '17 at 10:50
  • @user3666197 Ok that's interesting to know, however I am working on converting my script to fetching from google finance so that everyone can try it without having to pay for bloomberg. – Louis Falconer-Flavin Jul 28 '17 at 10:59
  • Missing all such information in your post. **Would you mind to read about how to ask the MCVE-based questions** ? StackOverflow encourages users to present a **M**inimum ( efficiency ) + **C**omplete ( self-contained -- Yes -- with data ) + **V**erifiable ( ready for re-runs ) + **E**xamples ( a full example, with all details+data, to allow for re-testing ) of code, that you struggle to make work.**The best next step is to learn about this Community practice+ revise & complete your MCVE above**. Anyway, welcome in this great Community of Knowledge & become our active, contributing member. – user3666197 Jul 28 '17 at 11:06
  • @user3666197 Would it help if I also provided my WORKING script for doing the same job but for a set start and end date? – Louis Falconer-Flavin Jul 28 '17 at 11:09
  • StackOverflow is definitely neither a "social-platform" nor a "write-my-piece-of-code-for-free", so the Community members would like to see your own efforts ( both research & algorithmisation-wise ) that you have put into the finding a feasible solution. Resorting to say 28th-day is EoM, or yelling *(cit.:)* "ERRORS!" is definitely not the way to go. If your quant-model cannot do better, it can still resort to Python `try: except: finally:` handling of anticipated errors, so work harder & experiment a lot, before starting a seriously meant question, ok? – user3666197 Jul 28 '17 at 11:13
  • @user3666197 I genuinely thought this was a platform to help find answers to coding problems. I also wasn't aware that I was harming or upsetting anyone by typing _(cit.:)_"ERRORS!" in caps... but I apologise if I did. I was simply discussing the topic with other users. I don't want a full complete free script, I just wanted to discuss the possibilities to solve what's missing. If someone is not interested in helping they can just ignore the thread. I am trying to also provide all the work that I've done which I have worked hard on, a stranger telling me to 'work harder' is frankly insulting. – Louis Falconer-Flavin Jul 28 '17 at 11:27
  • Negative, Sir. It ought be a common step to first read **any** Community Rules & to follow such Community Netiquette. Complaining before complying with shared culture & rules would never help become more familiar with the Community. Feel free to read a lot in the Community Best Practices, before accusing seasoned members of not helping you the way you wish, rather learn how to cooperate within the Community standards. **That works.** Hope this provides you both directions & many inspirations for your further work. **The best next step is to learn about this Community + revise your MCVE above** – user3666197 Jul 28 '17 at 11:33
  • @user3666197 Should I just delete this question completely then? – Louis Falconer-Flavin Jul 28 '17 at 11:36
  • Why? **Improve it.** The best next step is to learn about this Community + revise your MCVE above. Reduce frequent commenting instead of own code-improvement efforts. Search + read so many professional StackOverflow answers on topics that can relate to your own code-issues and again improve your solution. This is called a life-long learning and this Community members are indeed great in this. Good Luck, man. – user3666197 Jul 28 '17 at 11:41
  • @user3666197 I have sincerely tried to improve my question to be more focused and would honestly appreciate some constructive feedback from an experienced member. Maybe open a chat? – Louis Falconer-Flavin Jul 28 '17 at 12:11

1 Answers1

0

That is probably because 1 January 2016 was not a trading day. To check if I am right, try giving it the date 4 January 2016, which was the following Monday. If that works, then you will have to be more sophisticated about the dates you ask for.

Look in the documentaion for dm.BbgDataManager(). It is possible that you can ask it what dates are available.

BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • Yh I was hoping there would be a way to implement some kind of calendar that it can check for the earliest and latest trading day of each month. – Louis Falconer-Flavin Jul 28 '17 at 08:39
  • However I do think you are correct with the date not being a trading day as it now happens on the next month but just for the end date... so I guess I really do need some kind of calendar xD – Louis Falconer-Flavin Jul 28 '17 at 08:41
  • Maybe you can extract the first and last valid days of the month by looking at your data. You could also test if your start date is valid, and try the next day until you reach a valid one. – Thierry Lathuille Jul 28 '17 at 08:51
  • I would be amazed if there is not some function in `tia.bbg.datamgr` that returns trading calendars. It's a fundamental requirement when you work with trading data. – BoarGules Jul 28 '17 at 09:38
  • @BoarGules you are correct, if I print a set of data I can see the dates skip weekends and holidays, however because I am fetching the date that I am determining if data doesn't exist for that specific day then ERROR! So maybe l need to just make the script look for the next available day with data after the first of the month or before the last? – Louis Falconer-Flavin Jul 28 '17 at 10:04