Position Sizing Label
Thu Nov 25 2021 13:03:41 GMT+0000 (Coordinated Universal Time)
Saved by @finallynitin
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © finallynitin //@version=4 study(title="Position Sizing Label", shorttitle="Position Sizing", overlay=true) // Only show content if on a daily timeframe onDailyChart = timeframe.isdaily //Supertrend Supertrend(Factor, Pd) => Up=hl2-(Factor*atr(Pd)) Dn=hl2+(Factor*atr(Pd)) TrendUp = 0.0 TrendUp := close[1]>TrendUp[1] ? max(Up,TrendUp[1]) : Up TrendDown = 0.0 TrendDown := close[1]<TrendDown[1]? min(Dn,TrendDown[1]) : Dn Trend = 0.0 Trend := close > TrendDown[1] ? 1: close< TrendUp[1]? -1: nz(Trend[1],1) Tsl = Trend==1? TrendUp: TrendDown S_Buy = Trend == 1 ? 1 : 0 S_Sell = Trend != 1 ? 1 : 0 [Trend, Tsl] [Trend1,Tsl1] = Supertrend(3, 7) [Trend2,Tsl2] = Supertrend(3, 7) [Trend3,Tsl3] = Supertrend(3, 10) ST1_Trend_MTF = security(syminfo.tickerid, "W", Trend1, lookahead=barmerge.lookahead_on) ST1_Tsl_MTF = security(syminfo.tickerid, "W", Tsl1, lookahead=barmerge.lookahead_on) ST2_Trend_MTF = security(syminfo.tickerid, "D", Trend2, lookahead=barmerge.lookahead_on) ST2_Tsl_MTF = security(syminfo.tickerid, "D", Tsl2, lookahead=barmerge.lookahead_on) ST3_Trend_MTF = security(syminfo.tickerid, "60", Trend3, lookahead=barmerge.lookahead_on) ST3_Tsl_MTF = security(syminfo.tickerid, "60", Tsl3, lookahead=barmerge.lookahead_on) //linecolor = ST2_Trend_MTF == 1 ? color.green : color.red //plot(ST2_Tsl_MTF, color = linecolor, linewidth = 2,title = "SuperTrend") UDST = ST2_Tsl_MTF[1] * 1.03 DDST = ST2_Tsl_MTF[1] * 0.97 WST = (close - ST1_Tsl_MTF) * 100 / close DST = (close - ST2_Tsl_MTF) * 100 / close HST = (close - ST3_Tsl_MTF) * 100 / close // -----------------------POSITION SIZING------------------------ capital = input(1000000, "Capital") riskPercent = input(0.5, "Risk") riskPercentD = 2.0 riskPercentO = 1.0 riskPercentH = 0.5 riskPercentQ = 0.25 stopLoss = ST2_Tsl_MTF target = close + (3 * (close - stopLoss)) suggestQuantity(price, capital, riskPercent, stopLoss) => // Max Loss Per Trade maxCapitalLoss = (capital * riskPercent) / 100 // SL and position size calculation valueLossPerShare = abs(price - stopLoss) // Calculate and return quantity floor(maxCapitalLoss / valueLossPerShare) maxAffordableQuantity = floor(capital / close) // Calculate trade size quantity = suggestQuantity(close, capital, riskPercent, stopLoss) suggestedTradeVolume = quantity * close affordableQuantity = (suggestedTradeVolume > capital) ? maxAffordableQuantity : quantity // -----------------------LABEL------------------------ lt1=tostring(affordableQuantity,'#.##') //lt2=tostring(target,'#.##') lcolor = (DST < 1 ? color.rgb(240, 83, 80) : color.rgb(38, 166, 154)) ourlabel= label.new(x=bar_index,y=na,yloc=yloc.belowbar,color=lcolor ,style=label.style_label_up, text="Qty: "+lt1,textcolor=color.white) label.delete(ourlabel[1]) //ourlabel= label.new(x=bar_index,y=na,yloc=yloc.belowbar,color=lcolor ,style=label.style_label_up, text="Qty: "+lt1+"\nTgt: "+lt2,textcolor=color.white) label.delete(ourlabel[1])
Comments