Maximum and minimum

Beto

New member
Hi all. Someone could help me, since I do not have the necessary knowledge. I need to make a script that places an vertical line (red) on the chart, when the price is 2% lower than the maximum price registered in the last 5 days. Also, place an vertical line (green) when the price is 2% higher than the minimum registered in the last 5 days. I appreciate your help.-
 
Last edited:
Solution
Hi all. Someone could help me, since I do not have the necessary knowledge. I need to make a script that places an vertical line (red) on the chart, when the price is 2% lower than the maximum price registered in the last 5 days. Also, place an vertical line (green) when the price is 2% higher than the minimum registered in the last 5 days. I appreciate your help.-

here is my guess on what you are asking for


find when price crosses a high % level or a low % level, and draw a vertical line.

find the highest and lowest price, over the last 5 days
calc the range , highest - lowest
base the % price levels on a % of the range, combined with the lowest and the highest.
look for price crossing the % levels


Ruby:
# days5_hilo_vert...
Hi all. Someone could help me, since I do not have the necessary knowledge. I need to make a script that places an vertical line (red) on the chart, when the price is 2% lower than the maximum price registered in the last 5 days. Also, place an vertical line (green) when the price is 2% higher than the minimum registered in the last 5 days. I appreciate your help.-

here is my guess on what you are asking for


find when price crosses a high % level or a low % level, and draw a vertical line.

find the highest and lowest price, over the last 5 days
calc the range , highest - lowest
base the % price levels on a % of the range, combined with the lowest and the highest.
look for price crossing the % levels


Ruby:
# days5_hilo_vert

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

def lastbar = (!isnan(close) and isnan(close[-1]));
def lastbar2 = (!isnan(close[1]) and isnan(close[0]));

input within_percent = 2;
input days = 5;

def diffday = if getday() != getday()[1] then 1 else 0;

def agg = AggregationPeriod.day;
def daycls = close(period = agg);

# find a day , x days before the last bar
def xdaysbefore = if ( !isnan(daycls[-(days-1)]) and isnan(daycls[-days])) then 1 else 0;

def dayxbar1 = if (diffday and xdaysbefore) then 1 else 0;

# this works , outside of if-then
def hiz = highest( high(period = agg)[-(days-1)], days);
def loz = lowest( low(period = agg)[-(days-1)], days);
#addchartbubble(1, high*1.02, hiz, color.yellow,yes);


def hi;
def lo;
if bn == 1 or lastbar2 then {
  hi = na;
  lo = na;
} else if dayxbar1 then {
  hi = hiz;
  lo = loz;
# these don't work in if-then
#def hiz = highest( high(period = agg)[-(days-1)], days);
#def loz = lowest( low(period = agg)[-(days-1)], days);
} else {
  hi = hi[1];
  lo = lo[1];
}

input show_horz_limit_lines = yes;
plot z1 = if show_horz_limit_lines then hi else na;
plot z2 = if show_horz_limit_lines then lo else na;
z1.setdefaultcolor(color.gray);
z2.setdefaultcolor(color.gray);

# percent levels , % of the range , highest - lowest
def rng = hi-lo;

def hi_per = round(hi - (rng * (within_percent/100)) ,2);
def lo_per = round(lo + (rng * (within_percent/100)) ,2);

input show_horz_percent_lines = yes;
plot z3 = if show_horz_limit_lines then hi_per else na;
plot z4 = if show_horz_limit_lines then lo_per else na;
z3.setdefaultcolor(color.gray);
z4.setdefaultcolor(color.gray);


# check if price crosses a percent level
def hix = if (high crosses hi_per) then 1 else 0;
def lox = if (low crosses lo_per) then 1 else 0;

addverticalline(hix, "CROSS HI", color.red, Curve.SHORT_DASH);
addverticalline(lox, "CROSS LO", color.green, Curve.SHORT_DASH);

# -----------------------------------------

input test5_hi = yes;
addchartbubble(test5_hi and (high == hi), high*1.001, hi, color.green,yes);
input test6_lo = yes;
addchartbubble(test6_lo and (low == lo), low*0.999, lo, color.red,no);

# -----------------------------------------

input test1 = no;
addchartbubble(test1 and xdaysbefore, low, "xx", color.magenta,no);

input test2 = no;
addchartbubble(test2  and diffday, high, diffday, color.magenta,yes);

input test3_dayxbar1 = no;
#addchartbubble(test3 and diffday and xdaysbefore, low*0.99, "X", color.cyan,no);
addchartbubble(test3_dayxbar1 and dayxbar1, low*0.99, "X", color.cyan,no);

# show the highest value
input test4 = no;
addchartbubble(test4 , high, hi + "\n" + lo, color.magenta,yes);
#


TJX 10D 5m
5JPWf62.jpg
 
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
129 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