Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from threading import Thread
- import numpy as np
- import time
- from binance import Client
- class Volume:
- def __init__(self):
- self.threads = []
- self.binance = Client('', '')
- pass
- def _process(self, market, daily_volume):
- book = market.get_info()[0]
- price = (book[0] + book[1]) / 2
- unit_time = 300
- while True:
- bg = time.time()
- coef = 1
- try:
- book = market.get_info()[0]
- spread = book[0] / book[1] - 1
- kline_24h = self.binance.get_klines(symbol='BTCUSDT', interval='1d', limit=2)[0]
- kline_5m = self.binance.get_klines(symbol='BTCUSDT', interval='5m', limit=2)[0]
- V = daily_volume / float(kline_24h[7]) * float(kline_5m[7])
- coef = np.sqrt(V / (daily_volume / (24 * 12)))
- if V >= 100:
- mean_cd = 15
- elif V >= 50:
- mean_cd = 30
- else:
- mean_cd = 60
- mean_vol = V * (mean_cd / 300)
- cd = min(120, max(1, np.random.uniform(mean_cd / 10, mean_cd * 1.9)))
- cur_step = 0.1 ** market.symbol[3]
- cur_price = max(min(price, book[0] - cur_step), book[1] + cur_step)
- qty = min(500, max(6, np.random.normal(mean_vol, mean_vol / 3))) / cur_price
- print(f"iteration: {qty}, {cur_price}")
- if cur_price < book[0] and cur_price > book[1]:
- print(f"proceeding {type(market)}")
- threads = []
- threads.append(Thread(target=market.new_limit, args=(cur_price, qty, False)))
- threads[-1].start()
- threads.append(Thread(target=market.new_limit, args=(cur_price, qty, True)))
- threads[-1].start()
- for t in threads:
- t.join()
- coef = qty / (daily_volume / 84600 * cd)
- except:
- print('exception: {type(market)}')
- finally:
- market.cancel_open_orders()
- time.sleep(cd)
- ed = time.time()
- dt = (ed - bg) / unit_time
- w = np.random.normal(0, np.sqrt(dt))
- mu = spread / 5
- sigma = coef * spread / 5
- price *= np.exp((mu - 0.5 * (sigma ** 2)) * dt + sigma * w)
- def add_market(self, market, daily_volume):
- self.threads.append(Thread(target=self._process, args=(market, daily_volume)))
- def run_event_loop(self):
- for thread in self.threads:
- thread.start()
- for thread in self.threads:
- thread.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement