Public
Edited
Mar 10, 2023
Insert cell
Insert cell
symbol = 'UFTBUSD'
Insert cell
Insert cell
timeframe = '15m'
Insert cell
endpoint = `https://api.binance.com/api/v3/klines?symbol=${symbol}&interval=${timeframe}&limit=1000`
Insert cell
candles = (await fetch(endpoint)).json()
Insert cell
normalizedCandles = candles.map(c => ({timestamp: new Date(c[0]), close: Number(c[4]), volume: Number(c[5])}))
Insert cell
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.lineY(logRenko.slice(-120), {x: 'timestamp', y: 'price'})
],
grid: true,
style: {
width: 1024,
}
})
Insert cell
Insert cell
Insert cell
filteredStates = {
const dt = 1
const state2 = { price: logRenko[0].price, velocity: 0.0, acceleration: 0.0 }
const covariance = [
[dt, 0.0, 0.0],
[0.0, dt, 0.0],
[0.0, 0.0, dt]
]
const filter = new ExtendedKalmanFilter(state2, covariance)
const states = []
const predictedStates = []

logRenko.forEach((observation, i) => {
filter.update(observation.price, [[dt]])
const state = {
timestamp: observation.timestamp,
...filter.state
}
filter.predict(dt)
const predictedState = {
predictedPrice: filter.state.price,
predictedVelocity: filter.state.velocity,
predictedAcceleration: filter.state.acceleration,
}
predictedStates.push(predictedState)
states.push({
...state,
...(predictedStates[i-1] ? predictedStates[i-1] : predictedState),
truth: observation.price
})
})

return states
}

Insert cell
Insert cell
Plot.plot({
marks: [
// Plot.ruleY([0]),
// Plot.dot(filteredStates.slice(500).slice(-100), {x: 'timestamp', y: 'predictedVelocity', stroke: 'green'}),
// Plot.lineY(filteredStates.slice(500).slice(-100), {x: 'timestamp', y: 'predictedVelocity', stroke: 'lightgreen'}),
Plot.lineY(filteredStates.slice(-60), {x: 'timestamp', y: 'velocity', stroke: 'green'}),
// Plot.dot(filteredStates.slice(500).slice(-100), {x: 'timestamp', y: 'predictedAcceleration', stroke: 'red'}),
// Plot.lineY(filteredStates.slice(500).slice(-100), {x: 'timestamp', y: 'predictedAcceleration', stroke: 'pink'}),
Plot.lineY(filteredStates.slice(-60), {x: 'timestamp', y: 'acceleration', stroke: 'red'}),
],
grid: true,
style: {
width: 1024,
}
})
Insert cell
{
const audio = new Float32Array(filteredStates.map(s => s.velocity))
const audioCtx = new AudioContext();
const buffer = audioCtx.createBuffer(1, audio.length, audioCtx.sampleRate);
const channelData = buffer.getChannelData(0);
channelData.set(audio);
const source = audioCtx.createBufferSource();
source.buffer = buffer;
source.connect(audioCtx.destination);
source.start();
}
Insert cell
{
const audio = new Float32Array(filteredStates.map(s => s.velocity))
const audioCtx = new AudioContext();

// Assuming you already have a Float32Array buffer with the original audio data
const sourceBuffer = audioCtx.createBuffer(1, audio.length, audioCtx.sampleRate);
const channelData = sourceBuffer.getChannelData(0);
channelData.set(audio);
// Calculate the new length and sample rate
const newLength = sourceBuffer.length * 1000; // Double the length
const newSampleRate = audioCtx.sampleRate;
// Create a new OfflineAudioContext with the new length and sample rate
const offlineCtx = new OfflineAudioContext(1, newLength, newSampleRate);
// Create an AudioBufferSourceNode and connect it to the OfflineAudioContext
const source = offlineCtx.createBufferSource();
source.buffer = sourceBuffer;
source.connect(offlineCtx.destination);
// Start playing the source
source.start(0);
// Render the OfflineAudioContext and get the resampled buffer
offlineCtx.startRendering().then(renderedBuffer => {
// Use the resampled buffer as the source for your AudioBufferSourceNode
const resampledSource = audioCtx.createBufferSource();
resampledSource.buffer = renderedBuffer;
resampledSource.connect(audioCtx.destination);
// Start playing the resampled source
resampledSource.start(0);
});
}
Insert cell
{
const audio = new Float32Array(filteredStates.map(s => s.velocity))
const audioCtx = new AudioContext();
// Assuming you already have a Float32Array buffer with the original audio data
const sourceBuffer = audioCtx.createBuffer(1, audio.length, audioCtx.sampleRate);
const channelData = sourceBuffer.getChannelData(0);
channelData.set(audio);
// Calculate the new sample rate
const newSampleRate = 44100; // For example, resample to 44.1 kHz
// Calculate the new length of the buffer
const newLength = Math.round(channelData.length * (newSampleRate / audioCtx.sampleRate));
// Create a new buffer with the new length and the new sample rate
const newBuffer = audioCtx.createBuffer(1, newLength, newSampleRate);
// Fill the new buffer with the resampled data
const newChannelData = newBuffer.getChannelData(0);
for (let i = 0; i < newLength; i++) {
const originalIndex = (i / newLength) * channelData.length;
const floorIndex = Math.floor(originalIndex);
const ceilIndex = Math.ceil(originalIndex);
const fraction = originalIndex - floorIndex;
const floorValue = channelData[floorIndex];
const ceilValue = channelData[ceilIndex];
const interpolatedValue = (1 - fraction) * floorValue + fraction * ceilValue;
newChannelData[i] = interpolatedValue;
}
// Create an AudioBufferSourceNode and connect it to the AudioContext destination
const source = audioCtx.createBufferSource();
source.buffer = newBuffer;
source.connect(audioCtx.destination);
// Start playing the source
source.start(0);
}
Insert cell
{
const audio = new Float32Array(filteredStates.map(s => s.acceleration))
const audioCtx = new AudioContext();
// Assuming you already have a Float32Array buffer with the original audio data
const sourceBuffer = audioCtx.createBuffer(1, audio.length, audioCtx.sampleRate);
const channelData = sourceBuffer.getChannelData(0);
channelData.set(audio);
// Calculate the playback rate
const playbackRate = 0.005; // For example, play the sound at half speed
// Create an AudioBufferSourceNode and connect it to the AudioContext destination
const source = audioCtx.createBufferSource();
source.buffer = sourceBuffer;
source.playbackRate.value = playbackRate;
source.connect(audioCtx.destination);
// Start playing the source
source.start(0);
}
Insert cell
normalizedPrices = {
const result = []
const initialPrice = 1
result.push(price)
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more