Last active
January 6, 2021 23:41
-
-
Save 0ex-d/0d1765a75d2df8d1ae4cb88c67c61c84 to your computer and use it in GitHub Desktop.
Price alert snippet for monitoring market cap on assets exchanges.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Price alert snippet | |
* For monitoring market cap on assets exchanges | |
* Precious Akin | |
*/ | |
// Available exchange assets | |
enum ASSET { | |
'NGN' = '\u20A6', // ₦ | |
'USD' = '$' | |
} | |
/** | |
* Calculate percent change in value | |
* @param oldPrice Initial value | |
* @param newPrice Most recent value | |
* @returns The percentage change between old and new value. | |
*/ | |
const calcPercentDiff = (oldPrice:number, newPrice:number):number => { | |
let diff = newPrice - oldPrice; | |
let calcPrice = (diff)/oldPrice * 100; | |
return Number(calcPrice.toFixed(2)); | |
} | |
/** | |
* People friendly money value | |
* @param value An amount to format | |
* @returns A comma-seperated value | |
*/ | |
const formatMoney = (value:number):string => { | |
return `${value.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')}`; | |
}; | |
/** | |
* Check price change in the network | |
* @param oldData The previous market data | |
* @param newData The most recent market data | |
*/ | |
export const priceCheck = (oldData, newData) => { | |
const diff = calcPercentDiff(oldData, newData); | |
let msg; | |
oldData = ASSET.NGN + formatMoney(oldData); | |
newData = ASSET.NGN + formatMoney(newData); | |
if(oldData==newData) { | |
msg = (`${ASSET.NGN + newData}`); | |
return {diff, msg, xValue:'GAIN'}; | |
} | |
if(oldData < newData) { | |
msg = (`Price has increased -> From ${oldData} to ${newData}`); | |
return {diff, msg, xValue:'GAIN'}; | |
} | |
if(oldData > newData) { | |
msg = (`Price has decreased -> From ${oldData} to ${newData}`); | |
return {diff, msg, xValue:'LOSS'}; | |
} | |
} | |
// Use case | |
const {msg, diff, xValue} = priceCheck(39000, 80000); | |
console.log(`${msg} | |
${Math.abs(diff)}% (${xValue})`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: This is a Typescript file; to run with node you must have Typescript installed globally and then transpile to javascript file.
Run the below commands if you have Node.js installed on your machine.