write it in js
- User
write it in js
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
write it in js
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a JavaScript version of the script that converts Binance trade history CSV to candlestick data. This example uses Node.js and assumes you have the csv-parser and fs modules available.
First, ensure you have the required modules installed:
npm install csv-parser
Then, you can use the following JavaScript code:
const fs = require('fs');
const csv = require('csv-parser');
function csvToCandlestick(csvFilePath, intervalMs = 250) {
return new Promise((resolve, reject) => {
const candlesticks = {};
fs.createReadStream(csvFilePath)
.pipe(csv({ headers: ['time', 'price', 'quantity'], skipLines: 1 })) // Adjust headers and skipLines as necessary
.on('data', (row) => {
const time = parseInt(row.time);
const intervalStart = Math.floor(time / intervalMs) * intervalMs;
if (!candlesticks[intervalStart]) {
candlesticks[intervalStart] = {
open: parseFloat(row.price),
high: parseFloat(row.price),
low: parseFloat(row.price),
close: parseFloat(row.price),
volume: parseFloat(row.quantity),
};
} else {
const candle = candlesticks[intervalStart];
candle.high = Math.max(candle.high, parseFloat(row.price));
candle.low = Math.min(candle.low, parseFloat(row.price));
candle.close = parseFloat(row.price);
candle.volume += parseFloat(row.quantity);
}
})
.on('end', () => {
// Convert the candlesticks object to an array and sort it by time
const result = Object.keys(candlesticks).sort((a, b) => a - b).map((key) => ({
time: parseInt(key),
...candlesticks[key],
}));
resolve(result);
})
.on('error', (error) => {
reject(error);
});
});
}
// Example usage
const csvFilePath = 'path/to/your/trades.csv'; // Update this path
csvToCandlestick(csvFilePath)
.then((candlesticks) => {
console.log(candlesticks);
// Further process or save the candlesticks data
})
.catch((error) => {
console.error('Error processing CSV:', error);
});
Notes for JavaScript Version:
csv-parser module is used to parse the CSV file. You might need to adjust the headers option or the skipLines option based on the structure of your CSV file.time, open, high, low, close, and volume properties, sorted by time.csvToCandlestick function returns a Promise that resolves with the candlesticks data or rejects with an error if one occurs.Adjust the file path and any necessary CSV parsing options to fit your specific use case.