# Position Size Calculator
# Author: @bottomphishing
# Date: 6/20/2021
# Position Size Calculator based on max % account risk with variable position riskPercent %.
declare upper;
#input showlabel = yes;
input ATRLength = 21;
input averagetype = AverageType.WILDERS;
input BasePeriod = AggregationPeriod.DAY;
def ATR = MovingAverage (averagetype, TrueRange(high(period = BasePeriod)[1], close(period = BasePeriod)[1], low(period = BasePeriod)[1]), ATRLength);
# Input max percentage of a portfolio that one stock should be
input Max_Percent = 0.03;
input price = close;
def netLiq = GetNetLiq();
# Risk of Portfolio
def twoPerc = netLiq * (Max_Percent / 100);
#AddLabel(yes, Max_Percent + "% Act Risk: " + AsDollars(twoperc), Color.CYAN);
# Input position risk
input RiskPercent = 0.02;
# Position Size
def currentPrice = price;
def dollarRisk = (currentPrice * RiskPercent);
def stopPrice = (currentPrice - dollarRisk);
def sharesToBuy = twoPerc / (currentPrice - stopPrice);
#AddLabel (yes, " POS " + SHAREStoBUY, Color.CYAN);
# Additional code by @netarchitech - 6/21/21
# Please double-check for accuracy
def stopATR = currentPrice - ATR;
def stopLoss = currentPrice - stopATR;
def tradeRisk = stopLoss * sharesToBuy;
input profitMultiple = 2.00;
def potentialProfit = (profitMultiple * ATR) * sharesToBuy;
def potentialProfitPerShare = potentialProfit / sharesToBuy;
def takeProfit = currentPrice + potentialProfitPerShare;
AddLabel(1," Shares to Buy: " + round(sharesToBuy, 0), Color.WHITE);
AddLabel(1," Stop Loss: " + round(stopATR, 2), Color.WHITE);
AddLabel(1," Take Profit: " + round(takeProfit, 2), Color.WHITE);
AddLabel(1," Trade Risk: " + AsDollars(tradeRisk), Color.WHITE);
AddLabel(1," Potential Profit: " + AsDollars(potentialProfit), Color.WHITE);