Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate a International Securities Identification Number (ISIN) number

Tags:

math

financial

If I am not wrong, ISIN numbers last position is a verification digit. What is the mathematical function that determines its value in function of the first 11 digits?

like image 530
Pablo Francisco Pérez Hidalgo Avatar asked Apr 22 '13 06:04

Pablo Francisco Pérez Hidalgo


People also ask

What is an international securities identification number (ISIN)?

You are encouraged to solve this task according to the task description, using any language you may know. An International Securities Identification Number (ISIN) is a unique international identifier for a financial security such as a stock or bond. Write a function or program that takes a string as input, and checks whether it is a valid ISIN.

What is ISIN number in finance?

The International Securities Identification Number (ISIN) is a code that uniquely identifies a specific securities issue. The organization that allocates ISINs in any particular country is the country's respective National Numbering Agency (NNA). ... An ISIN is administered by the relevant National Numbering Agency.

How do I find an ISIN number for a security?

Visit the ISIN Database. Securities with which ISINs can be used include debt securities, such as notes or bonds as well shares, such as common stock or shares of a fund, options, derivatives and futures. The ISIN identifies a security.

What is an Isin and how do I validate it?

An ISIN is a 12-character code that identifies a financial security. • A single check digit. Financial and referential instruments issued in the European Union but not relating to a specific EU country jurisdiction would be allocated an ISIN with an EU-prefix. On this page, you can enter a full ISIN you would like to validate.


1 Answers

I'd like to share my implementation in R. It does not require any specific package.

The mgsub is a support function that allows the substitution of all the characters in the ISIN code in one single command. It is copied from Replace multiple letters with accents with gsub

the iso3166alpha2$Code contains the list of countries that Grenade has listed

The algorithm is implemented in the isIsin(x) function, which returns TRUE in case of valid ISIN code

mgsub <- function(pattern, replacement, x, ...) {
  if (length(pattern)!=length(replacement)) {
    stop("pattern and replacement do not have the same length.")
  }
  result <- x
  for (i in 1:length(pattern)) {
    result <- gsub(pattern[i], replacement[i], result, ...)
  }
  result
}

isIsin <- function (identifier) {

  correctPrefix <- substr(identifier, 1, 2) %in% c(iso3166alpha2$Code, "XS")

  correctLength <- nchar(identifier) == 12  

  correctCharset <- !grepl('[[:punct:]]', identifier)

  if(!correctPrefix | !correctLength | !correctCharset) {
    return(FALSE)
  }

  # replace all character with its equivalent number  
  identifierOnlyNumbers <- mgsub(LETTERS, seq(10, 35), substr(identifier, 1, 11))

  # split the identifier in single digits and reverse its order
  characterVector <- rev(unlist(strsplit(identifierOnlyNumbers, "")))

  # Double every second digit of the group of digits with the rightmost character
  characterVector[seq(1, nchar(identifierOnlyNumbers), 2)] <- 
    as.character(as.numeric(characterVector[seq(1, nchar(identifierOnlyNumbers), 2)]) * 2)

  # Subtract 9 if > 9 (can apply to all since no digit can be greater than 9 before doubling)
  # Add up the digits
  summation <- sum(ifelse(as.numeric(characterVector) > 9, as.numeric(characterVector) - 9, as.numeric(characterVector)))

  # Take the 10s modulus of the sum, subtract it from 10 and take the 10s modulus of the result 
  # this final step is important in the instance where the modulus of the sum is 0, as the resulting check digit would be 10
  correctCheckDigit <- (10 - (summation %% 10)) %% 10 == as.numeric(substr(identifier, 12, 12))

  correctCheckDigit 

}
like image 89
Simone Raba Avatar answered Sep 27 '22 22:09

Simone Raba