Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. import requests
  2. import re
  3. import time
  4. import os
  5. from pprint import pprint
  6.  
  7. #Don't change this
  8. sym= "BNBETH"
  9. lim = 50
  10.  
  11. s=requests.session()
  12.  
  13. # bypass proxy if any
  14. s.trust_env = False
  15.  
  16. os.system('touch log.txt')
  17. '''
  18. Load previous data, if any, from 'log.txt' file
  19. 1) Each record in log.txt is separated by \n\n
  20. 2) For each record, the first line is the last price of the symbol, the second is time,
  21.   the third line is all the active bids, and the fourth is all the open orders
  22. 3) Each record in bids and asks are separated by '|'
  23. 4) Each record in bids and asks consists of price and quantity separated by a comma ','
  24. '''
  25.  
  26. # Parsing previous data if any
  27.  
  28. print("Searching Old data")
  29. log = open('log.txt','r+')
  30.  
  31. prev_data_t = log.read().split('\n\n')
  32. prev_data = []
  33.  
  34. for items in prev_data_t:
  35.     if items:
  36.         items = items.split('\n')
  37.         l_p = items[0]
  38.         close_time = items[1]
  39.         #print(l_p)
  40.         bids_raw = items[2].split('|')
  41.         bids=[]
  42.         for temp in bids_raw:
  43.             if temp:
  44.                 bids.append(temp.split(','))
  45.         asks_raw = items[3].split('|')
  46.         asks=[]
  47.         for temp in asks_raw:
  48.             if temp:
  49.                 asks.append(temp.split(','))
  50.         prev_data.append([l_p,bids,asks])
  51. print("Found "+str(len(prev_data))+" records in log.txt")
  52.  
  53. #URL's and get parameters for price and depth
  54. price_u = 'https://binance.com/api/v1/ticker/24hr'
  55. depth='https://binance.com/api/v1/depth'
  56. data_p={
  57.     'symbol':sym
  58. }
  59. data_d = {
  60.     'symbol':sym,
  61.     'limit':lim
  62. }
  63.  
  64. i=0
  65.  
  66. #getting data
  67. while True:
  68.     try:
  69.         #write raw_data into log.txt
  70.         raw_data = ''
  71.  
  72.         #temp holder list to add into prev_data
  73.         list_data = []
  74.  
  75.         #'get'-ting price and time od last transaction
  76.         resp = s.get(price_u,params=data_p)
  77.         l_p = resp.json()['lastPrice']
  78.         close_time = resp.json()['closeTime']
  79.         raw_data += str(l_p)+str('\n')+str(close_time)+str('\n')
  80.         list_data.append(str(l_p))
  81.         list_data.append(str(close_time))
  82.  
  83.         #'get'-ting depth
  84.         resp = s.get(depth,params=data_d)
  85.         parsed = resp.json()
  86.  
  87.         #parsing bids and asks
  88.         bids = parsed['bids']
  89.  
  90.         #temp holder variable for list_data
  91.         temp= []
  92.         for item in bids:
  93.             price,quan = item[:2]
  94.             temp.append([price,quan])
  95.             #print(str(price)+','+str(quan))
  96.             raw_data += str(price)+str(',')+str(quan) + str('|')
  97.         raw_data += str('\n')
  98.        
  99.         list_data.append(temp)
  100.  
  101.         asks = parsed['asks']
  102.         temp=[]
  103.         for item in asks:
  104.             price,quan = item[:2]
  105.             temp.append([price,quan])
  106.             #print(str(price)+','+str(quan))
  107.             raw_data += str(price)+str(',')+str(quan) + str('|')
  108.         raw_data += str('\n\n')
  109.         list_data.append(temp)
  110.         prev_data.append(list_data)
  111.         print("Total no of records:"+str(len(prev_data)))
  112.  
  113.         #writing raw_data into log.txt
  114.         log = open('log.txt','a+')
  115.         log.write(raw_data)
  116.         log.close()
  117.         i+=1
  118.         time.sleep(0.5)
  119.     except Exception as e:
  120.         print(e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement