Here is a risk calculation study I put together.
-- pick a % of account to use per trade, 20%
-- pick a risk amount (stop level)
-- enter 4 percentages, for the target levels
-- enter a stock price. if it = 0 then it tracks the current price.
labels show shares and cost
bubbles show stop and targets, with % and $
Here are some other scripts that you might be interested in:
https://www.google.com/search?q=use...GIAf8VkgEFMjEuMTCYAQCgAQGwAQo&sclient=gws-wiz
-- 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: