-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Candle builder from raw websocket fills #1
Comments
When a new trade fill is received over the websocket connection, add it to the current candle. To do this, you will need to store the current candle in memory and update the open, high, low, and close prices as necessary based on the new trade fill data. When the current candle reaches the end of its time period (e.g. 1 minute, 5 minutes, 1 hour, etc.), store the candle in a database or other storage system and create a new candle for the next time period. Repeat this process indefinitely to build realtime candles as new trade fill data is received over the websocket connection. To note that websockets are typically more efficient than REST APIs for receiving realtime data, as they allow you to receive updates as they happen without having to make repeated requests. However, websockets do require a constant connection to the server, so you will need to handle any connection issues or errors that may occur. You may also need to consider the possibility of gaps in the data if the websocket connection is lost or the server goes offline. In this case, you will need to decide how to handle the gaps in the data when building your candles.
Make a request to the API endpoint to retrieve the trade fill data for a specific time period. The API may require you to specify the time period in the request, or you may need to make multiple requests to retrieve data for different time periods. Parse the response from the API to extract the trade fill data. This will likely be in the form of a list of trade fill objects, each with properties such as the price, quantity, and timestamp of the trade. Organize the trade fill data into candles. A candle is a data point that represents the price movement of a security over a specific time period, typically with an open, high, low, and close price. To build candles from trade fill data, you will need to group the trade fills by time period (e.g. 1 minute, 5 minutes, 1 hour, etc.) and calculate the open, high, low, and close prices for each time period. Store the candles in a database or other storage system for later use. The API may have rate limits in place, as mentioned in my previous response. You will need to handle these rate limits appropriately to avoid exceeding the limit and causing errors in your requests. You may also need to consider the possibility of gaps in the data if the API does not have complete data for the time period you are requesting. In this case, you will need to decide how to handle the gaps in the data when building your candles. |
When generating candles from ticks, you'd want to save the volume information too. |
const request = require('request'); // API endpoint for retrieving trade fill data // Time period for each candle, in minutes // Object to store the current candle // Function to reset the current candle and create a new one // Function to update the current candle with a new trade fill // Function to retrieve trade fill data from the API
}); // Function to process the trade fill data and generate candles // Calculate the start and end times of the current candle period // Check if the current candle period has ended |
solved by Chat Bot ;) |
😄 const request = require('request');
// API endpoint for retrieving trade fill data
const API_ENDPOINT = 'https://api.example.com/tradefills';
// Time period for each candle, in minutes
const CANDLE_PERIOD = 5;
// Object to store the current candle
let currentCandle = {
open: 0,
high: 0,
low: 0,
close: 0,
volumeFrom: 0,
volumeTo: 0,
startTime: 0,
endTime: 0
};
// Function to reset the current candle and create a new one
function resetCurrentCandle() {
currentCandle = {
open: 0,
high: 0,
low: 0,
close: 0,
volumeFrom: 0,
volumeTo: 0,
startTime: 0,
endTime: 0
};
}
// Function to update the current candle with a new trade fill
function updateCandle(tradeFill) {
// Update the open price if it hasn't been set yet
if (currentCandle.open === 0) {
currentCandle.open = tradeFill.price;
}
// Update the high and low prices
currentCandle.high = Math.max(currentCandle.high, tradeFill.price);
currentCandle.low = Math.min(currentCandle.low, tradeFill.price);
// Update the close price and the volumeFrom and volumeTo totals
currentCandle.close = tradeFill.price;
currentCandle.volumeFrom += tradeFill.quantity;
currentCandle.volumeTo += tradeFill.quantity * tradeFill.price;
}
// Function to retrieve trade fill data from the API
function getTradeFills(startTime, endTime, callback) {
// Make a request to the API endpoint to retrieve trade fill data for the specified time period
request(`${API_ENDPOINT}?startTime=${startTime}&endTime=${endTime}`, (error, response, body) => {
if (error) {
// If there was an error, return the error to the callback
callback(error);
return;
}
// Parse the response body to extract the trade fill data
const tradeFills = JSON.parse(body);
// Return the trade fill data to the callback
callback(null, tradeFills);
});
}
// Function to process the trade fill data and generate candles
function processTradeFills(tradeFills) {
// Loop through the trade fills and update the current candle with each one
for (const tradeFill of tradeFills) {
updateCandle(tradeFill);
}
// Calculate the start and end times of the current candle period
const startTime = currentCandle.startTime || tradeFills[0].timestamp;
const endTime = tradeFills[tradeFills.length - 1].timestamp;
// Check if the current candle period has ended
if (endTime - startTime >= CANDLE_PERIOD * 60 * 1000 |
The text was updated successfully, but these errors were encountered: