示例#1
0
    def getdata(self, quote, datedebut=None, datefin=None):

        # get historic data itself !
        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)

        mic = euronextmic(quote.market(), quote.place())

        format = '%Y-%m-%d %H:%M:%S'
        #origin = "1970-01-01 00:00:00"
        datefrom = str(datedebut) + " 02:00:00"
        dateto = str(datefin) + " 23:00:00"
        datefrom = time.mktime(time.strptime(datefrom, format))
        datefromurl = str(int(datefrom / 100)) + '00000'
        dateto = time.mktime(time.strptime(dateto, format))
        datefinurl = str(int(dateto) / 100) + '00000'
        endurl = 'typefile=csv&layout=vertical&typedate=dmy&separator=comma&mic=%s&isin=%s&name=&namefile=Price_Data_Historical&from=%s&to=%s&adjusted=1&base=0' % (
            mic, quote.isin(), datefromurl, datefinurl)

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

        query = (
            ('typefile', 'csv'),
            ('layout', 'vertical'),
            ('typedate', 'dmy'),
            ('separator', 'comma'),
            ('mic', mic),
            ('isin', quote.isin()),
            ('name', ''),
            ('namefile', 'Price_Data_Historical'),
            ('from', datefromurl),
            ('to', datefinurl),
            ('adjusted', '1'),
            ('base', '0'),
        )

        query = map(lambda (var, val): '%s=%s' % (var, str(val)), query)
        query = string.join(query, '&')
        url = self.m_url + '?' + query

        #print url
        debug("Import_euronext:getdata: url=%s ", url)
        try:

            req = urllib2.Request(url)
            req.add_header(
                'User-Agent',
                'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041202 Firefox/1.0'
            )

            f = urllib2.urlopen(req)
            buf = f.read()
            f.close()

            #buf=self.m_connection.getDataFromUrl(url)
        except:
            debug('Import_euronext:unable to connect :-(')
            return None

        # pull data
        lines = self.splitLines(buf)
        data = ''
        #print lines

        for eachLine in lines[4:]:
            eachLine = eachLine.replace('","', ';')
            eachLine = eachLine.replace('"', '')
            sdata = string.split(eachLine, ';')

            if len(sdata) == 11:
                #print sdata
                #if (sdata[0] != "Date") and (quote.list() == QLIST_INDICES):
                sdate = jjmmaa2yyyymmdd(sdata[2])
                open = self.parseFValue(sdata[3].replace(',', '.'))
                high = self.parseFValue(sdata[4].replace(',', '.'))
                low = self.parseFValue(sdata[5].replace(',', '.'))
                value = self.parseFValue(sdata[6].replace(',', '.'))
                volume = self.parseLValue(sdata[7])
                #print quote.key(),sdate,open,high,low,value,volume

                # 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, ';')
                #print line

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

        mic = euronextmic(quote.market(),quote.place())

        query = (
            ('isin', quote.isin()),
            ('mic', mic),
        )
        query = map(lambda (var, val): '%s=%s' % (var, str(val)), query)
        query = string.join(query, '&')

        url = self.m_url + query
        #print 'url:',url
        debug("LiveUpdate_Euronext:getdata: url=%s ",url)

        try:

            req = urllib2.Request(url)
            req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041202 Firefox/1.0')

            f = urllib2.urlopen(req)
            buf = f.read()
            f.close()
            
        except:
            debug('LiveUpdate_Euronext:unable to connect :-(')
            return None

        # pull data
        lines = self.splitLines(buf)

        i = 0
        count = 0
        for eachLine in lines:
            count = count + 1


            if '"datetimeLastvalue">' in eachLine:
                iDate = eachLine[eachLine.find('"datetimeLastvalue">')+20:eachLine.find('</span>')].replace('CET','').replace('BST','')
                iDate = iDate.rstrip()
                #print count,'iDate:',iDate
                i = i +1
                                         
            if '"lastPriceint">' in eachLine:
                lastPriceint = eachLine[eachLine.find('"lastPriceint">')+15:eachLine.find('</span>')].replace(',','.')
                lastPriceint = lastPriceint.replace(',','.')
                i = i +1
            if '"lastPricefract">' in eachLine:
                lastPricefract = eachLine[eachLine.find('"lastPricefract">')+17:eachLine.find('</sup>')]
                i = i +1
                iLast = lastPriceint + lastPricefract
                #print count,'iLast:',iLast
                
            if '"cnDiffRelvalue">(' in eachLine:
                iPercent = eachLine[eachLine.find('"cnDiffRelvalue">(')+18:eachLine.find(')</span>')]
                iPercent = iPercent.replace('%','').replace(',','.').replace('+','')
                i = i +1
                #print count,'iPercent:',iPercent

            if '"todayVolumevalue">' in eachLine:
                iVolume = eachLine[eachLine.find('"todayVolumevalue">')+19:eachLine.find('&nbsp')].replace('.','').replace(',','')
                i = i +1
                #print count,'iVolume:',iVolume

            if '>Ouvert<' in eachLine:
                eachLine = lines[count]
                iOpen = eachLine[:eachLine.find('</td>')].replace('.','').replace(',','.')
                if '%' in iOpen:
                    iOpen = iOpen[iOpen.find('%')+1:]
                elif '$' in iOpen:
                    iOpen = iOpen[iOpen.find('$')+1:]
                elif '&euro;' in iOpen :
                    iOpen = iOpen[iOpen.find('&euro;')+6:]   
                elif '&pound;' in iOpen :
                    iOpen = iOpen[iOpen.find('&pound;')+7:]   
                elif '-' in iOpen:
                    iOpen = '0'
                    return None
                i = i + 1
                #print count,'iOpen:',iOpen

            if '"highPricevalue">' in eachLine:
                iHigh = eachLine[eachLine.find('"highPricevalue">')+17:eachLine.find('&nbsp')].replace('.','').replace(',','.')
                if '-' in iHigh:
                    iHigh = '0'
                    return None
                i = i +1
                #print count,'iHigh:',iHigh

            if '"lowPricevalue">' in eachLine:
                iLow = eachLine[eachLine.find('"lowPricevalue">')+16:eachLine.find('&nbsp')].replace('.','').replace(',','.')
                if '-' in iLow:
                    iLow = '0'
                    return None
                i = i +1
                #print count,'iLow:',iLow

            if i == 7 and (quote.list()==QLIST_INDICES):
                iVolume = '0'
                i = i + 1

            if i == 8:
                count = 0
                i = 0
                c_datetime = datetime.today()
                c_date = "%04d%02d%02d" % (c_datetime.year,c_datetime.month,c_datetime.day)
                #print 'Today is :', c_date
            

                sdate,sclock = self.euronextDate(iDate)

                # be sure we have volume (or indices)
                if (quote.list() == QLIST_INDICES or iVolume != ''):

                    # be sure not an oldest day !
                    if (c_date==sdate) or (quote.list() == QLIST_INDICES):
                        key = quote.key()
                        self.m_dcmpd[key] = sdate
                        self.m_dateindice[key] = str(sdate[6:8]) + '/' + str(sdate[4:6]) + '/' +str(sdate[0:4])
                        self.m_clock[key] = self.convertClock(quote.place(),sclock,sdate)

                    # ISIN;DATE;OPEN;HIGH;LOW;CLOSE;VOLUME;PERCENT
                    data = ';'.join([quote.key(),sdate,iOpen,iHigh,iLow,iLast,iVolume,iPercent])
                    return data
            
        return None
    def getdata(self, quote):
        self.m_connected = False
        debug("LiveUpdate_Euronext_bonds:getdata quote:%s market:%s" %
              (quote, self.m_market))

        mic = euronextmic(quote.market(), quote.place())

        query = (
            ('isin', quote.isin()),
            ('mic', mic),
        )
        query = map(lambda (var, val): '%s=%s' % (var, str(val)), query)
        query = string.join(query, '&')

        url = self.m_url + query
        print 'url_liveupdate:', url
        debug("LiveUpdate_Euronext_bonds:getdata: url=%s ", url)

        try:

            req = urllib2.Request(url)
            req.add_header(
                'User-Agent',
                'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041202 Firefox/1.0'
            )

            f = urllib2.urlopen(req)
            buf = f.read()
            f.close()

            #buf=self.m_connection.getDataFromUrl(url)
        except:
            debug('LiveUpdate_Euronext_bonds:unable to connect :-(')
            return None

        # pull data
        lines = self.splitLines(buf)

        i = 0
        count = 0
        for eachLine in lines:
            count = count + 1

            if '"datetimeLastvalue">' in eachLine:
                iDate = eachLine[eachLine.find('"datetimeLastvalue">') +
                                 20:eachLine.find(' CET</span>')]
                i = i + 1

            if '"lastPriceint">' in eachLine:
                lastPriceint = eachLine[eachLine.find('"lastPriceint">') +
                                        15:eachLine.find('</span>')].replace(
                                            ',', '.')
                lastPriceint = lastPriceint.replace(',', '.')
                i = i + 1
            if '"lastPricefract">' in eachLine:
                lastPricefract = eachLine[eachLine.find('"lastPricefract">') +
                                          17:eachLine.find('</sup>')]
                i = i + 1
                iLast = lastPriceint + lastPricefract

            if '"cnDiffRelvalue">(' in eachLine:
                iPercent = eachLine[eachLine.find('"cnDiffRelvalue">(') +
                                    18:eachLine.find(')</span>')]
                iPercent = iPercent.replace('%',
                                            '').replace(',',
                                                        '.').replace('+', '')
                i = i + 1

            if '"todayVolumevalue">' in eachLine:
                iVolume = eachLine[eachLine.find('"todayVolumevalue">') +
                                   19:eachLine.find('&nbsp')].replace(
                                       '.', '').replace(',', '')
                i = i + 1

            if '>Ouvert<' in eachLine:
                eachLine = lines[count]
                iOpen = eachLine[:eachLine.find('</td>')].replace(',', '.')

                if '%' in iOpen:
                    iOpen = iOpen[iOpen.find('%') + 1:]
                elif '$' in iOpen:
                    iOpen = iOpen[iOpen.find('$') + 1:]
                elif '&euro;' in iOpen:
                    iOpen = iOpen[iOpen.find('&euro;') + 6:]
                elif '&pound;' in iOpen:
                    iOpen = iOpen[iOpen.find('&pound;') + 7:]

                i = i + 1

            if '"highPricevalue">' in eachLine:
                iHigh = eachLine[eachLine.find('"highPricevalue">') +
                                 17:eachLine.find('&nbsp')].replace(',', '.')
                count = count + 1
                i = i + 1

            if '"lowPricevalue">' in eachLine:
                iLow = eachLine[eachLine.find('"lowPricevalue">') +
                                16:eachLine.find('&nbsp')].replace(',', '.')
                count = count + 1
                i = i + 1

            if i == 8:
                count = 0
                i = 0
                c_datetime = datetime.today()
                c_date = "%04d%02d%02d" % (c_datetime.year, c_datetime.month,
                                           c_datetime.day)
                #print 'Today is :', c_date

                sdate, sclock = self.euronextDate(iDate)

                # be sure we have volume (or indices)
                if (quote.list() == QLIST_INDICES or iVolume != ''):

                    # be sure not an oldest day !
                    if (c_date == sdate) or (quote.list() == QLIST_INDICES):
                        key = quote.key()
                        self.m_dcmpd[key] = sdate
                        self.m_dateindice[key] = str(sdate[6:8]) + '/' + str(
                            sdate[4:6]) + '/' + str(sdate[0:4])
                        self.m_clock[key] = self.convertClock(
                            quote.place(), sclock, sdate)

                    # ISIN;DATE;OPEN;HIGH;LOW;CLOSE;VOLUME;PERCENT
                    data = ';'.join([
                        quote.key(), sdate, iOpen, iHigh, iLow, iLast, iVolume,
                        iPercent
                    ])
                    return data

        return None
    def getdata(self,quote,datedebut=None,datefin=None):

        # get historic data itself !
        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)

        mic = euronextmic(quote.market(),quote.place())
        
        format = '%Y-%m-%d %H:%M:%S'
        #origin = "1970-01-01 00:00:00"
        datefrom = str(datedebut) + " 02:00:00"
        dateto = str(datefin) + " 23:00:00"
        datefrom = time.mktime(time.strptime(datefrom, format))
        datefromurl =str(int(datefrom/100))+'00000'
        dateto = time.mktime(time.strptime(dateto, format))
        datefinurl =str(int(dateto)/100)+'00000'
        endurl = 'typefile=csv&layout=vertical&typedate=dmy&separator=comma&mic=%s&isin=%s&name=&namefile=Price_Data_Historical&from=%s&to=%s&adjusted=1&base=0' % (mic,quote.isin(),datefromurl,datefinurl)

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

        query = (
            ('typefile', 'csv'),
            ('layout', 'vertical'),
            ('typedate', 'dmy'),
            ('separator', 'comma'),
            ('mic', mic),
            ('isin', quote.isin()),
            ('name', ''),
            ('namefile', 'Price_Data_Historical'),
            ('from', datefromurl),
            ('to', datefinurl),
            ('adjusted', '1'),
            ('base', '0'),
        )

        query = map(lambda (var, val): '%s=%s' % (var, str(val)), query)
        query = string.join(query, '&')
        url = self.m_url + '?' + query
        
        #print url
        debug("Import_euronext:getdata: url=%s ",url)
        try:

            req = urllib2.Request(url)
            req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041202 Firefox/1.0')

            f = urllib2.urlopen(req)
            buf = f.read()
            f.close()
            
            #buf=self.m_connection.getDataFromUrl(url)
        except:
            debug('Import_euronext:unable to connect :-(')
            return None

        # pull data
        lines = self.splitLines(buf)
        data = ''
        #print lines

        for eachLine in lines[4:]:
            eachLine = eachLine.replace('","',';')
            eachLine = eachLine.replace('"','')
            sdata = string.split(eachLine,';')
            
            if len(sdata)== 11:
                #print sdata
                #if (sdata[0] != "Date") and (quote.list() == QLIST_INDICES):
                sdate = jjmmaa2yyyymmdd(sdata[2])
                open = self.parseFValue(sdata[3].replace(',','.'))
                high = self.parseFValue(sdata[4].replace(',','.'))
                low = self.parseFValue(sdata[5].replace(',','.'))
                value = self.parseFValue(sdata[6].replace(',','.'))
                volume = self.parseLValue(sdata[7])
                #print quote.key(),sdate,open,high,low,value,volume

                # 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, ';')
                #print line

                # append
                data = data + line + '\r\n'
        return data