Пример #1
0
    def getTradingSymbols(self, quote_assets: list = None):
        """ Gets All symbols which are tradable (currently) 
            Params
            --
                `quote_assets`
                    a list of all the quote assets we are interested in;
                    if empty or None, we will return all trading pairs, 
                    indifferent of quote asset
            Returns
            --
                a list of symbols that are currently trading on Binance 
        """
        url = Binance.BASE_URL + Binance.ENDPOINTS["exchangeInfo"]
        data = self._get(url)
        if not Binance.isValidResponse(data):
            raise ExchangeConnectionException()

        symbols_list = []
        if quote_assets != None and len(quote_assets) > 0:
            for pair in data['symbols']:
                if pair['status'] == 'TRADING':
                    if pair['quoteAsset'] in quote_assets:
                        symbols_list.append(pair['symbol'])
        else:
            for pair in data['symbols']:
                if pair['status'] == 'TRADING':
                    symbols_list.append(pair['symbol'])

        return symbols_list
Пример #2
0
    def getOHLCV(self, symbol:str, interval:str, 
    limit:int=1000, end_time:any=False, start_time:any=False, cast_to:type=float):
        """
        Gets candlestick data for one symbol 
        
        Parameters
        --
            symbol str:
                The symbol for which to get the trading data
            interval str:
                The interval on which to get the trading data
                minutes: '1m' '3m' '5m' '15m' '30m' 
                hours: '1h' '2h' '4h' '6h' '8h' '12h'
                days: '1d' '3d' weeks: '1w' months: '1M'
            limit int: 
                The number of data points (candles) to return
            `end_time` bool/int:
                The time of the last candle (if False, uses current time)
            
        Returns
        --
            DataFrame containing OHLCV data.
        """

        if limit > 1000:
            return self._getSymbolKlinesExtra(
                symbol, interval, limit, end_time, cast_to=cast_to)
        
        params = '?&symbol='+symbol+'&interval='+interval+'&limit='+str(limit)
        if end_time:
            params = params + '&endTime=' + str(int(end_time))
        if start_time:
            params = params + '&startTime=' + str(int(start_time))

        url = Binance.BASE_URL + Binance.ENDPOINTS['klines'] + params

        # download data
        try:
            data = requests.get(url)
        except requests.exceptions.ConnectionError:
            raise ExchangeConnectionException()
        dictionary = json.loads(data.text)

        # put in dataframe and clean-up
        df = pandas.DataFrame.from_dict(dictionary)
        df = df.drop(range(6, 12), axis=1)

        # rename columns
        col_names = ['time', 'open', 'high', 'low', 'close', 'volume']
        df.columns = col_names

        # transform values from strings to floats
        for col in col_names:
            df[col] = df[col].astype(cast_to)

        df['date'] = pandas.to_datetime(df['time'] * 1000000, infer_datetime_format=True)

        return df
Пример #3
0
    def _get(self, url, params=None, headers=None):
        """ Implements a get request for this exchange """
        try:
            response = requests.get(url, params=params, headers=headers)
            payload = json.loads(response.text)
            if type(payload) == type([]):
                data = dict(payload=payload, url=url)
            else:
                data = payload
                data['url'] = url

        except requests.exceptions.ConnectionError:
            raise ExchangeConnectionException()
        except Exception as e:
            print("Exception occured when trying to GET from " + url)
            print_exc()
            data = {'code': '-1', 'url': url, 'msg': e}
            # raise ExchangeConnectionException()
        return data