Advertisement
Guest User

hitbtc

a guest
May 25th, 2018
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | None | 0 0
  1. import time
  2. import queue
  3. import requests
  4. from hitbtc import HitBTC
  5. c = HitBTC()
  6. c.start() # start the websocket connection
  7. time.sleep(2) # Give the socket some time to connect
  8. c.login()
  9. session = requests.Session()
  10. session.auth = ("0961f144add31d878ba6708d87c1707d", "f93d9de8807146d71dc12cd93c955c81")
  11. candles = session.get('https://api.hitbtc.com/api/2/public/candles/BTCUSD').json()
  12. stream = session.get('https://api.hitbtc.com/api/2/public/ticker/BTCUSD').json()
  13. candles_amount=100
  14.  
  15. def ema(candles_amount, close, n):
  16.     EMA = float(close[n])
  17.     if n != 0:
  18.         EMA = float(close[n])*2.0/(float(candles_amount)+1)+ema(candles_amount, close, n-1)*(1-2.0/(float(candles_amount)+1))
  19.     return EMA
  20. def get_data(candles, stream, value):
  21.     y=[0 for x in range(candles_amount)]
  22.     n = 0
  23.     while n<candles_amount:
  24.         candle = str(candles[n])
  25.         open=candle[51:58]
  26.         close=candle[71:78]
  27.         min=candle[89:96]
  28.         max=candle[107:114]
  29.         if value == "min":
  30.             y[n] = min
  31.         if value == 'max':
  32.             y[n] = max
  33.         if value == 'open':
  34.             y[n] = open
  35.         if value == 'close':
  36.             y[n] = close
  37.         n=n+1
  38.     return(y)
  39.    
  40.  
  41. def cross(ema_cur_short, ema_cur_long, ema_prev_short, ema_prev_long):
  42.     if all([(ema_cur_short > ema_cur_long * 0.109346), (ema_cur_short < ema_cur_long * 4.000654)]):
  43.         if ema_prev_short < ema_cur_short:
  44.             return 'buy'
  45.         else:
  46.             return 'sell'
  47.     else:
  48.         return 'hold on'
  49.  
  50. stream = str(session.get('https://api.hitbtc.com/api/2/public/ticker/BTCUSD').json())
  51. cur_ask=stream[9:16]
  52. cur_bid=stream[27:34]
  53. cur_last=stream[46:53]
  54.  
  55. close_list = []
  56. close_list = get_data(candles,stream,'close')
  57. u=[0 for x in range(13)]
  58. d=[0 for x in range(13)]
  59. ud=[0 for x in range(13)]
  60. RSI_list=[0 for x in range(13)]
  61. u_count = 0
  62. d_count = 0
  63. n=0
  64. while n<13:
  65.     ud[n] = float(close_list[n+1]) - float(close_list[n])
  66.     if ud[n] > 0:
  67.         u[u_count] = float(ud[n])
  68.         u_count = u_count+1
  69.     else:
  70.         d[d_count] = -float(ud[n])
  71.         d_count = d_count+1
  72.     print('u ', ema(candles_amount, u, u_count))
  73.     print('d ', ema(candles_amount, d, d_count))
  74.     #RSI_list[n] = ema(candles_amount, u, u_count) /  ema(candles_amount, d, d_count)
  75.     #print(RSI_list[n])
  76.     n=n+1
  77. print(u)
  78. print(d)
  79. print(ud)
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93. ema_period = 10
  94. ema_count = 0
  95.  
  96.  
  97. #while ema_count < candles_amount:
  98. #print("Ema: %f", ema(ema_period, close_list, candles_amount-ema_count-1))
  99. #ema_count = ema_count+1
  100.  
  101. #print("Close: ", close_list[0])
  102.  
  103.  
  104. x1 = ema(8, close_list, candles_amount-1)
  105. x2 = ema(13, close_list, candles_amount-1)
  106. close_list_kastr=close_list[:-1]
  107. y1 = ema(8, close_list_kastr, candles_amount-2)
  108. y2 = ema(13, close_list_kastr, candles_amount-2)
  109. print(cross(x1, x2, y1, y2))
  110.  
  111. while True:
  112.     try:
  113.         data = c.recv()
  114.     except queue.Empty:
  115.         continue
  116.  
  117. c.stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement