Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | 1x 6x 6x 6x 6x 6x 1x 4x 4x 4x 1x 1x 3x 3x 3x 1x 2x 2x 2x 2x 2x 1x | import { OptionType } from '@node-dlc/core';
/**
* Composes a Deribit formatted datestring for a given date.
*
* @param date Date or timestamp in seconds
* @returns deribit formatted datestring (e.g. '14MAY22')
*/
export const getStrDate = (date: number | Date): string => {
Eif (typeof date === 'number') date = new Date(date * 1000);
const day = date.getUTCDate();
const month = date
.toLocaleString('en-US', { month: 'short', timeZone: 'UTC' })
.toUpperCase()
.replace('.', '');
const year = date.toLocaleDateString('pl', {
year: '2-digit',
timeZone: 'UTC',
});
return `${day}${month}${year}`;
};
/**
* An object returned by parseInstrumentName,
* which contains the expiry, strike price, and option type.
*
* @param expiry - expiry date
* @param strikePrice - strike price
* @param type - option type (call or put)
*/
type OptionInstrument = {
expiry: Date;
strikePrice: number;
type: OptionType;
};
export const composeInstrumentName = ({
expiry,
type,
strikePrice,
}: OptionInstrument): string => {
const strDate = getStrDate(expiry.getTime() / 1000);
const typeSymbol = type === 'call' ? 'C' : 'P';
return `BTC-${strDate}-${strikePrice}-${typeSymbol}`;
};
const EXPIRY_REGEX = /(\d{1,2})([A-Z]+)(\d{1,2})/;
/**
* Parses an instrument name into its expiry, strike price, and option type.
*
* @param instrumentName e.g. BTC-1JAN19-120000-C
*/
export const parseInstrumentName = (
instrumentName: string,
): OptionInstrument => {
try {
const [, expiryString, _strikePrice, typeSymbol] =
instrumentName.split('-');
if (
!typeSymbol ||
!EXPIRY_REGEX.test(expiryString) ||
(typeSymbol !== 'C' && typeSymbol !== 'P')
) {
throw new Error(`Only option instrument names are supported.`);
}
const [, day, month, year] = expiryString.match(EXPIRY_REGEX);
const expiry = new Date(`${month}-${day}-${year} 08:00:00 GMT`);
const strikePrice = parseInt(_strikePrice, 10);
const type = typeSymbol === 'C' ? 'call' : 'put';
return {
expiry,
strikePrice,
type,
};
} catch (e) {
throw new Error(`Unsupported instrument name: ${instrumentName}`);
}
};
|