Ejemplo n.º 1
0
    def __init__(self, host, port, user, password, db=None, logger=None):
        """
        Initialization of the databse
        The IP and PORT to the MySQL server is needed, also the user and
        password.
        Also the database name must be specified.
        """

        self.__user = user
        self.__password = password
        self.host = host
        self.port = port
        self.db = db
        self.con = None
        self.cursor = None
        self.tables = None

        if logger is None:
            from utils.createLogger import createLogger
            self.logger = createLogger()
        else:
            self.logger = logger
Ejemplo n.º 2
0
                           ("side",side),
                           ("account",self.account)])
        return simplejson.dumps(msg)
    
    def placeOrder(self,ticker,price,quantity,side):
        msg = self.make_order_msg(ticker,price,quantity,side)
        self.ws.send(msg)
        
    def delete(self):
        #Delete the WS
        self.logger.debug("Deleting the WS and threads")
        self.stopping.set()
        try:
            self.ws.close()
            if self.t_ws.isAlive():
                self.t_ws.join()
            if self.export_to_db:
                if self.t_process.isAlive():
                    self.t_process.join()
            self.logger.debug("All threads and websocket closed")
        except:
            self.logger.exception("Error trying to close the WS and close the thread.")

        
if __name__ == '__main__':
    from utils.createLogger import createLogger
    logger = createLogger()
    WS = websocket(user="******", password="******", entorno=1, account='REM898')
    
  
Ejemplo n.º 3
0
    def __init__(self):
        #create the logger
        self.logger = createLogger.createLogger()

        self.ticker = 'BTCUSDT'
        #read the credentials from the file
        self.__api_key = credentials_binance['key']
        self.__api_secret = credentials_binance['secret']
        self.__mysql_host = credentials_mysql['host']
        self.__mysql_port = credentials_mysql['port']
        self.__mysql_user = credentials_mysql['user']
        self.__mysql_password = credentials_mysql['password']
        self.reconnect_count = 0

        #set no proxies, for the case that the proxy is configured in the system
        self.proxies = {'http': '', 'https': ''}

        #define the Client
        self.client = Client(self.__api_key, self.__api_secret,
                             {'proxies': self.proxies})

        self.exchange_info = self.client.get_exchange_info()
        self.logger.info("Client initialized")

        #%% Websocket to get the MD
        self.bm = BinanceSocketManager(self.client, user_timeout=60)

        #open DB class handle
        self.db = DBtools.DB(host=self.__mysql_host,
                             port=self.__mysql_port,
                             user=self.__mysql_user,
                             password=self.__mysql_password,
                             db='binance',
                             logger=self.logger)

        # start any sockets here, i.e a trade socket
        self.createWS()
        # then start the socket manager
        self.startWS()

        try:
            while True:
                if not self.bm.isAlive():
                    #if the connection is lost, wait for a second and reconnect.
                    self.logger.error(
                        "The connection was lost, maybe from the server side")
                    self.logger.debug("trying to reconnect")
                    time.sleep(1)
                    self.bm.start()
                    if self.bm.isAlive():
                        self.logger.debug("connection re-established")
                    else:
                        self.logger.error(
                            "Could not reconnect, sleeping for 1 minute and retry"
                        )
                        time.sleep(60)
        except KeyboardInterrupt:
            self.logger.info("BTCReader closed by user")
            self.closeWS()
            self.db.close()
        except Exception:
            self.logger.exception("BTCReader closed by error")
            self.logger.debug(
                "Clossing connection to the websocket and database")
            self.closeWS()
            self.db.close()
            sys.exit()