import websocket, json, pprint, talib, numpy
import config
from binance.client import Client
from binance.enums import *

# Stream name <symbol><stream><interval>
# See: https://github.com/binance/binance-spot-api-docs/blob/master/web-socket-streams.md#klinecandlestick-streams

SOCKET = "wss://stream.binance.com:9443/ws/dogeusdt@kline_1m"
RSI_PERIOD = 14
RSI_OVERBOUGHT = 72
RSI_OVERSOLD = 28
TRADE_SYMBOL = 'DOGEUSDT'
TRADE_QUANTITY = 250

closes = []
in_position = 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:
        print("error")
        print(e)
        return False

    return True

def on_message(ws, message):                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
    global closes, in_position
    print("received message")
    json_message = json.loads(message)                   
    pprint.pprint(json_message)

    candle = json_message["k"]           
    is_candle_closed = candle["x"]
    close = candle["c"]

    if is_candle_closed:
        print("Candle closed at {}".format(close))                                  
        closes.append(float(close))
        print("closes")
        print(closes)

        if len(closes) > RSI_PERIOD:
            np_closes = numpy.array(closes)
            rsi = talib.RSI(np_closes, RSI_PERIOD)
            print("all rsis calculated so far")
            print(rsi)      
            last_rsi = rsi[-1]
            print("the current rsi is {}".format(last_rsi))

            if last_rsi > RSI_OVERBOUGHT:
                if in_position:
                    print("overbought SELL")
                    order_succeeded = order(SIDE_SELL, TRADE_QUANTITY, TRADE_SYMBOL)
                    if order_succeeded:
                        in_position = False
                else:
                    print("overbought, not owned, nothing to do")
            
            if last_rsi < RSI_OVERSOLD:
                if in_position:
                    print("oversold, owned, nothing to do")
                else:
                    print("oversold BUY")
                    order_succeeded = order(SIDE_BUY, TRADE_QUANTITY, TRADE_SYMBOL)
                    if order_succeeded:
                        in_position = True

                                 
def on_error(ws, error):                                         
    print("received error", error)
       

def on_close(ws):
    print("closed connection")


def on_open(ws):    
    print("opened connection")


ws = websocket.WebSocketApp(SOCKET,
                            on_open=on_open,
                            on_close=on_close,
                            on_message=on_message,
                            on_error=on_error)

ws.run_forever()           

e1ebfd1d6cbc10a3a20f6413d16766493d9413c8
<mask token>


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:
        print('error')
        print(e)
        return False
    return True


def on_message(ws, message):
    global closes, in_position
    print('received message')
    json_message = json.loads(message)
    pprint.pprint(json_message)
    candle = json_message['k']
    is_candle_closed = candle['x']
    close = candle['c']
    if is_candle_closed:
        print('Candle closed at {}'.format(close))
        closes.append(float(close))
        print('closes')
        print(closes)
        if len(closes) > RSI_PERIOD:
            np_closes = numpy.array(closes)
            rsi = talib.RSI(np_closes, RSI_PERIOD)
            print('all rsis calculated so far')
            print(rsi)
            last_rsi = rsi[-1]
            print('the current rsi is {}'.format(last_rsi))
            if last_rsi > RSI_OVERBOUGHT:
                if in_position:
                    print('overbought SELL')
                    order_succeeded = order(SIDE_SELL, TRADE_QUANTITY,
                        TRADE_SYMBOL)
                    if order_succeeded:
                        in_position = False
                else:
                    print('overbought, not owned, nothing to do')
            if last_rsi < RSI_OVERSOLD:
                if in_position:
                    print('oversold, owned, nothing to do')
                else:
                    print('oversold BUY')
                    order_succeeded = order(SIDE_BUY, TRADE_QUANTITY,
                        TRADE_SYMBOL)
                    if order_succeeded:
                        in_position = True


def on_error(ws, error):
    print('received error', error)


def on_close(ws):
    print('closed connection')


def on_open(ws):
    print('opened connection')


<mask token>

<mask token>


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:
        print('error')
        print(e)
        return False
    return True


def on_message(ws, message):
    global closes, in_position
    print('received message')
    json_message = json.loads(message)
    pprint.pprint(json_message)
    candle = json_message['k']
    is_candle_closed = candle['x']
    close = candle['c']
    if is_candle_closed:
        print('Candle closed at {}'.format(close))
        closes.append(float(close))
        print('closes')
        print(closes)
        if len(closes) > RSI_PERIOD:
            np_closes = numpy.array(closes)
            rsi = talib.RSI(np_closes, RSI_PERIOD)
            print('all rsis calculated so far')
            print(rsi)
            last_rsi = rsi[-1]
            print('the current rsi is {}'.format(last_rsi))
            if last_rsi > RSI_OVERBOUGHT:
                if in_position:
                    print('overbought SELL')
                    order_succeeded = order(SIDE_SELL, TRADE_QUANTITY,
                        TRADE_SYMBOL)
                    if order_succeeded:
                        in_position = False
                else:
                    print('overbought, not owned, nothing to do')
            if last_rsi < RSI_OVERSOLD:
                if in_position:
                    print('oversold, owned, nothing to do')
                else:
                    print('oversold BUY')
                    order_succeeded = order(SIDE_BUY, TRADE_QUANTITY,
                        TRADE_SYMBOL)
                    if order_succeeded:
                        in_position = True


def on_error(ws, error):
    print('received error', error)


def on_close(ws):
    print('closed connection')


def on_open(ws):
    print('opened connection')


<mask token>
ws.run_forever()

<mask token>
SOCKET = 'wss://stream.binance.com:9443/ws/dogeusdt@kline_1m'
RSI_PERIOD = 14
RSI_OVERBOUGHT = 72
RSI_OVERSOLD = 28
TRADE_SYMBOL = 'DOGEUSDT'
TRADE_QUANTITY = 250
closes = []
in_position = 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:
        print('error')
        print(e)
        return False
    return True


def on_message(ws, message):
    global closes, in_position
    print('received message')
    json_message = json.loads(message)
    pprint.pprint(json_message)
    candle = json_message['k']
    is_candle_closed = candle['x']
    close = candle['c']
    if is_candle_closed:
        print('Candle closed at {}'.format(close))
        closes.append(float(close))
        print('closes')
        print(closes)
        if len(closes) > RSI_PERIOD:
            np_closes = numpy.array(closes)
            rsi = talib.RSI(np_closes, RSI_PERIOD)
            print('all rsis calculated so far')
            print(rsi)
            last_rsi = rsi[-1]
            print('the current rsi is {}'.format(last_rsi))
            if last_rsi > RSI_OVERBOUGHT:
                if in_position:
                    print('overbought SELL')
                    order_succeeded = order(SIDE_SELL, TRADE_QUANTITY,
                        TRADE_SYMBOL)
                    if order_succeeded:
                        in_position = False
                else:
                    print('overbought, not owned, nothing to do')
            if last_rsi < RSI_OVERSOLD:
                if in_position:
                    print('oversold, owned, nothing to do')
                else:
                    print('oversold BUY')
                    order_succeeded = order(SIDE_BUY, TRADE_QUANTITY,
                        TRADE_SYMBOL)
                    if order_succeeded:
                        in_position = True


def on_error(ws, error):
    print('received error', error)


def on_close(ws):
    print('closed connection')


def on_open(ws):
    print('opened connection')


ws = websocket.WebSocketApp(SOCKET, on_open=on_open, on_close=on_close,
    on_message=on_message, on_error=on_error)
ws.run_forever()

import websocket, json, pprint, talib, numpy
import config
from binance.client import Client
from binance.enums import *
SOCKET = 'wss://stream.binance.com:9443/ws/dogeusdt@kline_1m'
RSI_PERIOD = 14
RSI_OVERBOUGHT = 72
RSI_OVERSOLD = 28
TRADE_SYMBOL = 'DOGEUSDT'
TRADE_QUANTITY = 250
closes = []
in_position = 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:
        print('error')
        print(e)
        return False
    return True


def on_message(ws, message):
    global closes, in_position
    print('received message')
    json_message = json.loads(message)
    pprint.pprint(json_message)
    candle = json_message['k']
    is_candle_closed = candle['x']
    close = candle['c']
    if is_candle_closed:
        print('Candle closed at {}'.format(close))
        closes.append(float(close))
        print('closes')
        print(closes)
        if len(closes) > RSI_PERIOD:
            np_closes = numpy.array(closes)
            rsi = talib.RSI(np_closes, RSI_PERIOD)
            print('all rsis calculated so far')
            print(rsi)
            last_rsi = rsi[-1]
            print('the current rsi is {}'.format(last_rsi))
            if last_rsi > RSI_OVERBOUGHT:
                if in_position:
                    print('overbought SELL')
                    order_succeeded = order(SIDE_SELL, TRADE_QUANTITY,
                        TRADE_SYMBOL)
                    if order_succeeded:
                        in_position = False
                else:
                    print('overbought, not owned, nothing to do')
            if last_rsi < RSI_OVERSOLD:
                if in_position:
                    print('oversold, owned, nothing to do')
                else:
                    print('oversold BUY')
                    order_succeeded = order(SIDE_BUY, TRADE_QUANTITY,
                        TRADE_SYMBOL)
                    if order_succeeded:
                        in_position = True


def on_error(ws, error):
    print('received error', error)


def on_close(ws):
    print('closed connection')


def on_open(ws):
    print('opened connection')


ws = websocket.WebSocketApp(SOCKET, on_open=on_open, on_close=on_close,
    on_message=on_message, on_error=on_error)
ws.run_forever()

import websocket, json, pprint, talib, numpy
import config
from binance.client import Client
from binance.enums import *

# Stream name <symbol><stream><interval>
# See: https://github.com/binance/binance-spot-api-docs/blob/master/web-socket-streams.md#klinecandlestick-streams

SOCKET = "wss://stream.binance.com:9443/ws/dogeusdt@kline_1m"
RSI_PERIOD = 14
RSI_OVERBOUGHT = 72
RSI_OVERSOLD = 28
TRADE_SYMBOL = 'DOGEUSDT'
TRADE_QUANTITY = 250

closes = []
in_position = 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:
        print("error")
        print(e)
        return False

    return True

def on_message(ws, message):                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
    global closes, in_position
    print("received message")
    json_message = json.loads(message)                   
    pprint.pprint(json_message)

    candle = json_message["k"]           
    is_candle_closed = candle["x"]
    close = candle["c"]

    if is_candle_closed:
        print("Candle closed at {}".format(close))                                  
        closes.append(float(close))
        print("closes")
        print(closes)

        if len(closes) > RSI_PERIOD:
            np_closes = numpy.array(closes)
            rsi = talib.RSI(np_closes, RSI_PERIOD)
            print("all rsis calculated so far")
            print(rsi)      
            last_rsi = rsi[-1]
            print("the current rsi is {}".format(last_rsi))

            if last_rsi > RSI_OVERBOUGHT:
                if in_position:
                    print("overbought SELL")
                    order_succeeded = order(SIDE_SELL, TRADE_QUANTITY, TRADE_SYMBOL)
                    if order_succeeded:
                        in_position = False
                else:
                    print("overbought, not owned, nothing to do")
            
            if last_rsi < RSI_OVERSOLD:
                if in_position:
                    print("oversold, owned, nothing to do")
                else:
                    print("oversold BUY")
                    order_succeeded = order(SIDE_BUY, TRADE_QUANTITY, TRADE_SYMBOL)
                    if order_succeeded:
                        in_position = True

                                 
def on_error(ws, error):                                         
    print("received error", error)
       

def on_close(ws):
    print("closed connection")


def on_open(ws):    
    print("opened connection")


ws = websocket.WebSocketApp(SOCKET,
                            on_open=on_open,
                            on_close=on_close,
                            on_message=on_message,
                            on_error=on_error)

ws.run_forever()           
