Pine Script Max Trades Per Day
Counts every new entry against a per-session cap and blocks further trades once the limit is hit — the simplest overtrading guard available for a funded account.
Pine Script
//@version=6
strategy("Target Filled - Max Trades Per Day Example", overlay=true, calc_on_every_tick=false)
// --- Inputs ---------------------------------------------------
dailyCap = input.int(3, "Max Trades Per Day", minval=1)
// --- Session trade counter ---------------------------------------
var int fillsToday = 0
if session.isfirstbar_regular
fillsToday := 0
if strategy.opentrades > strategy.opentrades[1]
fillsToday += 1
underCap = fillsToday < dailyCap
// --- Sample signal — replace with your own entry logic ---------
fastMA = ta.ema(close, 9)
slowMA = ta.ema(close, 21)
canLong = ta.crossover(fastMA, slowMA) and underCap
canShort = ta.crossunder(fastMA, slowMA) and underCap
if canLong
strategy.entry("Long", strategy.long)
strategy.exit("Long Exit", "Long", profit=20, loss=10)
if canShort
strategy.entry("Short", strategy.short)
strategy.exit("Short Exit", "Short", profit=20, loss=10)
// Visual: red background once the cap is reached
bgcolor(not underCap ? color.new(color.red, 90) : na, title="Daily Trade Cap Hit") Settings
| Input | Default | Purpose |
|---|---|---|
| dailyCap | 3 | Number of entries permitted per session before the gate closes. |
How to use
- Paste into the Pine Editor and swap the EMA-crossover placeholder for your own entry conditions.
- The counter resets on
session.isfirstbar_regular, so every trading day starts back at zero. - An entry is counted the moment it fills, via the
strategy.opentradescomparison — not when it later closes. - Red background shading means the cap has been hit; no new entries fire again until the next session.
Frequently Asked Questions
How do I limit the number of trades per day in Pine Script?
Keep a persistent counter with
var int, zero it out on session.isfirstbar_regular, and increment it whenever a new position opens. Every entry condition then needs an extra and counter < cap clause, so the gate closes the instant the limit is reached rather than after the fact.How do I count trades in Pine Script?
strategy.opentrades ticks up the moment a fill opens a position; strategy.closedtrades ticks up once it closes. Comparing either against its value on the prior bar ([1]) turns the event into a boolean. Counting on opentrades means the cap applies at entry time, which matters if you also want to block a trade that would push you over the limit.