Advertisement
Guest User

fdsa

a guest
Feb 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. import pandas as pd
  2. from urllib.request import Request, urlopen
  3. import json
  4. from pandas.io.json import json_normalize
  5.  
  6.  
  7.  
  8.  
  9. Prefix = "https://api.iextrading.com/1.0/tops?symbols="
  10. #Suffix = input()
  11. Suffix1 = "IBM"
  12. Suffix2 = "AAPL"
  13. linke1 = Prefix + Suffix1
  14. linke2 = Prefix + Suffix2
  15.  
  16. #print (linke1)
  17. #print (linke2)
  18.  
  19. def return_valid_securities():
  20. """
  21. Return a list of valid securities
  22. :param securities: list of securities
  23. :return: list of valid securities
  24. """
  25.  
  26.  
  27. valid_securities = _url_to_dataframe(linke1)['symbol']
  28. valid_securities = _url_to_dataframe(linke2)['symbo2']
  29.  
  30. return [x for x in securities if x in set(valid_securities)]
  31.  
  32.  
  33. def _url_to_dataframe(url, nest=None):
  34.  
  35. """
  36. Takes a url and returns the response in a pandas dataframe
  37. :param url: str url
  38. :param nest: column with nested data
  39. :return: pandas dataframe containing data from url
  40. """
  41.  
  42. request = Request(url)
  43. response = urlopen(request)
  44. elevations = response.read()
  45. data = json.loads(elevations)
  46.  
  47. if nest:
  48. data = json_normalize(data[nest])
  49. else:
  50. data = json_normalize(data)
  51.  
  52. return pd.DataFrame(data)
  53.  
  54.  
  55.  
  56. while (Suffix1 == "IBM" and Suffix2 == "AAPL"):
  57. def get_latest_quote_and_trade1():
  58.  
  59. """
  60. Gets latest quote and trade data
  61. :param securities: list of securities
  62. :return: pandas dataframe containing data for valid securities
  63. """
  64.  
  65. df = _url_to_dataframe(linke1)
  66. df['lastSaleTime'] = pd.to_datetime(df['lastSaleTime'], unit='ms')
  67. df['lastUpdated'] = pd.to_datetime(df['lastUpdated'], unit='ms')
  68. df.set_index(['symbol'], inplace=True)
  69. return df.to_string()
  70. #return df.to_excel('exceloutput.xlsx', sheet_name='sheet1', index=False,)
  71.  
  72.  
  73.  
  74.  
  75.  
  76. def get_latest_quote_and_trade2():
  77. """
  78. Gets latest quote and trade data
  79. :param securities: list of securities
  80. :return: pandas dataframe containing data for valid securities
  81. """
  82.  
  83. df = _url_to_dataframe(linke2)
  84. df['lastSaleTime'] = pd.to_datetime(df['lastSaleTime'], unit='ms')
  85. df['lastUpdated'] = pd.to_datetime(df['lastUpdated'], unit='ms')
  86. df.set_index(['symbol'], inplace=True)
  87. return df.to_string()
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94. print(get_latest_quote_and_trade1())
  95. print(get_latest_quote_and_trade2())
  96.  
  97.  
  98.  
  99. print(linke1)
  100. print(linke2)
  101.  
  102. f = open('1530', 'a')
  103. #output1 = get_latest_quote_and_trade1()
  104. output2 = get_latest_quote_and_trade2()
  105. f.write (output2)
  106.  
  107.  
  108.  
  109. #to_excel('test.xlsx', sheet_name='sheet1', index=False,)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement