HomePine Script Snippets › Max Trades Per Day

Pine Script Max Trades Per Day

Counts entries per session and blocks new trades once you hit the cap — the simplest overtrading guard for prop accounts.

//@version=6
strategy("Max Trades Per Day", overlay=true, calc_on_every_tick=false)

// ── Inputs ──────────────────────────────────────────────
maxTrades = input.int(3, "Max Trades Per Day", minval=1)

// ── Trade counter ────────────────────────────────────────
var int tradesToday = 0

// Reset at each session open
if session.isfirstbar_regular
    tradesToday := 0

// Count each new entry the moment it opens
if strategy.opentrades > strategy.opentrades[1]
    tradesToday += 1

canTrade = tradesToday < maxTrades

// ── Entry logic (replace with your own conditions) ───────
longCondition  = ta.crossover(ta.ema(close, 9), ta.ema(close, 21)) and canTrade
shortCondition = ta.crossunder(ta.ema(close, 9), ta.ema(close, 21)) and canTrade

if longCondition
    strategy.entry("Long", strategy.long)
    strategy.exit("Long Exit", "Long", profit=20, loss=10)

if shortCondition
    strategy.entry("Short", strategy.short)
    strategy.exit("Short Exit", "Short", profit=20, loss=10)

// Visual
bgcolor(not canTrade ? color.new(color.red, 90) : na, title="Trade Limit Hit")

Settings

How to use

Frequently Asked Questions

How do I limit the number of trades per day in Pine Script?

Keep a persistent var int counter, reset it to zero on session.isfirstbar_regular, increment it when a new position opens, and require the counter to be below your max in every entry condition. Once the cap is hit, the gate blocks all further entries until the next session.

How do I count trades in Pine Script?

strategy.opentrades rises when an entry fills and strategy.closedtrades rises when one closes. Comparing either to its previous-bar value detects the event. This snippet counts entries via strategy.opentrades so the cap applies the moment a trade opens, not after it closes.

Related

Want a complete eval-ready strategy with discipline coded in?

This snippet is a building block. Our paid scripts are complete strategies — entries, trade caps, kill switch, session filter, and EOD flatten tested on 3 years of MES/NQ data. Instant delivery from $50.

View Plans — From $50