danpaquin / coinbasepro-python

The unofficial Python client for the Coinbase Pro API
MIT License
1.82k stars 733 forks source link

Output formatting #364

Closed raphdeknop closed 5 years ago

raphdeknop commented 5 years ago

Hi,

I am under Ubuntu 19.04, and everything seems to work fine. However, the output is really painful to read because it doesn't display as it would in a browser for example. There is no indent, it never goes to the line and so on...

What would be the requirements to have the same formatting as in a browser ?

Thanks

noah222 commented 5 years ago

what specific output are you looking at? What do you want it to look like? what kind of browser do you want it to be like? Are you talking about the sourcecode or something else?

raphdeknop commented 5 years ago

Thanks for asking for precision.

After having run

import cbpro
public_client = cbpro.PublicClient()

If I run public_client.get_product_ticker(product_id='ETH-USD'), my output is something like

{'trade_id': 48521080, 'price': '238.51000000', 'size': '0.62577557', 'time': '2019-06-06T19:49:56.811Z', 'bid': '238.5', 'ask': '238.51', 'volume': '127764.41733600'}

and I would like it to be displayed like this (or similarly, maybe with semicolons aligned):

{
'trade_id': 48521080, 
'price': '238.51000000', 
'size': '0.62577557', 
'time': '2019-06-06T19:49:56.811Z', 
'bid': '238.5', 
'ask': '238.51', 
'volume': '127764.41733600'
}

I don't know if it is supposed to be that way or if it could be an enhancement.

noah222 commented 5 years ago

This is just a json response as it comes from the server. It is just the data and does not really need to all be displayed, unless you have a specific need to pull something from it.

First tell python to interpret the text string as a dictionary with the ast.literal_eval() and then use what you want from it.

So you can select what you want with the keyword like this:

import ast Data = public_client.get_product_ticker(product_id='ETH-USD') DataDictionary = ast.literal_eval(Data) print(DataDictionary["price"]) print(DataDictionary["size"])

After initially getting familiar with working with the data I would avoid using the ticker price since it can lag, is subject to major delays during peak trading moments and can have inaccuracies. Much better is the websocket feed, specifically the 'matches' feed.

I'm trading pretty much year round with this library, so let me know if you have specific questions and I will try to help, if I can.

raphdeknop commented 5 years ago

Thank you for your help. I won't hesitate to if I need other precisions.

However, I might just have found another solution. As you said, it has to do with json. First, import json in python, so run

import json

then execute your command (for example public_client.get_product_ticker(product_id='ETH-USD')) and finally run

print(json.dumps(_, indent=4))

The underscore (_) returns the last output in python. For example, executing 2*4 then _-3, will produce 5 as the output. Here, we also have specified an indent of 4 characters.

It may not be perfect in every case of figure and can certainly be improved but for an occasional use, it is a simple solution

noah222 commented 5 years ago

you really need to assign your ticker response to a variable, or else you will be asking for trouble. Think of a situation involving quickly checking the price, submitting a buy order, and then cancelling it. You need to have a status of each action saved in a separate variable if you want to stand a chance of keeping pace with a live market. This is because one or all of those requests could stall out at any time and you need to know what one stalled so you can respond appropriately.

raphdeknop commented 5 years ago

You're right, this kind of tool is mostly used for speed and automation. What I asked for was just a feature and is by no means the main objective of the commands provided.

noah222 commented 5 years ago

What you asked for was how to format the text returned by coinbase servers, and I showed you how to do that. It is not the job of the api interface to parse the data, that is your job.

You will also want to surround each api call with a try, except block, because although you are not trying to be fast you will at least want your data collection to work over many tries.