:))
I've made a crypto trading bot in python connected to my Binance account. My problem is to count HL2 source ((High+Low)/2). I want to make a sma(hl2, 17) but I can't figure out how to do it. If I use simply close (or open, low, high) it works correctly.
My script:
import websocket, json, pprint, talib, numpy
from binance.client import Client
from binance.enums import *
import config
SOCKET = "wss://stream.binance.com:9443/ws/xrpusdt@kline_1m"
#sma-data
SLOWSMA_PERIOD = 6 #source hl2
#trade-data
TRADE_SYMBOL = 'XRPUSDT'
TRADE_QUANTITY = 1
closes = []
lows = []
highs = []
in_positon = False
client = Client(config.API_KEY, config.API_SECRET)
def order(side, quantity, symbol, order_type=ORDER_TYPE_MARKET):
try:
print("sending order")
order = client.create_order(symbol=symbol, side=side, type=order_type, quantity=quantity)
print(order)
except Exception as e:
return False
return True
def on_open(ws):
print('opened connection')
def on_close(ws):
print('closed connection')
def on_message(ws, message):
global closes; global lows; global highs
print('received message')
json_message = json.loads(message)
pprint.pprint(json_message)
candle = json_message['k']
is_candle_closed = candle['x']
close = candle['c']
low = candle['l']
high = candle['h']
if is_candle_closed:
print("candle closed at {}".format(close))
closes.append(float(close))
print("closes")
print(closes)
lows.append(float(low))
print("lows")
print(lows)
highs.append(float(high))
print("highs")
print(highs)
if len(closes) > SLOWSMA_PERIOD:
np_hl2 = numpy.array((closes+highs)/2)
slowSma = talib.SMA(np_hl2, SLOWSMA_PERIOD)
print("slowSma")
print(slowSma)
last_slowSma = slowSma[-1]
print("the currect slowSma is {}".format(last_slowSma))
ws = websocket.WebSocketApp(SOCKET, on_open=on_open, on_close=on_close, on_message=on_message)
ws.run_forever()
-> The important part:
if len(closes) > SLOWSMA_PERIOD:
np_hl2 = numpy.array((closes+highs)/2)
slowSma = talib.SMA(np_hl2, SLOWSMA_PERIOD)
print("slowSma")
print(slowSma)
last_slowSma = slowSma[-1]
print("the currect slowSma is {}".format(last_slowSma))
The script is running but doesn't list 'slowSma'.
----------------------Can anybody help me? :)) ----------------------
I've tried some way and search for soultion in order to get HL2 source, but I didn't find any.
I've found the way finally: :))
Way to calculate HL2:
hl2s.append(numpy.mean([float(high),float(low)]))
print("hl2s")
print(hl2s)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.