Python Forum
"if statement" and downloading a dataset - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: "if statement" and downloading a dataset (/thread-7707.html)



"if statement" and downloading a dataset - Alberto - Jan-21-2018

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)



RE: "if statement" and downloading a dataset - ka06059 - Jan-25-2018

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.