@discomedia/utils 1.0.26 → 1.0.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14679,6 +14679,18 @@ Websocket example
14679
14679
  this.log(`Received trade update: event ${update.event} for an order to ${update.order.side} ${update.order.qty} of ${update.order.symbol}`);
14680
14680
  });
14681
14681
  alpacaAPI.connectWebsocket(); // necessary to connect to the WebSocket
14682
+
14683
+ Portfolio History examples
14684
+ // Get standard portfolio history
14685
+ const portfolioHistory = await alpacaAPI.getPortfolioHistory({
14686
+ timeframe: '1D',
14687
+ period: '1M'
14688
+ });
14689
+
14690
+ // Get daily portfolio history with current day included (if available from hourly data)
14691
+ const dailyHistory = await alpacaAPI.getPortfolioDailyHistory({
14692
+ period: '1M'
14693
+ });
14682
14694
  */
14683
14695
  class AlpacaTradingAPI {
14684
14696
  static new(credentials) {
@@ -15378,6 +15390,57 @@ class AlpacaTradingAPI {
15378
15390
  const response = await this.makeRequest(`/account/portfolio/history?${queryParams.toString()}`);
15379
15391
  return response;
15380
15392
  }
15393
+ /**
15394
+ * Get portfolio daily history for the account, ensuring the most recent day is included
15395
+ * by combining daily and hourly history if needed.
15396
+ *
15397
+ * This function performs two API calls:
15398
+ * 1. Retrieves daily portfolio history
15399
+ * 2. Retrieves hourly portfolio history to check for more recent data
15400
+ *
15401
+ * If hourly history has timestamps more recent than the last timestamp in daily history,
15402
+ * it appends one additional day to the daily history using the most recent hourly values.
15403
+ *
15404
+ * @param params Parameters for the portfolio history request (same as getPortfolioHistory except timeframe is forced to '1D')
15405
+ * @returns Portfolio history data with daily timeframe, including the most recent day if available from hourly data
15406
+ */
15407
+ async getPortfolioDailyHistory(params) {
15408
+ // Get daily history
15409
+ const dailyParams = { ...params, timeframe: '1D' };
15410
+ const dailyHistory = await this.getPortfolioHistory(dailyParams);
15411
+ // Get hourly history for the last day to check for more recent data
15412
+ const hourlyParams = { timeframe: '1H', period: '1D' };
15413
+ const hourlyHistory = await this.getPortfolioHistory(hourlyParams);
15414
+ // If no hourly history, return daily as-is
15415
+ if (!hourlyHistory.timestamp || hourlyHistory.timestamp.length === 0) {
15416
+ return dailyHistory;
15417
+ }
15418
+ // Get the last timestamp from daily history
15419
+ const lastDailyTimestamp = dailyHistory.timestamp[dailyHistory.timestamp.length - 1];
15420
+ // Check if hourly history has more recent data
15421
+ const recentHourlyData = hourlyHistory.timestamp
15422
+ .map((timestamp, index) => ({ timestamp, index }))
15423
+ .filter(({ timestamp }) => timestamp > lastDailyTimestamp);
15424
+ // If no more recent hourly data, return daily history as-is
15425
+ if (recentHourlyData.length === 0) {
15426
+ return dailyHistory;
15427
+ }
15428
+ // Get the most recent hourly data point
15429
+ const mostRecentHourly = recentHourlyData[recentHourlyData.length - 1];
15430
+ const mostRecentIndex = mostRecentHourly.index;
15431
+ // Calculate the timestamp for the new daily entry (most recent day + 1 day worth of seconds)
15432
+ const oneDayInSeconds = 24 * 60 * 60;
15433
+ const newDailyTimestamp = mostRecentHourly.timestamp + oneDayInSeconds;
15434
+ // Create a new daily history entry with the most recent hourly values
15435
+ const updatedDailyHistory = {
15436
+ ...dailyHistory,
15437
+ timestamp: [...dailyHistory.timestamp, newDailyTimestamp],
15438
+ equity: [...dailyHistory.equity, hourlyHistory.equity[mostRecentIndex]],
15439
+ profit_loss: [...dailyHistory.profit_loss, hourlyHistory.profit_loss[mostRecentIndex]],
15440
+ profit_loss_pct: [...dailyHistory.profit_loss_pct, hourlyHistory.profit_loss_pct[mostRecentIndex]],
15441
+ };
15442
+ return updatedDailyHistory;
15443
+ }
15381
15444
  /**
15382
15445
  * Get option contracts based on specified parameters
15383
15446
  * @param params Parameters to filter option contracts