niver code

This commit is contained in:
olli 2024-07-15 17:10:48 +02:00
parent 2bdc7d3ab2
commit e06b575e46

View File

@ -2,14 +2,12 @@ const urlParams = new URLSearchParams(window.location.search);
const symbol = urlParams.get('symbol'); const symbol = urlParams.get('symbol');
const time = urlParams.get('time'); const time = urlParams.get('time');
//window.dispatchEvent(new Event('resize'));
function timeToLocal(originalTime) { function timeToLocal(originalTime) {
const d = new Date(originalTime * 1000); const d = new Date(originalTime * 1000);
return Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds()) / 1000; return Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds()) / 1000;
} }
function parseCSV(data) { function parseCSV(data) {
const rows = data.split("\n"); const rows = data.split("\n");
const result = []; const result = [];
let start = Math.max(rows.length - 298, 0); let start = Math.max(rows.length - 298, 0);
@ -39,24 +37,43 @@ function timeToLocal(originalTime) {
} }
} }
return result; return result;
} }
// Create the Lightweight Chart within the container element function getCrosshairDataPoint(series, param) {
const chart = LightweightCharts.createChart(document.getElementById('container'), if (!param.time) {
{ return null;
}
const dataPoint = param.seriesData.get(series);
return dataPoint || null;
}
function syncCrosshair(chart, series, dataPoint) {
if (dataPoint) {
chart.setCrosshairPosition(dataPoint.value, dataPoint.time, series);
return;
}
chart.clearCrosshairPosition();
}
// Create the Lightweight Chart within the container element
const chart = LightweightCharts.createChart(document.getElementById('container'),
{
rightPriceScale: { rightPriceScale: {
minimumWidth: 100, minimumWidth: 100,
borderVisible: false borderVisible: false
}, },
height: 500, height: 500,
crosshair: { crosshair: {
mode: 0, mode: 0,
}, },
timeScale: { timeScale: {
timeVisible: true, timeVisible: true,
secondsVisible: false, secondsVisible: false,
}, },
layout: { layout: {
background: { background: {
type: 'solid', type: 'solid',
@ -64,13 +81,15 @@ function timeToLocal(originalTime) {
}, },
textColor: '#DDD', textColor: '#DDD',
}, },
grid: { grid: {
vertLines: { color: '#444' }, vertLines: { color: '#444' },
horzLines: { color: '#444' }, horzLines: { color: '#444' },
}, },
});
chart.applyOptions({ });
chart.applyOptions({
watermark: { watermark: {
visible: true, visible: true,
fontSize: 18, fontSize: 18,
@ -78,31 +97,33 @@ function timeToLocal(originalTime) {
vertAlign: 'left', vertAlign: 'left',
color: '#DDD', color: '#DDD',
text: symbol + " " + time, text: symbol + " " + time,
}}); }
});
// define chart
const candleSeries = chart.addCandlestickSeries();
const lineSeriesEMA12 = chart.addLineSeries({ color: 'red', lineWidth: 1, priceLineVisible: false});
const lineSeriesEMA26 = chart.addLineSeries({ color: 'pink', lineWidth: 1, lineStyle: 2, priceLineVisible: false});
const lineSeriesEMA50 = chart.addLineSeries({ color: 'cyan', lineWidth: 1, priceLineVisible: false});
const lineSeriesEMA100 = chart.addLineSeries({ color: 'yellow', lineWidth: 1, priceLineVisible: false});
const lineSeriesEMA200 = chart.addLineSeries({ color: 'white', lineWidth: 1, priceLineVisible: false});
const lineSeriesEMA400 = chart.addLineSeries({ color: 'orange', lineWidth: 1, priceLineVisible: false});
const lineSeriesEMA800 = chart.addLineSeries({ color: 'purple', lineWidth: 1, priceLineVisible: false});
// define chart // RSI Chart
const candleSeries = chart.addCandlestickSeries(); const chartrsi = LightweightCharts.createChart(document.getElementById("container"),
const lineSeriesEMA12 = chart.addLineSeries({ color: 'red', lineWidth: 1, priceLineVisible: false}); {
const lineSeriesEMA26 = chart.addLineSeries({ color: 'pink', lineWidth: 1, lineStyle: 2, priceLineVisible: false});
const lineSeriesEMA50 = chart.addLineSeries({ color: 'cyan', lineWidth: 1, priceLineVisible: false});
const lineSeriesEMA100 = chart.addLineSeries({ color: 'yellow', lineWidth: 1, priceLineVisible: false});
const lineSeriesEMA200 = chart.addLineSeries({ color: 'white', lineWidth: 1, priceLineVisible: false});
const lineSeriesEMA400 = chart.addLineSeries({ color: 'orange', lineWidth: 1, priceLineVisible: false});
const lineSeriesEMA800 = chart.addLineSeries({ color: 'purple', lineWidth: 1, priceLineVisible: false});
// RSI Chart
const chartrsi = LightweightCharts.createChart(document.getElementById("container"),
{
rightPriceScale: { rightPriceScale: {
minimumWidth: 100, minimumWidth: 100,
borderVisible: false borderVisible: false
}, },
height: 200, height: 200,
timeScale: { timeScale: {
visible: false, visible: false,
}, },
layout: { layout: {
background: { background: {
type: 'solid', type: 'solid',
@ -110,13 +131,14 @@ function timeToLocal(originalTime) {
}, },
textColor: '#DDD', textColor: '#DDD',
}, },
grid: { grid: {
vertLines: { color: '#444' }, vertLines: { color: '#444' },
horzLines: { color: '#444' }, horzLines: { color: '#444' },
}, },
}); });
chartrsi.applyOptions({ chartrsi.applyOptions({
watermark: { watermark: {
visible: true, visible: true,
fontSize: 18, fontSize: 18,
@ -124,25 +146,28 @@ function timeToLocal(originalTime) {
vertAlign: 'left', vertAlign: 'left',
color: '#DDD', color: '#DDD',
text: 'RSI 5,14,21', text: 'RSI 5,14,21',
}}); }
});
const lineSeriesRSI5 = chartrsi.addLineSeries({ color: 'orange', lineWidth: 1, lineStyle: 2, priceLineVisible: false}); const lineSeriesRSI5 = chartrsi.addLineSeries({ color: 'orange', lineWidth: 1, lineStyle: 2, priceLineVisible: false});
const lineSeriesRSI14 = chartrsi.addLineSeries({ color: 'yellow', lineWidth: 2, priceLineVisible: false}); const lineSeriesRSI14 = chartrsi.addLineSeries({ color: 'yellow', lineWidth: 2, priceLineVisible: false});
const lineSeriesRSI21 = chartrsi.addLineSeries({ color: 'lightgreen', lineWidth: 1, lineStyle: 2, priceLineVisible: false}); const lineSeriesRSI21 = chartrsi.addLineSeries({ color: 'lightgreen', lineWidth: 1, lineStyle: 2, priceLineVisible: false});
// MACD Chart // MACD Chart
const chartmacd = LightweightCharts.createChart(document.getElementById("container"), const chartmacd = LightweightCharts.createChart(document.getElementById("container"),
{ {
rightPriceScale: { rightPriceScale: {
minimumWidth: 100, minimumWidth: 100,
borderVisible: false borderVisible: false
}, },
height: 200, height: 200,
timeScale: { timeScale: {
timeVisible: true, timeVisible: true,
secondsVisible: false, secondsVisible: false,
}, },
layout: { layout: {
background: { background: {
type: 'solid', type: 'solid',
@ -150,13 +175,14 @@ function timeToLocal(originalTime) {
}, },
textColor: '#DDD', textColor: '#DDD',
}, },
grid: { grid: {
vertLines: { color: '#444' }, vertLines: { color: '#444' },
horzLines: { color: '#444' }, horzLines: { color: '#444' },
}, },
}); });
chartmacd.applyOptions({ chartmacd.applyOptions({
watermark: { watermark: {
visible: true, visible: true,
fontSize: 18, fontSize: 18,
@ -164,23 +190,22 @@ function timeToLocal(originalTime) {
vertAlign: 'left', vertAlign: 'left',
color: '#DDD', color: '#DDD',
text: 'MACD 12 26', text: 'MACD 12 26',
}}); }
});
const lineSeriesMACD = chartmacd.addLineSeries({ color: 'blue', lineWidth: 1, lineStyle: 0, priceLineVisible: false}); const lineSeriesMACD = chartmacd.addLineSeries({ color: 'blue', lineWidth: 1, lineStyle: 0, priceLineVisible: false});
const lineSeriesMACDSignal = chartmacd.addLineSeries({ color: 'orange', lineWidth: 1, lineStyle: 0, priceLineVisible: false}); const lineSeriesMACDSignal = chartmacd.addLineSeries({ color: 'orange', lineWidth: 1, lineStyle: 0, priceLineVisible: false});
const histogramSeriesMACD = chartmacd.addHistogramSeries({ const histogramSeriesMACD = chartmacd.addHistogramSeries({
priceFormat: { priceFormat: {
type: 'volume', type: 'volume',
color: 'orange', color: 'orange',
}, },
//priceScaleId: '', // set as an overlay by setting a blank priceScaleId });
});
fetch("/botdata/asset-histories/" + symbol + ".history." + time + ".csv")
fetch("/botdata/asset-histories/" + symbol + ".history." + time + ".csv") .then(response => response.text())
.then(response => response.text()) .then(data => {
.then(data => {
const parsedData = parseCSV(data); const parsedData = parseCSV(data);
// OHLC Data // OHLC Data
@ -275,51 +300,35 @@ function timeToLocal(originalTime) {
color: item[100] color: item[100]
})); }));
histogramSeriesMACD.setData(histogramSeriesMACDData); histogramSeriesMACD.setData(histogramSeriesMACDData);
}); });
// Lines for price levels // Lines for price levels
fetch("/botdata/asset-histories/" + symbol + ".history.csv.levels") fetch("/botdata/asset-histories/" + symbol + ".history.csv.levels")
.then(response => response.text()) .then(response => response.text())
.then(text => { .then(text => {
const levels = text.split('\n'); const levels = text.split('\n');
levels.forEach(function(level) { levels.forEach(function(level) {
candleSeries.createPriceLine({price: level, color: "darkblue", lineWidth: 0.5, lineStyle: 0, axisLabelVisible: true, title: 'Level'}); candleSeries.createPriceLine({price: level, color: "darkblue", lineWidth: 0.5, lineStyle: 0, axisLabelVisible: true, title: 'Level'});
}); });
}); });
// Sync charts timeScale // Sync charts timeScale
chart.timeScale().fitContent(); chart.timeScale().fitContent();
chart.timeScale().subscribeVisibleLogicalRangeChange(timeRange => { chart.timeScale().subscribeVisibleLogicalRangeChange(timeRange => {
chartrsi.timeScale().setVisibleLogicalRange(timeRange); chartrsi.timeScale().setVisibleLogicalRange(timeRange);
chartmacd.timeScale().setVisibleLogicalRange(timeRange); chartmacd.timeScale().setVisibleLogicalRange(timeRange);
}); });
chartrsi.timeScale().subscribeVisibleLogicalRangeChange(timeRange => { chartrsi.timeScale().subscribeVisibleLogicalRangeChange(timeRange => {
chart.timeScale().setVisibleLogicalRange(timeRange); chart.timeScale().setVisibleLogicalRange(timeRange);
}); });
chartmacd.timeScale().subscribeVisibleLogicalRangeChange(timeRange => { chartmacd.timeScale().subscribeVisibleLogicalRangeChange(timeRange => {
chart.timeScale().setVisibleLogicalRange(timeRange); chart.timeScale().setVisibleLogicalRange(timeRange);
}); });
function getCrosshairDataPoint(series, param) {
if (!param.time) {
return null;
}
const dataPoint = param.seriesData.get(series);
return dataPoint || null;
}
function syncCrosshair(chart, series, dataPoint) {
if (dataPoint) {
chart.setCrosshairPosition(dataPoint.value, dataPoint.time, series);
return;
}
chart.clearCrosshairPosition();
}
chart.subscribeCrosshairMove(param => { chart.subscribeCrosshairMove(param => {
const dataPoint = getCrosshairDataPoint(lineSeriesEMA50, param); const dataPoint = getCrosshairDataPoint(lineSeriesEMA50, param);
syncCrosshair(chartrsi, lineSeriesRSI14, dataPoint); syncCrosshair(chartrsi, lineSeriesRSI14, dataPoint);
@ -331,7 +340,6 @@ chartrsi.subscribeCrosshairMove(param => {
syncCrosshair(chart, lineSeriesEMA50, dataPoint); syncCrosshair(chart, lineSeriesEMA50, dataPoint);
const dataPointmacd = getCrosshairDataPoint(lineSeriesRSI14, param); const dataPointmacd = getCrosshairDataPoint(lineSeriesRSI14, param);
syncCrosshair(chartmacd, lineSeriesMACD, dataPointmacd); syncCrosshair(chartmacd, lineSeriesMACD, dataPointmacd);
}); });
chartmacd.subscribeCrosshairMove(param => { chartmacd.subscribeCrosshairMove(param => {
const dataPoint = getCrosshairDataPoint(lineSeriesMACD, param); const dataPoint = getCrosshairDataPoint(lineSeriesMACD, param);
@ -339,4 +347,3 @@ chartmacd.subscribeCrosshairMove(param => {
const dataPointrsi = getCrosshairDataPoint(lineSeriesMACD, param); const dataPointrsi = getCrosshairDataPoint(lineSeriesMACD, param);
syncCrosshair(chartrsi, lineSeriesRSI14, dataPointrsi); syncCrosshair(chartrsi, lineSeriesRSI14, dataPointrsi);
}); });