0

before this I successfully made a trading bot with the help of you guys -> How to keep the index of my pandas dataframe after normalazation. json

Now I try to use the ccxt library to connect my bot to other exchanges.

When i use the fetch openorder method I get a response that is I think a json inside a list. It works trough ccxt unified api. anyway. so I can print the list[{}, {}] like that. and I can print like the first order or the third, but not all the orders without the [].

I would like to end up with the exact same table as in the above linked forum topic.

here is some code I tried and the response.

        openorders = coinbase.fetch_open_orders('BTC/EUR')
        data = openorders[3]
        print('openorders')
        print(openorders)
        print('data')
        print(data)
        pprint(data)
        pprint(openorders)

output from ccxt :

[{'id': '7c754fdb-c6dd-44cb-9e99-e780052d6d62', 'clientOrderId': None, 'info': {'id': '7c754fdb-c6dd-44cb-9e99-e780052d6d62', 'price': '45971.00000000', 'size': '0.00100000', 'product_id': 'BTC-EUR', 'profile_id': '0ef9a216-77ff-469f-a31b-b62393208a5e', 'side': 'sell', 'type': 'limit', 'time_in_force': 'GTC', 'post_only': False, 'created_at': '2021-03-09T20:38:02.495824Z', 'fill_fees': '0.0000000000000000', 'filled_size': '0.00000000', 'executed_value': '0.0000000000000000', 'status': 'open', 'settled': False}, 'timestamp': 1615322282495, 'datetime': '2021-03-09T20:38:02.495Z', 'lastTradeTimestamp': None, 'status': 'open', 'symbol': 'BTC/EUR', 'type': 'limit', 'timeInForce': 'GTC', 'postOnly': False, 'side': 'sell', 'price': 45971.0, 'stopPrice': None, 'cost': 0.0, 'amount': 0.001, 'filled': 0.0, 'remaining': 0.001, 'fee': {'cost': 0.0, 'currency': 'EUR', 'rate': None}, 'average': None, 'trades': None}, {'id': '9ed743a9-3364-4179-b3e2-936ba6b3d9c7', 'clientOrderId': None, 'info': {'id': '9ed743a9-3364-4179-b3e2-936ba6b3d9c7', 'price': '45997.00000000', 'size': '0.00100000', 'product_id': 'BTC-EUR', 'profile_id': '0ef9a216-77ff-469f-a31b-b62393208a5e', 'side': 'sell', 'type': 'limit', 'time_in_force': 'GTC', 'post_only': False, 'created_at': '2021-03-09T20:38:03.507036Z', 'fill_fees': '0.0000000000000000', 'filled_size': '0.00000000', 'executed_value': '0.0000000000000000', 'status': 'open', 'settled': False}, 'timestamp': 1615322283507, 'datetime': '2021-03-09T20:38:03.507Z', 'lastTradeTimestamp': None, 'status': 'open', 'symbol': 'BTC/EUR', 'type': 'limit', 'timeInForce': 'GTC', 'postOnly': False, 'side': 'sell', 'price': 45997.0, 'stopPrice': None, 'cost': 0.0, 'amount': 0.001, 'filled': 0.0, 'remaining': 0.001, 'fee': {'cost': 0.0, 'currency': 'EUR', 'rate': None}, 'average': None, 'trades': None}, {'id': 'abe9d82f-1d5b-4c00-83b7-664cfda76ac8', 'clientOrderId': None, 'info': {'id': 'abe9d82f-1d5b-4c00-83b7-664cfda76ac8', 'price': '46023.00000000', 'size': '0.00100000', 'product_id': 'BTC-EUR', 'profile_id': '0ef9a216-77ff-469f-a31b-b62393208a5e', 'side': 'sell', 'type': 'limit', 'time_in_force': 'GTC', 'post_only': False, 'created_at': '2021-03-09T20:38:04.738401Z', 'fill_fees': '0.0000000000000000', 'filled_size': '0.00000000', 'executed_value': '0.0000000000000000', 'status': 'open', 'settled': False}, 'timestamp': 1615322284738, 'datetime': '2021-03-09T20:38:04.738Z', 'lastTradeTimestamp': None, 'status': 'open', 'symbol': 'BTC/EUR', 'type': 'limit', 'timeInForce': 'GTC', 'postOnly': False, 'side': 'sell', 'price': 46023.0, 'stopPrice': None, 'cost': 0.0, 'amount': 0.001, 'filled': 0.0, 'remaining': 0.001, 'fee': {'cost': 0.0, 'currency': 'EUR', 'rate': None}, 'average': None, 'trades': None}, {'id': '69c18563-e862-493e-af89-92984ad4ccdb', 'clientOrderId': None, 'info': {'id': '69c18563-e862-493e-af89-92984ad4ccdb', 'price': '45633.00000000', 'size': '0.00100600', 'product_id': 'BTC-EUR', 'profile_id': '0ef9a216-77ff-469f-a31b-b62393208a5e', 'side': 'buy', 'type': 'limit', 'time_in_force': 'GTC', 'post_only': False, 'created_at': '2021-03-09T20:38:05.492519Z', 'fill_fees': '0.0000000000000000', 'filled_size': '0.00000000', 'executed_value': '0.0000000000000000', 'status': 'open', 'settled': False}, 'timestamp': 1615322285492, 'datetime': '2021-03-09T20:38:05.492Z', 'lastTradeTimestamp': None, 'status': 'open', 'symbol': 'BTC/EUR', 'type': 'limit', 'timeInForce': 'GTC', 'postOnly': False, 'side': 'buy', 'price': 45633.0, 'stopPrice': None, 'cost': 0.0, 'amount': 0.001006, 'filled': 0.0, 'remaining': 0.001006, 'fee': {'cost': 0.0, 'currency': 'EUR', 'rate': None}, 'average': None, 'trades': None}, {'id': 'abf57e52-22b1-4f67-99ad-ccc0fe64f685', 'clientOrderId': None, 'info': {'id': 'abf57e52-22b1-4f67-99ad-ccc0fe64f685', 'price': '45607.00000000', 'size': '0.00100600', 'product_id': 'BTC-EUR', 'profile_id': '0ef9a216-77ff-469f-a31b-b62393208a5e', 'side': 'buy', 'type': 'limit', 'time_in_force': 'GTC', 'post_only': False, 'created_at': '2021-03-09T20:38:06.48702Z', 'fill_fees': '0.0000000000000000', 'filled_size': '0.00000000', 'executed_value': '0.0000000000000000', 'status': 'open', 'settled': False}, 'timestamp': 1615322286487, 'datetime': '2021-03-09T20:38:06.487Z', 'lastTradeTimestamp': None, 'status': 'open', 'symbol': 'BTC/EUR', 'type': 'limit', 'timeInForce': 'GTC', 'postOnly': False, 'side': 'buy', 'price': 45607.0, 'stopPrice': None, 'cost': 0.0, 'amount': 0.001006, 'filled': 0.0, 'remaining': 0.001006, 'fee': {'cost': 0.0, 'currency': 'EUR', 'rate': None}, 'average': None, 'trades': None}, {'id': '7681b8cf-ebd8-4666-b810-a951c2ea6a93', 'clientOrderId': None, 'info': {'id': '7681b8cf-ebd8-4666-b810-a951c2ea6a93', 'price': '45581.00000000', 'size': '0.00100600', 'product_id': 'BTC-EUR', 'profile_id': '0ef9a216-77ff-469f-a31b-b62393208a5e', 'side': 'buy', 'type': 'limit', 'time_in_force': 'GTC', 'post_only': False, 'created_at': '2021-03-09T20:38:07.488203Z', 'fill_fees': '0.0000000000000000', 'filled_size': '0.00000000', 'executed_value': '0.0000000000000000', 'status': 'open', 'settled': False}, 'timestamp': 1615322287488, 'datetime': '2021-03-09T20:38:07.488Z', 'lastTradeTimestamp': None, 'status': 'open', 'symbol': 'BTC/EUR', 'type': 'limit', 'timeInForce': 'GTC', 'postOnly': False, 'side': 'buy', 'price': 45581.0, 'stopPrice': None, 'cost': 0.0, 'amount': 0.001006, 'filled': 0.0, 'remaining': 0.001006, 'fee': {'cost': 0.0, 'currency': 'EUR', 'rate': None}, 'average': None, 'trades': None}]

I would like to end up wit a table like in the link above.

ps. sorry somehow I can't get the response to print nicely in the forum ?

duhh
  • 53
  • 1
  • 5
  • 1
    "I get a response that is I think a json inside a list". Clarification about terminology: there is no such thing as "a json". In the JSON, format, `{}` indicates what we call an **object**. In Python, the equivalent data structure is a **dictionary**. In the case of the ccxt library, I suspect that it makes an HTTP request and receives back a JSON string. By the time you see this data in your code, it has been parsed into a Python dictionary...or more accurately a list of dictionaries. With this terminology in hand, you can google something like "list of dictionaries to dataframe" to get help. – Code-Apprentice Mar 09 '21 at 21:12
  • 1
    Does this answer your question? [Convert list of dictionaries to a pandas DataFrame](https://stackoverflow.com/questions/20638006/convert-list-of-dictionaries-to-a-pandas-dataframe) – Code-Apprentice Mar 09 '21 at 21:13
  • 1
    Which leads to links such as this ^ – Code-Apprentice Mar 09 '21 at 21:14

1 Answers1

0

You have to convert openorders from a python list of dictionaries to pandas' df DataFrame type, however, before doing that, you want to remove all nested substructures:

# get list of dicts
openorders = coinbase.fetch_open_orders('BTC/EUR')

# remove nested structures
openorders = [coinbase.omit(order, [ 'info', 'fee', 'fees' ]) for order in openorders]

# convert to df
df = pd.DataFrame(openorders)

# print the dataframe as table
print(df)
Igor Kroitor
  • 1,548
  • 13
  • 16