Study Plotting Under Every Bar

bottomphishing

New member
Having trouble with my code this section is plotting under every bar no matter the time frame any ideas?:

plot s1 = if IsNaN(currentPrice) then Double.NaN else (currentPrice - (currentPrice * RiskPercent));
s1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
s1.SetDefaultColor(Color.PINK);

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 = .05 ;

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

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

input showlabel = yes;

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

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

AddLabel (yes, " Pos " + RoundUp ((SHAREStoBUY), 0), Color.LIGHT_GREEN);

#Position Targets
def GetAveragePrice = GetAveragePrice();
def TargetPrice = Round((GetAveragePrice * .05) + GetAveragePrice);
def TargetPrice2 = Round((GetAveragePrice * .10) + GetAveragePrice);
def TargetPrice3 = Round((GetAveragePrice * .20) + GetAveragePrice);
def TargetPrice4 = Round((GetAveragePrice * .50) + GetAveragePrice);
def TargetPrice5 = Round((GetAveragePrice * 1.00) + GetAveragePrice);
def dollarStop = Round(GetAveragePrice - (GetAveragePrice * RiskPercent));

AddLabel (yes, "Stop " + AsDollars(dollarStop), Color.LIGHT_RED);
AddLabel (yes, "5% " + AsDollars(TargetPrice), if GetAveragePrice <= 0 then Color.Black else Color.LIGHT_GREEN);
AddLabel (yes, "10% " + AsDollars(TargetPrice2), if GetAveragePrice <= 0 then Color.Black else Color.LIGHT_GREEN);
AddLabel (yes, "20% " + AsDollars(TargetPrice3), if GetAveragePrice <= 0 then Color.Black else Color.LIGHT_GREEN);
# AddLabel (yes, "50% " + AsDollars(TargetPrice4), if GetAveragePrice <= 0 then Color.Black else Color.LIGHT_GREEN);
# AddLabel (yes, "100% " + AsDollars(TargetPrice5), if GetAveragePrice <= 0 then Color.Black else Color.GREEN);

plot s1 = if IsNaN(currentPrice) then Double.NaN else (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 by a moderator:
Solution
So I need it to test of the current bar is close or not to calculate on the current open bar?
This test determining the cruuent bar,IsNaN(close[-1]), will limit the plot to the current bar. I added s1ext to extend the line to the right edge, but you can limit it to just the current bar by substituting (currentPrice - (currentPrice * RiskPercent)) for s1ext. You can derive other similar tests to limit the plot.

Ruby:
def s1ext = if IsNaN(close) then s1ext[1] else (currentPrice - (currentPrice * RiskPercent));
plot s1   = if IsNaN(close[-1]) then s1ext else Double.NaN;
you didn't say what you want to have happen, so i can't tell you how to get there.
that plot statement has 1 condition
...if IsNaN(currentPrice) then Double.NaN
to test if close is not a valid price. if close has a number, then it plots, so it plots on all the bars, and not after the last bar.
if you want it to plot on only some of the bars, you will need a formula that tests for some condition.
 

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

So I need it to test of the current bar is close or not to calculate on the current open bar?
This test determining the cruuent bar,IsNaN(close[-1]), will limit the plot to the current bar. I added s1ext to extend the line to the right edge, but you can limit it to just the current bar by substituting (currentPrice - (currentPrice * RiskPercent)) for s1ext. You can derive other similar tests to limit the plot.

Ruby:
def s1ext = if IsNaN(close) then s1ext[1] else (currentPrice - (currentPrice * RiskPercent));
plot s1   = if IsNaN(close[-1]) then s1ext else Double.NaN;
 
Solution
This test determining the cruuent bar,IsNaN(close[-1]), will limit the plot to the current bar. I added s1ext to extend the line to the right edge, but you can limit it to just the current bar by substituting (currentPrice - (currentPrice * RiskPercent)) for s1ext. You can derive other similar tests to limit the plot.
@bottomphishing @SleepyZ I'm having some issues with this, maybe you can shed some light or provide an updated version? I can't seem to get any of the plot lines to work except for s1. Everything else just bottoms out to 0 because the stop price always remains at $0.

For some stocks it doesn't show anything except s1.

Screen-Shot-2022-06-10-at-8-42-30-AM.png


For other stocks it draws the other lines at the far left.

Screen-Shot-2022-06-10-at-8-49-18-AM.png


Below is the code:

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 = .05 ;

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

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

input showlabel = yes;

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

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

AddLabel (yes, " Pos " + RoundUp ((SHAREStoBUY), 0), Color.LIGHT_GREEN);

#Position Targets
def GetAveragePrice = GetAveragePrice();
def TargetPrice = Round((GetAveragePrice * .05) + GetAveragePrice);
def TargetPrice2 = Round((GetAveragePrice * .10) + GetAveragePrice);
def TargetPrice3 = Round((GetAveragePrice * .20) + GetAveragePrice);
def TargetPrice4 = Round((GetAveragePrice * .50) + GetAveragePrice);
def TargetPrice5 = Round((GetAveragePrice * 1.00) + GetAveragePrice);
def dollarStop = Round(GetAveragePrice - (GetAveragePrice * RiskPercent));

AddLabel (yes, "Stop " + AsDollars(dollarStop), Color.LIGHT_RED);
AddLabel (yes, "5% " + AsDollars(TargetPrice), if GetAveragePrice <= 0 then Color.Black else Color.LIGHT_GREEN);
AddLabel (yes, "10% " + AsDollars(TargetPrice2), if GetAveragePrice <= 0 then Color.Black else Color.LIGHT_GREEN);
AddLabel (yes, "20% " + AsDollars(TargetPrice3), if GetAveragePrice <= 0 then Color.Black else Color.LIGHT_GREEN);
# AddLabel (yes, "50% " + AsDollars(TargetPrice4), if GetAveragePrice <= 0 then Color.Black else Color.LIGHT_GREEN);
# AddLabel (yes, "100% " + AsDollars(TargetPrice5), if GetAveragePrice <= 0 then Color.Black else Color.GREEN);

def s1ext = if IsNaN(close) then s1ext[1] else (currentPrice - (currentPrice * RiskPercent));
plot s1 = if IsNaN(close[-1]) then s1ext 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);
 
@bottomphishing @SleepyZ I'm having some issues with this, maybe you can shed some light or provide an updated version? I can't seem to get any of the plot lines to work except for s1. Everything else just bottoms out to 0 because the stop price always remains at $0.

For some stocks it doesn't show anything except s1.

Screen-Shot-2022-06-10-at-8-42-30-AM.png


For other stocks it draws the other lines at the far left.

Screen-Shot-2022-06-10-at-8-49-18-AM.png


Below is the code:

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 = .05 ;

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

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

input showlabel = yes;

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

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

AddLabel (yes, " Pos " + RoundUp ((SHAREStoBUY), 0), Color.LIGHT_GREEN);

#Position Targets
def GetAveragePrice = GetAveragePrice();
def TargetPrice = Round((GetAveragePrice * .05) + GetAveragePrice);
def TargetPrice2 = Round((GetAveragePrice * .10) + GetAveragePrice);
def TargetPrice3 = Round((GetAveragePrice * .20) + GetAveragePrice);
def TargetPrice4 = Round((GetAveragePrice * .50) + GetAveragePrice);
def TargetPrice5 = Round((GetAveragePrice * 1.00) + GetAveragePrice);
def dollarStop = Round(GetAveragePrice - (GetAveragePrice * RiskPercent));

AddLabel (yes, "Stop " + AsDollars(dollarStop), Color.LIGHT_RED);
AddLabel (yes, "5% " + AsDollars(TargetPrice), if GetAveragePrice <= 0 then Color.Black else Color.LIGHT_GREEN);
AddLabel (yes, "10% " + AsDollars(TargetPrice2), if GetAveragePrice <= 0 then Color.Black else Color.LIGHT_GREEN);
AddLabel (yes, "20% " + AsDollars(TargetPrice3), if GetAveragePrice <= 0 then Color.Black else Color.LIGHT_GREEN);
# AddLabel (yes, "50% " + AsDollars(TargetPrice4), if GetAveragePrice <= 0 then Color.Black else Color.LIGHT_GREEN);
# AddLabel (yes, "100% " + AsDollars(TargetPrice5), if GetAveragePrice <= 0 then Color.Black else Color.GREEN);

def s1ext = if IsNaN(close) then s1ext[1] else (currentPrice - (currentPrice * RiskPercent));
plot s1 = if IsNaN(close[-1]) then s1ext 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);

This study seems to populate when you actually enter a trade as it uses getaverageprice(), which is the price when a trade is entered. So go to simulated trading (paper trading versioon of TOS) and enter a trade and see if it works then.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
292 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