2

I have a code below that tries to ask a user to enter 5 countries so that in return she gets the data for stock index price of that country (Say if she enters USA in return she gets info for S&P 500). I use error-handling properties so that if there is no country with the index it asks the user to re-enter the country. The problem is that the code does not work and I am stuck. Can you please, help me with it.

import quandl                     #To extract the data from Quandl website
from quandl.errors.quandl_error import NotFoundError #for error handling 
from datetime import date, timedelta                 # to define the date and time, while "timedelta" - for 1 month back
import matplotlib.pyplot as plt      #Plotting library
import pandas as pd                  #Time series working library
from scipy import interpolate        #Scipy library for interpolation 
import numpy as np
from scipy.optimize import leastsq

def MainFormula():
    exchange1, exchange2, exchange3, exchange4, exchange5  = None    
    while exchange1 is None or exchange2  is None or exchange3  is None or exchange4  is None or exchange5  is None:
      exchange1, exchange2, exchange3, exchange4, exchange5 = input('Please, enter 5 countries for obtaining corresponding stock indices (with a comma in between): ').split(',')
      try:
        exchange1 = str(exchange1)  #ensure that entered exchage is string type
        exchange2 = str(exchange2)  #ensure that entered exchage is string type
        exchange3 = str(exchange3)  #ensure that entered exchage is string type
        exchange4 = str(exchange4)  #ensure that entered exchage is string type
        exchange5 = str(exchange5)  #ensure that entered exchage is string type
        if exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "France":
            ticker = "CHRIS/LIFFE_FCE1"
        elif exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "USA":
            ticker = "MULTPL/SP500_REAL_PRICE_MONTH"
        elif exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "Germany":
            ticker = "CHRIS/EUREX_FDAX1"
        elif exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "Hong_Kong":
            ticker = "CHRIS/HKEX_HSI1"    
        elif exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "India":
            ticker = "NSE/CNX_NIFTY"      
        elif exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "Japan":
            ticker = "NIKKEI/ALL_STOCK"     
        elif exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "England":
            ticker = "CHRIS/LIFFE_Z1"                 
        elif exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "England":
            ticker = "WFE/INDEXES_SHANGHAISESSECOMPOSITEINDEX"                    
        date_time = date.today() #define today's date
        one_month_ago = date_time - timedelta(years=10) # define the date one month ago
        end = date_time.strftime("%Y/%m/%d") #make sure that date is in Y-m-d format
        start = one_month_ago.strftime("%Y/%m/%d") #make sure that date is in Y-m-d format
        global data   #definre dataframe as global
        data = quandl.get(ticker, start_date=start, end_date=end)             
      except (SyntaxError, NotFoundError):
        " "
        print('Incorrect arguments. Please, try another country.')
        ticker = None               
    return "Here are the results"
program = MainFormula()               
print(program)  
be_good_do_good
  • 4,311
  • 3
  • 28
  • 42
Alberto Alvarez
  • 805
  • 3
  • 11
  • 20

1 Answers1

0

Ensure each of your exchange variables is assigned a None, this is giving the error.

Then, timedelta has no years, it has days though. Instead, you could use something like from_date = datetime.datetime.now() - datetime.timedelta(days=2*365).

Finally, you're returning a string and then printing the string, i think you instead want to print the data.

This then becomes:

import quandl
from quandl.errors.quandl_error import NotFoundError #for error handling
import datetime

def MainFormula():
    exchange1, exchange2, exchange3, exchange4, exchange5  = None, None, None, None, None
    while exchange1 is None or exchange2  is None or exchange3  is None or exchange4  is None or exchange5  is None:
      exchange1, exchange2, exchange3, exchange4, exchange5 = input('Please, enter 5 countries for obtaining corresponding stock indices (with a comma in between): ').split(',')
      try:
        exchange1 = str(exchange1)  #ensure that entered exchage is string type
        exchange2 = str(exchange2)  #ensure that entered exchage is string type
        exchange3 = str(exchange3)  #ensure that entered exchage is string type
        exchange4 = str(exchange4)  #ensure that entered exchage is string type
        exchange5 = str(exchange5)  #ensure that entered exchage is string type
        if exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "France":
            ticker = "CHRIS/LIFFE_FCE1"
        elif exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "USA":
            ticker = "MULTPL/SP500_REAL_PRICE_MONTH"
        elif exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "Germany":
            ticker = "CHRIS/EUREX_FDAX1"
        elif exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "Hong_Kong":
            ticker = "CHRIS/HKEX_HSI1"
        elif exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "India":
            ticker = "NSE/CNX_NIFTY"
        elif exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "Japan":
            ticker = "NIKKEI/ALL_STOCK"
        elif exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "England":
            ticker = "CHRIS/LIFFE_Z1"
        elif exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "England":
            ticker = "WFE/INDEXES_SHANGHAISESSECOMPOSITEINDEX"

        till_today = datetime.date.today() #define today's date
        from_date = datetime.datetime.now() - datetime.timedelta(days=2*365) # define the date one month ago
        end = till_today.strftime("%Y/%m/%d") #make sure that date is in Y-m-d format
        start = from_date.strftime("%Y/%m/%d") #make sure that date is in Y-m-d format
        data = quandl.get(ticker, start_date=start, end_date=end)

      except (SyntaxError, NotFoundError):
        print('Incorrect arguments. Please, try another country.')
        ticker = None
    return data
program = MainFormula()
print(program)
Chris
  • 1,287
  • 12
  • 31