Advertisement
Guest User

Untitled

a guest
Oct 28th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.81 KB | None | 0 0
  1. #!/usr/bin/env
  2. #
  3. # VStock daemon - v.1.0
  4. # voku.xyz
  5.  
  6.  
  7. import mysql.connector
  8. import datetime
  9. from datetime import timedelta
  10. import re
  11. import os
  12. import quandl
  13. import sys
  14. import string
  15. import time
  16. import random
  17.  
  18. # UBER
  19. conn = mysql.connector.connect(user='root',host='localhost',password='fuckpasswords')
  20. cursor = conn.cursor()
  21. keys = []
  22. miscdb = 'vsmisc'
  23. quandl.ApiConfig.api_key = 'vr8w7D5UrxJXGmb_7ZQi'
  24. path = os.path.dirname(os.path.realpath(__file__))
  25.  
  26. # Initialize the database structure
  27.  
  28.     # args
  29.  
  30. # initialize all the tables and databases
  31. #
  32. # tables:
  33. #
  34.     # vusers --> User Storage
  35.     # pf --> Portfolios
  36.     # cache --> Quick cache for the main database. Truncated each day
  37.     # apistore --> API key retrieval, account info and more
  38. #
  39. # Databases:
  40. #
  41.     # vstock --> Stock data is stored here
  42.     # vsmisc --> User data, cache index, api key storage and portfolio storage
  43.     # vscache --> Cache store
  44.     # vslog --> Used to log read/writes to and from the database
  45. #
  46.  
  47. def initDBStructure():
  48.  
  49.     tables = [
  50.     'CREATE TABLE `vusers`(id INT NOT NULL AUTO_INCREMENT, uid TEXT NOT NULL, email TEXT NOT NULL, `hash` TEXT NOT NULL, useragent TEXT NOT NULL, ip TEXT NOT NULL, PRIMARY KEY(id))',
  51.     'CREATE TABLE `pf` (id INT NOT NULL AUTO_INCREMENT, pid TEXT NOT NULL, `creator` TEXT NOT NULL, `views` INT NOT NULL, `stocks` TEXT NOT NULL, `rank` INT, `created` INT NOT NULL, PRIMARY KEY(id))',
  52.     'CREATE TABLE `cache` (id INT NOT NULL AUTO_INCREMENT, cid TEXT NOT NULL, `description` TEXT NOT NULL, `uid` TEXT NOT NULL, `created` INT NOT NULL, `expiration` INT NOT NULL, PRIMARY KEY(id))',
  53.     'CREATE TABLE `apistore`(id INT NOT NULL AUTO_INCREMENT, `uid` TEXT NOT NULL, `aid` TEXT NOT NULL, `public` TEXT NOT NULL, `private` TEXT NOT NULL, PRIMARY KEY(id))'
  54.     ]
  55.  
  56.     dbs = [
  57.     #'CREATE DATABASE `vstock`',
  58.     'CREATE DATABASE `vsmisc`',
  59.     'CREATE DATABASE `vscache`',
  60.     'CREATE DATABASE `vslog`'
  61.     ]
  62.  
  63.     def writeTo():
  64.         # write db's
  65.         for k in dbs:
  66.             print('Creating Database %s' %(re.findall('`\w+`',k)[0].replace('`','')))
  67.             cursor.execute(k)
  68.             conn.commit()
  69.  
  70.         # use `vstock` for the following tables
  71.         cursor.execute('USE `vsmisc`')
  72.         conn.commit()
  73.  
  74.         for k in tables:
  75.             print('Initializing table: %s' %(re.findall('`\w+`',k)[0].replace('`','')))
  76.             cursor.execute(k)
  77.             conn.commit()
  78.  
  79.         return True
  80.  
  81.     return writeTo()
  82.  
  83.  
  84. # Purge the cache server
  85. def purgeCache():
  86.     cursor.execute('DROP DATABASE `vscache`;')
  87.     cursor.execute('CREATE DATABASE `vscache`')
  88.     conn.commit()
  89.  
  90.  
  91. # Search data in the cache database by the description / user
  92. def searchCache(query, userdata):
  93.     cursor.execute('USE `vsmisc`')
  94.     sQueryDesc = "SELECT `cid` FROM `cache` WHERE description = '%s'" %(query)
  95.     sQueryCid = "SELECT `cid` FROM `cache` WHERE uid = '%s'" %(query)
  96.     cursor.execute(sQueryDesc)
  97.  
  98.     for cid in cursor:
  99.         if cid is None:
  100.             #search by userid
  101.             cursor.execute(sQueryCid)
  102.  
  103.             for cid in cursor:
  104.                 if cid is None:
  105.                     return False
  106.                 else:
  107.                     return cid
  108.         else:
  109.             return cid
  110.  
  111. # Chronologically order data then cache.
  112. # chrono() takes the ticker and re-orders ALL records of that particular stock.
  113. # it returns a cacheid of a new, fresh cache table set to expire in 5 days.
  114. #
  115. # categories sorted and cached:
  116.  
  117. def chrono(ticker):
  118.  
  119.     # define the lists
  120.     sopen = []
  121.     sclose = []
  122.     shigh = []
  123.     slow = []
  124.  
  125.  
  126. # read the ticker data and sort chronologically, then return
  127. #   val = priceObj[0]
  128. #   scomp = priceObj[1]
  129. #   endcomp = priceObj[2]
  130. #   freq = priceObj[3]
  131.     cursor.execute('USE `vstock`')
  132.  
  133.     # fetch the stock data and order it chronologically
  134.     sortQuery = "SELECT low, high, open, close FROM `%s` ORDER BY 'date' DESC" %(ticker)
  135.     cursor.execute(sortQuery)
  136.     for(low, high, stock_open, close) in cursor:
  137.         sopen.append(stock_open)
  138.         sclose.append(close)
  139.         slow.append(low)
  140.         shigh.append(high)
  141.  
  142.     returnList = [sopen, sclose, slow, shigh]
  143.     return returnList
  144.  
  145.  
  146. # Create a 'symbols.vk' file with all the stock symbols currently in the database from a delta file
  147. def mkSymbols(delta):
  148.     symb = []
  149.     if os.path.isfile(delta):
  150.             with open(delta) as f:
  151.                 for line in f:
  152.                     l = line.split(',')
  153.                     if l[0] not in symb:
  154.                         print(l[0])
  155.                         symb.append(l[0])
  156.  
  157.             # create the symb file
  158.             counter = 0
  159.             with open('symbols.vk','a') as f:
  160.                 for k in symb:
  161.                     f.write(k+'\n')
  162.  
  163.  
  164. # get the number of indices
  165. # we recalculate each time because the compute cost is less than the risk of having an innacurate ticker count
  166. def nSymbols():
  167.     if os.path.isfile('symbols.vk'):
  168.         with open('symbols.vk') as f:
  169.             for i, l in enumerate(f):
  170.                 pass
  171.         return i + 1
  172.     else:
  173.         return -1
  174.  
  175.  
  176. # Main Simulation function.
  177. #
  178. # File accepts the cacheid, userid and priceObject container and outputs a cacheid of adjusted values
  179. # per each frame of reference.
  180. #
  181. # ** Note - cache object must be chronologically organized before hand using the chrono() function.
  182. #
  183. # priceObject{
  184.     # value : Integer
  185.     # start_comparison : date()
  186.     # end_comparison : date()
  187.     # frequency (daily(d) | weekly(w) | monthly(m) | yearly(y) | per Decade(D) )
  188.     # }
  189. #
  190.  
  191. def sim(uid, ticker, priceObj):
  192.     val = priceObj[0]
  193.     scomp = priceObj[1]
  194.     ecomp = priceObj[2]
  195.     freq = priceObj[3]
  196.  
  197.     # load the stock data
  198.     obj = chrono(ticker)
  199.     compare = [obj[0][0],obj[1][0],obj[2][0],obj[3][0]]
  200.     perc = [0,0,0,0]
  201.     avg = [[],[],[],[]]
  202.     change = [[],[],[],[]]
  203.     i = 0
  204.     cacheID = ''.join(random.choice('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ') for i in range(12))
  205.     timestamp = time.time()
  206.     expTime = (datetime.datetime.fromtimestamp(timestamp)) + datetime.timedelta(days=5)
  207.  
  208.     # calculate the three averages
  209.     for (a,b,c,d) in zip(obj[0],obj[1],obj[2],obj[3]):
  210.  
  211.         perc[0] = ((compare[0] - a) / compare[0])
  212.         perc[1] = ((compare[1] - b) / compare[1])
  213.         perc[2] = ((compare[2] - c) / compare[2])
  214.         perc[3] = ((compare[3] - d) / compare[3])
  215.         for d in range(4):
  216.             avg[i].append(perc[i])
  217.  
  218.         compare[0] = a
  219.         compare[1] = b
  220.         compare[2] = c
  221.         compare[3] = d
  222.  
  223.     cursor.execute('USE `vscache`')
  224.     cacheCreateQuery = "CREATE TABLE `%s`(`id` INT NOT NULL AUTO_INCREMENT, `avg` FLOAT NOT NULL, PRIMARY KEY(id))" %(cacheID)
  225.     print(cacheCreateQuery)
  226.     cursor.execute(cacheCreateQuery)
  227.  
  228.     # calculate the final average and cache
  229.     for i in range(4):
  230.         avg[i].append((perc[2]+perc[3])/2)
  231.  
  232.  
  233.     #here
  234.     print(avg[1])
  235.     sys.exit()
  236.     #here
  237.  
  238.     for k in avg:
  239.         cacheInsert = "INSERT INTO %s (id,avg) VALUES(NULL, '%s')" %(cacheID,k)
  240.         print('Cache Insert --> %s') %(cacheID)
  241.         print(cacheInsert)
  242.         #cursor.execute(cacheInsert)
  243.         #conn.commit()
  244.  
  245.     # finally index the cache
  246.     cursor.execute('USE `vsmisc`')
  247.     insertQuery = "INSERT INTO `%s` (`id`, `cid`, `description`, `uid`, `created`, `expiration`) VALUES(NULL, %s, %s, %s, %s, %s)" %(ticker+'_'+timestamp, cacheID, 'AVG_ASC',uid,timestamp,expTime.timestamp())
  248.     cursor.execute(insertQuery)
  249.     conn.commit()
  250.  
  251.     return avg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement