Risk and sizing
Daily Loss Limit
A corner panel measuring how far the session has moved against the side you are on, next to the maximum daily loss you set in price terms, and turning red once that number is passed.
indicator
//@version=6
indicator("Daily Loss Limit", "Loss Cap", overlay = true)
maxLoss = input.float(50.0, "Maximum daily loss in price terms", minval = 0.0, step = 1, group = "Settings")
side = input.string("Long", "Side you are on", options = ["Long", "Short"], group = "Settings")
okCol = input.color(#26A69A, "Within the limit", group = "Style")
badCol = input.color(#EF5350, "Limit passed", group = "Style")
textCol = input.color(#787B86, "Text", group = "Style")
var float sessionOpen = na
var float worstMove = 0.0
newDay = na(sessionOpen) or ta.change(time("D")) != 0
if newDay
sessionOpen := open
worstMove := 0.0
move = close - sessionOpen
against = side == "Long" ? -move : move
worstMove := math.max(worstMove, against)
passed = maxLoss > 0 and worstMove >= maxLoss
var table panel = table.new(position.bottom_right, 2, 3, border_width = 1)
if barstate.islast
tone = passed ? color.new(badCol, 75) : color.new(okCol, 88)
table.cell(panel, 0, 0, "Session move", text_color = textCol, text_size = size.small)
table.cell(panel, 1, 0, str.tostring(move, format.mintick), text_color = textCol, text_size = size.small)
table.cell(panel, 0, 1, "Worst against you", text_color = textCol, text_size = size.small)
table.cell(panel, 1, 1, str.tostring(math.max(worstMove, 0), format.mintick), text_color = textCol, text_size = size.small)
table.cell(panel, 0, 2, "Daily limit", text_color = textCol, text_size = size.small, bgcolor = tone)
table.cell(panel, 1, 2, str.tostring(maxLoss, format.mintick), text_color = textCol, text_size = size.small, bgcolor = tone)
tripped = passed and not passed[1]
plotshape(tripped, "Daily limit passed", location = location.top, style = shape.xcross, color = badCol, size = size.tiny)
alertcondition(tripped, "Daily limit passed", "The session move against your side has passed the configured daily loss limit")
This runs in TradingView, not here
Pine Script only executes inside TradingView. Paste the source into the Pine Editor and add it to a chart to see it plotted.
What it will not do
- This measures price, not your account. It has no idea what size you are on, so the number is only a loss if your size matches.
- Drawing a limit on a chart enforces nothing whatsoever. Closing the position and stopping for the day is a decision you still have to make.
- The worst move is measured from the session open using closes, so an intrabar spike past the limit is not counted.
- The session boundary comes from the daily timeframe of the chart's exchange, which may not be where your own trading day starts.
Written from this description
Track the move from the session open in price terms, keep the worst move against a configured long or short side, show it in a corner table beside a maximum daily loss, colour the table red once the limit is passed and alert when it is first passed.
Educational only, not financial advice. The maths is simple arithmetic on the numbers you enter; it knows nothing about your broker, fees, slippage or the market. Something wrong with it? Say so in Site Feedback.