Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import re
- import time
- import os
- from pprint import pprint
- #Don't change this
- sym= "BNBETH"
- lim = 50
- s=requests.session()
- # bypass proxy if any
- s.trust_env = False
- os.system('touch log.txt')
- '''
- Load previous data, if any, from 'log.txt' file
- 1) Each record in log.txt is separated by \n\n
- 2) For each record, the first line is the last price of the symbol, the second is time,
- the third line is all the active bids, and the fourth is all the open orders
- 3) Each record in bids and asks are separated by '|'
- 4) Each record in bids and asks consists of price and quantity separated by a comma ','
- '''
- # Parsing previous data if any
- print("Searching Old data")
- log = open('log.txt','r+')
- prev_data_t = log.read().split('\n\n')
- prev_data = []
- for items in prev_data_t:
- if items:
- items = items.split('\n')
- l_p = items[0]
- close_time = items[1]
- #print(l_p)
- bids_raw = items[2].split('|')
- bids=[]
- for temp in bids_raw:
- if temp:
- bids.append(temp.split(','))
- asks_raw = items[3].split('|')
- asks=[]
- for temp in asks_raw:
- if temp:
- asks.append(temp.split(','))
- prev_data.append([l_p,bids,asks])
- print("Found "+str(len(prev_data))+" records in log.txt")
- #URL's and get parameters for price and depth
- price_u = 'https://binance.com/api/v1/ticker/24hr'
- depth='https://binance.com/api/v1/depth'
- data_p={
- 'symbol':sym
- }
- data_d = {
- 'symbol':sym,
- 'limit':lim
- }
- i=0
- #getting data
- while True:
- try:
- #write raw_data into log.txt
- raw_data = ''
- #temp holder list to add into prev_data
- list_data = []
- #'get'-ting price and time od last transaction
- resp = s.get(price_u,params=data_p)
- l_p = resp.json()['lastPrice']
- close_time = resp.json()['closeTime']
- raw_data += str(l_p)+str('\n')+str(close_time)+str('\n')
- list_data.append(str(l_p))
- list_data.append(str(close_time))
- #'get'-ting depth
- resp = s.get(depth,params=data_d)
- parsed = resp.json()
- #parsing bids and asks
- bids = parsed['bids']
- #temp holder variable for list_data
- temp= []
- for item in bids:
- price,quan = item[:2]
- temp.append([price,quan])
- #print(str(price)+','+str(quan))
- raw_data += str(price)+str(',')+str(quan) + str('|')
- raw_data += str('\n')
- list_data.append(temp)
- asks = parsed['asks']
- temp=[]
- for item in asks:
- price,quan = item[:2]
- temp.append([price,quan])
- #print(str(price)+','+str(quan))
- raw_data += str(price)+str(',')+str(quan) + str('|')
- raw_data += str('\n\n')
- list_data.append(temp)
- prev_data.append(list_data)
- print("Total no of records:"+str(len(prev_data)))
- #writing raw_data into log.txt
- log = open('log.txt','a+')
- log.write(raw_data)
- log.close()
- i+=1
- time.sleep(0.5)
- except Exception as e:
- print(e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement