Guest User

Untitled

a guest
Dec 21st, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. ## import libraries
  2. from time import sleep
  3. import mysql.connector as sql
  4. import pandas as pd
  5. import threading
  6. import numpy as np
  7. import gc
  8.  
  9. ## define function to get data from mysql database and make pandas dataframe
  10. def get_HLC():
  11. db=sql.connect(host='localhost',user='root',password='',database='algo')
  12. df=pd.read_sql('select *from ticks',con=db,parse_dates=True)
  13. df=pd.DataFrame(df)
  14. df=df.set_index(['timestamp'])
  15. df.index = pd.to_datetime(df.index.astype(np.int64), unit='ms')
  16. df2 = df.resample('60s', how={'last_price': 'ohlc'})
  17. df3 = df.resample('60s', how={'volume': 'sum'})
  18. df3.columns = pd.MultiIndex.from_tuples([('volume', 'sum')])
  19. df4 = pd.concat([df2, df3], axis=1)
  20. df4.iloc[:,3] = df4.iloc[:,3].fillna(method='ffill')
  21. df4.iloc[:,0] = df4.iloc[:,0].fillna(value=df4.iloc[:,3])
  22. df4.iloc[:,1] = df4.iloc[:,1].fillna(value=df4.iloc[:,3])
  23. df4.iloc[:,2] = df4.iloc[:,2].fillna(value=df4.iloc[:,3])
  24. df5 = df4.iloc[-230:,:]
  25. db.close()
  26. del(db, df, df2, df3, df4)
  27. gc.collect()
  28. return df5['last_price']['high'], df5['last_price']['low'], df5['last_price']['close']
  29.  
  30. ## define function that gets the data in a loop
  31. def thread1_Function():
  32. while True:
  33. global low
  34. df5 = get_HLC()
  35. low = df5[1]
  36. del(df5)
  37. gc.collect()
  38. sleep(30)
  39.  
  40. ## start a thread of the get data loop
  41. thread1 = threading.Thread(target=thread1_Function)
  42. thread1.start()
  43.  
  44. ## pause
  45. sleep(5)
  46.  
  47. ## define function that loops and does things with data of thread1
  48. def thread2_Function():
  49. while True:
  50. #do things with thread1 data
  51. print(low)
  52. sleep(15)
  53.  
  54. ## start a thread of the do things with data of thread1 loop
  55. thread2 = threading.Thread(target=thread2_Function)
  56. thread2.start()
Add Comment
Please, Sign In to add comment