Last active
April 13, 2022 18:53
-
-
Save stav/a4f0fa7787d85403a7e89db97f2d27c4 to your computer and use it in GitHub Desktop.
Candles prices horizontal
This file contains hidden or 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
// If you run this in Deno: | |
priceCandles (prices: PriceBars) { | |
// Function scope finals | |
const bars = prices.PriceBars | |
const lows = bars.map(bar => bar.Low) | |
const highs = bars.map(bar => bar.High) | |
const high = Math.max(...highs) | |
const low = Math.min(...lows) | |
const diff = high - low | |
const blockSize = 100 / diff | |
const colWidth = Math.ceil(blockSize) - blockSize/3 | |
// Debug print finals | |
console.debug( | |
low, high, | |
diff.toFixed(1), | |
green(blockSize.toFixed(1)), | |
colWidth.toFixed(1), | |
prices.PartialPriceBar, | |
) | |
// Print header | |
let header = ' '.repeat(69) | |
for (let i=0; i<=9; i++) | |
header += sprintf('| %-*d', 9, (low + i * diff / 9).toFixed(diff < 9 ? 1 : 0)) | |
console.log(header) | |
// Loop thru bars printing candles | |
for (const bar of bars) { | |
const timestamps = bar.BarDate.match(/\d+/) | |
if (timestamps) { | |
const lowOffset = bar.Low - low | |
const lowBlocks = Math.floor(lowOffset * blockSize) | |
const highOffset = bar.High - low - lowOffset | |
const highBlocks = Math.floor(highOffset * blockSize) | |
const openIndex = Math.round((bar.Open - bar.Low) / (bar.High - bar.Low) * highBlocks) | |
const closeIndex = Math.round((bar.Close - bar.Low) / (bar.High - bar.Low) * highBlocks) | |
const min = Math.min(openIndex, closeIndex) | |
const max = Math.max(openIndex, closeIndex) | |
const body = max > min ? '='.repeat(max - min - 1) : '' | |
let candle = '-'.repeat(highBlocks) | |
candle = candle.substring(0, openIndex) + 'O' + candle.substring(openIndex + 1) | |
candle = candle.substring(0, closeIndex) + 'C' + candle.substring(closeIndex + 1) | |
candle = candle.substring(0, min + 1) + body + candle.substring(max + (max > min ? 0 : 1)) | |
candle = openIndex < closeIndex ? green(candle) : red(candle) | |
console.log( | |
new Date(+timestamps[0]), | |
blue (sprintf('%4.1f', bar.Low.toString())), | |
red (sprintf('%4.1f', bar.Open.toString())), | |
yellow(sprintf('%4.1f', bar.Close.toString())), | |
blue (sprintf('%4.1f', bar.High.toString())), | |
yellow(sprintf('%2d', Math.round(lowOffset).toString())), | |
blue (sprintf('%2d', lowBlocks.toString())), | |
blue (sprintf('%2d', highBlocks.toString())), | |
yellow(sprintf('%2d', openIndex.toString())), | |
yellow(sprintf('%2d', closeIndex.toString())), | |
' '.repeat(lowBlocks), | |
candle, | |
) | |
} | |
} | |
} | |
// and prices looks like this: | |
{ | |
PriceBars: [ | |
{ | |
BarDate: "/Date(1648598400000)/", | |
Open: 1919.53, | |
High: 1938.61, | |
Low: 1916.02, | |
Close: 1932.73 | |
}, | |
{ | |
BarDate: "/Date(1648684800000)/", | |
Open: 1933.24, | |
High: 1949.9, | |
Low: 1919.29, | |
Close: 1937.41 | |
}, | |
{ | |
BarDate: "/Date(1648771200000)/", | |
Open: 1937.48, | |
High: 1939.57, | |
Low: 1918.09, | |
Close: 1925.22 | |
}, | |
{ | |
BarDate: "/Date(1649030400000)/", | |
Open: 1925.55, | |
High: 1936.94, | |
Low: 1915.61, | |
Close: 1932.47 | |
}, | |
{ | |
BarDate: "/Date(1649116800000)/", | |
Open: 1932.97, | |
High: 1944.66, | |
Low: 1918.02, | |
Close: 1923.76 | |
}, | |
{ | |
BarDate: "/Date(1649203200000)/", | |
Open: 1923.83, | |
High: 1933.54, | |
Low: 1915.22, | |
Close: 1925.1 | |
}, | |
{ | |
BarDate: "/Date(1649289600000)/", | |
Open: 1925.02, | |
High: 1937.82, | |
Low: 1920.64, | |
Close: 1931.87 | |
}, | |
{ | |
BarDate: "/Date(1649376000000)/", | |
Open: 1931.73, | |
High: 1948.21, | |
Low: 1927.8, | |
Close: 1948.15 | |
}, | |
{ | |
BarDate: "/Date(1649635200000)/", | |
Open: 1946.57, | |
High: 1969.66, | |
Low: 1940.05, | |
Close: 1953.71 | |
}, | |
{ | |
BarDate: "/Date(1649721600000)/", | |
Open: 1952.97, | |
High: 1978.71, | |
Low: 1949.75, | |
Close: 1966.66 | |
} | |
], | |
PartialPriceBar: { | |
BarDate: "/Date(1649808000000)/", | |
Open: 1966.88, | |
High: 1981.59, | |
Low: 1962.92, | |
Close: 1977.96 | |
} | |
} | |
// then the console might look like this: | |
1915.22 1978.71 63.5 1.6 1.5 { | |
BarDate: "/Date(1649808000000)/", | |
Open: 1966.88, | |
High: 1981.59, | |
Low: 1962.92, | |
Close: 1977.96 | |
} | |
// | 1915 | 1922 | 1929 | 1936 | 1943 | 1950 | 1958 | 1965 | 1972 | 1979 | |
// 2022-03-30T00:00:00.000Z 1916.0 1919.5 1932.7 1938.6 1 1 35 5 26 -----O====================C-------- | |
// 2022-03-31T00:00:00.000Z 1919.3 1933.2 1937.4 1949.9 4 6 48 22 28 ----------------------O=====C------------------- | |
// 2022-04-01T00:00:00.000Z 1918.1 1937.5 1925.2 1939.6 3 4 33 30 11 -----------C==================O-- | |
// 2022-04-04T00:00:00.000Z 1915.6 1925.6 1932.5 1936.9 0 0 33 15 26 ---------------O==========C------ | |
// 2022-04-05T00:00:00.000Z 1918.0 1933.0 1923.8 1944.7 3 4 41 23 9 ---------C=============O----------------- | |
// 2022-04-06T00:00:00.000Z 1915.2 1923.8 1925.1 1933.5 0 0 28 13 15 -------------O=C------------ | |
// 2022-04-07T00:00:00.000Z 1920.6 1925.0 1931.9 1937.8 5 8 27 7 18 -------O==========C-------- | |
// 2022-04-08T00:00:00.000Z 1927.8 1931.7 1948.2 1948.2 13 19 32 6 32 ------O=========================C | |
// 2022-04-11T00:00:00.000Z 1940.1 1946.6 1953.7 1969.7 25 39 46 10 21 ----------O==========C------------------------ | |
// 2022-04-12T00:00:00.000Z 1949.8 1953.0 1966.7 1978.7 35 54 45 5 26 -----O====================C------------------ | |
// { span: 1, PriceBars: 10, priceType: "MID", interval: "DAY" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment