Missing decimals in the code - some suggestions?

Discussion in 'Python' started by hmjeltevik, Apr 16, 2013.

  1. hmjeltevik

    hmjeltevik Guest


    I am using ystockquote with the following code:

    def get_historical_prices(symbol, start_date, end_date):
    Get historical prices for the given ticker symbol.
    Date format is 'YYYYMMDD'

    Returns a nested list.
    url = 'http://ichart.yahoo.com/table.csv?s=%s&' % symbol + \
    'd=%s&' % str(int(end_date[4:6]) - 1) + \
    'e=%s&' % str(int(end_date[6:8])) + \
    'f=%s&' % str(int(end_date[0:4])) + \
    'g=d&' + \
    'a=%s&' % str(int(start_date[4:6]) - 1) + \
    'b=%s&' % str(int(start_date[6:8])) + \
    'c=%s&' % str(int(start_date[0:4])) + \
    days = urllib.urlopen(url).readlines()
    data = [day[:-2].split(',') for day in days]
    return data

    This code prints the data, but only 2 decimals. I need to print out 4 decimals.

    print ystockquote.get_historical_prices('EURUSD=X','20120101','20120301')

    Some suggestions?
    hmjeltevik, Apr 16, 2013
    1. Advertisements

  2. hmjeltevik

    Ian Kelly Guest

    This isn't a Python question. If you take a look at the csv file that
    you download from Yahoo, you will see that it only contains 2 digits
    of precision. There's no way to make Python print out 4 digits of
    precision when it is only provided with 2. You will need to find out
    if there is a way to ask for more precision in the Yahoo API.
    Ian Kelly, Apr 16, 2013
    1. Advertisements

  3. hmjeltevik

    MRAB Guest

    The code prints what it receives; the data it receives has only 2
    decimal places.

    This question:


    says that it's Yahoo doing the rounding to 2 decimal places.

    It looks like you'll have to find another way to get what you want.
    MRAB, Apr 16, 2013
  4. As far as I know, prices are only defined to the nearest penny* (2
    decimals) so I don't know what more you expect.

    *Not too long ago, prices were in 1/8ths of a dollar. Market makers
    complained about decimalization because it would squeeze the spread that
    they profited from -- which it did.
    Terry Jan Reedy, Apr 16, 2013
  5. Pish posh! It's easy to add extra precision.

    py> value = "2.57"
    py> import random
    py> n = random.randrange(0, 100)
    py> value += "%2d" % n
    py> print value

    On average, your numbers will only be off by five parts in a thousand. If
    the values represent dollars, that's half a cent. Who care about half a

    Adding-spurious-precision-for-fun-and-profit-ly y'rs,
    Steven D'Aprano, Apr 17, 2013
  6. hmjeltevik

    Ian Kelly Guest

    It's easy to add extra /digits/. Adding extra precision is much harder. ;-)
    Ian Kelly, Apr 17, 2013
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.