Ticket #6334: trac_6334_reviewer_edits.patch

File trac_6334_reviewer_edits.patch, 10.2 KB (added by rbeezer, 12 years ago)
  • sage/finance/stock.py

    # HG changeset patch
    # User Rob Beezer <beezer@ups.edu>
    # Date 1247802829 25200
    # Node ID a535c0aefe1d1918a4f26ef36602bd094c61d9c8
    # Parent  413454ed53b8b877a453ff021519fa9b113e9dd2
    Reviewer edits on #6334
    
    diff -r 413454ed53b8 -r a535c0aefe1d sage/finance/stock.py
    a b  
    1 """
     1r"""
    22Stock market price series
    33
    44AUTHORS:
     
    1818
    1919class OHLC:
    2020    def __init__(self, timestamp, open, high, low, close, volume):
    21         """
     21        r"""
    2222        Open, high, low, and close information for a stock. Also stores
    2323        a timestamp for that data along with the volume.
    2424
    2525        INPUT:
    2626
    27 
    2827        - ``timestamp`` - string
    29 
    3028        - ``open, high, low, close`` - float
    31 
    3229        - ``volume`` - int
    3330
    34 
    3531        EXAMPLES::
    3632
    3733            sage: sage.finance.stock.OHLC('18-Aug-04', 100.01, 104.06, 95.96, 100.34, 22353092)
     
    4036        self.timestamp = timestamp
    4137        self.open=float(open); self.high=float(high); self.low=float(low); self.close=float(close)
    4238        self.volume=int(volume)
    43        
     39
    4440    def __repr__(self):
    45         """
     41        r"""
    4642        Return string representation of stock OHLC data.
    4743
    4844        EXAMPLES::
     
    5450                   self.low, self.close, self.volume)
    5551
    5652    def __cmp__(self, other):
    57         """
     53        r"""
    5854        Compare self and other.
    5955
    6056        EXAMPLES::
     
    7470    Class for retrieval of stock market information.
    7571    """
    7672    def __init__(self, symbol, cid=''):
    77         """
     73        r"""
    7874        Create a Stock object. Optional initialization by cid: an identifier
    7975        for each equity used by Google Finance.
    8076
     
    8581                               supply the market, it is assumed to be
    8682                               NYSE or NASDAQ.
    8783                       eg. "goog" or "OTC:NTDOY"
    88 
    8984        - ``cid`` - Integer, a Google contract ID (optional)
    9085
    91 
    9286        LIMITATIONS:
    9387
    9488        Currently, the symbol and cid do not have to match.  When using
     
    115109        return "%s (%s)"%(self.symbol, self.market_value())
    116110   
    117111    def market_value(self):
    118         """
     112        r"""
    119113        Return the current market value of this stock.
    120114
    121115        OUTPUT:
    122116
    123 
    124117        - market value - float
    125118
    126 
    127119        EXAMPLES::
    128120
    129121            sage: finance.Stock('goog').market_value()   # random; optional - internet
     
    132124        return float(self.yahoo()['price'])
    133125
    134126    def yahoo(self):
    135         """
     127        r"""
    136128        Get Yahoo current price data for this stock.
    137129
    138130        OUTPUT:
    139131
    140 
    141132        - current price data, dict
    142133
    143 
    144134        EXAMPLES::
    145135
    146136            sage: finance.Stock('GOOG').yahoo()          # random; optional -- internet
     
    172162        return data
    173163
    174164    def google(self,startdate='Jan+1,+1900',enddate=date.today().strftime("%b+%d,+%Y"), histperiod='daily'):
    175         """
     165        r"""
    176166        Return an immutable sequence of historical price data
    177167        for this stock, obtained from Google. OHLC data is stored
    178168        internally as well. By default, returns the past year's daily
     
    192182
    193183        INPUT:
    194184
    195            
    196185        - ``startdate`` - string, (default: 'Jan+1,+1900')
    197 
    198186        - ``enddate`` - string, (default: current date )
    199 
    200187        - ``histperiod`` - string, ('daily' or 'weekly')
    201188
    202 
    203189        OUTPUT:
    204190
    205 
    206191        - financial data - sequence.Sequence
    207192
    208 
    209193        EXAMPLES:
    210194
    211195        By default, ``Stock.google()`` will retrieve the past year's financial
     
    231215            ]
    232216
    233217        Here is another example with explicit start and end dates::
    234            
     218
    235219            sage: finance.Stock('F').google('Jan+3,+1978', 'Jul+7,+2008')[:5] # optional -- internet
    236220            [
    237221              3-Jan-78 0.00 1.93 1.89 1.89    1618200,
     
    240224              6-Jan-78 0.00 1.84 1.82 1.83    3042500,
    241225              9-Jan-78 0.00 1.81 1.79 1.81    3916400
    242226            ]
    243            
     227
    244228        Note that when startdate is too far prior to a stock's actual start
    245229        date, Google Finance defaults to a year's worth of stock history
    246230        leading up to the specified enddate.  For example, Apple's (AAPL) stock
    247231        history only dates back to September 7, 1984::
    248            
     232
    249233            sage: finance.Stock('AAPL').google('Sep+1,+1900', 'Jan+1,+2000')[0:5] # optional -- internet
    250234            [
    251235              4-Jan-99 0.00 10.56 10.00 10.31   34031600,
     
    254238              7-Jan-99 0.00 11.27 10.53 11.25   51036400,
    255239              8-Jan-99 0.00 11.72 11.00 11.25   24240000
    256240            ]
    257            
     241
    258242        Here is an example where we create and get the history of a stock
    259243        that is not in NASDAQ or NYSE::
    260                
     244
    261245            sage: finance.Stock("OTC:NTDOY").google(startdate="Jan+1,+2007", enddate="Jan+1,+2008")[:5]  # optional -- internet
    262246            [
    263247              3-Jan-07 32.44 32.75 32.30 32.44     156283,
     
    266250              8-Jan-07 30.10 30.50 30.00 30.10     130765,
    267251              9-Jan-07 29.90 30.05 29.60 29.90     103338
    268252            ]
    269        
     253
    270254        Here, we create a stock by cid, and get historical data.
    271255        Note that when using historical, if a cid is specified,
    272256        it will take precedence over the stock's symbol.  So, if
    273257        the symbol and cid do not match, the history based on the
    274258        contract id will be returned.::
    275        
     259
    276260            sage: sage.finance.stock.Stock("AAPL", 22144).google(startdate='Jan+1,+1990')[:5] # optional -- internet
    277261            [
    278262              2-Jan-90 0.00 9.38 8.75 9.31    6542800,
     
    284268        """
    285269        cid = self.cid
    286270        symbol = self.symbol
    287        
     271
    288272        if self.cid=='':
    289273            if ':' in symbol:
    290274                R = self._get_data('', startdate, enddate, histperiod)
     
    308292
    309293        INPUT:
    310294
    311 
    312295        - ``startdate`` - string, (default: 'Jan+1,+1900')
    313 
    314296        - ``enddate`` - string, (default: current date)
    315 
    316297        - ``histperiod`` - string, ('daily' or 'weekly')
    317298
    318 
    319299        OUTPUT:
    320300
    321 
    322301        - TimeSeries - Close price data
    323302
    324 
    325303        EXAMPLES:
    326304        You can directly obtain Open data as so::
    327305
     
    344322            [55.6000, 56.9800, 58.0000, 57.6000, 60.3200 ... 56.5500, 59.3000, 60.0000, 59.7900, 59.2600]
    345323
    346324        Otherwise, ``self.google()`` will be called with the default
    347         arguements returning a year's worth of data::
     325        arguments returning a year's worth of data::
    348326
    349327            sage: finance.Stock('vmw').open()   # random; optional -- internet
    350328            [52.1100, 60.9900, 59.0000, 56.0500, 57.2500 ... 83.0500, 85.4900, 84.9000, 82.0000, 81.2500]
    351329
    352330        """
    353        
    354331        from time_series import TimeSeries
    355        
     332
    356333        if len(args) != 0:
    357334            return TimeSeries([x.open for x in self.google(*args, **kwds)])
    358            
     335
    359336        try:
    360337            return TimeSeries([x.open for x in self.__historical])
    361338        except AttributeError:
    362339            pass
    363        
     340
    364341        return TimeSeries([x.open for x in self.google(*args, **kwds)])
    365    
     342
    366343    def close(self, *args, **kwds):
    367344        r"""
    368345        Return the time series of all historical closing prices for this stock.
     
    371348
    372349        INPUT:
    373350
    374 
    375351        - ``startdate`` - string, (default: 'Jan+1,+1900')
    376 
    377352        - ``enddate`` - string, (default: current date)
    378 
    379353        - ``histperiod`` - string, ('daily' or 'weekly')
    380354
    381 
    382355        OUTPUT:
    383356
    384 
    385357        - TimeSeries - Close price data
    386358
    387359        EXAMPLES:
     360
    388361        You can directly obtain close data as so::
    389362
    390363            sage: finance.Stock('vmw').close(startdate='Jan+1,+2008', enddate='Feb+1,+2008')                 # optional -- internet
     
    411384            sage: finance.Stock('vmw').close()   # random; optional -- internet
    412385            [57.7100, 56.9900, 55.5500, 57.3300, 65.9900 ... 84.9900, 84.6000, 83.9500, 80.4900, 72.9900]
    413386        """
    414                
     387
    415388        from time_series import TimeSeries
    416        
     389
    417390        if len(args) != 0:
    418391            return TimeSeries([x.close for x in self.google(*args, **kwds)])
    419            
     392
    420393        try:
    421394            return TimeSeries([x.close for x in self.__historical])
    422395        except AttributeError:
    423396            pass
    424        
     397
    425398        return TimeSeries([x.close for x in self.google(*args, **kwds)])
    426        
     399
    427400    def load_from_file(self, file):
    428         """
     401        r"""
    429402        Load historical data from a local csv formatted data file. Note
    430403        that no symbol data is included in Google Finance's csv data.
    431404        The csv file must be formatted in the following way, just as
     
    435408
    436409        INPUT:
    437410
    438 
    439411        - ``file`` - local file with Google Finance formatted OHLC data
    440412
    441 
    442413        OUTPUTS:
    443414
    444 
    445415        - Sequence -- OHLC data
    446 n
    447416
    448417        EXAMPLES:
     418
    449419        Suppose you have a file in your home directoy containing Apple stock
    450420        OHLC data, such as that from Google Finance, called AAPL-minutely.csv.
    451421        One can load this information into a Stock object like so. Note that
     
    490460            return self.__historical
    491461        except IOError, msg:
    492462            print "Bad path or file name"
    493        
     463
    494464    def _load_from_csv(self, R):
    495         """
     465        r"""
    496466        Helper method that loads financial data from a CSV file.
    497467
    498468        EXAMPLES:
     469
    499470        This indirectly tests _load_from_csv()::
    500471
    501472            sage: finance.Stock('aapl').load_from_file(SAGE_ROOT + "/examples/finance/AAPL-minutely.csv")
     
    506477            1212407640 187.75 188.00 187.75 188.00       2000,
    507478            1212405780 187.80 187.80 187.80 187.80        100
    508479            ]
    509 
    510        
    511480        """
    512481        R = R.splitlines()
    513482        headings = R[0].split(',')
     
    523492        return hist_data
    524493       
    525494    def _get_data(self, exchange='', startdate='Jan+1,+1900', enddate=date.today().strftime("%b+%d,+%Y"), histperiod='daily'):
    526         """       
     495        r"""
    527496        This function is used internally.
    528497
    529498        EXAMPLES:
     499
    530500        This indirectly tests the use of get_data::
    531501
    532502            sage: finance.Stock('aapl').google(startdate='Jan+1,+1990')[:2]    # optional -- internet