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