@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.
- package/dist/alpaca-trading-api-6NxNgQBn.js +1413 -0
- package/dist/alpaca-trading-api-6NxNgQBn.js.map +1 -0
- package/dist/index-frontend.cjs +63 -0
- package/dist/index-frontend.cjs.map +1 -1
- package/dist/index-frontend.mjs +63 -0
- package/dist/index-frontend.mjs.map +1 -1
- package/dist/index.cjs +86 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +86 -21
- package/dist/index.mjs.map +1 -1
- package/dist/package.json +2 -2
- package/dist/test.js +909 -5745
- package/dist/test.js.map +1 -1
- package/dist/types/alpaca-trading-api.d.ts +33 -0
- package/dist/types/alpaca-trading-api.d.ts.map +1 -1
- package/dist/types-frontend/alpaca-trading-api.d.ts +33 -0
- package/dist/types-frontend/alpaca-trading-api.d.ts.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -1372,51 +1372,47 @@ function pLimit(concurrency) {
|
|
|
1372
1372
|
let activeCount = 0;
|
|
1373
1373
|
|
|
1374
1374
|
const resumeNext = () => {
|
|
1375
|
+
// Process the next queued function if we're under the concurrency limit
|
|
1375
1376
|
if (activeCount < concurrency && queue.size > 0) {
|
|
1376
|
-
queue.dequeue()();
|
|
1377
|
-
// Since `pendingCount` has been decreased by one, increase `activeCount` by one.
|
|
1378
1377
|
activeCount++;
|
|
1378
|
+
queue.dequeue()();
|
|
1379
1379
|
}
|
|
1380
1380
|
};
|
|
1381
1381
|
|
|
1382
1382
|
const next = () => {
|
|
1383
1383
|
activeCount--;
|
|
1384
|
-
|
|
1385
1384
|
resumeNext();
|
|
1386
1385
|
};
|
|
1387
1386
|
|
|
1388
1387
|
const run = async (function_, resolve, arguments_) => {
|
|
1388
|
+
// Execute the function and capture the result promise
|
|
1389
1389
|
const result = (async () => function_(...arguments_))();
|
|
1390
1390
|
|
|
1391
|
+
// Resolve immediately with the promise (don't wait for completion)
|
|
1391
1392
|
resolve(result);
|
|
1392
1393
|
|
|
1394
|
+
// Wait for the function to complete (success or failure)
|
|
1395
|
+
// We catch errors here to prevent unhandled rejections,
|
|
1396
|
+
// but the original promise rejection is preserved for the caller
|
|
1393
1397
|
try {
|
|
1394
1398
|
await result;
|
|
1395
1399
|
} catch {}
|
|
1396
1400
|
|
|
1401
|
+
// Decrement active count and process next queued function
|
|
1397
1402
|
next();
|
|
1398
1403
|
};
|
|
1399
1404
|
|
|
1400
1405
|
const enqueue = (function_, resolve, arguments_) => {
|
|
1401
|
-
// Queue
|
|
1402
|
-
// to preserve asynchronous context.
|
|
1403
|
-
new Promise(internalResolve => {
|
|
1406
|
+
// Queue the internal resolve function instead of the run function
|
|
1407
|
+
// to preserve the asynchronous execution context.
|
|
1408
|
+
new Promise(internalResolve => { // eslint-disable-line promise/param-names
|
|
1404
1409
|
queue.enqueue(internalResolve);
|
|
1405
|
-
}).then(
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
// `activeCount` to `concurrency`, because `activeCount` is updated asynchronously
|
|
1412
|
-
// after the `internalResolve` function is dequeued and called. The comparison in the if-statement
|
|
1413
|
-
// needs to happen asynchronously as well to get an up-to-date value for `activeCount`.
|
|
1414
|
-
await Promise.resolve();
|
|
1415
|
-
|
|
1416
|
-
if (activeCount < concurrency) {
|
|
1417
|
-
resumeNext();
|
|
1418
|
-
}
|
|
1419
|
-
})();
|
|
1410
|
+
}).then(run.bind(undefined, function_, resolve, arguments_)); // eslint-disable-line promise/prefer-await-to-then
|
|
1411
|
+
|
|
1412
|
+
// Start processing immediately if we haven't reached the concurrency limit
|
|
1413
|
+
if (activeCount < concurrency) {
|
|
1414
|
+
resumeNext();
|
|
1415
|
+
}
|
|
1420
1416
|
};
|
|
1421
1417
|
|
|
1422
1418
|
const generator = (function_, ...arguments_) => new Promise(resolve => {
|
|
@@ -1450,6 +1446,12 @@ function pLimit(concurrency) {
|
|
|
1450
1446
|
});
|
|
1451
1447
|
},
|
|
1452
1448
|
},
|
|
1449
|
+
map: {
|
|
1450
|
+
async value(array, function_) {
|
|
1451
|
+
const promises = array.map(value => this(function_, value));
|
|
1452
|
+
return Promise.all(promises);
|
|
1453
|
+
},
|
|
1454
|
+
},
|
|
1453
1455
|
});
|
|
1454
1456
|
|
|
1455
1457
|
return generator;
|
|
@@ -17111,6 +17113,18 @@ Websocket example
|
|
|
17111
17113
|
this.log(`Received trade update: event ${update.event} for an order to ${update.order.side} ${update.order.qty} of ${update.order.symbol}`);
|
|
17112
17114
|
});
|
|
17113
17115
|
alpacaAPI.connectWebsocket(); // necessary to connect to the WebSocket
|
|
17116
|
+
|
|
17117
|
+
Portfolio History examples
|
|
17118
|
+
// Get standard portfolio history
|
|
17119
|
+
const portfolioHistory = await alpacaAPI.getPortfolioHistory({
|
|
17120
|
+
timeframe: '1D',
|
|
17121
|
+
period: '1M'
|
|
17122
|
+
});
|
|
17123
|
+
|
|
17124
|
+
// Get daily portfolio history with current day included (if available from hourly data)
|
|
17125
|
+
const dailyHistory = await alpacaAPI.getPortfolioDailyHistory({
|
|
17126
|
+
period: '1M'
|
|
17127
|
+
});
|
|
17114
17128
|
*/
|
|
17115
17129
|
class AlpacaTradingAPI {
|
|
17116
17130
|
static new(credentials) {
|
|
@@ -17810,6 +17824,57 @@ class AlpacaTradingAPI {
|
|
|
17810
17824
|
const response = await this.makeRequest(`/account/portfolio/history?${queryParams.toString()}`);
|
|
17811
17825
|
return response;
|
|
17812
17826
|
}
|
|
17827
|
+
/**
|
|
17828
|
+
* Get portfolio daily history for the account, ensuring the most recent day is included
|
|
17829
|
+
* by combining daily and hourly history if needed.
|
|
17830
|
+
*
|
|
17831
|
+
* This function performs two API calls:
|
|
17832
|
+
* 1. Retrieves daily portfolio history
|
|
17833
|
+
* 2. Retrieves hourly portfolio history to check for more recent data
|
|
17834
|
+
*
|
|
17835
|
+
* If hourly history has timestamps more recent than the last timestamp in daily history,
|
|
17836
|
+
* it appends one additional day to the daily history using the most recent hourly values.
|
|
17837
|
+
*
|
|
17838
|
+
* @param params Parameters for the portfolio history request (same as getPortfolioHistory except timeframe is forced to '1D')
|
|
17839
|
+
* @returns Portfolio history data with daily timeframe, including the most recent day if available from hourly data
|
|
17840
|
+
*/
|
|
17841
|
+
async getPortfolioDailyHistory(params) {
|
|
17842
|
+
// Get daily history
|
|
17843
|
+
const dailyParams = { ...params, timeframe: '1D' };
|
|
17844
|
+
const dailyHistory = await this.getPortfolioHistory(dailyParams);
|
|
17845
|
+
// Get hourly history for the last day to check for more recent data
|
|
17846
|
+
const hourlyParams = { timeframe: '1H', period: '1D' };
|
|
17847
|
+
const hourlyHistory = await this.getPortfolioHistory(hourlyParams);
|
|
17848
|
+
// If no hourly history, return daily as-is
|
|
17849
|
+
if (!hourlyHistory.timestamp || hourlyHistory.timestamp.length === 0) {
|
|
17850
|
+
return dailyHistory;
|
|
17851
|
+
}
|
|
17852
|
+
// Get the last timestamp from daily history
|
|
17853
|
+
const lastDailyTimestamp = dailyHistory.timestamp[dailyHistory.timestamp.length - 1];
|
|
17854
|
+
// Check if hourly history has more recent data
|
|
17855
|
+
const recentHourlyData = hourlyHistory.timestamp
|
|
17856
|
+
.map((timestamp, index) => ({ timestamp, index }))
|
|
17857
|
+
.filter(({ timestamp }) => timestamp > lastDailyTimestamp);
|
|
17858
|
+
// If no more recent hourly data, return daily history as-is
|
|
17859
|
+
if (recentHourlyData.length === 0) {
|
|
17860
|
+
return dailyHistory;
|
|
17861
|
+
}
|
|
17862
|
+
// Get the most recent hourly data point
|
|
17863
|
+
const mostRecentHourly = recentHourlyData[recentHourlyData.length - 1];
|
|
17864
|
+
const mostRecentIndex = mostRecentHourly.index;
|
|
17865
|
+
// Calculate the timestamp for the new daily entry (most recent day + 1 day worth of seconds)
|
|
17866
|
+
const oneDayInSeconds = 24 * 60 * 60;
|
|
17867
|
+
const newDailyTimestamp = mostRecentHourly.timestamp + oneDayInSeconds;
|
|
17868
|
+
// Create a new daily history entry with the most recent hourly values
|
|
17869
|
+
const updatedDailyHistory = {
|
|
17870
|
+
...dailyHistory,
|
|
17871
|
+
timestamp: [...dailyHistory.timestamp, newDailyTimestamp],
|
|
17872
|
+
equity: [...dailyHistory.equity, hourlyHistory.equity[mostRecentIndex]],
|
|
17873
|
+
profit_loss: [...dailyHistory.profit_loss, hourlyHistory.profit_loss[mostRecentIndex]],
|
|
17874
|
+
profit_loss_pct: [...dailyHistory.profit_loss_pct, hourlyHistory.profit_loss_pct[mostRecentIndex]],
|
|
17875
|
+
};
|
|
17876
|
+
return updatedDailyHistory;
|
|
17877
|
+
}
|
|
17813
17878
|
/**
|
|
17814
17879
|
* Get option contracts based on specified parameters
|
|
17815
17880
|
* @param params Parameters to filter option contracts
|