How to automatically plot a price level line for "2 weeks lowest Price" minus 5 cents?

ronviv1

New member
After I buy a stock, I would like to use "10 day lowest price" (2 weeks lowest price) minus specified amount as a stop loss.

My Risk(R) = Purchase Price - (Purchase Price - (10 day lowest price - a few cents) and
my Target Price (2R) = Purchase Price + (2 * (Purchase Price - 10 day lowest price)

Please help me with automatically plotting a price level line for "Stop Loss = (10 day lowest price - a few cents) on the chart and also, a price level line for the target price.

Thank you.
 
Last edited:
Solution
Thank you everyone for your help.

After searching several times, I came across following link which helped me fulfill my need.

https://usethinkscript.com/threads/high-low-horizontal-lines.7934/page-2#posts

However, there is still one problem.

Please help me getting the "price" listed on this horizontal price level. Thank you.

Here is the code with modification to plot 2-week lowest price level horizontal line:

@ronviv1 You did fine. Just change weekLow to LowestALL.

Ruby:
plot weeklow = LowestAll( if (!IsNaN(close) and IsNaN(close[-1])) then l1w else Double.NaN);
Code:
input sPeriod = {default DAY};

#use this line for lowest low;
def Day_Low = low(period = sPeriod);

#use this line for lowest close;
def Day_Low = close(period = sPeriod);

#Lowest value selected above, over the last 10 days excluding current day.
def Ten_Day_Low = fold i=1 to 10 with DL=Day_Low[1] do if DL<Day_Low[i] then DL else Day_Low[i];

#My Risk(R) = Purchase Price - (Purchase Price-(10 day lowest price - a few cents)
plot Stop_Loss = GetAveragePrice()-(GetAveragePrice()-(Ten_Day_Low-.05));#5 cents
Stop_Loss.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

#Target Price (2R) = Purchase Price+ (2 * (Purchase Price-10 day lowest price)
plot Target_Price = GetAveragePrice()+(2*(GetAveragePrice()-Ten_Day_Low));
Target_Price.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
 
Last edited:
Thank you @Svanoy and @SuryaKiranC for your prompt response.

Two problems:
1: Stop Loss line is plotted for past all days. so instead of plotting one horizontal line, it looks like a continuous line for all previous days.
Is there anyway to limit the "Stop Loss" line to the last 2 weeks and extend it to the right?
Also, instead of predefined 10 days, is there anyway to give an option to input "number of days"?

2: For target price, there will be a need to enter the purchase price. Without adding purchase price, it cannot plot target price.

Thank you again.
 
Last edited:
GetAveragePrice() returns the price at which you bought.
If GetAveragePrice() returns 0 then the plots go negative.
If you want to manually enter this then add:

input Purchase_Price = <Your Purchase Price Here>;
and change every instance of GetAveragePrice() to Purchase_Price.

Code:
input sPeriod = {default DAY};
input Purchase_Price = <Your Purchase Price Here>;
input Number_of_Days = 10;

def Today = if GetDay()==GetLastDay() then 1 else 0;
def Day_Low = low(period = sPeriod);
def Ten_Day_Low = fold i=1 to Number_of_Days with DL=Day_Low[1] do if DL<Day_Low[i] then DL else Day_Low[i];

#My Risk(R) = Purchase Price - (Purchase Price - (10 day lowest price - a few cents)
#Plots to the right of close only.
def Stop_Loss = Purchase_Price-(Purchase_Price-(Ten_Day_Low-.05));#5 cents;

#Target Price (2R) = Purchase Price + (2 * (Purchase Price - 10 day lowest price)
def Target_Price = Purchase_Price+(2*(Purchase_Price-Ten_Day_Low));

plot SL = If Purchase_Price>0 and Today then Stop_Loss else Double.NaN;
SL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot TP = If Purchase_Price>0 and Today then Target_Price else Double.NaN;
TP.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
 
Last edited:
GetAveragePrice() returns the price at which you bought.
If GetAveragePrice() returns 0 then the plots go negative.
If you want to manually enter this then add:

input Purchase_Price = <Your Purchase Price Here>;
and change every instance of GetAveragePrice() to Purchase_Price.

Code:
input sPeriod = {default DAY};
input Purchase_Price = <Your Purchase Price Here>;
input Number_of_Days = 10;

def Today = if GetDay()==GetLastDay() then 1 else 0;
def Day_Low = low(period = sPeriod);
def Ten_Day_Low = fold i=1 to Number_of_Days with DL=Day_Low[1] do if DL<Day_Low[i] then DL else Day_Low[i];

#My Risk(R) = Purchase Price - (Purchase Price - (10 day lowest price - a few cents)
#Plots to the right of close only.
def Stop_Loss = Purchase_Price-(Purchase_Price-(Ten_Day_Low-.05));#5 cents;

#Target Price (2R) = Purchase Price + (2 * (Purchase Price - 10 day lowest price)
def Target_Price = Purchase_Price+(2*(Purchase_Price-Ten_Day_Low));

plot SL = If Stop_Loss>0 and Today then Stop_Loss else Double.NaN;
SL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot TP = If Target_Price>0 and Today then Target_Price else Double.NaN;
TP.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Sorry, I think, I have confused the matter. Because the study will apply to other charts of stocks that are not bought. So there is no purchase price to add. This causes the problem with the charts of the stocks that are not bought.

Let me try to simplify.

I started with the idea of just plotting a horizontal line for the 14 day lowest price level which extends to the right side beyond the current day. (and then unnecessarily went in the confusing the matter by adding stop loss and target).

Can you please help with
"plotting a horizontal line for the 14 day lowest price level which extends to the right side beyond the current day"

Thank you.
 
Last edited:
Thank you everyone for your help.

After searching several times, I came across following link which helped me fulfill my need.

https://usethinkscript.com/threads/high-low-horizontal-lines.7934/page-2#posts

However, there is still one problem.

Please help me getting the "price" listed on this horizontal price level. Thank you.

Here is the code with modification to plot 2-week lowest price level horizontal line:


Code:
#Show/hide aggregation periods

input show1w  = yes;
input offset = 1;


### 1w timeframe ###

def tf1w = AggregationPeriod.WEEK;
def valid1w = GetAggregationPeriod() <= AggregationPeriod.WEEK;
def h1w;
def l1w;


if valid1w and show1w {
    h1w = high (period = tf1w)[offset];
    l1w = low  (period = tf1w)[offset];

} else {
    h1w = Double.NaN;
    l1w = Double.NaN;
}

plot weeklow = HighestAll( if (!IsNaN(close) and IsNaN(close[-1])) then l1w else Double.NaN);
plot weekhigh = HighestAll( if (!IsNaN(close) and IsNaN(close[-1])) then h1w else Double.NaN);

weekhigh.SetDefaultColor(Color.WHITE);
weekhigh.SetStyle(Curve.SHORT_DASH);
weekhigh.SetLineWeight(1);


weeklow.SetDefaultColor(Color.WHITE);
weeklow.SetStyle(Curve.SHORT_DASH);
weeklow.SetLineWeight(1);
 
Last edited:
Thank you everyone for your help.

After searching several times, I came across following link which helped me fulfill my need.

https://usethinkscript.com/threads/high-low-horizontal-lines.7934/page-2#posts

However, there is still one problem.

Please help me getting the "price" listed on this horizontal price level. Thank you.

Here is the code with modification to plot 2-week lowest price level horizontal line:

@ronviv1 You did fine. Just change weekLow to LowestALL.

Ruby:
plot weeklow = LowestAll( if (!IsNaN(close) and IsNaN(close[-1])) then l1w else Double.NaN);
 
Solution

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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