def test_getOrdersInvalidStatus():
    filename = 'config.json'

    with open(filename) as config_file:
        config = json.load(config_file)

        api_key = ''
        api_secret = ''
        api_passphrase = ''
        api_url = ''
        if 'api_key' in config and 'api_secret' in config and 'api_pass' in config and 'api_url' in config:
            api_key = config['api_key']
            api_secret = config['api_secret']
            api_passphrase = config['api_pass']
            api_url = config['api_url']

        elif 'api_key' in config['coinbasepro'] and 'api_secret' in config[
                'coinbasepro'] and 'api_passphrase' in config[
                    'coinbasepro'] and 'api_url' in config['coinbasepro']:
            api_key = config['coinbasepro']['api_key']
            api_secret = config['coinbasepro']['api_secret']
            api_passphrase = config['coinbasepro']['api_passphrase']
            api_url = config['coinbasepro']['api_url']

    exchange = AuthAPI(api_key, api_secret, api_passphrase, api_url)
    assert type(exchange) is AuthAPI

    with pytest.raises(ValueError) as execinfo:
        exchange.getOrders(status='ERROR')
    assert str(execinfo.value) == 'Invalid order status.'
def test_get_orders():
    api_key = app.getAPIKey()
    api_secret = app.getAPISecret()
    api_passphrase = app.getAPIPassphrase()
    api_url = "https://public.sandbox.pro.coinbase.com"
    exchange = AuthAPI(api_key, api_secret, api_passphrase, api_url)
    assert type(exchange) is AuthAPI

    df = exchange.getOrders()
    assert type(df) is pandas.core.frame.DataFrame
    assert len(df) > 0
    actual = df.columns.to_list()
    expected = ['created_at', 'market', 'action', 'type', 'size', 'filled', 'status', 'price']
    assert len(actual) == len(expected)
    diff = set(actual) ^ set(expected)
    assert not diff
def test_getOrdersValidStatusActive():
    filename = 'config.json'

    with open(filename) as config_file:
        config = json.load(config_file)

        api_key = ''
        api_secret = ''
        api_passphrase = ''
        api_url = ''
        if 'api_key' in config and 'api_secret' in config and 'api_pass' in config and 'api_url' in config:
            api_key = config['api_key']
            api_secret = config['api_secret']
            api_passphrase = config['api_pass']
            api_url = config['api_url']

        elif 'api_key' in config['coinbasepro'] and 'api_secret' in config[
                'coinbasepro'] and 'api_passphrase' in config[
                    'coinbasepro'] and 'api_url' in config['coinbasepro']:
            api_key = config['coinbasepro']['api_key']
            api_secret = config['coinbasepro']['api_secret']
            api_passphrase = config['coinbasepro']['api_passphrase']
            api_url = config['coinbasepro']['api_url']

    exchange = AuthAPI(api_key, api_secret, api_passphrase, api_url)
    assert type(exchange) is AuthAPI

    df = exchange.getOrders(status='active')

    if len(df) == 0:
        pass
    else:
        actual = df.columns.to_list()
        expected = [
            'created_at', 'market', 'action', 'type', 'size', 'value',
            'status', 'price'
        ]
        assert len(actual) == len(expected)
        assert all([a == b for a, b in zip(actual, expected)])
def getValidOrderMarket() -> str:
    filename = 'config.json'
    assert os.path.exists(filename) == True
    with open(filename) as config_file:
        config = json.load(config_file)

        if 'api_key' in config and 'api_secret' in config and (
                'api_pass' in config
                or 'api_passphrase' in config) and 'api_url' in config:
            api_key = config['api_key']
            api_secret = config['api_secret']
            if 'api_pass' in config:
                api_passphrase = config['api_pass']
            else:
                api_passphrase = config['api_passphrase']
            api_url = config['api_url']
        elif 'coinbasepro' in config:
            if 'api_key' in config['coinbasepro'] and 'api_secret' in config[
                    'coinbasepro'] and 'api_passphrase' in config[
                        'coinbasepro'] and 'api_url' in config['coinbasepro']:
                api_key = config['coinbasepro']['api_key']
                api_secret = config['coinbasepro']['api_secret']
                api_passphrase = config['coinbasepro']['api_passphrase']
                api_url = config['coinbasepro']['api_url']
            else:
                return DEFAULT_ORDER_MARKET
        else:
            return DEFAULT_ORDER_MARKET

        time.sleep(0.5)
        exchange = AuthAPI(api_key, api_secret, api_passphrase, api_url)
        df = exchange.getOrders()
        if len(df) == 0:
            return DEFAULT_ORDER_MARKET

        return df['market'].tail(1).values[0]
    return DEFAULT_ORDER_MARKET
Exemplo n.º 5
0
    def getLastBuy(self) -> dict:
        """Retrieves the last exchange buy order and returns a dictionary"""

        try:
            if self.exchange == 'coinbasepro':
                api = CBAuthAPI(self.getAPIKey(), self.getAPISecret(),
                                self.getAPIPassphrase(), self.getAPIURL())
                orders = api.getOrders(self.getMarket(), '', 'done')

                if len(orders) == 0:
                    return None

                last_order = orders.tail(1)
                if last_order['action'].values[0] != 'buy':
                    return None

                return {
                    'side':
                    'buy',
                    'market':
                    self.getMarket(),
                    'size':
                    float(last_order['size']),
                    'filled':
                    float(last_order['filled']),
                    'price':
                    float(last_order['price']),
                    'fee':
                    float(last_order['fees']),
                    'date':
                    str(
                        pd.DatetimeIndex(
                            pd.to_datetime(
                                last_order['created_at']).dt.strftime(
                                    '%Y-%m-%dT%H:%M:%S.%Z'))[0])
                }
            elif self.exchange == 'binance':
                api = BAuthAPI(self.getAPIKey(), self.getAPISecret(),
                               self.getAPIURL())
                orders = api.getOrders(self.getMarket())

                if len(orders) == 0:
                    return None

                last_order = orders.tail(1)
                if last_order['action'].values[0] != 'buy':
                    return None

                return {
                    'side':
                    'buy',
                    'market':
                    self.getMarket(),
                    'size':
                    float(last_order['size']),
                    'filled':
                    float(last_order['filled']),
                    'price':
                    float(last_order['price']),
                    'fees':
                    float(last_order['size'] * 0.001),
                    'date':
                    str(
                        pd.DatetimeIndex(
                            pd.to_datetime(
                                last_order['created_at']).dt.strftime(
                                    '%Y-%m-%dT%H:%M:%S.%Z'))[0])
                }
            else:
                return None
        except Exception:
            return None
Exemplo n.º 6
0
            for config_item in config_list:
                if "cryptoMarket" in config:
                    base_currency = config["cryptoMarket"]
                elif "base_currency" in config:
                    base_currency = config["base_currency"]

                if "fiatMarket" in config:
                    quote_currency = config["fiatMarket"]
                elif "base_currency" in config:
                    quote_currency = config["quote_currency"]

                market = base_currency + "-" + quote_currency

                api = CBAuthAPI(api_key, api_secret, api_pass)

                orders = api.getOrders()
                df = pd.concat([df, orders])

                transfers = api.getTransfers()
                df = pd.concat([df, transfers])

    df["created_at"] = df["created_at"].map(
        lambda x: re.sub(r"\.\d{1,6}\+00$", "", str(x))
    )
    df["created_at"] = df["created_at"].map(lambda x: re.sub(r"\+00:00$", "", str(x)))

    try:
        df.to_csv("profitandloss.csv", index=False)
    except OSError:
        SystemExit("Unable to save: ", "profitandloss.csv")
Exemplo n.º 7
0
                total_markets += 1

                if "cryptoMarket" in config_item:
                    base_currency = config_item["cryptoMarket"]
                elif "base_currency" in config_item:
                    base_currency = config_item["base_currency"]

                if "fiatMarket" in config_item:
                    quote_currency = config_item["fiatMarket"]
                elif "base_currency" in config_item:
                    quote_currency = config_item["quote_currency"]

                market = base_currency + "-" + quote_currency

                api = CBAuthAPI(api_key, api_secret, api_pass)
                orders = api.getOrders(market)

                last_action = ""
                if len(orders) > 0:
                    for market in orders["market"].sort_values().unique():
                        df_market = orders[orders["market"] == market]
                else:
                    df_market = pd.DataFrame()

                df_buy = pd.DataFrame()
                df_sell = pd.DataFrame()

                pair = 0
                # pylint: disable=unused-variable
                for index, row in df_market.iterrows():
                    if row["action"] == "buy":