The Buy Flow
When SignalPipe receives a buy signal, it follows one of three paths depending on the current state of your positions.
Path 1: New Position
If you do not have an active deal for the ticker and you have not reached your max active deals limit:
- Reserve a deal slot -- SignalPipe uses a database transaction to atomically check for existing deals and reserve a slot. This prevents duplicate deals from concurrent webhook deliveries.
- Place a market buy order -- A market order is sent to Alpaca for the specified quantity of shares.
- Wait for fill confirmation -- Alpaca fills fractional share market orders asynchronously. SignalPipe polls for the fill price and quantity.
- Calculate take-profit target -- The TP price is calculated as:
filled_price * (1 + take_profit_percent / 100), rounded to the nearest cent. - Place a take-profit limit sell -- A limit sell order is placed at the calculated TP price. For crypto, the actual position quantity is used (accounting for Alpaca's fee deductions).
- Save the deal -- The deal is recorded in the database with all order details, entry price, invested amount, and TP target.
Example: New Position
- Signal: BUY 10 shares of AAPL
- Fill price: $150.00
- Take-profit setting: 5%
- TP price: $150.00 * 1.05 = $157.50
- Result: 10 shares bought, limit sell for 10 shares at $157.50 placed
Path 2: Existing Position (DCA Safety Order)
If you already have an active deal for the ticker, the buy signal is treated as a safety order. Safety orders are always processed regardless of the "Accept New Deals" setting -- this ensures existing positions can continue to be managed.
- Place a market buy order -- Additional shares are purchased at the current market price.
- Wait for fill confirmation -- Same polling mechanism as a new position.
- Recalculate weighted average entry -- The new average is:
(old_total_cost + new_cost) / new_total_shares. - Cancel the existing TP order -- The old limit sell is cancelled. Before cancelling, SignalPipe checks if the TP order has already been filled (race condition protection). If it has, the deal is marked as completed instead.
- Place a new TP order -- A new limit sell is placed at the updated average entry price plus the TP percentage, for the total number of shares now held.
- Update the deal -- The deal record is updated with the new order, recalculated averages, and new TP target.
Example: Safety Order
- Existing deal: 10 shares of AAPL, avg entry $150.00, TP at $157.50
- New signal: BUY 15 shares of AAPL
- Fill price: $140.00
- New total cost: (10 $150) + (15 $140) = $1,500 + $2,100 = $3,600
- New total shares: 25
- New avg entry: $3,600 / 25 = $144.00
- New TP price: $144.00 * 1.05 = $151.20
- Result: Old TP cancelled, new limit sell for 25 shares at $151.20 placed
Notice how the safety order lowered the average entry from $150.00 to $144.00, and the TP target moved from $157.50 down to $151.20 -- making it much more likely to be reached.
Path 3: At Max Active Deals
If you have already reached your configured maximum number of active deals and the buy signal is for a new ticker (not an existing position):
- The signal is silently skipped -- no order is placed
- An entry is added to the activity log noting the skip
- HTTP 200 is returned to TradingView (no error)
This is by design. The max deals limit prevents over-allocation of your account. When an existing deal closes (via TP fill or sell signal), the slot becomes available for the next buy signal.
Order Validation
Before placing any buy order, SignalPipe validates:
- Quantity must be greater than 0 -- Invalid quantities are rejected and logged
- Minimum order value -- Alpaca requires a minimum order value of $10. Orders below this threshold are rejected by Alpaca and logged as an error
- Asset existence -- If the ticker does not exist on Alpaca, the order is rejected and logged