Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import queue
- import requests
- from hitbtc import HitBTC
- c = HitBTC()
- c.start() # start the websocket connection
- time.sleep(2) # Give the socket some time to connect
- c.login()
- session = requests.Session()
- session.auth = ("0961f144add31d878ba6708d87c1707d", "f93d9de8807146d71dc12cd93c955c81")
- candles = session.get('https://api.hitbtc.com/api/2/public/candles/BTCUSD').json()
- stream = session.get('https://api.hitbtc.com/api/2/public/ticker/BTCUSD').json()
- candles_amount=100
- def ema(candles_amount, close, n):
- EMA = float(close[n])
- if n != 0:
- EMA = float(close[n])*2.0/(float(candles_amount)+1)+ema(candles_amount, close, n-1)*(1-2.0/(float(candles_amount)+1))
- return EMA
- def get_data(candles, stream, value):
- y=[0 for x in range(candles_amount)]
- n = 0
- while n<candles_amount:
- candle = str(candles[n])
- open=candle[51:58]
- close=candle[71:78]
- min=candle[89:96]
- max=candle[107:114]
- if value == "min":
- y[n] = min
- if value == 'max':
- y[n] = max
- if value == 'open':
- y[n] = open
- if value == 'close':
- y[n] = close
- n=n+1
- return(y)
- def cross(ema_cur_short, ema_cur_long, ema_prev_short, ema_prev_long):
- if all([(ema_cur_short > ema_cur_long * 0.109346), (ema_cur_short < ema_cur_long * 4.000654)]):
- if ema_prev_short < ema_cur_short:
- return 'buy'
- else:
- return 'sell'
- else:
- return 'hold on'
- stream = str(session.get('https://api.hitbtc.com/api/2/public/ticker/BTCUSD').json())
- cur_ask=stream[9:16]
- cur_bid=stream[27:34]
- cur_last=stream[46:53]
- close_list = []
- close_list = get_data(candles,stream,'close')
- u=[0 for x in range(13)]
- d=[0 for x in range(13)]
- ud=[0 for x in range(13)]
- RSI_list=[0 for x in range(13)]
- u_count = 0
- d_count = 0
- n=0
- while n<13:
- ud[n] = float(close_list[n+1]) - float(close_list[n])
- if ud[n] > 0:
- u[u_count] = float(ud[n])
- u_count = u_count+1
- else:
- d[d_count] = -float(ud[n])
- d_count = d_count+1
- print('u ', ema(candles_amount, u, u_count))
- print('d ', ema(candles_amount, d, d_count))
- #RSI_list[n] = ema(candles_amount, u, u_count) / ema(candles_amount, d, d_count)
- #print(RSI_list[n])
- n=n+1
- print(u)
- print(d)
- print(ud)
- ema_period = 10
- ema_count = 0
- #while ema_count < candles_amount:
- #print("Ema: %f", ema(ema_period, close_list, candles_amount-ema_count-1))
- #ema_count = ema_count+1
- #print("Close: ", close_list[0])
- x1 = ema(8, close_list, candles_amount-1)
- x2 = ema(13, close_list, candles_amount-1)
- close_list_kastr=close_list[:-1]
- y1 = ema(8, close_list_kastr, candles_amount-2)
- y2 = ema(13, close_list_kastr, candles_amount-2)
- print(cross(x1, x2, y1, y2))
- while True:
- try:
- data = c.recv()
- except queue.Empty:
- continue
- c.stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement