Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #43694 > unrolled thread

Missing decimals in the code - some suggestions?

Started byhmjeltevik@gmail.com
First post2013-04-16 11:02 -0700
Last post2013-04-16 14:37 -0400
Articles 6 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Missing decimals in the code - some suggestions? hmjeltevik@gmail.com - 2013-04-16 11:02 -0700
    Re: Missing decimals in the code - some suggestions? Ian Kelly <ian.g.kelly@gmail.com> - 2013-04-16 12:20 -0600
      Re: Missing decimals in the code - some suggestions? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-17 06:23 +0000
        Re: Missing decimals in the code - some suggestions? Ian Kelly <ian.g.kelly@gmail.com> - 2013-04-17 00:56 -0600
    Re: Missing decimals in the code - some suggestions? MRAB <python@mrabarnett.plus.com> - 2013-04-16 19:35 +0100
    Re: Missing decimals in the code - some suggestions? Terry Jan Reedy <tjreedy@udel.edu> - 2013-04-16 14:37 -0400

#43694 — Missing decimals in the code - some suggestions?

Fromhmjeltevik@gmail.com
Date2013-04-16 11:02 -0700
SubjectMissing decimals in the code - some suggestions?
Message-ID<8e158cbe-4b81-476a-ac48-a0d967956277@googlegroups.com>
Hi!

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])) + \
          'ignore=.csv'
    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?

[toc] | [next] | [standalone]


#43695

FromIan Kelly <ian.g.kelly@gmail.com>
Date2013-04-16 12:20 -0600
Message-ID<mailman.683.1366136461.3114.python-list@python.org>
In reply to#43694
On Tue, Apr 16, 2013 at 12:02 PM,  <hmjeltevik@gmail.com> wrote:
> Hi!
>
> 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])) + \
>           'ignore=.csv'
>     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?

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.

[toc] | [prev] | [next] | [standalone]


#43731

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-04-17 06:23 +0000
Message-ID<516e3fed$0$29872$c3e8da3$5496439d@news.astraweb.com>
In reply to#43695
On Tue, 16 Apr 2013 12:20:18 -0600, Ian Kelly wrote:

> 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.

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
2.5791


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 
cent?



Adding-spurious-precision-for-fun-and-profit-ly y'rs,


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#43736

FromIan Kelly <ian.g.kelly@gmail.com>
Date2013-04-17 00:56 -0600
Message-ID<mailman.706.1366181823.3114.python-list@python.org>
In reply to#43731
On Wed, Apr 17, 2013 at 12:23 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> On Tue, 16 Apr 2013 12:20:18 -0600, Ian Kelly wrote:
>
>> 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.
>
> Pish posh! It's easy to add extra precision.

It's easy to add extra /digits/.  Adding extra precision is much harder. ;-)

[toc] | [prev] | [next] | [standalone]


#43699

FromMRAB <python@mrabarnett.plus.com>
Date2013-04-16 19:35 +0100
Message-ID<mailman.686.1366137310.3114.python-list@python.org>
In reply to#43694
On 16/04/2013 19:02, hmjeltevik@gmail.com wrote:
> Hi!
>
> 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])) + \
>            'ignore=.csv'
>      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?
>
The code prints what it receives; the data it receives has only 2
decimal places.

This question:

http://stackoverflow.com/questions/11496418/yql-forex-historical-prices-queries-how-to-change-default-precision

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.

[toc] | [prev] | [next] | [standalone]


#43700

FromTerry Jan Reedy <tjreedy@udel.edu>
Date2013-04-16 14:37 -0400
Message-ID<mailman.687.1366137472.3114.python-list@python.org>
In reply to#43694
On 4/16/2013 2:02 PM, hmjeltevik@gmail.com wrote:
> Hi!
>
> 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])) + \
>            'ignore=.csv'
>      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.

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.

> print ystockquote.get_historical_prices('EURUSD=X','20120101','20120301')
>
> Some suggestions?

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web