使用适当的输入来下载库存数据

时间:2018-01-28 18:25:13

标签: database python-3.x input stocks quandl

我在下面有一个代码,试图让用户进入5个国家,以便作为回报她获得该国家股票指数价格的数据(假设她进入美国作为回报,她获得了S& P 500的信息) 。我使用错误处理属性,以便在没有索引的国家/地区时,它会要求用户重新进入该国家/地区。问题是代码不起作用,我被卡住了。可以请你帮我吧。

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)  

1 个答案:

答案 0 :(得分:0)

确保为exchange个变量分配了None,这就是错误。

然后,timedelta没有年限,但它有几天。相反,您可以使用类似from_date = datetime.datetime.now() - datetime.timedelta(days=2*365)的内容。

最后,您返回string然后打印字符串,我想您想要打印data

然后变为:

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)