Risk Management Tool [LUX] TradingView Script

0

 Good money management is one of the fundamental pillars of successful trading. With this indicator, we propose a simple way to manage trading positions. This tool shows Profit & Loss (P&L), suggests position size given a certain risk, sets stop losses and take profit levels using fixed price value/percentage/ATR/Range, and can also determine entries from crosses with technical indicators which is particularly handy if you don't want to set an entry manually.


1. Settings

Position Type: Determines if the position should be a "Long" or "Short".
Account Size: Determines the total capital of the trading account.
Risk: The maximum risk amount for a trade. Can be set as a percentage of the account size or as a fixed amount.
Entry Price: Determines the entry price of the position.
Entry From Cross: When enabled, allows to set the entry price where a cross with an external source was produced.

1.1 Stop Loss/Take Profit

Take Profit: Determines the take profit level, which can be determined by a value or percentage.
Stop Loss: Determines the stop loss level, which can be determined by a value or percentage.

2. Usage

One of the main usages of position management tools is to determine the position size to allocate given a specific risk amount and stop-loss. 2% of your capital is often recommended as a risk amount.

Our tool allows setting stop losses and take profits with different methods.



// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © LuxAlgo

//@version=4
study("Risk Management Tool [LUX]",overlay=true)
pos_type = input('Long','Position Type',options=['Long','Short'])
acc_size = input(1000.,'Account Size',minval=0)

risk = input(2.,'Risk               ',minval=0,inline='risk')
risk_type = input('%','',options=['%','USD'],inline='risk')

entry_price = input(40000.,'Entry Price',confirm=true)
entry_cross = input(false,'Entry From Cross',inline='entry')
cross_src = input(open,"",inline='entry')
//----
tp = input(5.,'Take Profit',minval=0
  ,group='Take Profit/Stop Loss',inline='tp',confirm=true)
tp_type = input('%','',options=['ATR','%','Price','Range']
  ,group='Take Profit/Stop Loss',inline='tp',confirm=true)
//----
sl = input(5.,'Stop Loss  ',minval=0
  ,group='Take Profit/Stop Loss',inline='sl',confirm=true)
sl_type = input('%','',options=['ATR','%','Price','Range']
  ,group='Take Profit/Stop Loss',inline='sl',confirm=true)
//----
profit_col = input(color.new(#009688,80),'Profit Color'
  ,group='Style',inline='col')
loss_col = input(color.new(#f44336,80),'Loss Color'
  ,group='Style',inline='col')
//------------------------------------------------------------------------------
tp_val = 0.
sl_val = 0.
entry = 0.
pos = pos_type == 'Long' ? 1 : -1

if entry_cross
    entry := valuewhen(cross(close,cross_src)[1],open,0)
else
    entry := entry_price

tp_range = highest(max(round(tp),1)) - lowest(max(round(tp),1))
sl_range = highest(max(round(sl),1)) - lowest(max(round(sl),1))

if tp_type == 'ATR'
    tp_val := entry + atr(max(round(tp),1))*pos
else if tp_type == '%'
    tp_val := entry + tp/100*entry*pos
else if tp_type == 'Price'
    tp_val := tp
else if tp_type == 'Range'
    tp_val := entry + (tp_range)*pos

if sl_type == 'ATR'
    sl_val := entry - atr(max(round(sl),1))*pos
else if sl_type == '%'
    sl_val := entry - sl/100*entry*pos
else if sl_type == 'Price'
    sl_val := sl
else if sl_type == 'Range'
    sl_val := entry - (sl_range)*pos
//------------------------------------------------------------------------------
open_pl = (close - entry)*pos
position_size = 0.
RR = abs((tp_val-entry)/(sl_val-entry))
if risk_type == '%'
    position_size := risk/100*acc_size/((entry-sl_val)*pos)
else
    position_size := risk/((entry-sl_val)*pos)
//------------------------------------------------------------------------------
n = bar_index
var tbl = table.new(position.bottom_right,2,3)
if barstate.islast
    box.delete(box.new(n,tp_val,n+100,entry,bgcolor=profit_col,border_width=0)[1])
    box.delete(box.new(n,entry,n+100,sl_val,bgcolor=loss_col,border_width=0)[1])
    
    label.delete(label.new(n+100,entry
      ,str.format('P&L : {0} {1} ({2,number,percent})',open_pl,syminfo.currency,open_pl/entry)
      ,color=open_pl>0?profit_col:loss_col
      ,style=label.style_label_left,textcolor=color.gray,textalign=text.align_left)[1]) 
    
    if tp
        label.delete(label.new(n+100,tp_val
          ,str.format('TP : {0} ({1,number,percent})',tp_val,(tp_val-entry)/entry)
          ,color=profit_col
          ,style=label.style_label_left,textcolor=color.gray,textalign=text.align_left)[1]) 
    if sl
        label.delete(label.new(n+100,sl_val
          ,str.format('TSL : {0} ({1,number,percent})',sl_val,(entry-sl_val)/entry)
          ,color=loss_col
          ,style=label.style_label_left,textcolor=color.gray,textalign=text.align_left)[1])
      
    table.cell(tbl,0,0,'Risk/Reward',text_color=color.gray,text_halign=text.align_center)
    table.cell(tbl,1,0,tostring(RR,'#.####'),text_color=color.gray,text_halign=text.align_center)
    table.cell(tbl,0,1,'Position Size',text_color=color.gray,text_halign=text.align_center)
    table.cell(tbl,1,1,tostring(position_size,'#.####')+' '+syminfo.basecurrency
      ,text_color=color.gray,text_halign=text.align_center)
//------------------------------------------------------------------------------
Close = plot(close,editable=false,display=display.none)
Entry = plot(entry,editable=false,display=display.none)
fill(Close,Entry,color=open_pl>0?profit_col:loss_col)
plotchar(open_pl,'Rolling Open PL%','',location.abovebar,open_pl > 0 ? profit_col : loss_col)

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

buttons=(Accept !) days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top