Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. import psycopg2
  2. import numpy as np
  3. from numpy import convolve
  4. import matplotlib.pyplot as plt
  5. from psycopg2 import Error
  6. from DataProcessing import StockData
  7.  
  8. ################################################################################
  9.  
  10. def pullColumn(cursor, ticker, column_name):
  11. # column_name expects [date, open, close, high, low, volume]
  12. # @params: a stock ticker
  13. # @params: a database cursor and a column name to take data
  14. # @returns: column data for the selected column name
  15.  
  16. TABLE_QUERY = ('SELECT {} FROM {}'.format(column_name, ticker))
  17. print("Your query: ", '\'', TABLE_QUERY, '\'', sep="")
  18. cursor.execute(TABLE_QUERY)
  19. column_data = cursor.fetchall()
  20. return_list = []
  21. for i in column_data:
  22. return_list.append(i[0])
  23. return return_list
  24.  
  25. ################################################################################
  26.  
  27. if __name__ == '__main__':
  28.  
  29. connection = None
  30. try:
  31. # Connect to the PostgreSQL Server & Databse
  32. print('Connecting to the PostgreSQL database...')
  33. connection = psycopg2.connect(host='206.189.181.163',
  34. database='rcos',
  35. user='rcos',
  36. password='hedgehogs_rcos')
  37.  
  38. # Create a Cursor & Print Version
  39. cursor = connection.cursor()
  40.  
  41. # Print PostgreSQL version
  42. print('PostgreSQL database version:', cursor.execute('SELECT version();'))
  43. record = cursor.fetchone()
  44. print('You are connected to - ', record, '\n')
  45.  
  46. print("COLUMN NAME OPTIONS:")
  47. print("\tdate\n\tlow\n\thigh\n\tvolume\n\tclose\n\topen\n")
  48.  
  49. ticker = "aapl"
  50. column_name = "open"
  51. data = pullColumn(cursor, ticker, column_name)
  52. dates = pullColumn(cursor, ticker, "date")
  53. print("data size: {}\ndates size: {}".format(len(data), len(dates)))
  54. AAPL = StockData(ticker, column_name, dates, data)
  55.  
  56.  
  57. except (Exception, psycopg2.DatabaseError) as error:
  58. print("Error while connecting to PostgreSQL", error)
  59.  
  60. finally:
  61. #Closing database connection.
  62. if (connection):
  63. cursor.close()
  64. connection.close()
  65. print("PostgreSQL connection is closed.")
  66.  
  67. ################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement