Python Forum
"if statement" and downloading a dataset
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"if statement" and downloading a dataset
#1
Dear Python Users,

I have the code below (attach). What I want to do is to prompt a user to enter a ticker from Quandl website (financial/economic data) so that the dataset is retrieved. One condition, the data should be of 1-month length starting from the time when a user enters ticker.Let'say if the program is run on 22/01/2018 so the data should be from 22/12/2017 till 22/01/2018 (That is the first thing I am struggling with). Second, I want to make sure that the ticker is entered correctly, so I want to make a condition such that "if the command "quandl.get("ticker", start_date=trim_start, end_date=trim_end)" does not retrieve the data- re-enter the ticker"
Please, advise me this issue.
    '''Installation steps - go to "Command line" and pass "pip install quandl"'''
     import quandl                        #To extract the data from Quandl website
     import matplotlib.pyplot as plt      #Plotting library
     import datetime
    def MainFormula():
    ticker = None 
    while ticker is None:
        ticker = input("Please, enter stock ticker (should be available in Quandl Website): ")
        try:
            trim_end=datetime.datetime.now()
            trim_start= trim_end - 30
            if quandl.get("ticker", start_date=trim_start, end_date=trim_end) == True:
                print("Here are the results: ")             
            else:            
                print("Please, enter correct stock ticker (check Quandl website)! ")
        except ValueError as e1:
            print("Please, enter correct alpha (should be float - example: 0.2) ")                 

     program = MainFormula()               
     print(program)
Reply
#2
import quandl                        
import matplotlib.pyplot as plt      
import datetime
def MainFormula(): #forgot to indent lines from 5 to 19?
    ticker = None 
    while ticker is None:
        ticker = input("Please, enter stock ticker (should be available in Quandl Website): ")
        try:
            trim_end=datetime.datetime.now()
            trim_start= trim_end - datetime.timedelta(30) #this is the correct math operation on datetime obj 
            if quandl.get(ticker, start_date=trim_start, end_date=trim_end) == True:  #ticker is variable,not string??
                print("Here are the results: ") 
                return #should return something from quandl related infos which will be printed in print(program) line???            
            else:            
                print("Please, enter correct stock ticker (check Quandl website)! ")
                ticker = None  #reset ticker to continue looping
        except ValueError as e1:
            print("Please, enter correct alpha (should be float - example: 0.2) ")
            ticker = None  #reset ticker to continue looping

program = MainFormula()
print (program)
few corrections i've made which i think not related to quandl website but the python code itself. check out the corrected lines marked with # or comment line.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Downloading images from webpages H84Gabor 2 1,879 Sep-29-2021, 05:39 PM
Last Post: snippsat
  Downloading a module Xlsxwriter dan789 6 11,224 Jan-26-2019, 02:13 PM
Last Post: dan789
  Downloading and using pyperclip PMPythonlearner 2 5,043 Dec-31-2017, 04:37 PM
Last Post: PMPythonlearner
  Problem downloading 2.7.8 Mac OSX Benjipincus 2 3,026 Dec-18-2017, 01:33 PM
Last Post: snippsat
  issues downloading xlsxwrite library tenichols 3 9,260 Jun-01-2017, 09:13 PM
Last Post: snippsat
  FTP not downloading files but showing success python_lover 2 4,421 Jan-25-2017, 02:31 PM
Last Post: python_lover

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020