TSLA 30-minute bars pricing

Use Gemini to create a Node-RED function node that combines with Alpaca's Range Bars to get the past 30 mins of pricing bars.

Description

Use Gemini to get a properly formatted Node-RED function node that -- combined with your prebuilt Alpaca Range Bars nodes -- will store the past 30 minute of pricing bars for use in a trading script.


  • Go to your instance and search for "range"
  • image1
  • Grab the Range Bar node and place it in your workspace.
  • image2
  • Right click on the node and click export
  • image4
  • Save the javascript into the clipboard.
  • image5

    Navigate to gemini.google.com and enter the following prompt:

    Create a node red function node requesting the past 30 mins of TSLA bars using iex data. If 30 mins of data isn't available, use the previous day's data but use the most recent 30 minutes. Pass the request to the prebuilt range bar node below: [paste them javascript from your clipboard here]

    Gemini Response

    To achieve this, you need a Function node that calculates the appropriate start and end times based on the current market state and formats them for the alpaca-range-bars node.

    The Alpaca node expects specific properties in the message object (like msg.symbol, msg.start, msg.end, and msg.timeframe). This function handles the "past 30 minutes" logic by checking if the current time is during market hours; if not (or if it's early in the session), it looks back to the previous trading day.

    1. The Function Node Code

    Copy and paste this into a new Function node placed before your alpaca-range-bars node.

    // Configuration
    const symbol = "TSLA";
    const timeframe = "1Min"; // Alpaca timeframe format
    const lookbackMinutes = 30;
    
    // Date Setup
    let now = new Date();
    let start = new Date(now.getTime() - (lookbackMinutes * 60000));
    
    // Market Logic: Check if we are in pre-market or if the session just started
    // Standard Market: 9:30 AM - 4:00 PM EST
    const hour = now.getUTCHours() - 5; // Rough EST conversion (Adjust for DST if needed)
    const minute = now.getMinutes();
    const isBeforeMarketOrEarly = (hour < 9) || (hour === 9 && minute < 30 + lookbackMinutes);
    
    if (isBeforeMarketOrEarly) {
        // If it's early or pre-market, look at the previous day
        // We set 'end' to yesterday at 4:00 PM EST and 'start' 30 mins before that
        let yesterday = new Date(now);
        yesterday.setDate(now.getDate() - 1);
        
        // Handle weekends (if today is Monday, look at Friday)
        if (now.getDay() === 1) yesterday.setDate(now.getDate() - 3);
        if (now.getDay() === 0) yesterday.setDate(now.getDate() - 2);
    
        yesterday.setHours(16, 0, 0, 0); // 4:00 PM
        msg.end = yesterday.toISOString();
        
        let startPrev = new Date(yesterday.getTime() - (lookbackMinutes * 60000));
        msg.start = startPrev.toISOString();
    } else {
        // Standard recent 30 mins
        msg.start = start.toISOString();
        msg.end = now.toISOString();
    }
    
    // Format for the Alpaca Range Bar node
    msg.symbol = symbol;
    msg.timeframe = timeframe;
    msg.payload = symbol; // Some versions use payload as the primary trigger
    
    return msg;

    2. Implementation Steps

    Step Action
    Step 1 Add an Inject node to trigger the flow (e.g., every 1 minute or manually).
    Step 2 Connect the Inject node to the Function node containing the code above.
    Step 3 Connect the Function node to your existing alpaca-range-bars node.
    Step 4 Ensure your Alpaca Config node (Paper) is selected inside the range bar node settings.

    How It Works

    Component Description
    Configuration Sets the symbol (TSLA), timeframe (1Min), and lookback period (30 minutes).
    Date Setup Calculates the current time and a start time 30 minutes ago.
    Market Logic Checks if it's pre-market or early in the session (before 10:00 AM EST).
    Previous Day Fallback If not enough data today, uses the last 30 minutes of the previous trading day (handles weekends).
    Message Formatting Sets msg.symbol, msg.timeframe, msg.start, and msg.end for the Alpaca node.

    Pro-Tip: You can easily modify the symbol and lookbackMinutes variables at the top of the function to use different stocks or time periods. For example, change to "AAPL" or set lookbackMinutes = 60 for an hour of data.

    This is what your flow will look like in your instance once it's added

  • Get a function node and name it "Gemini Function Node"
  • image6
  • Copy the Gemini Response. Right click and paste into the function node.
  • image7
  • Add an inject node at the start and a debug mode at the end.
  • image8
  • This is the contents of the msg.payload sent to the debug sidebar display the TSLA bars.
  • image9