예제 #1
0
    def getdata(self,quote,datedebut=None,datefin=None):
        if not datefin:
            datefin = date.today()

        if not datedebut:
            datedebut = date.today()
        if isinstance(datedebut,Datation):
            datedebut = datedebut.date()

        if isinstance(datefin,Datation):
            datefin = datefin.date()

        d1 = self.parseDate(datedebut)
        d2 = self.parseDate(datefin)

        debug("Import_yahoo:getdata quote:%s begin:%s end:%s" % (quote,d1,d2))

        sname = yahooTicker(quote.ticker(),quote.market(),quote.place())

        if sname[0]=='^':
            ss = "%5E" + sname[1:]
        else:
            ss = sname
        query = (
            ('s', ss),
            ('a', '%02d' % (int(d1[1])-1)),
            ('b', d1[2]),
            ('c', d1[0]),
            ('d', '%02d' % (int(d2[1])-1)),
            ('e', d2[2]),
            ('f', d2[0]),
            ('y', '0'),
            ('g', 'd'),
            ('ignore', '.csv'),
        )
        query = map(lambda (var, val): '%s=%s' % (var, str(val)), query)
        query = string.join(query, '&')
        url = yahooUrl(quote.market(),live=False) + '?' + query

        debug("Import_yahoo:getdata: url=%s ",url)
        try:
            buf=self.m_connection.getDataFromUrl(url)
        except:
            debug('Import_yahoo:unable to connect :-(')
            return None

        # pull data
        lines = self.splitLines(buf)
        if len(lines)<=0:
            # empty content
            return None
        header = string.split(lines[0],',')
        data = ""

        if (header[0] != "Date"):
            # no valid content
            return None

        for eachLine in lines:
            sdata = string.split (eachLine, ',')
            sdate = sdata[0]
            if (sdate != "Date"):
                if re_p3_1.match(sdate):
                    #print 'already good format ! ',sdate,sdata
                    pass
                else:
                    sdate = dd_mmm_yy2yyyymmdd(sdate)
                open = string.atof(sdata[1])
                high = string.atof(sdata[2])
                low = string.atof(sdata[3])
                value = string.atof(sdata[6])   #   Adj. Close*
                volume = string.atoi(sdata[5])

                if volume >= 0:
                    # encode in EBP format
                    # ISIN;DATE;OPEN;HIGH;LOW;CLOSE;VOLUME
                    line = (
                       quote.key(),
                       sdate,
                       open,
                       high,
                       low,
                       value,
                       volume
                    )
                    line = map(lambda (val): '%s' % str(val), line)
                    line = string.join(line, ';')
                    # append
                    data = data + line + '\r\n'

        return data
    def getdata(self,quote,datedebut=None,datefin=None):
        if not datefin:
            datefin = date.today()

        if not datedebut:
            datedebut = date.today()

        if isinstance(datedebut,Datation):
            datedebut = datedebut.date()

        if isinstance(datefin,Datation):
            datefin = datefin.date()

        d1 = self.parseDate(datedebut)
        d2 = self.parseDate(datefin)

        debug("Import_yahoo:getdata quote:%s begin:%s end:%s" % (quote,d1,d2))

        sname = yahooTicker(quote.ticker(),quote.market(),quote.place())

        if sname[0]=='^':
            ss = "%5E" + sname[1:]
        else:
            ss = sname

        query = (
            ('s', ss),
            ('a', '%02d' % (int(d1[1])-1)),
            ('b', d1[2]),
            ('c', d1[0]),
            ('d', '%02d' % (int(d2[1])-1)),
            ('e', d2[2]),
            ('f', d2[0]),
            ('y', '0'),
            ('g', 'd'),
            ('ignore', '.csv'),
        )
        query = map(lambda (var, val): '%s=%s' % (var, str(val)), query)
        query = string.join(query, '&')
        url = yahooUrl(quote.market(),live=False) + '?' + query

        debug("Import_yahoo:getdata: url=%s ",url)
        try:
            buf=self.m_connection.getDataFromUrl(url)
        except:
            debug('Import_yahoo:unable to connect :-(')
            return None

        # pull data
        lines = self.splitLines(buf)
        if len(lines)<=0:
            # empty content
            return None
        header = string.split(lines[0],',')
        data = ""

        if (header[0]<>"Date"):
            # no valid content
            return None

        for eachLine in lines:
            sdata = string.split (eachLine, ',')
            sdate = sdata[0]
            if (sdate<>"Date"):
                if re_p3_1.match(sdate):
                    #print 'already good format ! ',sdate,sdata
                    pass
                else:
                    sdate = dd_mmm_yy2yyyymmdd(sdate)
                open = string.atof(sdata[1])
                high = string.atof(sdata[2])
                low = string.atof(sdata[3])
                value = string.atof(sdata[6])   #   Adj. Close*
                volume = string.atoi(sdata[5])

                if volume>=0:
                    # encode in EBP format
                    # ISIN;DATE;OPEN;HIGH;LOW;CLOSE;VOLUME
                    line = (
                      quote.key(),
                      sdate,
                      open,
                      high,
                      low,
                      value,
                      volume
                    )
                    line = map(lambda (val): '%s' % str(val), line)
                    line = string.join(line, ';')

                    # append
                    data = data + line + '\r\n'
        return data
    def getdata(self,quote):
        debug("LiveUpdate_yahoo:getdata quote:%s " % quote)
        self.m_connected = False

        sname = yahooTicker(quote.ticker(),quote.market(),quote.place())

        if sname[0]=='^':
            ss = "%5E" + sname[1:]
        else:
            ss = sname

            
        query = (
            ('s', ss),
            ('f', 'sl1d1t1c1ohgv'),
            ('e', '.csv'),
        )
        query = map(lambda (var, val): '%s=%s' % (var, str(val)), query)
        query = string.join(query, '&')
        url = yahooUrl(quote.market(),live=True) + '?' + query

        debug("LiveUpdate_yahoo:getdata: url=%s",url)
        try:
            data=self.m_connection.getDataFromUrl(url)[:-2] # Get rid of CRLF
        except:
            debug('LiveUpdate_yahoo:unable to connect :-(')
            return None

        # pull data
        s400 = re.search(r"400 Bad Request", data, re.IGNORECASE|re.MULTILINE)
        if s400:
            if itrade_config.verbose:
                info('unknown %s quote (400 Bad Request) from Yahoo' % (quote.ticker()))
            return None


        sdata = string.split (data, ',')
        if len (sdata) < 9:
            if itrade_config.verbose:
                info('invalid data (bad answer length) for %s quote' % (quote.ticker()))
            return None

        #print sdata

        # connexion / clock
        self.m_connected = True

        # store for later use
        key = quote.key()

        sclock = sdata[3][1:-1]
        if sclock=="N/A" or sdata[2]=='"N/A"' or len(sclock)<5:
            if itrade_config.verbose:
                info('invalid datation for %s : %s %s' % (quote.ticker(),sclock,sdata[2]))
                #print sdata
            return None

        # start decoding
        symbol = sdata[0][1:-1]
        if symbol != sname:
            if itrade_config.verbose:
                info('invalid ticker : ask for %s and receive %s' % (sname,symbol))
            return None

        # date
        try:
            date = self.yahooDate(sdata[2])
            self.m_dcmpd[key] = sdata
            self.m_clock[key] = self.convertClock(quote.place(),sclock,date)
            self.m_dateindice[key] = sdata[2].replace('"','')
        except ValueError:
            if itrade_config.verbose:
                info('invalid datation for %s : %s %s' % (quote.ticker(),sclock,sdata[2]))
            return None

        # decode data
        value = string.atof (sdata[1])

        if (sdata[4]=='N/A'):
            debug('invalid change : N/A')
            change = 0.0
            return None
        else:
            change = string.atof (sdata[4])
        if (sdata[5]=='N/A'):
            debug('invalid open : N/A')
            open = 0.0
            return None
        else:
            open = string.atof (sdata[5])
        if (sdata[6]=='N/A'):
            debug('invalid high : N/A')
            high = 0.0
            return None
        else:
            high = string.atof (sdata[6])
        if (sdata[7]=='N/A'):
            debug('invalid low : N/A')
            low = 0.0
            return None
        else:
            low = string.atof (sdata[7])

        volume = string.atoi (sdata[8])
        if volume<0:
            debug('volume : invalid negative %d' % volume)
            return None
        if volume==0 and quote.list()!=QLIST_INDICES:
            debug('volume : invalid zero value %d' % volume)
            return None
        else:
            if value-change <= 0:
                return None
            else:
                percent = (change / (value - change))*100.0

        # ISIN;DATE;OPEN;HIGH;LOW;CLOSE;VOLUME;PERCENT;PREVIOUS
        data = (
          key,
          date,
          open,
          high,
          low,
          value,
          volume,
          percent,
          (value-change)
        )
        data = map(lambda (val): '%s' % str(val), data)
        data = string.join(data, ';')

        # temp: hunting an issue (SF bug 1848473)
        # if itrade_config.verbose:
        #    print data

        return data
    def getdata(self, quote):
        debug("LiveUpdate_yahoo:getdata quote:%s " % quote)
        self.m_connected = False

        sname = yahooTicker(quote.ticker(), quote.market(), quote.place())

        if sname[0] == '^':
            ss = "%5E" + sname[1:]
        else:
            ss = sname

        query = (
            ('s', ss),
            ('f', 'sl1d1t1c1ohgv'),
            ('e', '.csv'),
        )
        query = map(lambda (var, val): '%s=%s' % (var, str(val)), query)
        query = string.join(query, '&')
        url = yahooUrl(quote.market(), live=True) + '?' + query

        debug("LiveUpdate_yahoo:getdata: url=%s", url)
        try:
            data = self.m_connection.getDataFromUrl(url)[:
                                                         -2]  # Get rid of CRLF
        except:
            debug('LiveUpdate_yahoo:unable to connect :-(')
            return None

        # pull data
        s400 = re.search(r"400 Bad Request", data,
                         re.IGNORECASE | re.MULTILINE)
        if s400:
            if itrade_config.verbose:
                info('unknown %s quote (400 Bad Request) from Yahoo' %
                     (quote.ticker()))
            return None

        sdata = string.split(data, ',')
        if len(sdata) < 9:
            if itrade_config.verbose:
                info('invalid data (bad answer length) for %s quote' %
                     (quote.ticker()))
            return None

        #print sdata

        # connexion / clock
        self.m_connected = True

        # store for later use
        key = quote.key()

        sclock = sdata[3][1:-1]
        if sclock == "N/A" or sdata[2] == '"N/A"' or len(sclock) < 5:
            if itrade_config.verbose:
                info('invalid datation for %s : %s %s' %
                     (quote.ticker(), sclock, sdata[2]))
                #print sdata
            return None

        # start decoding
        symbol = sdata[0][1:-1]
        if symbol != sname:
            if itrade_config.verbose:
                info('invalid ticker : ask for %s and receive %s' %
                     (sname, symbol))
            return None

        # date
        try:
            date = self.yahooDate(sdata[2])
            self.m_dcmpd[key] = sdata
            self.m_clock[key] = self.convertClock(quote.place(), sclock, date)
            self.m_dateindice[key] = sdata[2].replace('"', '')
        except ValueError:
            if itrade_config.verbose:
                info('invalid datation for %s : %s %s' %
                     (quote.ticker(), sclock, sdata[2]))
            return None

        # decode data
        value = string.atof(sdata[1])

        if (sdata[4] == 'N/A'):
            debug('invalid change : N/A')
            change = 0.0
            return None
        else:
            change = string.atof(sdata[4])
        if (sdata[5] == 'N/A'):
            debug('invalid open : N/A')
            open = 0.0
            return None
        else:
            open = string.atof(sdata[5])
        if (sdata[6] == 'N/A'):
            debug('invalid high : N/A')
            high = 0.0
            return None
        else:
            high = string.atof(sdata[6])
        if (sdata[7] == 'N/A'):
            debug('invalid low : N/A')
            low = 0.0
            return None
        else:
            low = string.atof(sdata[7])

        volume = string.atoi(sdata[8])
        if volume < 0:
            debug('volume : invalid negative %d' % volume)
            return None
        if volume == 0 and quote.list() != QLIST_INDICES:
            debug('volume : invalid zero value %d' % volume)
            return None
        else:
            if value - change <= 0:
                return None
            else:
                percent = (change / (value - change)) * 100.0

        # ISIN;DATE;OPEN;HIGH;LOW;CLOSE;VOLUME;PERCENT;PREVIOUS
        data = (key, date, open, high, low, value, volume, percent,
                (value - change))
        data = map(lambda (val): '%s' % str(val), data)
        data = string.join(data, ';')

        # temp: hunting an issue (SF bug 1848473)
        # if itrade_config.verbose:
        #    print data

        return data