Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 KB | None | 0 0
  1. #! /usr/bin/python
  2. import psycopg2
  3. import sys
  4. from sqlalchemy import create_engine
  5. import Quandl
  6. import pandas
  7. import numpy
  8. import re
  9. import time
  10.  
  11.  
  12. futures_code='ICE/CTH2016'
  13. futures=(futures_code.split('/'))[1]
  14.  
  15. #ticker_month, year
  16. splitted_futures=re.split('(\d+)',futures)
  17.  
  18. ticker = (splitted_futures[0])[:-1]
  19. month = (splitted_futures[0])[-1]
  20. year = int((splitted_futures[1]))
  21. df = Quandl.get(futures_code,authtoken='MY_TOKEN')
  22. #data.head()
  23. print list(df.columns.values)
  24. df['Ticker']=ticker
  25. df['Month']=month
  26. df['Year']= year
  27. df['Futures']= year
  28. cols = ['Open', 'High', 'Low', 'Settle', 'Volume',
  29. 'Prev. Day Open Interest', 'EFP Volume', 'EFS Volume', 'Block Volume',
  30. 'Ticker', 'Month', 'Year']
  31. df=df[cols]
  32. print df
  33.  
  34. #postgresql+pg8000://user:password@host:port/dbname[?key=value&key=value...]
  35. engine = create_engine('postgresql://seasis:310793@localhost:5432/seasis')
  36. con = None
  37. start_time = time.time()
  38. try:
  39.     print 'connecting'
  40.     con = psycopg2.connect(dbname='seasis', user='seasis',
  41.                            host='localhost', password='310793')
  42.     cur = con.cursor()
  43.     print 'cursor created'
  44.  
  45.     cur.execute('CREATE TABLE IF NOT EXISTS ct('
  46.                 'open NUMERIC (20,10), '
  47.                 'high NUMERIC (20,10), '
  48.                 'low NUMERIC (20,10), '
  49.                 'settle NUMERIC (20,10), '
  50.                 'volume INTEGER, '
  51.                 'prev_day_open_interest INTEGER, '
  52.                 'efp_volume INTEGER, '
  53.                 'efs_volume INTEGER, '
  54.                 'block_volume INTEGER, '
  55.                 'ticker VARCHAR, '
  56.                 'month VARCHAR, '
  57.                 'year SMALLINT)')
  58.     print("--- %s seconds ---" % (time.time() - start_time))
  59.     print 'table created'
  60.     df.to_sql("ct", engine, if_exists='append')
  61.     print("--- %s seconds ---" % (time.time() - start_time))
  62.     print 'added to db'
  63.     con.commit()
  64. except psycopg2.DatabaseError, e:
  65.     print 'Error %s' % e    
  66.     sys.exit(1)
  67. finally:
  68.    
  69.     if con:
  70.         con.close()
  71. print 'end'
  72. print("--- %s seconds ---" % (time.time() - start_time))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement