Skip to content
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

Open
2 tasks
tiagosiebler opened this issue Jul 4, 2022 · 5 comments
Open
2 tasks

Candle builder from raw websocket fills #1

tiagosiebler opened this issue Jul 4, 2022 · 5 comments

Comments

@tiagosiebler
Copy link
Owner

  • Consume raw trade fills (websocket) to build realtime candles.
  • Consume historic trade fills (REST API) to build historic candles.
@McFlyOne
Copy link

McFlyOne commented Dec 16, 2022

  • Connect to the websocket endpoint provided by the API provider. This will typically involve making a request to a specific URL and then listening for messages on the connection.

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.

  • Identify the API endpoint for retrieving trade fill data. This information should be available in the API documentation provided by the API provider.

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.

@trasherdk
Copy link

When generating candles from ticks, you'd want to save the volume information too.
volumeFrom and volumeTo as sum of total amount of asset traded, and sum of price per tick.

@McFlyOne
Copy link

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

@McFlyOne
Copy link

solved by Chat Bot ;)

@trasherdk
Copy link

😄

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants