使用适当的输入下载股票数据



我在下面有一个代码,试图要求用户输入5个国家,以便她获得该国家/地区的股票指数价格数据(假设她进入美国作为回报,她会获得标准普尔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)  

确保为每个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)

相关内容

  • 没有找到相关文章

最新更新