"<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>BTCUSDC Moving…
- User
"
- Assistant
"
Certainly! Here’s how you can modify your code to achieve your requirements:
Remove the plotting of the second derivative data from your plotData function.
Calculate the moving average 20 of derivativeOfMA20DerivativeData and plot it.
Here are the steps:
1. Modify the calculation of ma20Derivative to use the correct window size of 20:
Change the window size in the moving average calculation from 99 to 20 for ma20Derivative:
// Calculate MA20 of the derivative
const ma20Derivative = movingAverage(derivative, 20);
2. Calculate the moving average 20 of derivativeOfMA20DerivativeData:
Add the following line to calculate the moving average:
// Calculate moving average 20 of derivativeOfMA20Derivative
const ma20OfDerivativeOfMA20Derivative = movingAverage(derivativeOfMA20Derivative, 20);
3. Adjust the startingIndex to ensure all data arrays have valid data:
Since we have additional moving averages, update startingIndex to account for the longest warm-up period:
// The earliest index where all data arrays have valid data
const startingIndex = 140; // Adjusted starting index based on window sizes
4. Prepare the new data array for plotting:
Slice ma20OfDerivativeOfMA20Derivative starting from startingIndex:
const ma20OfDerivativeOfMA20DerivativeData = ma20OfDerivativeOfMA20Derivative.slice(startingIndex);
5. Modify the plotData function call to include the new data series and remove secondDerivativeData:
Update the arguments passed to plotData:
// Plot the data
plotData(
labels,
priceData,
ma99Data,
derivativeData,
/* secondDerivativeData, */ // Comment out or remove this line
ma20DerivativeData,
derivativeOfMA20DerivativeData,
ma20OfDerivativeOfMA20DerivativeData, // Add this line
specialPointsData,
totalAssetValueData
);
6. Modify the plotData function to accept and plot the new data series, and remove secondDerivativeData:
Update the function definition and datasets:
function plotData(labels, priceData, ma99Data, derivativeData, /* secondDerivativeData, */ ma20DerivativeData, derivativeOfMA20DerivativeData, ma20OfDerivativeOfMA20DerivativeData, specialPointsData, totalAssetValueData) {
const ctx = document.getElementById('chart').getContext('2d');
// Create an array of zeros for the zero line
const zeroData = labels.map(() => 0);
// Clear existing chart if any
if (window.myChart instanceof Chart) {
window.myChart.destroy();
}
// Register the zoom plugin (necessary in Chart.js v4)
Chart.register(ChartZoom);
window.myChart = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [
{
label: 'Actual Price',
data: priceData,
borderColor: 'black',
fill: false,
yAxisID: 'y',
pointRadius: 0,
},
{
label: 'Moving Average (99)',
data: ma99Data,
borderColor: 'blue',
fill: false,
yAxisID: 'y',
pointRadius: 0,
},
{
label: 'First Derivative',
data: derivativeData,
borderColor: 'green',
fill: false,
yAxisID: 'y1',
pointRadius: 0,
},
// Comment out or remove the Second Derivative dataset
/*
{
label: 'Second Derivative',
data: secondDerivativeData,
borderColor: 'red',
fill: false,
yAxisID: 'y1',
pointRadius: 0,
},
*/
// Zero line for derivative axis
{
label: 'Derivative Zero Line',
data: zeroData,
borderColor: 'gray',
borderDash: [5, 5],
pointRadius: 0,
borderWidth: 1,
fill: false,
yAxisID: 'y1',
},
// Existing datasets for MA20 of Derivative and its Derivative
{
label: 'MA20 of Derivative',
data: ma20DerivativeData,
borderColor: 'magenta',
fill: false,
yAxisID: 'y1',
pointRadius: 0,
},
{
label: 'Derivative of MA20 of Derivative',
data: derivativeOfMA20DerivativeData,
borderColor: 'cyan',
fill: false,
yAxisID: 'y1',
pointRadius: 0,
},
// Add the new dataset
{
label: 'MA20 of Derivative of MA20 of Derivative',
data: ma20OfDerivativeOfMA20DerivativeData,
borderColor: 'orange',
fill: false,
yAxisID: 'y1',
pointRadius: 0,
},
// Dataset for special points
{
label: 'Special Points (Buy Signal)',
data: specialPointsData,
borderColor: 'purple',
backgroundColor: 'purple',
pointRadius: 5,
pointHoverRadius: 7,
showLine: false,
yAxisID: 'y',
type: 'line',
},
// Dataset for total asset value
{
label: 'Total Asset Value',
data: totalAssetValueData,
borderColor: 'orange',
fill: false,
yAxisID: 'y2',
pointRadius: 0,
borderWidth: 2,
tension: 0.1,
},
],
},
options: {
// ... existing options ...
},
});
// Add double-click event listener to reset zoom
const canvas = document.getElementById('chart');
canvas.addEventListener('dblclick', function () {
window.myChart.resetZoom();
});
}
7. Ensure that the simulateTrading function remains unchanged if you still want to use secondDerivativeData in your trading logic.
Even though you're no longer plotting secondDerivativeData, your trading strategy still depends on it. So, keep the secondDerivativeData calculations and pass it to simulateTrading as before:
// Simulate trading strategy
const { specialPointsData, tradeSignals, totalAssetValueData } = simulateTrading(
priceData,
derivativeData,
secondDerivativeData,
derivativeOfMA20DerivativeData,
ma99Data
);
By making these changes, you'll stop plotting the second derivative and instead plot the moving average 20 of derivativeOfMA20DerivativeData as requested.
Note: Be cautious with variable names and window sizes to ensure consistency. In your code, ma20Derivative was calculated with a window size of 99. I've corrected this to use a window size of 20 to align with the variable name.
Here's the full adjusted code within your main function and the subsequent functions:
async function main() {
const startDateInput = document.getElementById('startDate').value;
const endDateInput = document.getElementById('endDate').value;
// Convert dates to Unix timestamps in milliseconds
const startTime = new Date(startDateInput).getTime();
const endTime = new Date(endDateInput).getTime();
// Fetch BTCUSDC 1-minute prices from Binance API
const symbol = 'BTCUSDC';
const interval = '1m';
const klines = await fetchKlines(symbol, interval, startTime, endTime);
if (klines.length === 0) {
alert('No data available for the selected date range.');
return;
}
// Extract closing prices and timestamps
const timestamps = klines.map(k => k[0]); // Open time
const closePrices = klines.map(k => parseFloat(k[4])); // Close price
// Calculate 99-point moving average
const ma99 = movingAverage(closePrices, 99);
// Calculate first derivative of MA99
const derivative = firstDerivative(ma99);
// Calculate second derivative of MA99
const secondDerivative = firstDerivative(derivative);
// Calculate MA20 of the derivative
const ma20Derivative = movingAverage(derivative, 20);
// Calculate derivative of MA20 derivative
const derivativeOfMA20Derivative = firstDerivative(ma20Derivative);
// Calculate moving average 20 of derivativeOfMA20Derivative
const ma20OfDerivativeOfMA20Derivative = movingAverage(derivativeOfMA20Derivative, 20);
// The earliest index where all data arrays have valid data
const startingIndex = 140;
// Prepare data for plotting
const labels = timestamps.slice(startingIndex).map(ts => new Date(ts).toLocaleString());
const priceData = closePrices.slice(startingIndex);
const ma99Data = ma99.slice(startingIndex);
const derivativeData = derivative.slice(startingIndex);
const secondDerivativeData = secondDerivative.slice(startingIndex);
const ma20DerivativeData = ma20Derivative.slice(startingIndex);
const derivativeOfMA20DerivativeData = derivativeOfMA20Derivative.slice(startingIndex);
const ma20OfDerivativeOfMA20DerivativeData = ma20OfDerivativeOfMA20Derivative.slice(startingIndex);
// Simulate trading strategy
const { specialPointsData, tradeSignals, totalAssetValueData } = simulateTrading(
priceData,
derivativeData,
secondDerivativeData,
derivativeOfMA20DerivativeData,
ma99Data
);
// Plot the data
plotData(
labels,
priceData,
ma99Data,
derivativeData,
/* secondDerivativeData, */ // No longer plotted
ma20DerivativeData,
derivativeOfMA20DerivativeData,
ma20OfDerivativeOfMA20DerivativeData, // Newly added dataset
specialPointsData,
totalAssetValueData
);
}
By implementing these changes, your chart will no longer display the second derivative, but will include the new moving average of the derivative of the MA20 derivative, as you requested.