Skip to content

Instantly share code, notes, and snippets.

@yaakov123
Last active April 3, 2025 10:34
Show Gist options
  • Save yaakov123/bbce20527b95d86b879fb4fecac81214 to your computer and use it in GitHub Desktop.
Save yaakov123/bbce20527b95d86b879fb4fecac81214 to your computer and use it in GitHub Desktop.
//@version=5
indicator("ATR Risk Calc (1min TF, Custom SL, Rounded) - Explicit Format", shorttitle="ATR Risk (1m, Custom) EF", overlay=true)
// --- User Inputs ---
i_atrLength = input.int(14, title="ATR Length (on 1min TF)", minval=1)
i_riskAmount = input.float(100.0, title="Max Desired Risk ($)", minval=0.0, step=1.0) // Renamed for clarity vs Actual Risk
// --- Instrument Specific Inputs ---
i_pointValue = input.float(2.00, title="Point Value ($) - e.g., MNQ=2.00, MES=5.00", minval=0.01, step=0.01)
i_atrMultiplier = input.float(2.0, title="ATR Multiplier for Stop Loss", minval=0.1, step=0.1) // Defaulted to 2x
// --- Display ---
i_tablePosition = input.string("top_right", "Table Position", options = ["top_right", "top_left", "bottom_right", "bottom_left", "middle_right", "middle_left"])
// --- Calculations ---
// Request the ATR calculation specifically from the 1-minute timeframe
atrValue1min = request.security(syminfo.tickerid, "1", ta.atr(i_atrLength))
// Calculate initial Stop Loss distance in points using the ATR Multiplier
initialStopLossPoints = atrValue1min * i_atrMultiplier
// Round the Stop Loss points DOWN to the nearest multiple of 5
// Example: 31.2 -> floor(31.2 / 5) * 5 = floor(6.24) * 5 = 6 * 5 = 30
// Example: 34.2 -> floor(34.2 / 5) * 5 = floor(6.84) * 5 = 6 * 5 = 30
// Example: 29.9 -> floor(29.9 / 5) * 5 = floor(5.98) * 5 = 5 * 5 = 25
// Example: 35.1 -> floor(35.1 / 5) * 5 = floor(7.02) * 5 = 7 * 5 = 35
roundedStopLossPoints = initialStopLossPoints > 0 ? math.floor(initialStopLossPoints / 5) * 5 : 0.0 // Avoid division by zero if ATR is 0/na
// Calculate Risk per single contract based on the ROUNDED SL distance in points and point value
riskPerContract = roundedStopLossPoints * i_pointValue
// Calculate Position Size (Number of Contracts)
// Divide total *desired* risk by the (updated) risk per contract. Use math.floor() to round DOWN.
positionSizeContracts = riskPerContract > 0 ? math.floor(i_riskAmount / riskPerContract) : 0.0
// Calculate the ACTUAL dollar risk based on the final number of contracts and the risk per contract
actualRiskAmount = positionSizeContracts * riskPerContract
// --- Table Display ---
// Declare table variable 'var' so it persists across bars
// Increased rows to 6 to display Actual Risk
var table riskInfoTable = table.new(position = i_tablePosition, columns = 2, rows = 6, bgcolor = color.new(color.gray, 85), border_width = 1)
// Only draw/update the table on the last historical bar and all real-time bars
if (barstate.islast)
// --- Populate Table ---
// Column 0: Labels
table.cell(riskInfoTable, 0, 0, "1min ATR (" + str.tostring(i_atrLength) + "):", text_halign = text.align_right, text_color=color.white, bgcolor=color.new(#000000, 0))
table.cell(riskInfoTable, 0, 1, "SL Points (" + str.tostring(i_atrMultiplier) + "x ATR, Rnd↓5):", text_halign = text.align_right, text_color=color.white, bgcolor=color.new(#000000, 0)) // Label updated
table.cell(riskInfoTable, 0, 2, "Risk / Contract:", text_halign = text.align_right, text_color=color.white, bgcolor=color.new(#000000, 0))
table.cell(riskInfoTable, 0, 3, "Max Desired Risk:", text_halign = text.align_right, text_color=color.white, bgcolor=color.new(#000000, 0)) // Label updated
table.cell(riskInfoTable, 0, 4, "Contracts (Floor):", text_halign = text.align_right, text_color=color.white, bgcolor=color.new(#000000, 0))
table.cell(riskInfoTable, 0, 5, "Actual Risk Amt:", text_halign = text.align_right, text_color=color.white, bgcolor=color.new(#000000, 0)) // New Row for Actual Risk
// Column 1: Values (Using explicit format strings)
// ATR value in points
table.cell(riskInfoTable, 1, 0, str.tostring(atrValue1min, '#.######'), text_halign = text.align_left, text_color=color.white, bgcolor=color.new(#000000, 0))
// ROUNDED Stop loss distance in points
table.cell(riskInfoTable, 1, 1, str.tostring(roundedStopLossPoints, '#.##') + " pts", text_halign = text.align_left, text_color=color.white, bgcolor=color.new(#000000, 0))
// Risk per contract in $ (based on rounded SL)
table.cell(riskInfoTable, 1, 2, "$" + str.tostring(riskPerContract, '#.##'), text_halign = text.align_left, text_color=color.white, bgcolor=color.new(#000000, 0))
// Total DESIRED risk amount in $
table.cell(riskInfoTable, 1, 3, "$" + str.tostring(i_riskAmount, '#.##'), text_halign = text.align_left, text_color=color.white, bgcolor=color.new(#000000, 0))
// Calculated number of contracts (rounded down)
table.cell(riskInfoTable, 1, 4, str.tostring(positionSizeContracts, '0'), text_halign = text.align_left, text_color=color.white, bgcolor=color.new(#000000, 0))
// ACTUAL calculated risk amount based on contracts used
table.cell(riskInfoTable, 1, 5, "$" + str.tostring(actualRiskAmount, '#.##'), text_halign = text.align_left, text_color=color.white, bgcolor=color.new(#000000, 0)) // New Row Value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment