Position Risk / Size Calculator w/ Plotted Targets

bottomphishing

New member
Having trouble getting the target lines to plot correctly. The problem is they are plotting current trade averages and prior averages for trades I haven't been in in quiet some time. I also added a predetermined stop loss for trades I have not entered yet in order to see my risk real time at the current price. This plots under all candles displayed on the chart. Idk why. Lastly I'm having trouble extending these target plots right. Any help will be greatly appreciated.

Screenshots:
No position: https://ibb.co/NTrzWMp
No positions showing past trades targets: https://ibb.co/dWzgZmz
With Position: https://ibb.co/Fqjz3gY

Below is the code::

# Position Size Calculator based on max % account risk with variable position riskPercent %
# Includes % targets and chart plots
# Author: @bottomphishing
# Date: 6/20/2021

declare upper;

# Input max percentage of a portfolio that one stock should be
input Max_Percent = 0.25;
input price = close;
input Extend_to_right = yes;

def netliq = GetNetLiq();


## Risk of Portfolio

def twoperc = netliq * (Max_Percent / 100);
AddLabel(yes, Max_Percent + "% ActRisk " + AsDollars(twoperc), Color.CYAN);

# Input position risk
input RiskPercent = .03 ;

# Position Size
def currentPrice = close;
def dollarRisk = (currentPrice * RiskPercent);

def stopPrice = (currentPrice - dollarRisk);
def SHAREStoBUY = twoperc / (currentPrice - stopPrice);

input showlabel = yes;
AddLabel (yes, " Pos " + RoundUp ((SHAREStoBUY), 0), Color.CYAN);

AddLabel (yes, AsPercent(RiskPercent) + " Risk " + AsDollars (currentPrice - (currentPrice * RiskPercent)), Color.VIOLET);

AddLabel (yes, " Cap " + AsDollars (SHAREStoBUY * currentPrice), Color.CYAN);

#Position Targets
def GetAveragePrice = GetAveragePrice();
def TargetPrice = Round((GetAveragePrice * .03) + GetAveragePrice); # Change Percent to 1 to 1
def TargetPrice2 = Round((GetAveragePrice * .06) + GetAveragePrice);
def TargetPrice3 = Round((GetAveragePrice * .09) + GetAveragePrice);
def TargetPrice4 = Round((GetAveragePrice * .50) + GetAveragePrice);
def TargetPrice5 = Round((GetAveragePrice * 1.00) + GetAveragePrice);
def dollarStop = Round(GetAveragePrice - (GetAveragePrice * RiskPercent));

AddLabel(1, " Targets: Stop: " + AsDollars(dollarStop) + " | 3%: " + AsDollars(TargetPrice) + " | 6%: " + AsDollars(TargetPrice2) + " | 9%: " + AsDollars(TargetPrice3) + " | 50%: " + AsDollars(TargetPrice4) + " | 100%: " + AsDollars(TargetPrice5), if GetAveragePrice <= 0 then Color.Black else Color.GREEN);

plot s1 = (currentPrice - (currentPrice * RiskPercent));
s1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
s1.SetDefaultColor(Color.PINK);

plot r1 = dollarStop;
r1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
r1.SetDefaultColor(Color.RED);

plot a1 = GetAveragePrice;
a1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
a1.SetDefaultColor(Color.BLUE);

plot t1 = TargetPrice;
t1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
t1.SetDefaultColor(Color.GREEN);
plot t2 = TargetPrice2;
t2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
t2.SetDefaultColor(Color.GREEN);
plot t3 = TargetPrice3;
t3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
t3.SetDefaultColor(Color.GREEN);
plot t4 = TargetPrice4;
t4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
t4.SetDefaultColor(Color.GREEN);
plot t5 = TargetPrice5;
t5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
t5.SetDefaultColor(Color.GREEN);
 
Last edited:
Tried a few things:
To only plot under current candle and not all candles (didnt work):
plot s1 = if (currentPrice - (currentPrice * RiskPercent)) then (currentPrice - (currentPrice * RiskPercent)) else Double.NaN;
s1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
s1.SetDefaultColor(Color.PINK);

To only plot current trade and not all prior trades since account inception (didnt work)
plot t1 = if TargetPrice then TargetPrice and Extend_to_Right else Double.NAN;
t1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
t1.SetDefaultColor(Color.GREEN);

Extend_to_right not working either.

Photo of chart with explanation: https://ibb.co/QmGDNJs
I am very new to this my code it prob really sloppy but Im just searching around and trying to piece together something that works our of trial and error.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Tried a few things:
To only plot under current candle and not all candles (didnt work):
plot s1 = if (currentPrice - (currentPrice * RiskPercent)) then (currentPrice - (currentPrice * RiskPercent)) else Double.NaN;
s1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
s1.SetDefaultColor(Color.PINK);

To only plot current trade and not all prior trades since account inception (didnt work)
plot t1 = if TargetPrice then TargetPrice and Extend_to_Right else Double.NAN;
t1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
t1.SetDefaultColor(Color.GREEN);

Extend_to_right not working either.

Photo of chart with explanation: https://ibb.co/QmGDNJs
I am very new to this my code it prob really sloppy but Im just searching around and trying to piece together something that works our of trial and error.
Hello, just wondering if you ever were able to work out the issues you were having with this study?
 
@bottomphishing
@poutses
This is a older thread, but the first time I'm seeing it. So just incase, here are the fixes requested.
I did have to alter the target levels to get them to fit my chart.
EJbPsQk.png

Ruby:
# Position Size Calculator based on max % account risk with variable position riskPercent %
# Includes % targets and chart plots
# Author: @bottomphishing
# Date: 6/20/2021

declare upper;

# Input max percentage of a portfolio that one stock should be
input Max_Percent = 0.25;
input price = close;
input Extend_to_right = yes;

def netliq = GetNetLiq();


## Risk of Portfolio

def twoperc = netliq * (Max_Percent / 100);
AddLabel(yes, Max_Percent + "% ActRisk " + AsDollars(twoperc), Color.CYAN);

# Input position risk
input RiskPercent = .03 ;

# Position Size
###################################################################
#edit
def currentPrice = if IsNaN(close) then currentPrice[1] else close;
###################################################################
def dollarRisk = (currentPrice * RiskPercent);

def stopPrice = (currentPrice - dollarRisk);
def SHAREStoBUY = twoperc / (currentPrice - stopPrice);

input showlabel = yes;
AddLabel (yes, " Pos " + RoundUp ((SHAREStoBUY), 0), Color.CYAN);

AddLabel (yes, AsPercent(RiskPercent) + " Risk " + AsDollars (currentPrice - (currentPrice * RiskPercent)), Color.VIOLET);

AddLabel (yes, " Cap " + AsDollars (SHAREStoBUY * currentPrice), Color.CYAN);

#############################################################################
#edit
def bn = BarNumber();
def BarNum = if !IsNaN(close) and BarNumber() > 0 then bn else BarNum[1];
def VBar = HighestAll(BarNum);

def LastAveragePrice = fold a = 0 to AbsValue(VBar) while !IsNaN(GetValue(close, -a)) do GetValue(GetAveragePrice(), -a);
def GetAveragePrice = if IsNaN(close) then GetAveragePrice[1] else if LastAveragePrice == GetAveragePrice() then GetAveragePrice() else GetAveragePrice[1];
#############################################################################

#Position Targets
def TargetPrice = Round((GetAveragePrice * .01) + GetAveragePrice); # Change Percent to 1 to 1
def TargetPrice2 = Round((GetAveragePrice * .02) + GetAveragePrice);
def TargetPrice3 = Round((GetAveragePrice * .03) + GetAveragePrice);
def TargetPrice4 = Round((GetAveragePrice * .04) + GetAveragePrice);
def TargetPrice5 = Round((GetAveragePrice * .05) + GetAveragePrice);
def dollarStop = Round(GetAveragePrice - (GetAveragePrice * RiskPercent));

AddLabel(1, " Targets: Stop: " + AsDollars(dollarStop) + " | 3%: " + AsDollars(TargetPrice) + " | 6%: " + AsDollars(TargetPrice2) + " | 9%: " + AsDollars(TargetPrice3) + " | 50%: " + AsDollars(TargetPrice4) + " | 100%: " + AsDollars(TargetPrice5), if GetAveragePrice <= 0 then Color.Black else Color.GREEN);

####################################################################
#edit
plot s1 = if IsNaN(close[-1]) then (currentPrice - (currentPrice * RiskPercent)) else Double.NaN;
####################################################################
s1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
s1.SetDefaultColor(Color.PINK);

plot r1 = dollarStop;
r1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
r1.SetDefaultColor(Color.RED);

plot a1 = GetAveragePrice;
a1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
a1.SetDefaultColor(Color.BLUE);

plot t1 = TargetPrice;
t1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
t1.SetDefaultColor(Color.GREEN);
plot t2 = TargetPrice2;
t2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
t2.SetDefaultColor(Color.GREEN);
plot t3 = TargetPrice3;
t3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
t3.SetDefaultColor(Color.GREEN);
plot t4 = TargetPrice4;
t4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
t4.SetDefaultColor(Color.GREEN);
plot t5 = TargetPrice5;
t5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
t5.SetDefaultColor(Color.GREEN);
 
New here. Is there a way to isolate a given value of a bar, specifically the ATR of the bar previous to the bar on which a trade was executed? I'm trying to make an indicator that plots once a position is open but uses factors of the bar previous to the opening bar's ATR to plot Stops and Exits.

I hope this is reasonable place to post this.
 
New here. Is there a way to isolate a given value of a bar, specifically the ATR of the bar previous to the bar on which a trade was executed? I'm trying to make an indicator that plots once a position is open but uses factors of the bar previous to the opening bar's ATR to plot Stops and Exits.

I hope this is reasonable place to post this.
Does this move you along on your quest:
https://usethinkscript.com/threads/entries-and-target-for-thinkorswim.10302/#post-93780
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
209 Online
Create Post

Similar threads

Similar threads

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.
Back
Top