@discomedia/utils 1.0.56 → 1.0.57

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/index.mjs CHANGED
@@ -18562,6 +18562,50 @@ class AlpacaTradingAPI {
18562
18562
  throw error;
18563
18563
  }
18564
18564
  }
18565
+ /**
18566
+ * Create a stop order (stop or stop-limit)
18567
+ * @param symbol (string) - the symbol of the order
18568
+ * @param qty (number) - the quantity of the order
18569
+ * @param side (string) - the side of the order
18570
+ * @param stopPrice (number) - the stop price that triggers the order
18571
+ * @param position_intent (string) - the position intent of the order
18572
+ * @param limitPrice (number) - optional limit price (if provided, creates a stop-limit order)
18573
+ * @param client_order_id (string) - optional client order id
18574
+ * @returns The created stop order
18575
+ */
18576
+ async createStopOrder(symbol, qty, side, stopPrice, position_intent, limitPrice, client_order_id) {
18577
+ const isStopLimit = limitPrice !== undefined;
18578
+ const orderType = isStopLimit ? 'stop-limit' : 'stop';
18579
+ this.log(`Creating ${orderType} ${side.toUpperCase()} ${qty} shares for ${symbol} with stop price ${stopPrice}${isStopLimit ? ` and limit price ${limitPrice}` : ''}`, {
18580
+ symbol,
18581
+ });
18582
+ const body = {
18583
+ symbol,
18584
+ qty: Math.abs(qty).toString(),
18585
+ side,
18586
+ position_intent,
18587
+ order_class: 'simple',
18588
+ type: isStopLimit ? 'stop_limit' : 'stop',
18589
+ stop_price: this.roundPriceForAlpaca(stopPrice),
18590
+ time_in_force: 'gtc',
18591
+ };
18592
+ if (isStopLimit) {
18593
+ body.limit_price = this.roundPriceForAlpaca(limitPrice);
18594
+ }
18595
+ if (client_order_id !== undefined) {
18596
+ body.client_order_id = client_order_id;
18597
+ }
18598
+ try {
18599
+ return await this.makeRequest(`/orders`, 'POST', body);
18600
+ }
18601
+ catch (error) {
18602
+ this.log(`Error creating ${orderType} order: ${error}`, {
18603
+ symbol,
18604
+ type: 'error',
18605
+ });
18606
+ throw error;
18607
+ }
18608
+ }
18565
18609
  /**
18566
18610
  * Create a market order
18567
18611
  * @param symbol (string) - the symbol of the order
@@ -18674,6 +18718,57 @@ class AlpacaTradingAPI {
18674
18718
  throw error;
18675
18719
  }
18676
18720
  }
18721
+ /**
18722
+ * Create an OCO (One-Cancels-Other) order with take profit and stop loss
18723
+ * @param symbol (string) - the symbol of the order
18724
+ * @param qty (number) - the quantity of the order
18725
+ * @param side (string) - the side of the order (buy or sell)
18726
+ * @param position_intent (string) - the position intent of the order
18727
+ * @param limitPrice (number) - the limit price for the entry order (OCO orders must be limit orders)
18728
+ * @param takeProfitPrice (number) - the take profit price
18729
+ * @param stopLossPrice (number) - the stop loss price
18730
+ * @param stopLossLimitPrice (number) - optional limit price for stop loss (creates stop-limit instead of stop)
18731
+ * @param client_order_id (string) - optional client order id
18732
+ * @returns The created OCO order
18733
+ */
18734
+ async createOCOOrder(symbol, qty, side, position_intent, limitPrice, takeProfitPrice, stopLossPrice, stopLossLimitPrice, client_order_id) {
18735
+ this.log(`Creating OCO order ${side.toUpperCase()} ${qty} shares for ${symbol} at limit ${limitPrice} with take profit ${takeProfitPrice} and stop loss ${stopLossPrice}`, {
18736
+ symbol,
18737
+ });
18738
+ const body = {
18739
+ symbol,
18740
+ qty: Math.abs(qty).toString(),
18741
+ side,
18742
+ position_intent,
18743
+ order_class: 'oco',
18744
+ type: 'limit',
18745
+ limit_price: this.roundPriceForAlpaca(limitPrice),
18746
+ time_in_force: 'gtc',
18747
+ take_profit: {
18748
+ limit_price: this.roundPriceForAlpaca(takeProfitPrice),
18749
+ },
18750
+ stop_loss: {
18751
+ stop_price: this.roundPriceForAlpaca(stopLossPrice),
18752
+ },
18753
+ };
18754
+ // If stop loss limit price is provided, create stop-limit order
18755
+ if (stopLossLimitPrice !== undefined) {
18756
+ body.stop_loss.limit_price = this.roundPriceForAlpaca(stopLossLimitPrice);
18757
+ }
18758
+ if (client_order_id !== undefined) {
18759
+ body.client_order_id = client_order_id;
18760
+ }
18761
+ try {
18762
+ return await this.makeRequest(`/orders`, 'POST', body);
18763
+ }
18764
+ catch (error) {
18765
+ this.log(`Error creating OCO order: ${error}`, {
18766
+ symbol,
18767
+ type: 'error',
18768
+ });
18769
+ throw error;
18770
+ }
18771
+ }
18677
18772
  /**
18678
18773
  * Get the current trail percent for a symbol, assuming that it has an open position and a trailing stop order to close it. Because this relies on an orders request for one symbol, you can't do it too often.
18679
18774
  * @param symbol (string) - the symbol of the order