Advertisement
den4ik2003

Untitled

Nov 2nd, 2024
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 KB | None | 0 0
  1. from threading import Thread
  2. import numpy as np
  3. import time
  4. from binance import Client
  5.  
  6. class Volume:
  7.     def __init__(self):
  8.         self.threads = []
  9.         self.binance = Client('', '')
  10.         pass
  11.  
  12.     def _process(self, market, daily_volume):
  13.         book = market.get_info()[0]
  14.         price = (book[0] + book[1]) / 2
  15.         unit_time = 300
  16.         while True:
  17.             bg = time.time()
  18.             coef = 1
  19.             try:
  20.                 book = market.get_info()[0]
  21.                 spread = book[0] / book[1] - 1
  22.                 kline_24h = self.binance.get_klines(symbol='BTCUSDT', interval='1d', limit=2)[0]
  23.                 kline_5m = self.binance.get_klines(symbol='BTCUSDT', interval='5m', limit=2)[0]
  24.                 V = daily_volume / float(kline_24h[7]) * float(kline_5m[7])
  25.                 coef = np.sqrt(V / (daily_volume / (24 * 12)))
  26.                 if V >= 100:
  27.                     mean_cd = 15
  28.                 elif V >= 50:
  29.                     mean_cd = 30
  30.                 else:
  31.                     mean_cd = 60
  32.                 mean_vol = V * (mean_cd / 300)
  33.                 cd = min(120, max(1, np.random.uniform(mean_cd / 10, mean_cd * 1.9)))
  34.                 cur_step = 0.1 ** market.symbol[3]
  35.                 cur_price = max(min(price, book[0] - cur_step), book[1] + cur_step)
  36.                 qty = min(500, max(6, np.random.normal(mean_vol, mean_vol / 3))) / cur_price
  37.                 print(f"iteration: {qty}, {cur_price}")
  38.                 if cur_price < book[0] and cur_price > book[1]:
  39.                     print(f"proceeding {type(market)}")
  40.                     threads = []
  41.                     threads.append(Thread(target=market.new_limit, args=(cur_price, qty, False)))
  42.                     threads[-1].start()
  43.                     threads.append(Thread(target=market.new_limit, args=(cur_price, qty, True)))
  44.                     threads[-1].start()
  45.                     for t in threads:
  46.                         t.join()
  47.                     coef = qty / (daily_volume / 84600 * cd)
  48.             except:
  49.                 print('exception: {type(market)}')
  50.             finally:
  51.                 market.cancel_open_orders()
  52.                 time.sleep(cd)
  53.                 ed = time.time()
  54.                 dt = (ed - bg) / unit_time
  55.                 w = np.random.normal(0, np.sqrt(dt))
  56.                 mu = spread / 5
  57.                 sigma = coef * spread / 5
  58.                 price *= np.exp((mu - 0.5 * (sigma ** 2)) * dt + sigma * w)
  59.  
  60.     def add_market(self, market, daily_volume):
  61.         self.threads.append(Thread(target=self._process, args=(market, daily_volume)))
  62.  
  63.     def run_event_loop(self):
  64.         for thread in self.threads:
  65.             thread.start()
  66.         for thread in self.threads:
  67.             thread.join()
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement