Indicator · Pine Script v4 · On the chart

StudyNameHere - DRR

by myn

This script provides a boilerplate risk management template for TradingView indicators.

View on GitHub

GitHub stars4on the repository
Pine versionv4older, still runs
Lines of code139
Settings11inputs
LicenceApache-2.0
Last update6 years ago15 Apr 2020

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

139 lines of Pine Script v4 from RiskManagement-TradingView-Study-v4-Template.pine, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

RiskManagement-TradingView-Study-v4-Template.pine
//@version=4
study("StudyNameHere [Study] - DRR", overlay=true)

//################### Strategy Begin  #############################



//################### Strategy End  #############################



//################### Conditions Begin #############################
bool longCond = na
bool shortCond = na
longCond := longSignal
shortCond := shortSignal

//################### Conditions End #############################



// Count your long short conditions for more control with Pyramiding

sectionLongs = 0
sectionLongs := nz(sectionLongs[1])
sectionShorts = 0
sectionShorts := nz(sectionShorts[1])

if longCond
    sectionLongs := sectionLongs + 1
    sectionShorts := 0

if shortCond
    sectionLongs := 0
    sectionShorts := sectionShorts + 1
    
// Pyramiding Inputs

pyramidingDivider = input(false,"######################################################")
pyrl = input(1, "Pyramiding", type = input.integer)

// These check to see your signal and cross references it against the pyramiding settings above

longCondition = longCond and sectionLongs <= pyrl 
shortCondition = shortCond and sectionShorts <= pyrl 

// Get the price of the last opened long or short

float  last_open_longCondition = na
float last_open_shortCondition = na
last_open_longCondition := longCondition ? high[1] : nz(last_open_longCondition[1])
last_open_shortCondition := shortCondition ? low[1] : nz(last_open_shortCondition[1])

// Check if your last postion was a long or a short

float last_longCondition = na
float last_shortCondition = na
last_longCondition := longCondition ? time : nz(last_longCondition[1])
last_shortCondition := shortCondition ? time : nz(last_shortCondition[1])

in_longCondition = last_longCondition > last_shortCondition
in_shortCondition = last_shortCondition > last_longCondition

// Take profit

isTPl = input(false, "Take Profit Long")
isTPs = input(false, "Take Profit Short")
tpl = input(15, "Take Profit Long %", type=input.float, step=0.10)
tps = input(30, "Take Profit Short %", type=input.float, step=0.10)
long_tp = isTPl and crossover(high, (1+(tpl/100))*last_open_longCondition) and in_longCondition  == 1
short_tp = isTPs and crossunder(low, (1-(tps/100))*last_open_shortCondition) and in_shortCondition == 1 

// Stop Loss
stopLossDivider = input(false,"######################################################")
isSLl = input(false, "Stop Loss Long")
isSLs = input(false, "Stop Loss Short")
sll = 0.0
sll := input(3, "Stop Loss Long %", type=input.float, step=0.10)
sls = 0.0
sls := input(3, "Stop Loss Short %", type=input.float, step=0.10)
long_sl = isSLl and crossunder(low, (1-(sll/100))*last_open_longCondition) and longCondition == 0 and in_longCondition == 1
short_sl = isSLs and crossover(high, (1+(sls/100))*last_open_shortCondition) and shortCondition == 0 and in_shortCondition == 1


// Create a single close for all the different closing conditions.

long_close = long_tp or long_sl ? 1 : 0
short_close = short_tp or short_sl ? 1 : 0

// Get the time of the last close

float last_long_close = na
float last_short_close = na
last_long_close := long_close ? time : nz(last_long_close[1])
last_short_close := short_close ? time : nz(last_short_close[1])

// Alerts & Signals

bton(b) => b ? 1 : 0

plotshape(longCondition, title = "Buy Signal", style=shape.triangleup, location=location.belowbar, color = color.lime, transp = 0, size=size.small)
plotshape(shortCondition, title = "Sell Signal", style=shape.triangledown, location=location.abovebar, color = color.red, transp = 0, size=size.small)

plotshape(long_tp and last_longCondition > nz(last_long_close[1]), text ="TP", title="Take Profit Long", style=shape.triangledown, 
   location=location.abovebar, color = color.red, editable = false, transp = 0, size=size.small) 
plotshape(short_tp and last_shortCondition > nz(last_short_close[1]) , text ="TP", title="Take Profit Short", style=shape.triangleup, 
   location=location.belowbar, color = color.lime, editable = false, transp = 0, size=size.small) 

ltp = iff(long_tp and last_longCondition > nz(last_long_close[1]), (1+(tpl/100))*last_open_longCondition, na)
plot(ltp, style=plot.style_cross, linewidth=3, color = color.white, editable = false)
stp = iff(short_tp and last_shortCondition > nz(last_short_close[1]), (1-(tps/100))*last_open_shortCondition, na)
plot(stp, style = plot.style_cross, linewidth=3, color = color.white, editable = false)

plotshape(long_sl and last_longCondition > nz(last_long_close[1]), text ="SL", title="Stop Loss Long", style=shape.triangledown, 
   location=location.abovebar, color = color.red, editable = false, transp = 0, size=size.small) 
plotshape(short_sl and last_shortCondition > nz(last_short_close[1]), text ="SL", title="Stop Loss Short", style=shape.triangleup, 
   location=location.belowbar, color = color.lime, editable = false, transp = 0), size=size.small 

lsl = iff(long_sl and last_longCondition > nz(last_long_close[1]), (1-(sll/100))*last_open_longCondition, na)
plot(lsl, style=plot.style_cross, linewidth=3, color = color.white, editable = false)
ssl = iff(short_sl and last_shortCondition > nz(last_short_close[1]), (1+(sls/100))*last_open_shortCondition, na)
plot(ssl, style = plot.style_cross, linewidth=3, color = color.white, editable = false)
plotshape(longCondition[1]==1, title= "Long Alert Completed", location=location.bottom, color=color.green, transp=0, style=shape.circle, size=size.small, offset=-1)
plotshape(shortCondition[1]==1, title= "Short Alert Completed", location=location.top, color=color.red, transp=0, style=shape.circle, size=size.small, offset=-1)


alertcondition(bton(longCondition), title="*Buy Alert*", 
   message = "a=MYNCRYPTOBMT e=bitmex-testnet s=xbtusd c=order t=open delay=1 | a=MYNCRYPTOBMT e=bitmex-testnet s=xbtusd c=position b=short t=market delay=2 | a=MYNCRYPTOBMT e=bitmex-testnet s=xbtusd b=long p=-0.50 l=1 q=90%") 

alertcondition(bton(shortCondition or (long_tp and high > close and last_longCondition > nz(last_long_close[1]))), title="*Sell Alert*", 
   message = "a=MYNCRYPTOBMT e=bitmex-testnet s=xbtusd c=order t=open delay=1 | a=MYNCRYPTOBMT e=bitmex-testnet s=xbtusd c=position b=long t=market delay=2 | a=MYNCRYPTOBMT e=bitmex-testnet s=xbtusd b=short p=0.50 l=1 q=90%") 

alertcondition(bton((long_tp and high > close and last_longCondition > nz(last_long_close[1])) or 
   (long_sl and last_longCondition > nz(last_long_close[1]))), title="*TP/SL Sell Alert*", 
   message = "a=MYNCRYPTOBMT e=bitmex-testnet s=xbtusd c=order t=open delay=1 | a=MYNCRYPTOBMT e=bitmex-testnet s=xbtusd c=position b=long t=market delay=2")
   
alertcondition(bton((short_tp and low < close and last_shortCondition > nz(last_short_close[1])) or 
   (short_sl and last_shortCondition > nz(last_short_close[1]))), title="*TP/SL Buy Alert*",
   message = "a=MYNCRYPTOBMT e=bitmex-testnet s=xbtusd c=order t=open delay=1 | a=MYNCRYPTOBMT e=bitmex-testnet s=xbtusd c=position b=short t=market delay=2")

Overview

StudyNameHere - DRR processes long and short signals to manage pyramiding depth and tracks position entry prices. The template calculates percentage-based take profit and stop loss threshold conditions based on previous high and low levels. Signal shapes for entries are plotted directly on the main price chart overlay. Triggers can be customized via inputs for pyramiding limits, take profit percentages, and stop loss percentages, allowing developers to standardize risk logic inside indicator-based trading setups.

Details

Published by

myn on GitHub

Type

Indicator, Pine Script v4

Draws

On the price chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
######################################################boolfalsepyramidingDivider
Pyramidingint1pyrl
Take Profit LongboolfalseisTPl
Take Profit ShortboolfalseisTPs
Take Profit Long %float15tpl
Take Profit Short %float30tps
######################################################boolfalsestopLossDivider
Stop Loss LongboolfalseisSLl
Stop Loss ShortboolfalseisSLs
Stop Loss Long %float3sll
Stop Loss Short %float3sls
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.

Similar Pine Scripts

948 stars

The Adaptive Laguerre Filter calculates a smoothed, dynamically adjusted trendline plotted directly on the main price chart. It dynamically adjusts its damping factor (alpha) based on recent price range variations and median percentile calculations over a spec

Indicator v4 TrendMoving averages
Updated 10 days ago · GPL-3.0 GitHub repository of Adaptive Laguerre Filter
575 stars

The indicator plots ICT displacement candles on the price chart and can switch to retracement scanning. Displacement requires a move larger than a set share of the 14-bar average high-low range, lasting at least a chosen number of bars, with optional close, pr

Indicator v6 SMC / ICT / price actionCandlestick patterns
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Displacement Candles Indicator
330 stars

This script plots three moving averages on the price chart: a fast EMA or SMA with 10 periods, a slow EMA or SMA with 20 periods, and a trend EMA or SMA with 100 periods. It signals trend changes by drawing a white cross when the fast moving average crosses ab

Indicator v4 TrendMoving averages
Updated 2 months ago · None stated GitHub repository of 3 EMA/SMA + Cross [Alorse]
318 stars

This indicator calculates an ATR trailing stop line derived from the highest high minus an average true range multiple over a specified lookback period. It plots the trailing stop line directly on the price chart and colors price bars based on whether the clos

Indicator v5 TrendVolatility & bands
Updated 2 months ago · MIT GitHub repository of ATR Trailing Stoploss

1833 Tracker

by Allen1212
303 stars

1833 Tracker is an overlay indicator that visualizes historical buy and sell transactions executed by ARK Invest ETFs for the stock on the main price chart. It displays trade dates using green buy and red sell labels positioned relative to price bars, indicati

Indicator v4
Updated 2 years ago · None stated GitHub repository of 1833 Tracker

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers