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.




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]
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.
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;
| 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. |
| 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.



