Portfolio · condition block
Take profit and stop loss (trade P&L)
The trade P&L block is how a bot takes profit and stops losses. It measures the open trade against its average entry price, in percent or in dollars, and can trigger on the close or as soon as the candle's wick touches the level.
How the engine evaluates it
The reference is the volume-weighted average entry of the open position: every buy since the bot was last flat, weighted by size. It resets when the position is fully closed.
In percent mode the target level is entry × (1 + X/100) for up and entry × (1 − X/100) for down. In dollar mode it is the price at which the open profit or loss equals $X: entry ± X / quantity.
On the close (the default), up fires when the close is at or above the level and down when it is at or below. The fill is the close.
on wick also fires when the candle's high (up) or low (down) touches the level, and the trade fills at the level itself rather than the close. If the whole candle is beyond the level, the fill is clamped to the candle's high or low.
Costs follow the order each exit behaves like. A take profit on the wick (pnl up … on wick) fills like a limit order: the bot's fee, no slippage. A stop loss on the wick (pnl down … on wick) fills like a stop order, so the bot's slippage moves the fill lower. Close-triggered exits are market fills at the close and pay both.
The block is false when the bot holds nothing.
level (percent) = average entry × (1 ± X / 100)
level (dollars) = average entry ± X / quantity
close trigger: close ≥ level (up) or close ≤ level (down)
wick trigger: high ≥ level (up) or low ≤ level (down), fill at the level
Botscript syntax
In the Lab you pick this block from the Portfolio group and fill in its fields. In the code view the same condition is written like this:
pnl up 5%
pnl down 8%
pnl down 8% on wick
pnl up $500
pnl down $300 on wick
Settings
| Setting | Values | Default | Notes |
|---|---|---|---|
| Direction | up (take profit), down (stop loss) | up | — |
| Unit | percent, dollars | percent | Percent of the average entry, or dollars of open P&L. |
| Amount | 0 – 100,000% or any dollar amount | 5% or $500 | — |
| Trigger | close, wick | close | on wick reacts inside the candle and fills at the level. |
How traders use it
- Fixed take profit:
pnl up 5%withsell 100%. - Hard stop:
pnl down 8% on wickwithsell 100%. - Scale out:
pnl up 3%withsell 50%, thenpnl up 6%withsell 100%.
Example bot
bot "Dip Buyer With Exits"
when pnl up 6%
then sell 100%
when pnl down 4% on wick
then sell 100%
when price down 3% in 24
then buy 50%
- Exit rules come first, so a candle that triggers a buy is not then tested against its own wick.
- Takes profit when a close is 6% above the average entry.
- Stops out as soon as a wick reaches 4% below the entry, filling at that level.
Pitfalls
- Rule order matters. Rules run top to bottom on each candle. If a buy rule sits above a wick-based stop, a fresh position can be stopped out by the same candle's earlier low. Put exits above entries.
- A wick stop fills at its level less the slippage set on the bot. In fast moves real stops can slip much further than a fixed percentage, so keep the slippage setting conservative.
- A stop that is too tight turns normal noise into losses; one that is too wide lets losers run.
Take profit / stop loss: frequently asked questions
What is the entry price when I bought several times?
The volume-weighted average of every buy since the position was last flat. It resets after a full exit.
What is the difference between close and wick triggers?
A close trigger waits for a candle to close beyond the level and fills at the close. A wick trigger fires as soon as the high or low touches the level and fills at the level.
Can I use a dollar stop instead of a percentage?
Yes: pnl down $300 fires when the open loss reaches $300, given the position's size and average entry.
Are fees and slippage included?
Yes, the fee and slippage set on the bot. A wick-based take profit fills at its level like a limit order, so it pays the fee but no slippage; every other exit pays both.
How do I take partial profits?
Use two rules: pnl up 3% with sell 50%, then pnl up 6% with sell 100%. The average entry stays the same while you hold, so the second target is still measured from the original entry.
Related blocks
Simulated results are hypothetical. They include only the fee and slippage you set on the bot, not every real-world cost or market condition, and past performance does not guarantee future results. Nothing here is financial advice.