def Import_ListOfQuotes_NYSE(quotes,market='NYSE',dlg=None,x=0):
    print 'Update %s list of symbols' % market
    connection = ITradeConnection(cookies = None,
                               proxy = itrade_config.proxyHostname,
                               proxyAuth = itrade_config.proxyAuthentication,
                               connectionTimeout = itrade_config.connectionTimeout
                               )
    if market=='NYSE':
        url = "http://www.nysedata.com/nysedata/asp/download.asp?s=txt&prod=symbols"
    else:
        return False

    def splitLines(buf):
        lines = string.split(buf, '\n')
        lines = filter(lambda x:x, lines)
        def removeCarriage(s):
            if s[-1]=='\r':
                return s[:-1]
            else:
                return s
        lines = [removeCarriage(l) for l in lines]
        return lines

    try:
        data=connection.getDataFromUrl(url)
    except:
        debug('Import_ListOfQuotes_NYSE:unable to connect :-(')
        return False

    # returns the data
    lines = splitLines(data)

    for line in lines:
        data = string.split (line, '|')
        if len(data)==5:
            country,issuer,issue = extractCUSIP(data[1])
            if issue=='10':
                #print data[1],country,issuer,issue,data[2]
                if country=='US':
                    isin = buildISIN(country,data[1])
                    name = filterName(data[2])
                    quotes.addQuote(isin=isin,name=name,ticker=data[0],market='NYSE',currency='USD',place='NYC',country='US')

    print 'Imported %d lines from NYSE data.' % len(lines)

    return True
Example #2
0
    def import_letter(letter, dlg, x):

        if dlg:
            dlg.Update(x, "BARCHART %s:'%s'" % (market, letter))
            #print "BARCHART %s:'%s'"%(market,letter)

        if not barchart_data.has_key("%s.%s" % (letter, m_country)):
            # read file (for debugging) or get file from network
            try:
                fn = open('barchart%s.htm' % letter, 'r')
            except IOError:
                # can't open the file (existing ?)
                fn = None

            if not fn:
                try:
                    data = connection.getDataFromUrl(url % letter)
                except:
                    print 'Import_ListOfQuotes_BARCHART:unable to connect to', url % letter
                    return False
            else:
                data = fn.read()

            # returns the data
            barchart_data["%s.%s" % (letter, m_country)] = data

        #
        lines = splitLines(barchart_data["%s.%s" % (letter, m_country)])
        ticker = ''
        name = ''
        exchange = ''
        count = 0

        for line in lines:
            # Typical lines :
            # <TD class=bcText><a href="http://quote.barchart.com/quote.asp?sym=ONJP&code=BSTK" class=bcMLink>1 900 JACKPOT INC</a></TD>
            # <TD class=bcText align=right><a href="http://quote.barchart.com/quote.asp?sym=ONJP&code=BSTK" class=bcMLink>ONJP</a></TD>
            # <TD class=bcText align=right> OTCBB </TD>

            scode = re.search('align=right', line,
                              re.IGNORECASE | re.MULTILINE)
            if scode:
                scode = scode.end()
                sexch = re.search(' </td>', line[scode + 2:],
                                  re.IGNORECASE | re.MULTILINE)
                if sexch:
                    sexch = sexch.start()
                    data = line[scode + 2:]
                    data = data[:sexch]
                    exchange = data.upper()

            sstart = re.search('class=bcMLink>', line,
                               re.IGNORECASE | re.MULTILINE)
            if sstart:
                sstart = sstart.end()
                send = re.search('</a></td>', line[sstart:],
                                 re.IGNORECASE | re.MULTILINE)
                if send:
                    send = send.start()
                    data = line[sstart:]
                    data = data[:send]

                    if scode:
                        ticker = data.upper()
                    else:
                        name = data

            if name != '' and ticker != '' and exchange != '':
                #print '"',name, '" - "',ticker, '" - "',exchange,'"'
                name = filterName(name)

                if exchange == 'TSX': exchange = 'TORONTO EXCHANGE'
                if exchange == 'TSX.V': exchange = 'TORONTO VENTURE'

                #print '"',name, '" - "',ticker, '" - "',exchange,'" market:',market

                if exchange == market:
                    if ticker[-3:] == '.TO' or ticker[-3:] == '.VN':
                        ticker = ticker[:-3]

                    quotes.addQuote(isin='',
                                    name=name,
                                    ticker=ticker,
                                    market=exchange,
                                    currency=m_currency,
                                    place=m_place,
                                    country=m_country)
                    count = count + 1

                # reset everything
                name = ''
                ticker = ''
                exchange = ''

        print 'Imported %d lines from BARCHART data (letter=%s)' % (count,
                                                                    letter)
def Import_ListOfQuotes_Euronext(quotes,market='EURONEXT',dlg=None,x=0):
    print 'Update %s list of symbols' % market
    connection = ITradeConnection(cookies = None,
                               proxy = itrade_config.proxyHostname,
                               proxyAuth = itrade_config.proxyAuthentication,
                               connectionTimeout = max(45,itrade_config.connectionTimeout)
                               )

    cha = "7213"

    if market=='EURONEXT':
        url = "http://www.euronext.com/search/download/trapridownloadpopup.jcsv?pricesearchresults=actif&filter=1&lan=EN&belongsToList=market_EURLS&cha=%s&format=txt&formatDecimal=.&formatDate=dd/MM/yy" % cha
    elif market=='ALTERNEXT':
        url = "http://www.euronext.com/search/download/trapridownloadpopup.jcsv?pricesearchresults=actif&filter=1&lan=EN&belongsToList=market_ALTX&cha=%s&format=txt&formatDecimal=.&formatDate=dd/MM/yy" % cha
    elif market=='PARIS MARCHE LIBRE':
        url = "http://www.euronext.com/search/download/trapridownloadpopup.jcsv?pricesearchresults=actif&filter=1&lan=EN&belongsToList=market_MC&cha=%s&format=txt&formatDecimal=.&formatDate=dd/MM/yy" % cha
    elif market=='BRUXELLES MARCHE LIBRE':
        url = "http://www.euronext.com/search/download/trapridownloadpopup.jcsv?pricesearchresults=actif&filter=1&lan=EN&belongsToList=market_BRUMC&cha=%s&format=txt&formatDecimal=.&formatDate=dd/MM/yy" % cha
    else:
        return False

    def splitLines(buf):
        lines = string.split(buf, '\n')
        lines = filter(lambda x:x, lines)
        def removeCarriage(s):
            if s[-1]=='\r':
                return s[:-1]
            else:
                return s
        lines = [removeCarriage(l) for l in lines]
        return lines

    try:
        data=connection.getDataFromUrl(url)
    except:
        debug('Import_ListOfQuotes_Euronext:unable to connect :-(')
        return False

    indice = {}
    """
    "Instrument's name";
    "ISIN";
    "Euronext code";
    "MEP";
    "Symbol";
    "ICB Sector (Level 4)";
    "Trading currency";
    "Last";
    "Volume";
    "D/D-1 (%)";
    "Date - time (CET)";
    "Turnover";
    "Total number of shares";
    "Capitalisation";
    "Trading mode";
    "Day First";
    "Day High";
    "Day High / Date - time (CET)";
    "Day Low";
    "Day Low / Date - time (CET)";
    "31-12/Change (%)";
    "31-12/High";
    "31-12/High/Date";
    "31-12/Low";
    "31-12/Low/Date";
    "52 weeks/Change (%)";
    "52 weeks/High";
    "52 weeks/High/Date";
    "52 weeks/Low";
    "52 weeks/Low/Date";
    "Suspended";
    "Suspended / Date - time (CET)";
    "Reserved";
    "Reserved / Date - time (CET)"
    """

    # returns the data
    lines = splitLines(data)
    count = 0

    for line in lines:
        data = string.split (line, ';')

        if len(data)>2:
            if not indice.has_key("ISIN"):
                i = 0
                for ind in data:
                    indice[ind] = i
                    i = i + 1

                iName = indice["Instrument's name"]
                iISIN = indice["ISIN"]
                iMEP = indice["MEP"]
                iTicker = indice["Symbol"]
                iCurr = indice["Trading currency"]

            else:
                if data[iISIN]!="ISIN":
                    if checkISIN(data[iISIN]):
                        if data[iMEP]=='PAR' or data[iMEP]=='BRU' or data[iMEP]=='AMS' or data[iMEP]=='LIS':
                            name = filterName(data[iName])
                            quotes.addQuote(isin=data[iISIN],name=name,ticker=data[iTicker],market=market,currency=data[iCurr],place=data[iMEP],country=None)
                            count = count + 1
                        else:
                            if itrade_config.verbose:
                                print 'unknown place :',data[iMEP],'>>>',data
                    else:
                        if itrade_config.verbose:
                            print 'invalid ISIN :',data[iISIN],'>>>',data

        else:
            print len(data),'>>>',data

    print 'Imported %d/%d lines from %s data.' % (count,len(lines),market)

    return True
    def import_letter(letter,dlg,x):

        if dlg:
            dlg.Update(x,"BARCHART %s:'%s'"%(market,letter))
            #print "BARCHART %s:'%s'"%(market,letter)

        if not barchart_data.has_key("%s.%s" % (letter,m_country)):
            # read file (for debugging) or get file from network
            try:
                fn = open('barchart%s.htm' % letter,'r')
            except IOError:
                # can't open the file (existing ?)
                fn = None

            if not fn:
                try:
                    data=connection.getDataFromUrl(url%letter)
                except:
                    print 'Import_ListOfQuotes_BARCHART:unable to connect to',url%letter
                    return False
            else:
                data=fn.read()

            # returns the data
            barchart_data["%s.%s" % (letter,m_country)] = data

        #
        lines = splitLines(barchart_data["%s.%s" % (letter,m_country)])
        ticker = ''
        name = ''
        exchange = ''
        count = 0

        for line in lines:
            # Typical lines :
            # <TD class=bcText><a href="http://quote.barchart.com/quote.asp?sym=ONJP&code=BSTK" class=bcMLink>1 900 JACKPOT INC</a></TD>
            # <TD class=bcText align=right><a href="http://quote.barchart.com/quote.asp?sym=ONJP&code=BSTK" class=bcMLink>ONJP</a></TD>
            # <TD class=bcText align=right> OTCBB </TD>

            scode = re.search('align=right',line,re.IGNORECASE|re.MULTILINE)
            if scode:
                scode = scode.end()
                sexch = re.search(' </td>',line[scode+2:],re.IGNORECASE|re.MULTILINE)
                if sexch:
                    sexch = sexch.start()
                    data = line[scode+2:]
                    data = data[:sexch]
                    exchange = data.upper()

            sstart = re.search('class=bcMLink>',line,re.IGNORECASE|re.MULTILINE)
            if sstart:
                sstart = sstart.end()
                send = re.search('</a></td>',line[sstart:],re.IGNORECASE|re.MULTILINE)
                if send:
                    send = send.start()
                    data = line[sstart:]
                    data = data[:send]

                    if scode:
                        ticker = data.upper()
                    else:
                        name = data

            if name!='' and ticker!='' and exchange!='':
                #print '"',name, '" - "',ticker, '" - "',exchange,'"'
                name = filterName(name)

                if exchange=='TSX': exchange='TORONTO EXCHANGE'
                if exchange=='TSX.V' : exchange='TORONTO VENTURE'

                #print '"',name, '" - "',ticker, '" - "',exchange,'" market:',market

                if exchange==market:
                    if ticker[-3:]=='.TO' or ticker[-3:]=='.VN':
                        ticker = ticker[:-3]

                    quotes.addQuote(isin='',name=name,ticker=ticker,market=exchange,currency=m_currency,place=m_place,country=m_country)
                    count = count + 1

                # reset everything
                name = ''
                ticker = ''
                exchange = ''

        print 'Imported %d lines from BARCHART data (letter=%s)' % (count,letter)
def Import_ListOfQuotes_Euronext(quotes, market='EURONEXT', dlg=None, x=0):
    print 'Update %s list of symbols' % market
    connection = ITradeConnection(cookies=None,
                                  proxy=itrade_config.proxyHostname,
                                  proxyAuth=itrade_config.proxyAuthentication,
                                  connectionTimeout=max(
                                      45, itrade_config.connectionTimeout))

    cha = "7213"

    if market == 'EURONEXT':
        url = "http://www.euronext.com/search/download/trapridownloadpopup.jcsv?pricesearchresults=actif&filter=1&lan=EN&belongsToList=market_EURLS&cha=%s&format=txt&formatDecimal=.&formatDate=dd/MM/yy" % cha
    elif market == 'ALTERNEXT':
        url = "http://www.euronext.com/search/download/trapridownloadpopup.jcsv?pricesearchresults=actif&filter=1&lan=EN&belongsToList=market_ALTX&cha=%s&format=txt&formatDecimal=.&formatDate=dd/MM/yy" % cha
    elif market == 'PARIS MARCHE LIBRE':
        url = "http://www.euronext.com/search/download/trapridownloadpopup.jcsv?pricesearchresults=actif&filter=1&lan=EN&belongsToList=market_MC&cha=%s&format=txt&formatDecimal=.&formatDate=dd/MM/yy" % cha
    elif market == 'BRUXELLES MARCHE LIBRE':
        url = "http://www.euronext.com/search/download/trapridownloadpopup.jcsv?pricesearchresults=actif&filter=1&lan=EN&belongsToList=market_BRUMC&cha=%s&format=txt&formatDecimal=.&formatDate=dd/MM/yy" % cha
    else:
        return False

    def splitLines(buf):
        lines = string.split(buf, '\n')
        lines = filter(lambda x: x, lines)

        def removeCarriage(s):
            if s[-1] == '\r':
                return s[:-1]
            else:
                return s

        lines = [removeCarriage(l) for l in lines]
        return lines

    try:
        data = connection.getDataFromUrl(url)
    except:
        debug('Import_ListOfQuotes_Euronext:unable to connect :-(')
        return False

    indice = {}
    """
    "Instrument's name";
    "ISIN";
    "Euronext code";
    "MEP";
    "Symbol";
    "ICB Sector (Level 4)";
    "Trading currency";
    "Last";
    "Volume";
    "D/D-1 (%)";
    "Date - time (CET)";
    "Turnover";
    "Total number of shares";
    "Capitalisation";
    "Trading mode";
    "Day First";
    "Day High";
    "Day High / Date - time (CET)";
    "Day Low";
    "Day Low / Date - time (CET)";
    "31-12/Change (%)";
    "31-12/High";
    "31-12/High/Date";
    "31-12/Low";
    "31-12/Low/Date";
    "52 weeks/Change (%)";
    "52 weeks/High";
    "52 weeks/High/Date";
    "52 weeks/Low";
    "52 weeks/Low/Date";
    "Suspended";
    "Suspended / Date - time (CET)";
    "Reserved";
    "Reserved / Date - time (CET)"
    """

    # returns the data
    lines = splitLines(data)
    count = 0

    for line in lines:
        data = string.split(line, ';')

        if len(data) > 2:
            if not indice.has_key("ISIN"):
                i = 0
                for ind in data:
                    indice[ind] = i
                    i = i + 1

                iName = indice["Instrument's name"]
                iISIN = indice["ISIN"]
                iMEP = indice["MEP"]
                iTicker = indice["Symbol"]
                iCurr = indice["Trading currency"]

            else:
                if data[iISIN] != "ISIN":
                    if checkISIN(data[iISIN]):
                        if data[iMEP] == 'PAR' or data[iMEP] == 'BRU' or data[
                                iMEP] == 'AMS' or data[iMEP] == 'LIS':
                            name = filterName(data[iName])
                            quotes.addQuote(isin=data[iISIN],
                                            name=name,
                                            ticker=data[iTicker],
                                            market=market,
                                            currency=data[iCurr],
                                            place=data[iMEP],
                                            country=None)
                            count = count + 1
                        else:
                            if itrade_config.verbose:
                                print 'unknown place :', data[
                                    iMEP], '>>>', data
                    else:
                        if itrade_config.verbose:
                            print 'invalid ISIN :', data[iISIN], '>>>', data

        else:
            print len(data), '>>>', data

    print 'Imported %d/%d lines from %s data.' % (count, len(lines), market)

    return True