Beispiel #1
0
class CoinBasePro:

    public_client = cbpro.PublicClient()

    def __init__(self, config):
        self.auth_client = cbpro.AuthenticatedClient(config['API_KEY'],
                                                     config['SECRET'],
                                                     config['PASSPHRASE'])

    def wallet(self, id_wallet):
        return self.auth_client.get_account(id_wallet)

    def eth_price(self):
        return self.public_client.get_product_ticker(
            product_id='ETH-EUR')['price']

    def total_amount(self):

        try:
            eth_wallet = self.wallet('c9b25431-6e04-4fc7-bd80-3f8d3e1ff060')
            eur_wallet = self.wallet('ad260d00-a9bd-4dd6-995c-36b01ffb4efb')

            eth_wallet = float(eth_wallet['balance'])
            eur_wallet = float(eur_wallet['balance'])

            eth_price = float(self.eth_price())

            total = round(eur_wallet + (eth_wallet * eth_price), 2)

            return total
        except KeyError as err:
            raise err
        except ValueError as err:
            raise err
Beispiel #2
0
def get_currency_price(currency_ticker, client=None):
    if currency_ticker == "CS":
        return 1.0
    if client is None:
        client = cbpro.PublicClient()
    pstr = client.get_product_24hr_stats("{0}-USD".format(currency_ticker))["last"]
    return float(pstr)
Beispiel #3
0
 def btc_true():
     client = cbpro.PublicClient()
     x = client.get_product_ticker(product_id='BTC-USD')
     for i in x:
         a = x['price']
         b = float(a)
     return b
Beispiel #4
0
def get_btc_price():
    public_client = cbpro.PublicClient()
    btc = public_client.get_product_ticker("BTC-EUR")

    for key, value in btc.items():
        if key == "price":
            return value
def process(m_granularity: Granularity = Granularity.ONE_MINUTE,
            **kwargs) -> None:
    start_time, end_time = find_time_span(time_span=kwargs.get('time_span'),
                                          start_time=kwargs.get('start_time'),
                                          end_time=kwargs.get('end_time'),
                                          granularity=m_granularity)

    host = Config['mongo_db']['host']
    port = Config['mongo_db']['port']
    db_name = Config['mongo_db']['database']
    historical_candle_stick_suffix = Config['mongo_db']['collection_suffix']

    mongo_client = MongoClient(host, port)

    db: Database = mongo_client[db_name]

    cb_public_client: PublicClient = cbpro.PublicClient()

    product_collection = cb_public_client.get_products()

    for product in product_collection:
        get_historic_candles_for_product(cb_public_client,
                                         db,
                                         historical_candle_stick_suffix,
                                         product,
                                         start_time,
                                         end_time,
                                         m_granularity=m_granularity)
Beispiel #6
0
 def __init__(self, API_KEY, SECRET_KEY, PASSPHRASE):
     self.API_KEY = API_KEY
     self.SECRET_KEY = SECRET_KEY
     self.PASSPHRASE = PASSPHRASE
     self.auth_client = cbpro.AuthenticatedClient(API_KEY, SECRET_KEY,
                                                  PASSPHRASE)
     self.public_client = cbpro.PublicClient()
Beispiel #7
0
def get_cb_client(client_type,
                  key=None,
                  secret=None,
                  passphrase=None,
                  api_url="https://api.pro.coinbase.com"):
    """

    :param client_type:
    :param key:
    :param secret:
    :param passphrase:
    :param api_url:
    :return:
    usage:
    >>> key='1b19ba867bf0e06f93f583b88825d485'
    >>> secret = 'TKCdKrstN+eX7KKZAhG08rPCh3Yo7kMcf9CUXEaE9TlQ/HAOzXely9UkdVv4PdVUtieW8eP97I9kXd0d4I2AGQ=='
    >>> passphrase = 'uihizupz01'
    >>> api_url = 'https://api.pro.coinbase.com'
    >>> client = get_cb_client('private', key, secret, passphrase)
    """
    if client_type == 'public':
        client = cbpro.PublicClient()
    else:
        client = cbpro.AuthenticatedClient(key,
                                           secret,
                                           passphrase,
                                           api_url=api_url)
    return client
Beispiel #8
0
    def get_historical_data(self, num_periods=200):
        cbpro_client = cbpro.PublicClient()

        end = datetime.datetime.utcnow()
        end_iso = end.isoformat()
        start = end - datetime.timedelta(seconds=(self.period_size * num_periods))
        start_iso = start.isoformat()

        ret_base = cbpro_client.get_product_historic_rates(self.base, granularity=self.period_size, start=start_iso, end=end_iso)
        ret_quoted = cbpro_client.get_product_historic_rates(self.quoted, granularity=self.period_size, start=start_iso, end=end_iso)
        # Check if we got rate limited, which will return a JSON message
        while not isinstance(ret_base, list):
            time.sleep(3)
            ret_base = cbpro_client.get_product_historic_rates(self.base, granularity=self.period_size, start=start_iso, end=end_iso)
        while not isinstance(ret_quoted, list):
            time.sleep(3)
            ret_quoted = cbpro_client.get_product_historic_rates(self.quoted, granularity=self.period_size, start=start_iso, end=end_iso)
        hist_data_base = np.array(ret_base, dtype='object')
        hist_data_quoted = np.array(ret_quoted, dtype='object')
        array_size = min(len(ret_base), len(ret_quoted))
        hist_data_base.resize(array_size, 6)
        hist_data_quoted.resize(array_size, 6)

        for row in hist_data_base:
            row[0] = datetime.datetime.fromtimestamp(row[0], pytz.utc)
        for row in hist_data_quoted:
            row[0] = datetime.datetime.fromtimestamp(row[0], pytz.utc)

        hist_data = np.ndarray((len(hist_data_base), 6), dtype='object')
        hist_data[:, 0] = hist_data_base[:, 0]
        hist_data[:, [1,2,3,4]] = hist_data_base[:, [1,2,3,4]]/hist_data_quoted[:, [1,2,3,4]]
        total_price = (hist_data_base[:, 4] + hist_data_quoted[:, 4])
        hist_data[:, 5] = ((hist_data_base[:, 4] / total_price) * hist_data_base[:, 5]) + ((hist_data_base[:, 4] / total_price)  * hist_data_quoted[:, 5])
        hist_data[:, 5] = hist_data[:, 5] * hist_data[:, 4]
        return np.flipud(hist_data)
Beispiel #9
0
def main():
    """
    The main portion of the application.
    """
    pc = cbpro.PublicClient()

    while True:
        try:
            current_ticker = pc.get_product_ticker(product_id=PRODUCT_ID)
            current_ticker = float(current_ticker['price'])
            print(f"{PRODUCT_ID} = {current_ticker}")
            if current_ticker >= PRICE_THRSHOLD and THRESHOLD_DIRECTION == "UPPER":
                pn.pushbullet_message(
                    f"{PRODUCT_ID} Alert",
                    f"Price of {PRODUCT_ID}: {current_ticker}\nThreshold set to: {PRICE_THRSHOLD}"
                )
                time.sleep(WAIT_TIME)  # sleep for 6 hours.
            if current_ticker <= PRICE_THRSHOLD and THRESHOLD_DIRECTION == "LOWER":
                pn.pushbullet_message(
                    f"{PRODUCT_ID} Alert",
                    f"Price of {PRODUCT_ID}: {current_ticker}\nThreshold set to: {PRICE_THRSHOLD}"
                )
                time.sleep(WAIT_TIME)  # sleep for 6 hours.
        except KeyboardInterrupt:
            break
        except ConnectionError:
            continue
        finally:
            time.sleep(10)
Beispiel #10
0
def updateValues(oldDf):
    '''
    Gets newwest prices from coinbase api
    * oldDf - the dataframe currently used, with old prices and dates. Check inniPriceHistory.
    '''
    startDate = oldDf.iloc[-1, 0]
    public_client = cbpro.PublicClient()  # connect to public API
    new_list = public_client.get_product_historic_rates(
        'BTC-EUR',
        granularity=60,
        start=startDate,
        end=public_client.get_time().get('iso'))

    df = pd.DataFrame(
        new_list, columns=['time', 'low', 'high', 'open', 'close', 'volume'])

    df.drop_duplicates(inplace=True)

    dates = [
        datetime.datetime.utcfromtimestamp(i).strftime('%Y-%m-%dT%H:%M:%S.%f')
        [:-3] + 'Z' for i in df.time
    ]  # converts time
    df['time'] = dates
    df = df.sort_values(by='time').reset_index(drop=True)

    if len(df) > 1:
        df = pd.concat([oldDf, df], axis=0)
        didUpdate = 1
    else:
        df = oldDf
        didUpdate = 0

    df = df[len(df) - 28800:]

    return (df, didUpdate)
Beispiel #11
0
 def on_open(self):
     self.url = "wss://ws-feed.pro.coinbase.com/"
     logging.info('Downloads product:{}'.format(self.products))
     self.message_count = 0
     self.file = None
     self.public_client = cbpro.PublicClient()
     save_snapshot(self.public_client, self.data_dir)
Beispiel #12
0
def get_price(coin, current_balance):
    if current_balance != 0:
        price = float(cbpro.PublicClient().get_product_ticker(
            product_id=f"{coin}-USD")["price"])
        return round(price * current_balance)
    else:
        return 0
Beispiel #13
0
 def __init__(self, initial_money=100000):
     self.current_money = initial_money
     self.symbol_table = create_symbol_table()
     self.trans_table = create_transaction_table()
     self.pnl_table = create_pnl_table()
     self.client = cbpro.PublicClient()
     self.stocks = {}
Beispiel #14
0
    def __init__(self,
                 api_key,
                 secret_key,
                 passphrase,
                 sym_list,
                 base_currency='USD',
                 offset_value=70,
                 is_sandbox=False):
        self.wallets = {}

        if is_sandbox:
            api_base = 'https://api-public.sandbox.pro.coinbase.com'
            auth_client = cbpro.AuthenticatedClient(api_key,
                                                    secret_key,
                                                    passphrase,
                                                    api_url=api_base)
            sleep(PRIVATE_SLEEP)
            pub_client = cbpro.PublicClient(api_url=api_base)
        else:
            auth_client = cbpro.AuthenticatedClient(api_key, secret_key,
                                                    passphrase)
            sleep(PRIVATE_SLEEP)
            pub_client = cbpro.PublicClient()

        self.auth = auth_client

        for product_id in sym_list:
            # The base can be put in the symbols in the format Quote-Base for use with multiple currencies
            if (type(base_currency) is list) or (type(base_currency) is tuple):
                product_dat = product_id.split('-')
                symbol = product_dat[0]
                base = product_dat[1]
                if base not in base_currency:
                    continue
            else:
                symbol = product_id
                base = base_currency
            self.wallets[product_id] = Wallet(api_key,
                                              secret_key,
                                              passphrase,
                                              base=base,
                                              sym=symbol,
                                              auth_client=auth_client,
                                              pub_client=pub_client)
            self.wallets[product_id].offset_value = offset_value

        self.symbols = sym_list
def get_data_by_market_coinbasepro(exchange, market_name):
    market_name = market_name.split(sep='/')
    market_name = '-'.join(market_name)
    coinbasepro = cbpro.PublicClient()
    client = ccxt.gdax
    start_point_time = '2019-04-08T00:00:00'
    actual = epoch_now
    listI = []
    start_point_time_epock = client.parse8601(start_point_time)
    end_point_time = start_point_time_epock + 864000000
    try:
        next_date_start_t = client.iso8601(start_point_time_epock)
        end_point_time_ = client.iso8601(end_point_time)
        epoch_next_date_start = 0
        flag = True
        while epoch_next_date_start < actual and flag is True:
            time.sleep(1)
            hist = coinbasepro.get_product_historic_rates(
                market_name,
                start=next_date_start_t,
                end=end_point_time_,
                granularity=3600)
            listI = listI + hist
            next_date_start_t = end_point_time_
            end_point_time_ = client.parse8601(end_point_time_)
            end_point_time_ = end_point_time_ + 864000000
            epoch_next_date_start = end_point_time_
            if end_point_time_ >= actual:
                flag = False
            else:
                end_point_time_ = client.iso8601(end_point_time_)
        df = pd.DataFrame(
            listI,
            columns=["timestamp", "open", "high", "low", "close", "volume"])
        df.to_sql(name='{}_{}'.format(exchange, market_name),
                  con=mydb,
                  if_exists='replace',
                  index=False,
                  index_label='id')
        print(listI)
    except Exception as error:

        # Create a custom logger
        logger = logging.getLogger(__name__)

        # Create handlers
        f_handler = logging.FileHandler('coinbasepro_exchanges.log')

        # Create formatters and add it to handlers
        f_format = logging.Formatter(
            '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        f_handler.setFormatter(f_format)

        # Add handlers to the logger
        logger.addHandler(f_handler)
        logger.error('EXCHANGE: %s ERROR %s', exchange, error)
        logging.error('COIN: %s', market_name)

        pass
Beispiel #16
0
 def __init__(self, key: str = '', secret: str = '', pwd: str = ''):
     # by default will have a public client
     self.public_client = cbpro.PublicClient()
     # whether to create an authenticated client
     if len(key) > 0 and len(secret) > 0:
         self.auth_client = Client(key, secret)
     else:
         self.auth_client = None
Beispiel #17
0
 def __init__(self, key, b64secret, passphrase):
     self.url = "https://api.pro.coinbase.com"
     self.auth_client = cbpro.AuthenticatedClient(key, b64secret,
                                                  passphrase)
     self.public_client = cbpro.PublicClient()
     self.account_data_w_balance = []
     self.instrument_list = []
     self.set_account_data()
Beispiel #18
0
 def fetch_price_history_data_nonAPI(self,symbol,start,end,granularity):
     # define class
     public_client = cbpro.PublicClient()
     try:
         history = public_client.get_product_historic_rates(symbol, start, end, granularity)
     except:
         print('Error pulling historical data')
     return history
def get_product_data(pair):
    output = {}
    public_client = cbpro.PublicClient()
    products = public_client.get_products()
    for item in products:
        if item['id'] == pair:
            output.update(item)
    return output
Beispiel #20
0
    def get_price(self, product_id):
        # Import cbpro classes
        # Get price for the given currency to USD.
        public_client = cbpro.PublicClient()

        ticker_info = public_client.get_product_ticker(product_id + "-USD")
        price = ticker_info['price']

        return price
def get_historic_data(pair='BTC-USD', granularity=900, **options):
    public_client = cbpro.PublicClient()
    history = public_client.get_product_historic_rates(pair,
                                                       granularity=granularity)
    history_array = np.array(history)
    history_pd = pd.DataFrame(
        history_array,
        columns=['time', 'low', 'high', 'open', 'close', 'volume'])
    return history_pd, history_array
Beispiel #22
0
def get_current_price(coin):
    last_update = time.time()
    current_price = Decimal('0.0')
    if not coin in PRICE_CACHE:
        public_client = cbpro.PublicClient()
        ticker = public_client.get_product_ticker(product_id=coin)
        current_price = Decimal(ticker['price'])
    else:
        # check cache age
        if time.time() - PRICE_CACHE[coin]['last_update'] > PRICE_CACHE_RATE:
            public_client = cbpro.PublicClient()
            ticker = public_client.get_product_ticker(product_id=coin)
            current_price = Decimal(ticker['price'])
        else:
            last_update = PRICE_CACHE[coin]['last_update']
            current_price = PRICE_CACHE[coin]['price']
    PRICE_CACHE[coin] = {'price': current_price, 'last_update': last_update}
    return Decimal(current_price)
Beispiel #23
0
	def __init__(self):
		self.folder = "./log_cbpro/"

		self._public_client = cbpro.PublicClient()

		self._key = os.getenv("API_KEY_SANDBOX")
		self._b64secret = os.getenv("API_SECRET_SANDBOX")
		self._passphrase = os.getenv("API_PASSPHRASE_SANDBOX")

		self._auth_client = cbpro.AuthenticatedClient(self._key, self._b64secret, self._passphrase, api_url="https://api-public.sandbox.pro.coinbase.com")
Beispiel #24
0
def fetchCoins():
    # url = 'https://api.coindesk.com/v1/bpi/currentprice.json'
    # raw_response = requests.get(url)
    # print(raw_response.json())

    public_client = cbpro.PublicClient()
    btc_price = public_client.get_product_24hr_stats('BTC-USD')['last']
    eth_price = public_client.get_product_24hr_stats('ETH-USD')['last']
    ltc_price = public_client.get_product_24hr_stats('LTC-USD')['last']
    return (btc_price, eth_price, ltc_price)
Beispiel #25
0
    def __init__(self,
                 api_key,
                 secret_key,
                 passphrase,
                 prediction_ticker='ETH',
                 is_sandbox_api=False,
                 auth_client=None,
                 pub_client=None,
                 base_ticker='USD'):

        self.product_id = prediction_ticker.upper() + '-' + base_ticker
        if not (base_ticker == 'USD'):
            prediction_ticker = self.product_id
        self.orders = {'buy': {}, 'sell': {}}
        self.usd_decimal_num = EXCHANGE_CONSTANTS[prediction_ticker][
            'resolution']
        self.usd_res = 10**(-self.usd_decimal_num)
        self.quote_order_min = QUOTE_ORDER_MIN
        self.base_order_min = EXCHANGE_CONSTANTS[prediction_ticker][
            'base order min']
        self.base_decimal_num = EXCHANGE_CONSTANTS[prediction_ticker][
            'base resolution']
        self.crypto_res = 10**(-self.base_decimal_num)
        self.filt_fills = None
        self.all_fills = None
        self.order_book = None
        self.filtered_fill_times = None
        self.fill_time = 0

        if auth_client is None:
            if is_sandbox_api:
                self.api_base = 'https://api-public.sandbox.pro.coinbase.com'
                self.auth_client = cbpro.AuthenticatedClient(
                    api_key, secret_key, passphrase, api_url=self.api_base)
                self.pub_client = cbpro.PublicClient(api_url=self.api_base)
            else:
                self.auth_client = cbpro.AuthenticatedClient(
                    api_key, secret_key, passphrase)
                self.pub_client = cbpro.PublicClient()
        else:
            self.auth_client = auth_client
            self.pub_client = pub_client
Beispiel #26
0
    def __init__(self, UIGraph, Settings):

        currencies = CBProCurrencies.instance()
        first_currency = currencies.get_all_pairs()[0]
        currencies.get_min_sizes()
        super(CBProController, self).__init__(product_id=first_currency,
                                              log_to=False)

        self.theUIGraph = UIGraph
        # Application settings data instance
        self.theSettings = Settings

        self.webSocketIsOpened = False
        self.isRunning = True
        self.requestAccountsBalanceUpdate = True
        self.backgroundOperationsCounter = 0

        self.tickBestBidPrice = 0
        self.tickBestAskPrice = 0
        self.liveBestBidPrice = 0
        self.liveBestAskPrice = 0
        self.midMarketPrice = 0
        self.currentOrderId = 0
        self.currentOrderState = "NONE"  # SUBMITTED / OPENED / FILLED / NONE
        self.currentOrderInitialSizeInCrypto = 0
        self.currentOrderFilledSizeInCrypto = 0
        self.currentOrderAverageFilledPriceInFiat = 0

        self.productStr = self.theSettings.SETT_GetSettings()["strTradingPair"]
        self.productFiatStr = self.theSettings.SETT_GetSettings(
        )["strFiatType"]
        self.productCryptoStr = self.theSettings.SETT_GetSettings(
        )["strCryptoType"]
        self.bFiatAccountExists = False
        self.bCryptoAccountExists = False

        self.HistoricData = []
        self.HistoricDataReadIndex = 0
        self.HistoricDataSubSchedulingIndex = 0

        self.IsConnectedAndOperational = "False"

        self.clientPublic = cbpro.PublicClient()

        # Start background thread
        threadRefreshPrice = threading.Timer(
            1, self.updateRealTimePriceInBackground)
        threadRefreshPrice.start()

        # WebSocket thread
        # Websocket thread is launched by parent classes
        self.webSocketLock = threading.Lock()

        print("CBPro - CBPro Controler Initialization")
Beispiel #27
0
    def run(self, ):

        while 1:
            public_client = cbpro.PublicClient()
            binance = ccxt.binance() 
            ticker = binance.fetch_ticker('BTC/USDT')
            self.lock.acquire()
            self.binance_price[0] = ticker['close']
            self.coinbase_price[0] = public_client.get_product_ticker(product_id='BTC-USD')['price']
            self.lock.release()
            time.sleep(2)
def get_latest(pair='BTC-USD', granularity=900):
    public_client = cbpro.PublicClient()
    start = datetime.now()
    end = start + timedelta(minutes=int(granularity / 60))
    history = public_client.get_product_historic_rates(
        pair,
        granularity=granularity,
        start=str(start.isoformat()),
        end=str(end.isoformat()))
    #history_array = np.array(history)
    return history
Beispiel #29
0
def get_all_prices(update, context):
    # Coinbace Pro Public API
    # https://github.com/danpaquin/coinbasepro-python

    btc_usd = cbpro.PublicClient().get_product_ticker(
        product_id="BTC-USD")["price"]
    xrp_usd = cbpro.PublicClient().get_product_ticker(
        product_id="XRP-USD")["price"]
    eth_usd = cbpro.PublicClient().get_product_ticker(
        product_id="ETH-USD")["price"]
    bch_usd = cbpro.PublicClient().get_product_ticker(
        product_id="BCH-USD")["price"]
    ltc_usd = cbpro.PublicClient().get_product_ticker(
        product_id="LTC-USD")["price"]

    updater.bot.send_message(
        chat_id=LIST_OF_ADMINS[0],
        text=
        f"BTC-USD {str(btc_usd)}\nXRP-USD {str(xrp_usd)}\nETH-USD {str(eth_usd)}\nBCH-USD {str(bch_usd)}\nLTC-USD {str(ltc_usd)}\n\nCoinbasePro rates\n{str(datetime.utcnow())[:19]} (UTC)",
    )
Beispiel #30
0
def scraper(security='BTC-USD',
            request_time=10,
            dump_time=3600,
            send_email=True):
    # BASH script should automatically restart upon any failures
    # Therefore we send an email on reconnect and disconnect
    email_notification('RECONNECT DETECTED')

    public_client = cbpro.PublicClient()

    # Get the current working directory for print statement
    path = os.getcwd()

    historical_ob = pd.DataFrame(columns=['Time', 'Bids', 'Asks'])

    while True:
        time.sleep(0.5)
        try:
            cur_time = math.floor(time.time())
            if cur_time % request_time == 0:
                # Get the order book at level_2 granularity - this is the top 50 bids and asks
                ob = public_client.get_product_order_book(security, 2)

                # Pandas allows for enlargement using loc
                historical_ob.loc[len(historical_ob)] = [
                    cur_time, ob['bids'], ob['asks']
                ]

                # Every hour collected data is flushed to a csv, cleared, and continued
                if cur_time % dump_time == 0:
                    file_name = str(cur_time) + '.csv'
                    historical_ob.to_csv(file_name, index=False)
                    # ToDo: Save to Google Drive
                    print('File:', cur_time, 'saved in', path)
                    historical_ob.drop(historical_ob.index, inplace=True)

                # Ensures only 1 call every 10 seconds - additionally that calls are evenly
                # spaced even when flushing every hour which will take substantially longer
                # running under assumption that a flush is less than 9 seconds plus overhead
                # of first call
                time.sleep(1)
        except:
            print('Error hit')
            if send_email:
                print('Sending email to restart')
                email_notification('DISCONNECT DETECTED')

            print('Saving partial historical order book')
            file_name = str(cur_time) + '.csv'
            historical_ob.to_csv(file_name, index=False)
            print('File:', cur_time, 'saved in', path)

            return -1