Stop loss, trailing stop and risk limits for backtests, checked on every bar

Exit rules decide when a position closes. Risk rules decide how many lots a new one may have. Day rules cap the whole account each day. In Veloce they are stages that review the strategy’s targets on every closed candle, in a fixed order, before any order exists.

Six steps from a target to an order

A strategy says what it wants to hold. Each stage takes the targets from the one before, looks at the account, and passes on what it allows.
  1. 1

    Strategy targets

    The positions the strategy wants after this closed candle, in signed lots.

    Strategy.on_bar
  2. 2

    Exit by

    After the cutoff time, every position closes and nothing opens until the next day.

    ExitBy
  3. 3

    Daily target and stop

    Once the day’s P&L reaches the target or the stop, everything closes for the rest of the day.

    DayGuard
  4. 4

    Skipped days

    On skipped weekdays or expiry days, targets can shrink but nothing new opens.

    SkipDays
  5. 5

    Exit rules

    The first rule met closes its whole group of legs.

    ExitManager
  6. 6

    Risk rules

    New lots are cut to what every limit allows. Closing is never blocked.

    RiskManager

What survives the last stage becomes an order for the difference from what is already held, and fills at the next candle’s open. Day rules come first so a closed day stays closed; risk comes last so nothing gets past its limits.

Four ways to close a position

Rules are listed in order, and the first one met closes the position. The reason is recorded on the trade, so the report can break P&L down by exit.
  • Stop loss

    stop_loss

    Closes when

    The group’s loss on the closed candle reaches the amount.

    Measured in

    Rupees, or a share of the credit received, the premium paid or the best profit so far

  • Take profit

    take_profit

    Closes when

    The group’s profit on the closed candle reaches the amount.

    Measured in

    Rupees, or a share of the credit received, the premium paid or the best profit so far

  • Trailing stop

    trailing_stop

    Closes when

    Profit falls from its best by the distance, once the stop is armed: from entry, or after the profit first reaches a threshold.

    Measured in

    Rupees, or a share of the credit, the premium paid or the best profit so far

  • Exit before expiry

    before_expiry

    Closes when

    Any leg is within the set number of calendar days of its expiry; 0 exits on expiry day.

    Measured in

    Calendar days

Per leg, or per spread

Exits group positions one of two ways. By leg, each contract is checked on its own: the hedged straddle stops out a sold call that has lost 30% of its premium and keeps the put. By expiry, every leg on the same underlying and expiry is checked together, as one spread: the iron condor takes profit when the four legs together have kept half the credit.

Credit, cost and peak

A share of the credit measures a sold spread against what it took in. A share of the cost measures a bought position against what it paid. A share of the peak sets a trailing distance as a fraction of the best profit so far, so the trail widens as the trade does better.

A stop is a decision, not a fill price

Exit rules are checked on closed candles, and the exit fills at the next candle’s open with slippage, like any other order. If the price gaps through the stop, the loss is bigger than the stop.

A stop loss on one NIFTY call, 1 lot of 65

Stop at 30% of the premium paid. Illustrative prices.

Bought at
₹120.00
Stop: 30% of ₹7,800 paid
₹2,340.00
Candle closes at ₹82.00, loss
−₹2,470.00
Stop met; next candle opens at
₹76.00
Fill, one tick of slippage
₹75.95
Loss taken, before charges
−₹2,863.25
(₹120.00 − ₹75.95) × 65 = ₹2,863.25, against a stop of ₹2,340.00. The rule fired on the close; the market decided the fill.

Limits that cut new lots down to size

Every risk rule answers one question: how many lots may this position hold? The smallest answer wins. Rules only ever cut; a target that closes or shrinks a position passes unchecked.
Risk rules and what each limits
RuleLimits new lots so thatApplies to
Lots per contractmax_lotsNo more than a set number of lots of any one contract.Every contract
Share of equityequity_shareThe lots whose value fits in a share of current equity, per contract.Every contract
Gross exposuregross_exposureThe value of every open position together stays within a rupee limit.All positions
CashcashAn option is bought only with cash on hand.Option buys
Futures marginfutures_marginEquity must cover margin, at the rate you set, on every open future.Futures
Loss at expirymax_loss_at_expiryThe worst loss at expiry of all positions on that expiry stays within a share of equity.Options
Daily loss limitdaily_loss_limitAfter a rupee loss from the day’s opening equity, only trades that reduce positions go through.The account

Loss at expiry, worked out at every strike

An option position’s value at expiry is a straight line between strikes, so its worst loss is at zero or at one of the strikes involved. The rule prices every option on the same underlying and expiry at each of those points, adds the new lots, and allows the most lots that keep the worst case within the share of equity you set.

A sold call with no bought call above it has no worst case: its loss grows without limit as the index rises. So the rule allows short calls only as far as bought calls on that expiry already cover them. This is why spread and condor legs list their hedges first: the wing is bought, then the short is allowed against it.

Every cut is on the record

When a limit changes a target, Veloce records the time, the contract, the lots wanted and allowed, the price and the equity. The report sums them up, for example “Risk limits cut 4 entries, 1 of them to 0 lots”, with one cut worked through: the lots asked for and allowed, and the cash, margin or loss limit that set them.

The daily loss limit

After the account loses a set amount from the day’s opening equity, the limit stops every new lot for the rest of the day. Positions already open keep their own exits, and trades that reduce them go through.

Rules for the whole account, each day

Day rules run before exits and risk, so once a day is closed, nothing else can reopen it.
Exit by
Close every position at a set time and open nothing until the next day, whatever the strategy wants.
Daily target and daily stop
In rupees or as a share of the day’s opening equity. When the day’s P&L reaches either, everything closes and stays closed until the next day.
Skip days
Open nothing new on chosen weekdays, on the days contracts expire, or both. Open positions can still shrink and close.

Risk and exit questions

Does a stop loss in a Veloce backtest fill at the stop price?

No. Exit rules are checked on closed candles, and the exit order fills at the next candle’s open with slippage, like every other order. If the price gaps past the stop, the loss is larger than the stop amount, as it would be in a real market.

What is the difference between the daily stop and the daily loss limit?

The daily stop is a day rule: when the day’s P&L reaches it, every position is closed and nothing opens until the next day. The daily loss limit is a risk rule: after the loss it stops new lots, but leaves open positions to their own exits.

Can a risk rule add lots to a position?

No. Risk rules only cut new lots down to what every limit allows. A target that closes or shrinks a position is never checked, so a limit can never stop a strategy from getting out.

How does the loss-at-expiry limit treat a short call?

A sold call that is not covered by a bought call on the same underlying and expiry can lose without limit at expiry, so the rule allows short calls only as far as bought calls already cover them. That is why spreads and condors list their hedges first.

Do I see when a risk rule changed a trade?

Yes. Every cut is recorded with the time, the lots wanted and allowed, the price and the equity, and the report notes how many entries the limits cut and how many to zero.

Test your exits before the market does

Create an account with a verified email and phone number, pick a strategy and a market, and see every fill, fee and day of P&L.

Create account
esc
↑↓Move↵Open