1Copy the scriptPress Copy script above, or the copy button on the code below.
2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
3Paste over the templateCreate a new library, select all of the template text and paste. Press Save and give it a name.
4Publish or import itA library draws nothing. Publish it from the editor, then import its functions in your own scripts.
Source Code
19 lines of Pine Script v6 from Codon_Risk_Manager.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.
Codon_Risk_Manager.pine
// 🧬 Generated by Codon.pro | The AI Pine Script Builder
// Module: Risk Management Calculator (Position Sizing)
// Description: Calculates position size based on % risk of equity.
//@version=6
library("CodonRiskManager", overlay=true)
// @function Calculates quantity based on risk percentage and stop loss distance
// @param riskPct The percentage of equity to risk per trade (e.g., 1.0 for 1%)
// @param entryPrice The planned entry price
// @param stopLossPrice The planned stop loss price
// @returns float The calculated position size (quantity)
export calcSize(float riskPct, float entryPrice, float stopLossPrice) =>
float riskPerShare = math.abs(entryPrice - stopLossPrice)
float riskAmount = strategy.equity * (riskPct / 100)
// Safety check to prevent division by zero
float qty = riskPerShare > 0 ? (riskAmount / riskPerShare) : 0
qty
Overview
The function takes risk percentage, entry price, and stop loss price as inputs and returns the quantity of contracts or shares. It helps traders limit losses to a set fraction of their account equity without manual calculation. Main settings let users adjust the risk percentage per trade and the planned stop loss distance. The library draws no elements on the price chart as it is a utility for strategy scripts. It has no repainting or lookahead behavior.
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.
This Pine Script v5 library provides functions to calculate and plot linear regression channels using sample point arrays. It evaluates paired X and Y arrays to calculate the regression slope, intercept, fitted values, maximum deviation, minimum deviation, and
This Pine Script v6 library calculates trade position sizes and provides risk management functions for external scripts. It computes position sizes from account equity, percentage risk, and stop-loss distance, and offers dynamic sizing adjustments based on vol
Chart Patterns is a Pine Script v6 library designed to scan for and render technical chart patterns directly on the price chart. It defines custom user-defined types to configure scanning parameters, such as Zigzag pivot offsets, error ratios, flat ratios, and
AuthLib is a Pine Script utility library providing access control and state management structures for technical indicators. It checks callers against a hardcoded array of authorized TradingView usernames before executing operations. The library exports user-de
WebhookMessageLibrary is a helper library designed to construct structured JSON payload messages for automated trading integrations like TradersPost. It defines custom exported types and constants for trade actions, market sentiments, quantities, limit prices,
PineDraw is a Pine Script v6 library that provides object primitives for drawing and updating price levels on the chart. It enables indicators and strategies to maintain lines, boxes, and labels in place by updating existing objects instead of deleting and rec