← Back to Documentation Hub
Lot-Level Matching100% Independent Alerts

Pyramiding & FIFO Lot Matching Guide

How the bot manages multiple buy/sell alerts for the same stock without mixing share counts or stop-loss orders.

Why This Matters (In Plain English)

Suppose you add two different alerts in TradingView for the same share (e.g. TPLP): Strategy A buys 500 shares at 10:00 AM, and Strategy B buys another 300 shares at 11:30 AM. How does the bot know which shares to sell when an exit alert triggers?

In simpler trading bots, multiple signals for the same stock overwrite each other or cause the bot to sell everything by mistake. Our v4.2 engine treats every entry as an independent lot with its own timestamp and Stop-Loss order. When selling, it matches lots in chronological order (FIFO).

How Two BUY Alerts Work Independently

Lot #1 (Oldest Entry)10:00:00 AM
Bought: 500 Shares of TPLP
Entry Price: 15.00 PKR
Active SL: req-stop-001 (Protects 500 shares)
Lot #2 (Newer Entry)11:30:00 AM
Bought: 300 Shares of TPLP
Entry Price: 15.20 PKR
Active SL: req-stop-002 (Protects 300 shares)
Total Account Holding: 800 Shares | Both lots operate independently without mixing!

What Happens When a SELL Signal Arrives?

The bot automatically chooses the correct lot matching rule based on what TradingView sends:

TradingView Alert PayloadWhich Lot is Sold?What Happens to Stop-Loss Orders?
Explicit Quantity:
"contracts": 500
First-In, First-Out (FIFO): Sells only Lot #1 (500 shares).✅ Cancels only req-stop-001.
Lot #2 and its Stop-Loss (req-stop-002) remain OPEN and protected!
Full Exit / Pyramided Exit:
"reason": "EXIT_ALL"
Aggregate Sweep: Sums all open pyramided trades (500 + 300 = 800 shares).✅ Cancels all active Stop-Loss orders for TPLP in parallel.
✅ Sells 800 shares cleanly in one consolidated order.

Quick Reference FAQ

What if both alerts fire at the exact same second?

Our 15-Second Mutex Lock (`withTickerLock`) prevents race conditions. The first signal locks the ticker for milliseconds while placing its order; the second signal waits in memory and processes immediately afterward.

Do I need to write special Pine Script for Pyramiding?

No! You can simply set pyramiding=2 (or more) in your Pine Script strategy header. When each BUY webhook arrives, the backend automatically calculates how many shares you can afford and creates a new lot.

← Previous: Stop-Loss & Circuit BreakersNext: Webhook Setup →