Writing a random stock generator of Mosbirzhe in JavaScript

The idea came about after I accidentally saw a similar generator for the US exchange NASDAQ , where the bash script downloads a summary list of American papers from an FTP server and transforms it into JSON, consisting of only tickers, and then displays the bootstrap framework and pure JavaScript on the screen a random stock ticker while linking to the popular Yahoo! Finance


The "magic" of the CodePen platform for the Moscow Exchange

The code was adapted for Russian realities, and in addition to receiving a list of securities from the Moscow Stock Exchange, profitability calculation for the last n years was made.

An additional server was not required, because the Mosbirzi API can do the issuance immediately in JSON format.

What is needed for the operation of the random share generator?


  1. Take information from the exchange about the existing โ€œboardid identifiersโ€ (boardid) of the Moscow Exchange.

    //      
    url = 'https://iss.moex.com/iss/engines/stock/markets/shares/boards/'
  2. Take a complete list of papers for some boardid at Mosbirzhe.

    boardid = 'TQBR' //  
    url = 'https://iss.moex.com/iss/engines/stock/markets/shares/boards/'+ boardid +'/securities.json?iss.meta=off&iss.only=securities&securities.columns=SECID,SECNAME'
  3. Check the summary statistics of securities on the Moscow Exchange.
  4. Find out the current price for a particular security.

    boardid = 'TQBR' //  
    url = 'http://iss.moex.com/iss/engines/stock/markets/shares/boards/'+ boardid +'/securities.json?iss.meta=off&iss.only=securities&securities.columns=SECID,PREVADMITTEDQUOTE'
  5. Get the price n years ago on a specific date. To do this, in the Mosbirzhi API we pass the boardid, SECID and date, for example:
    - boardid = TQBR
    - SECID = SBER
    - date = 2015-01-10
    .

    boardid = 'TQBR' //   
    SECID = 'SBER' // 
    date = '2015-01-10' // 
    url = 'http://iss.moex.com/iss/history/engines/stock/markets/shares/boards/'+ boardid +'/securities/'+ SECID +'.json?iss.meta=off&iss.only=history&history.columns=SECID,TRADEDATE,CLOSE&limit=1&from=' + date
  6. Compare yesterday's asset price with the price 5 years ago to find out the profitability.

    curStock = 'AFKS'
    dateNow = '2020-03-06'
    datePre = '2015-03-06'
    fetch(getCost(curStock, datePre)).then(result => {
        return (result.json())
    }).then(res => {
        let costPre = res.history.data[0][2];
        return (costPre)
    }).then(costPre => {
        fetch(getCost(curStock, dateNow)).then(result => {
            return (result.json())
        }).then(res => {
            let costNow = res.history.data[0][2];
            console.log(costPre);
            console.log(costNow);
            console.log(parseInt((costNow * 100) / costPre, 10) - 100)
        })
    })
    
    function getCost(id, date) {
        let url = `http://iss.moex.com/iss/history/engines/stock/markets/shares/boards/TQBR/securities/${id}.json?iss.meta=off&iss.only=history&history.columns=SECID,TRADEDATE,CLOSE&limit=1&from=${date}`
        return url;
    }

There were no big requirements to the code, it was important that it works and performs its task.


GIF with a demonstration of the Mosbirzi random generator generator in JavaScript

For some reason, the Mosbirzi API has been issuing historical data only since June 2014, that is, it is not possible to get earlier data through a request.

Full code on GitHub and CodePen .

Total


The random stock generator from the Moscow Exchange works and issues not only random papers, but also considers the yield for a custom time interval.

I also want to note that it is in no way connected with the Moscow Exchange and I use ISS Mosbirzhy only for personal interests.

Author: Mikhail Shardin .
Code: Alexander Palachev .

April 3, 2020

All Articles