Risk / Reward Based On Inputs For ThinkOrSwim

halcyonguy

Moderator - Expert
VIP
Lifetime
Here is a risk calculation study I put together.

-- pick a % of account to use per trade, 20%
calcs how many shares to buy​

-- pick a risk amount (stop level)
...can use the ATR as the risk amount or enter a dollar amount​

-- enter 4 percentages, for the target levels

-- enter a stock price. if it = 0 then it tracks the current price.
if a number, then it stays constant, and draws lines across the chart.​

labels show shares and cost
bubbles show stop and targets, with % and \$

Code:
``````# risk_calc_profit_loss

#https://usethinkscript.com/threads/can-someone-please-make-an-executable-risk-calc.15147/
#CAN SOMEONE PLEASE MAKE AN EXECUTABLE RISK CALC?

def na = Double.NaN;
def bn = BarNumber();

def cls = close;

#def lastbn = HighestAll(If(IsNaN(close), 0, bn));
#def lastbar = if (bn == lastbn) then 1 else 0;
def lastbar = !IsNaN(close[0]) and IsNaN(close[-1]);

#--------------------------
# ATR
# TD Ameritrade IP Company, Inc. (c) 2014-2023
def atr_length = 14;
def averageType = AverageType.WILDERS;
def ATR1 = if bn == 1 then round(MovingAverage(averageType, TrueRange(high, close, low), atr_length)) else atr1[1];
#------------------------

input stock_price = 0.0;
#hint stock_price: set this to 0 to track with current price
def stk = if stock_price == 0 then close else stock_price;

input risk_type = { default dollars , ATR };

input risk_dollars = 0.10;

input target1_factor = 1.0;
input target2_factor = 1.5;
input target3_factor = 2.0;
input target4_factor = 3.0;

input account_size = 1000;
input account_risk_percent_per_trade = 20.0;
def trade_amt = account_size * (account_risk_percent_per_trade/100);

def risk1;
switch(risk_type) {
case dollars:
risk1 = risk_dollars;
case atr:
risk1 = atr1;
}

def risk2 = if isnan(close) then risk2[1] else risk1;

def clsx = if isnan(close) then clsx[1]
else if stock_price == 0 then round(close, 2)
else stock_price;
#plot z = clsx;

def stop = clsx - risk2;
def t1f = clsx + (target1_factor * risk2);
def t2f = clsx + (target2_factor * risk2);
def t3f = clsx + (target3_factor * risk2);
def t4f = clsx + (target4_factor * risk2);

#then it would show price targets of 1R, 1.5R 2R & 3R etc
#it would then show the stop of 1R: example 2.40\$
#targets: example 2.60-.2.65-2.70-2.80

def shares = floor(trade_amt/clsx);
def cost = shares * clsx;

# gain %
def gper1 = round(100*(t1f - clsx)/clsx,2);
def gper2 = round(100*(t2f - clsx)/clsx,2);
def gper3 = round(100*(t3f - clsx)/clsx,2);
def gper4 = round(100*(t4f - clsx)/clsx,2);

# gain
def gain1 = round(gper1 * cost/100,2);
def gain2 = round(gper2 * cost/100,2);
def gain3 = round(gper3 * cost/100,2);
def gain4 = round(gper4 * cost/100,2);

# loss
def lossper = round(100*(clsx - stop)/clsx,2);
def loss = round(lossper * cost/100,2);

addlabel(1, " ", color.black);
addlabel(1, asdollars(stk) + " stock price", color.white);

addlabel(1, "risk " + asdollars(risk2), color.yellow);
addlabel(1, "trade amount " + asdollars(trade_amt), color.yellow);
addlabel(1, "shares " + shares, color.magenta);
addlabel(1, "cost " + cost, color.magenta);
addlabel(1, "ATR " + atr1, color.yellow);

# define a range of bars , x, to draw a line, after last bar
input line_length = 4;
input line_offset = 1;
def x = !isNaN(cls[line_offset+line_length]) and isNaN(cls[line_offset]);

input show_bubbles = yes;
def bub = show_bubbles and !isNaN(cls[line_offset+line_length]) and isNaN(cls[line_offset+line_length-1]);
def buboff = line_length + line_offset;

input show_lines = yes;
plot zt4 = if show_lines and x then t1f else na;
plot zt3 = if show_lines and x then t2f else na;
plot zt2 = if show_lines and x then t3f else na;
plot zt1 = if show_lines and x then t4f else na;
plot zcl = if show_lines and x then clsx else na;
plot zst = if show_lines and x then stop else na;

zt4.setdefaultcolor(color.green);
zt3.setdefaultcolor(color.green);
zt2.setdefaultcolor(color.green);
zt1.setdefaultcolor(color.green);
zcl.setdefaultcolor(color.white);
zst.setdefaultcolor(color.red);

# gains %
def t1per = round(100*(t1f-clsx)/clsx,1);

addchartbubble(bub, t4f, t4f + "  Target 4  " + gper4 + "%  " + gain4 + " gain" , color.green, yes);
addchartbubble(bub, t3f, t3f + "  Target 3  " + gper3 + "%  " + gain3 + " gain" , color.green, yes);
addchartbubble(bub, t2f, t2f + "  Target 2  " + gper2 + "%  " + gain2 + " gain" , color.green, yes);
addchartbubble(bub, t1f, t1f + "  Target 1  " + gper1 + "%  " + gain1 + " gain" , color.green, yes);
addchartbubble(bub, clsx, clsx + "  close  " + shares[buboff] + " shares  " + cost[buboff] + " cost", color.white, no);
addchartbubble(bub, stop, stop + "  stop  " + lossper + "%  " + loss + " loss\n" + risk2 + " risk" , color.magenta, no);

#---------------------------
# non 0 lines
#input show_lines = yes;
plot zt4b = if show_lines and stock_price > 0 and !isnan(close) then t1f else na;
plot zt3b = if show_lines and stock_price > 0 and !isnan(close) then t2f else na;
plot zt2b = if show_lines and stock_price > 0 and !isnan(close) then t3f else na;
plot zt1b = if show_lines and stock_price > 0 and !isnan(close) then t4f else na;
plot zclb = if show_lines and stock_price > 0 and !isnan(close) then clsx else na;
plot zstb = if show_lines and stock_price > 0 and !isnan(close) then stop else na;

zt4b.setdefaultcolor(color.gray);
zt3b.setdefaultcolor(color.gray);
zt2b.setdefaultcolor(color.gray);
zt1b.setdefaultcolor(color.gray);
zclb.setdefaultcolor(color.white);
zstb.setdefaultcolor(color.gray);
#``````

Here are some other scripts that you might be interested in:
https://www.google.com/search?q=use...GIAf8VkgEFMjEuMTCYAQCgAQGwAQo&sclient=gws-wiz

Last edited by a moderator:
Hi halcyonguy,

This is an awesome script. But difficult to use as you need to calc and enter the "risk dollars" to get it to work.

It would be WAY more useful if the "risk type" (currently: dollars, ATR) had percentage in it as well. This way as long as the study was loaded it would be automatic for any ticker. (or am I missing something ;-) )

For instance my R value is 3% of the price. So if a stock is 1\$ I stop out at .97. But with this script I need to type in .03 to use it. Which kind of slows things down -a lot.

I often trade sub-dollar pennies, so maybe this script wouldn't work anyway....

Is adding risk type; percentage, even possible?

Many thanks and great work btw, its really an impressive study.

Hi halcyonguy,

This is an awesome script. But difficult to use as you need to calc and enter the "risk dollars" to get it to work.

It would be WAY more useful if the "risk type" (currently: dollars, ATR) had percentage in it as well. This way as long as the study was loaded it would be automatic for any ticker. (or am I missing something ;-) )

For instance my R value is 3% of the price. So if a stock is 1\$ I stop out at .97. But with this script I need to type in .03 to use it. Which kind of slows things down -a lot.

I often trade sub-dollar pennies, so maybe this script wouldn't work anyway....

Is adding risk type; percentage, even possible?

Many thanks and great work btw, its really an impressive study. hal_risk

ver1
added an option, to choose a % of close, as the risk amount
added a multiplying factor for ATR

if not enough money is allocated to buy, a cyan labels appears with a warning

Code:
``````#risk_calc_profit_loss_01

#https://usethinkscript.com/threads/risk-reward-based-on-inputs-for-thinkorswim.15147/
#Risk / Reward Based On Inputs For ThinkOrSwim

def na = Double.NaN;
def bn = BarNumber();

def cls = close;

#def lastbn = HighestAll(If(IsNaN(close), 0, bn));
#def lastbar = if (bn == lastbn) then 1 else 0;
def lastbar = !IsNaN(close[0]) and IsNaN(close[-1]);

#--------------------------
# ATR
# TD Ameritrade IP Company, Inc. (c) 2014-2023
def atr_length = 14;
def averageType = AverageType.WILDERS;
def ATR1 = if bn == 1 then Round(MovingAverage(averageType, TrueRange(high, close, low), atr_length)) else ATR1[1];
#------------------------

input stock_price = 0.0;
#hint stock_price: set this to 0 to track with current price
def stk = if stock_price == 0 then close else stock_price;

input risk_type = { default dollars , ATR, percent };
input risk_dollars = 0.10;
input risk_atr_factor = 1.0;
input risk_percent_diff_close = 0.08;
def risk_per = (risk_percent_diff_close / 100);

input target1_factor = 1.0;
input target2_factor = 1.5;
input target3_factor = 2.0;
input target4_factor = 3.0;

input account_size = 1000;
input account_risk_percent_per_trade = 20.0;
def trade_amt = account_size * (account_risk_percent_per_trade / 100);

def rtype;
def risk1;
switch (risk_type) {
case dollars:
rtype = 1;
risk1 = risk_dollars;
case ATR:
rtype = 2;
risk1 = ATR1*risk_atr_factor;
case percent:
rtype = 3;
risk1 = (close * risk_per);
}

def risk2 = if IsNaN(close) then risk2[1] else round(risk1,2);

def clsx = if IsNaN(close) then clsx[1]
else if stock_price == 0 then Round(close, 2)
else stock_price;
#plot z = clsx;

def stop = clsx - risk2;
def t1f = clsx + (target1_factor * risk2);
def t2f = clsx + (target2_factor * risk2);
def t3f = clsx + (target3_factor * risk2);
def t4f = clsx + (target4_factor * risk2);

#then it would show price targets of 1R, 1.5R 2R & 3R etc
#it would then show the stop of 1R: example 2.40\$
#targets: example 2.60-.2.65-2.70-2.80

# force integer shares
def shares = Floor(trade_amt / clsx);
# allow fractional shares
#def shares = round(trade_amt / clsx,1);
def cost = shares * clsx;

# gain %
def gper1 = Round(100 * (t1f - clsx) / clsx, 2);
def gper2 = Round(100 * (t2f - clsx) / clsx, 2);
def gper3 = Round(100 * (t3f - clsx) / clsx, 2);
def gper4 = Round(100 * (t4f - clsx) / clsx, 2);

# gain
def gain1 = Round(gper1 * cost / 100, 2);
def gain2 = Round(gper2 * cost / 100, 2);
def gain3 = Round(gper3 * cost / 100, 2);
def gain4 = Round(gper4 * cost / 100, 2);

# loss
def lossper = Round(100 * (clsx - stop) / clsx, 2);
def loss = Round(lossper * cost / 100, 2);

AddLabel(1, " ", Color.BLACK);
AddLabel(1, AsDollars(stk) + " stock price", Color.WHITE);
AddLabel(1, "risk " + AsDollars(risk2), Color.YELLOW);
addlabel(1, " ", color.black);
AddLabel(1, "trade amount " + AsDollars(trade_amt), Color.YELLOW);
addlabel(shares == 0, " ", color.black);
addlabel(shares == 0, "  <<< Not enough money to buy 1 share. >>>  ", color.cyan);
addlabel(shares == 0, " ", color.black);

AddLabel(1, "shares " + shares, Color.MAGENTA);
AddLabel(1, "cost " + cost, Color.MAGENTA);
addlabel(1, "risk % " +  risk_percent_diff_close + " risk \$ " + risk2, color.cyan);

AddLabel(0, "ATR " + ATR1, Color.YELLOW);

# define a range of bars , x, to draw a line, after last bar
input line_length = 4;
input line_offset = 1;
def x = !IsNaN(cls[line_offset + line_length]) and IsNaN(cls[line_offset]);

input show_bubbles = yes;
def bub = show_bubbles and !IsNaN(cls[line_offset + line_length]) and IsNaN(cls[line_offset + line_length - 1]);
def buboff = line_length + line_offset;

input show_lines = yes;
plot zt4 = if show_lines and x then t1f else na;
plot zt3 = if show_lines and x then t2f else na;
plot zt2 = if show_lines and x then t3f else na;
plot zt1 = if show_lines and x then t4f else na;
plot zcl = if show_lines and x then clsx else na;
plot zst = if show_lines and x then stop else na;

zt4.SetDefaultColor(Color.GREEN);
zt3.SetDefaultColor(Color.GREEN);
zt2.SetDefaultColor(Color.GREEN);
zt1.SetDefaultColor(Color.GREEN);
zcl.SetDefaultColor(Color.WHITE);
zst.SetDefaultColor(Color.RED);

# gains %
def t1per = Round(100 * (t1f - clsx) / clsx, 1);

AddChartBubble(bub, t4f, t4f + "  Target 4  " + gper4 + "%  " + gain4 + " gain" , Color.GREEN, yes);
AddChartBubble(bub, t3f, t3f + "  Target 3  " + gper3 + "%  " + gain3 + " gain" , Color.GREEN, yes);
AddChartBubble(bub, t2f, t2f + "  Target 2  " + gper2 + "%  " + gain2 + " gain" , Color.GREEN, yes);
AddChartBubble(bub, t1f, t1f + "  Target 1  " + gper1 + "%  " + gain1 + " gain" , Color.GREEN, yes);
AddChartBubble(bub, clsx, clsx + "  close  " + shares[buboff] + " shares  " + cost[buboff] + " cost", Color.WHITE, no);
AddChartBubble(bub, stop, stop + "  stop  " + lossper + "%  " + loss + " loss\n" + risk2 + " risk" , Color.MAGENTA, no);

#---------------------------
# non 0 lines
#input show_lines = yes;
plot zt4b = if show_lines and stock_price > 0 and !IsNaN(close) then t1f else na;
plot zt3b = if show_lines and stock_price > 0 and !IsNaN(close) then t2f else na;
plot zt2b = if show_lines and stock_price > 0 and !IsNaN(close) then t3f else na;
plot zt1b = if show_lines and stock_price > 0 and !IsNaN(close) then t4f else na;
plot zclb = if show_lines and stock_price > 0 and !IsNaN(close) then clsx else na;
plot zstb = if show_lines and stock_price > 0 and !IsNaN(close) then stop else na;

zt4b.SetDefaultColor(Color.GRAY);
zt3b.SetDefaultColor(Color.GRAY);
zt2b.SetDefaultColor(Color.GRAY);
zt1b.SetDefaultColor(Color.GRAY);
zclb.SetDefaultColor(Color.WHITE);
zstb.SetDefaultColor(Color.GRAY);
#

#-------------------------
addchartbubble(0, clsx*0.998,
clsx + "\n" +
t1f + "\n" +
trade_amt + "\n" +

shares + "\n" +
cost + "\n" +

gper1 + "\n" +
cost + "\n" +
gain1 + "\n" +
loss
, color.yellow, no);

#``````

Last edited:
@halcyonguy
Thanks tons for adding the % feature! Its a fantastic and accurate visual tool for trading fast moving tickers.
Really works like a charm, even on sub 1\$ pennies.

Thanks again!

Last edited by a moderator:
Indicator that displays the trades risk to reward ratio (based on your entry, stop loss and profit target) the live risk to reward ratio of the trade, your stop loss and profit targets dollar amount if price was to reach them and the distance in ticks to your stop loss and profit target. I cannot code to save my life and used chat GPT to make this because I thought it would be helpful.

Rich (BB code):
``````# User-defined inputs
input entryPrice = 0.0;
input stopLossPrice = 0.0;
input profitTargetPrice = 0.0;
input tickSize = 0.25;  # Change this to match your tick size
input tickValue = 5.0;  # Change this to match your tick value
input quantity = 1;     # Change this to match your trade quantity

# Calculate the dollar value of stop loss and profit target based on tick size and tick value
def stopLossValue = AbsValue(entryPrice - stopLossPrice) / tickSize * tickValue * quantity;
def profitTargetValue = AbsValue(entryPrice - profitTargetPrice) / tickSize * tickValue * quantity;

# Determine if trade is long or short based on stop loss and entry prices
def isLongTrade = stopLossPrice < entryPrice;

# Calculate risk to reward ratio based on user-defined entry and stop loss
def risk = if isLongTrade then entryPrice - stopLossPrice else stopLossPrice - entryPrice;
def reward = profitTargetPrice - entryPrice;
def riskRewardRatio = if risk != 0 then AbsValue(reward / risk) else 0;

# Calculate ticks to stop loss and profit target based on current price
def currentPrice = close;
def ticksToStopLoss = Round((currentPrice - stopLossPrice) / tickSize, 0);
def ticksToProfitTarget = Round((profitTargetPrice - currentPrice) / tickSize, 0);

# Calculate live risk to reward ratio based on current price
def liveRiskRewardRatio;
if (isLongTrade and currentPrice >= entryPrice and currentPrice < profitTargetPrice) or
(!isLongTrade and currentPrice <= entryPrice and currentPrice > profitTargetPrice) {
liveRiskRewardRatio = Max(0, AbsValue((currentPrice - entryPrice) / (entryPrice - stopLossPrice)));
} else {
liveRiskRewardRatio = 0;
}

# Calculate live PnL based on current price, entry price, quantity, tick size, tick value, and trade direction
def livePnL;
if (isLongTrade) {
livePnL = ((currentPrice - entryPrice) / tickSize) * tickValue * quantity;
} else {
livePnL = ((entryPrice - currentPrice) / tickSize) * tickValue * quantity;
}

# Alerts for hitting stop loss and profit target levels
Alert(ticksToStopLoss == 0, "Stop Loss Hit!", Alert.BAR, Sound.Bell);
Alert(ticksToProfitTarget == 0, "Profit Target Hit!", Alert.BAR, Sound.Chimes);

# Plot horizontal lines at entry, stop loss, and profit target prices
plot entryLine = entryPrice;
plot stopLossLine = stopLossPrice;
plot profitTargetLine = profitTargetPrice;

entryLine.SetDefaultColor(Color.ORANGE);
stopLossLine.SetDefaultColor(Color.RED);
profitTargetLine.SetDefaultColor(Color.GREEN);

# Plot labels
AddLabel(yes, "Stop Loss: \$" + AsText(stopLossValue), Color.RED);
AddLabel(yes, "Ticks to Stop Loss: " + AsText(ticksToStopLoss), Color.RED);
AddLabel(yes, "Profit Target: \$" + AsText(profitTargetValue), Color.GREEN);
AddLabel(yes, "Ticks to Profit Target: " + AsText(ticksToProfitTarget), Color.GREEN);
AddLabel(yes, "Risk to Reward Ratio: " + AsText(riskRewardRatio), Color.CYAN);
AddLabel(yes, "Live Risk to Reward Ratio: " + AsText(liveRiskRewardRatio), Color.MAGENTA);
AddLabel(yes, "Live PnL: \$" + AsText(livePnL), Color.YELLOW);``````

Last edited by a moderator:
Here is a risk calculation study I put together.

-- pick a % of account to use per trade, 20%
calcs how many shares to buy​

-- pick a risk amount (stop level)
...can use the ATR as the risk amount or enter a dollar amount​

-- enter 4 percentages, for the target levels

-- enter a stock price. if it = 0 then it tracks the current price.
if a number, then it stays constant, and draws lines across the chart.​

labels show shares and cost
bubbles show stop and targets, with % and \$
View attachment 18530

Code:
``````# risk_calc_profit_loss

#https://usethinkscript.com/threads/can-someone-please-make-an-executable-risk-calc.15147/
#CAN SOMEONE PLEASE MAKE AN EXECUTABLE RISK CALC?

def na = Double.NaN;
def bn = BarNumber();

def cls = close;

#def lastbn = HighestAll(If(IsNaN(close), 0, bn));
#def lastbar = if (bn == lastbn) then 1 else 0;
def lastbar = !IsNaN(close[0]) and IsNaN(close[-1]);

#--------------------------
# ATR
# TD Ameritrade IP Company, Inc. (c) 2014-2023
def atr_length = 14;
def averageType = AverageType.WILDERS;
def ATR1 = if bn == 1 then round(MovingAverage(averageType, TrueRange(high, close, low), atr_length)) else atr1[1];
#------------------------

input stock_price = 0.0;
#hint stock_price: set this to 0 to track with current price
def stk = if stock_price == 0 then close else stock_price;

input risk_type = { default dollars , ATR };

input risk_dollars = 0.10;

input target1_factor = 1.0;
input target2_factor = 1.5;
input target3_factor = 2.0;
input target4_factor = 3.0;

input account_size = 1000;
input account_risk_percent_per_trade = 20.0;
def trade_amt = account_size * (account_risk_percent_per_trade/100);

def risk1;
switch(risk_type) {
case dollars:
risk1 = risk_dollars;
case atr:
risk1 = atr1;
}

def risk2 = if isnan(close) then risk2[1] else risk1;

def clsx = if isnan(close) then clsx[1]
else if stock_price == 0 then round(close, 2)
else stock_price;
#plot z = clsx;

def stop = clsx - risk2;
def t1f = clsx + (target1_factor * risk2);
def t2f = clsx + (target2_factor * risk2);
def t3f = clsx + (target3_factor * risk2);
def t4f = clsx + (target4_factor * risk2);

#then it would show price targets of 1R, 1.5R 2R & 3R etc
#it would then show the stop of 1R: example 2.40\$
#targets: example 2.60-.2.65-2.70-2.80

def shares = floor(trade_amt/clsx);
def cost = shares * clsx;

# gain %
def gper1 = round(100*(t1f - clsx)/clsx,2);
def gper2 = round(100*(t2f - clsx)/clsx,2);
def gper3 = round(100*(t3f - clsx)/clsx,2);
def gper4 = round(100*(t4f - clsx)/clsx,2);

# gain
def gain1 = round(gper1 * cost/100,2);
def gain2 = round(gper2 * cost/100,2);
def gain3 = round(gper3 * cost/100,2);
def gain4 = round(gper4 * cost/100,2);

# loss
def lossper = round(100*(clsx - stop)/clsx,2);
def loss = round(lossper * cost/100,2);

addlabel(1, " ", color.black);
addlabel(1, asdollars(stk) + " stock price", color.white);

addlabel(1, "risk " + asdollars(risk2), color.yellow);
addlabel(1, "trade amount " + asdollars(trade_amt), color.yellow);
addlabel(1, "shares " + shares, color.magenta);
addlabel(1, "cost " + cost, color.magenta);
addlabel(1, "ATR " + atr1, color.yellow);

# define a range of bars , x, to draw a line, after last bar
input line_length = 4;
input line_offset = 1;
def x = !isNaN(cls[line_offset+line_length]) and isNaN(cls[line_offset]);

input show_bubbles = yes;
def bub = show_bubbles and !isNaN(cls[line_offset+line_length]) and isNaN(cls[line_offset+line_length-1]);
def buboff = line_length + line_offset;

input show_lines = yes;
plot zt4 = if show_lines and x then t1f else na;
plot zt3 = if show_lines and x then t2f else na;
plot zt2 = if show_lines and x then t3f else na;
plot zt1 = if show_lines and x then t4f else na;
plot zcl = if show_lines and x then clsx else na;
plot zst = if show_lines and x then stop else na;

zt4.setdefaultcolor(color.green);
zt3.setdefaultcolor(color.green);
zt2.setdefaultcolor(color.green);
zt1.setdefaultcolor(color.green);
zcl.setdefaultcolor(color.white);
zst.setdefaultcolor(color.red);

# gains %
def t1per = round(100*(t1f-clsx)/clsx,1);

addchartbubble(bub, t4f, t4f + "  Target 4  " + gper4 + "%  " + gain4 + " gain" , color.green, yes);
addchartbubble(bub, t3f, t3f + "  Target 3  " + gper3 + "%  " + gain3 + " gain" , color.green, yes);
addchartbubble(bub, t2f, t2f + "  Target 2  " + gper2 + "%  " + gain2 + " gain" , color.green, yes);
addchartbubble(bub, t1f, t1f + "  Target 1  " + gper1 + "%  " + gain1 + " gain" , color.green, yes);
addchartbubble(bub, clsx, clsx + "  close  " + shares[buboff] + " shares  " + cost[buboff] + " cost", color.white, no);
addchartbubble(bub, stop, stop + "  stop  " + lossper + "%  " + loss + " loss\n" + risk2 + " risk" , color.magenta, no);

#---------------------------
# non 0 lines
#input show_lines = yes;
plot zt4b = if show_lines and stock_price > 0 and !isnan(close) then t1f else na;
plot zt3b = if show_lines and stock_price > 0 and !isnan(close) then t2f else na;
plot zt2b = if show_lines and stock_price > 0 and !isnan(close) then t3f else na;
plot zt1b = if show_lines and stock_price > 0 and !isnan(close) then t4f else na;
plot zclb = if show_lines and stock_price > 0 and !isnan(close) then clsx else na;
plot zstb = if show_lines and stock_price > 0 and !isnan(close) then stop else na;

zt4b.setdefaultcolor(color.gray);
zt3b.setdefaultcolor(color.gray);
zt2b.setdefaultcolor(color.gray);
zt1b.setdefaultcolor(color.gray);
zclb.setdefaultcolor(color.white);
zstb.setdefaultcolor(color.gray);
#``````

Here are some other scripts that you might be interested in:
https://www.google.com/search?q=use...GIAf8VkgEFMjEuMTCYAQCgAQGwAQo&sclient=gws-wiz
I tried it and get the following errors: Expected double at 133:6
Expected double at 134:6
No such variable: bn at 10:15
No such variable: cls at 92:16

Here is a risk calculation study I put together.

-- pick a % of account to use per trade, 20%
calcs how many shares to buy​

-- pick a risk amount (stop level)
...can use the ATR as the risk amount or enter a dollar amount​

-- enter 4 percentages, for the target levels

-- enter a stock price. if it = 0 then it tracks the current price.
if a number, then it stays constant, and draws lines across the chart.​

labels show shares and cost
bubbles show stop and targets, with % and \$
View attachment 18530

Code:
``````# risk_calc_profit_loss

#https://usethinkscript.com/threads/can-someone-please-make-an-executable-risk-calc.15147/
#CAN SOMEONE PLEASE MAKE AN EXECUTABLE RISK CALC?

def na = Double.NaN;
def bn = BarNumber();

def cls = close;

#def lastbn = HighestAll(If(IsNaN(close), 0, bn));
#def lastbar = if (bn == lastbn) then 1 else 0;
def lastbar = !IsNaN(close[0]) and IsNaN(close[-1]);

#--------------------------
# ATR
# TD Ameritrade IP Company, Inc. (c) 2014-2023
def atr_length = 14;
def averageType = AverageType.WILDERS;
def ATR1 = if bn == 1 then round(MovingAverage(averageType, TrueRange(high, close, low), atr_length)) else atr1[1];
#------------------------

input stock_price = 0.0;
#hint stock_price: set this to 0 to track with current price
def stk = if stock_price == 0 then close else stock_price;

input risk_type = { default dollars , ATR };

input risk_dollars = 0.10;

input target1_factor = 1.0;
input target2_factor = 1.5;
input target3_factor = 2.0;
input target4_factor = 3.0;

input account_size = 1000;
input account_risk_percent_per_trade = 20.0;
def trade_amt = account_size * (account_risk_percent_per_trade/100);

def risk1;
switch(risk_type) {
case dollars:
risk1 = risk_dollars;
case atr:
risk1 = atr1;
}

def risk2 = if isnan(close) then risk2[1] else risk1;

def clsx = if isnan(close) then clsx[1]
else if stock_price == 0 then round(close, 2)
else stock_price;
#plot z = clsx;

def stop = clsx - risk2;
def t1f = clsx + (target1_factor * risk2);
def t2f = clsx + (target2_factor * risk2);
def t3f = clsx + (target3_factor * risk2);
def t4f = clsx + (target4_factor * risk2);

#then it would show price targets of 1R, 1.5R 2R & 3R etc
#it would then show the stop of 1R: example 2.40\$
#targets: example 2.60-.2.65-2.70-2.80

def shares = floor(trade_amt/clsx);
def cost = shares * clsx;

# gain %
def gper1 = round(100*(t1f - clsx)/clsx,2);
def gper2 = round(100*(t2f - clsx)/clsx,2);
def gper3 = round(100*(t3f - clsx)/clsx,2);
def gper4 = round(100*(t4f - clsx)/clsx,2);

# gain
def gain1 = round(gper1 * cost/100,2);
def gain2 = round(gper2 * cost/100,2);
def gain3 = round(gper3 * cost/100,2);
def gain4 = round(gper4 * cost/100,2);

# loss
def lossper = round(100*(clsx - stop)/clsx,2);
def loss = round(lossper * cost/100,2);

addlabel(1, " ", color.black);
addlabel(1, asdollars(stk) + " stock price", color.white);

addlabel(1, "risk " + asdollars(risk2), color.yellow);
addlabel(1, "trade amount " + asdollars(trade_amt), color.yellow);
addlabel(1, "shares " + shares, color.magenta);
addlabel(1, "cost " + cost, color.magenta);
addlabel(1, "ATR " + atr1, color.yellow);

# define a range of bars , x, to draw a line, after last bar
input line_length = 4;
input line_offset = 1;
def x = !isNaN(cls[line_offset+line_length]) and isNaN(cls[line_offset]);

input show_bubbles = yes;
def bub = show_bubbles and !isNaN(cls[line_offset+line_length]) and isNaN(cls[line_offset+line_length-1]);
def buboff = line_length + line_offset;

input show_lines = yes;
plot zt4 = if show_lines and x then t1f else na;
plot zt3 = if show_lines and x then t2f else na;
plot zt2 = if show_lines and x then t3f else na;
plot zt1 = if show_lines and x then t4f else na;
plot zcl = if show_lines and x then clsx else na;
plot zst = if show_lines and x then stop else na;

zt4.setdefaultcolor(color.green);
zt3.setdefaultcolor(color.green);
zt2.setdefaultcolor(color.green);
zt1.setdefaultcolor(color.green);
zcl.setdefaultcolor(color.white);
zst.setdefaultcolor(color.red);

# gains %
def t1per = round(100*(t1f-clsx)/clsx,1);

addchartbubble(bub, t4f, t4f + "  Target 4  " + gper4 + "%  " + gain4 + " gain" , color.green, yes);
addchartbubble(bub, t3f, t3f + "  Target 3  " + gper3 + "%  " + gain3 + " gain" , color.green, yes);
addchartbubble(bub, t2f, t2f + "  Target 2  " + gper2 + "%  " + gain2 + " gain" , color.green, yes);
addchartbubble(bub, t1f, t1f + "  Target 1  " + gper1 + "%  " + gain1 + " gain" , color.green, yes);
addchartbubble(bub, clsx, clsx + "  close  " + shares[buboff] + " shares  " + cost[buboff] + " cost", color.white, no);
addchartbubble(bub, stop, stop + "  stop  " + lossper + "%  " + loss + " loss\n" + risk2 + " risk" , color.magenta, no);

#---------------------------
# non 0 lines
#input show_lines = yes;
plot zt4b = if show_lines and stock_price > 0 and !isnan(close) then t1f else na;
plot zt3b = if show_lines and stock_price > 0 and !isnan(close) then t2f else na;
plot zt2b = if show_lines and stock_price > 0 and !isnan(close) then t3f else na;
plot zt1b = if show_lines and stock_price > 0 and !isnan(close) then t4f else na;
plot zclb = if show_lines and stock_price > 0 and !isnan(close) then clsx else na;
plot zstb = if show_lines and stock_price > 0 and !isnan(close) then stop else na;

zt4b.setdefaultcolor(color.gray);
zt3b.setdefaultcolor(color.gray);
zt2b.setdefaultcolor(color.gray);
zt1b.setdefaultcolor(color.gray);
zclb.setdefaultcolor(color.white);
zstb.setdefaultcolor(color.gray);
#``````

Here are some other scripts that you might be interested in:
https://www.google.com/search?q=use...GIAf8VkgEFMjEuMTCYAQCgAQGwAQo&sclient=gws-wiz
Hey Halcyonguy, thank you for the script. I'm wondering how to adjust the stop % distance as I'm having trouble doing so. The only way I've been able to adjust is with the risk percent diff close, which also changes the profit exit distances. I would like to keep profit exits fixed where they are and widen % stop loss. Thank you.

Last edited by a moderator:
ver1
added an option, to choose a % of close, as the risk amount
added a multiplying factor for ATR

if not enough money is allocated to buy, a cyan labels appears with a warning

Code:
``````#risk_calc_profit_loss_01

#https://usethinkscript.com/threads/risk-reward-based-on-inputs-for-thinkorswim.15147/
#Risk / Reward Based On Inputs For ThinkOrSwim

def na = Double.NaN;
def bn = BarNumber();

def cls = close;

#def lastbn = HighestAll(If(IsNaN(close), 0, bn));
#def lastbar = if (bn == lastbn) then 1 else 0;
def lastbar = !IsNaN(close[0]) and IsNaN(close[-1]);

#--------------------------
# ATR
# TD Ameritrade IP Company, Inc. (c) 2014-2023
def atr_length = 14;
def averageType = AverageType.WILDERS;
def ATR1 = if bn == 1 then Round(MovingAverage(averageType, TrueRange(high, close, low), atr_length)) else ATR1[1];
#------------------------

input stock_price = 0.0;
#hint stock_price: set this to 0 to track with current price
def stk = if stock_price == 0 then close else stock_price;

input risk_type = { default dollars , ATR, percent };
input risk_dollars = 0.10;
input risk_atr_factor = 1.0;
input risk_percent_diff_close = 0.08;
def risk_per = (risk_percent_diff_close / 100);

input target1_factor = 1.0;
input target2_factor = 1.5;
input target3_factor = 2.0;
input target4_factor = 3.0;

input account_size = 1000;
input account_risk_percent_per_trade = 20.0;
def trade_amt = account_size * (account_risk_percent_per_trade / 100);

def rtype;
def risk1;
switch (risk_type) {
case dollars:
rtype = 1;
risk1 = risk_dollars;
case ATR:
rtype = 2;
risk1 = ATR1*risk_atr_factor;
case percent:
rtype = 3;
risk1 = (close * risk_per);
}

def risk2 = if IsNaN(close) then risk2[1] else round(risk1,2);

def clsx = if IsNaN(close) then clsx[1]
else if stock_price == 0 then Round(close, 2)
else stock_price;
#plot z = clsx;

def stop = clsx - risk2;
def t1f = clsx + (target1_factor * risk2);
def t2f = clsx + (target2_factor * risk2);
def t3f = clsx + (target3_factor * risk2);
def t4f = clsx + (target4_factor * risk2);

#then it would show price targets of 1R, 1.5R 2R & 3R etc
#it would then show the stop of 1R: example 2.40\$
#targets: example 2.60-.2.65-2.70-2.80

# force integer shares
def shares = Floor(trade_amt / clsx);
# allow fractional shares
#def shares = round(trade_amt / clsx,1);
def cost = shares * clsx;

# gain %
def gper1 = Round(100 * (t1f - clsx) / clsx, 2);
def gper2 = Round(100 * (t2f - clsx) / clsx, 2);
def gper3 = Round(100 * (t3f - clsx) / clsx, 2);
def gper4 = Round(100 * (t4f - clsx) / clsx, 2);

# gain
def gain1 = Round(gper1 * cost / 100, 2);
def gain2 = Round(gper2 * cost / 100, 2);
def gain3 = Round(gper3 * cost / 100, 2);
def gain4 = Round(gper4 * cost / 100, 2);

# loss
def lossper = Round(100 * (clsx - stop) / clsx, 2);
def loss = Round(lossper * cost / 100, 2);

AddLabel(1, " ", Color.BLACK);
AddLabel(1, AsDollars(stk) + " stock price", Color.WHITE);
AddLabel(1, "risk " + AsDollars(risk2), Color.YELLOW);
addlabel(1, " ", color.black);
AddLabel(1, "trade amount " + AsDollars(trade_amt), Color.YELLOW);
addlabel(shares == 0, " ", color.black);
addlabel(shares == 0, "  <<< Not enough money to buy 1 share. >>>  ", color.cyan);
addlabel(shares == 0, " ", color.black);

AddLabel(1, "shares " + shares, Color.MAGENTA);
AddLabel(1, "cost " + cost, Color.MAGENTA);
addlabel(1, "risk % " +  risk_percent_diff_close + " risk \$ " + risk2, color.cyan);

AddLabel(0, "ATR " + ATR1, Color.YELLOW);

# define a range of bars , x, to draw a line, after last bar
input line_length = 4;
input line_offset = 1;
def x = !IsNaN(cls[line_offset + line_length]) and IsNaN(cls[line_offset]);

input show_bubbles = yes;
def bub = show_bubbles and !IsNaN(cls[line_offset + line_length]) and IsNaN(cls[line_offset + line_length - 1]);
def buboff = line_length + line_offset;

input show_lines = yes;
plot zt4 = if show_lines and x then t1f else na;
plot zt3 = if show_lines and x then t2f else na;
plot zt2 = if show_lines and x then t3f else na;
plot zt1 = if show_lines and x then t4f else na;
plot zcl = if show_lines and x then clsx else na;
plot zst = if show_lines and x then stop else na;

zt4.SetDefaultColor(Color.GREEN);
zt3.SetDefaultColor(Color.GREEN);
zt2.SetDefaultColor(Color.GREEN);
zt1.SetDefaultColor(Color.GREEN);
zcl.SetDefaultColor(Color.WHITE);
zst.SetDefaultColor(Color.RED);

# gains %
def t1per = Round(100 * (t1f - clsx) / clsx, 1);

AddChartBubble(bub, t4f, t4f + "  Target 4  " + gper4 + "%  " + gain4 + " gain" , Color.GREEN, yes);
AddChartBubble(bub, t3f, t3f + "  Target 3  " + gper3 + "%  " + gain3 + " gain" , Color.GREEN, yes);
AddChartBubble(bub, t2f, t2f + "  Target 2  " + gper2 + "%  " + gain2 + " gain" , Color.GREEN, yes);
AddChartBubble(bub, t1f, t1f + "  Target 1  " + gper1 + "%  " + gain1 + " gain" , Color.GREEN, yes);
AddChartBubble(bub, clsx, clsx + "  close  " + shares[buboff] + " shares  " + cost[buboff] + " cost", Color.WHITE, no);
AddChartBubble(bub, stop, stop + "  stop  " + lossper + "%  " + loss + " loss\n" + risk2 + " risk" , Color.MAGENTA, no);

#---------------------------
# non 0 lines
#input show_lines = yes;
plot zt4b = if show_lines and stock_price > 0 and !IsNaN(close) then t1f else na;
plot zt3b = if show_lines and stock_price > 0 and !IsNaN(close) then t2f else na;
plot zt2b = if show_lines and stock_price > 0 and !IsNaN(close) then t3f else na;
plot zt1b = if show_lines and stock_price > 0 and !IsNaN(close) then t4f else na;
plot zclb = if show_lines and stock_price > 0 and !IsNaN(close) then clsx else na;
plot zstb = if show_lines and stock_price > 0 and !IsNaN(close) then stop else na;

zt4b.SetDefaultColor(Color.GRAY);
zt3b.SetDefaultColor(Color.GRAY);
zt2b.SetDefaultColor(Color.GRAY);
zt1b.SetDefaultColor(Color.GRAY);
zclb.SetDefaultColor(Color.WHITE);
zstb.SetDefaultColor(Color.GRAY);
#

#-------------------------
addchartbubble(0, clsx*0.998,
clsx + "\n" +
t1f + "\n" +
trade_amt + "\n" +

shares + "\n" +
cost + "\n" +

gper1 + "\n" +
cost + "\n" +
gain1 + "\n" +
loss
, color.yellow, no);

#``````
Awesome Script . Very Helpful with real use case. Thank you halcyonguy.

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
479 Online

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• ‘Buy The Dip’ signal alerts
• Exclusive members-only content
• Add-ons and resources
• 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.