How to plot the average price based on the high & low of the last 5 bars?

zotz

New member
Hi all,
I trade /ES and /NQ

How do you get or plot a horizontal line for the average price based on the high & low of the last 5 bars?
Thanks
 
Solution
Here's a version that will work.

I didn't know if you wanted the average highs and lows combined (i.e. Average(H + L / 2, 5) or if you wanted them separate. This version has them separate, but you could easily modify this to just calculate the combined value.

Additionally, I don't know if you mean "prior" bars when you say "last 5 bars", so this one will include the last bar as one of the 5. It would have to be modified if you wanted to exclude the last bar and show bars 2-6 instead of 1-5.

There are also some other assumptions made, like you only want it plotted on the last 5 bars. Again, it would require modification if that's not the case, but it wasn't specified, so this is what was done.


Ruby:
# DECLARATIONS
declare upper...
Here's a version that will work.

I didn't know if you wanted the average highs and lows combined (i.e. Average(H + L / 2, 5) or if you wanted them separate. This version has them separate, but you could easily modify this to just calculate the combined value.

Additionally, I don't know if you mean "prior" bars when you say "last 5 bars", so this one will include the last bar as one of the 5. It would have to be modified if you wanted to exclude the last bar and show bars 2-6 instead of 1-5.

There are also some other assumptions made, like you only want it plotted on the last 5 bars. Again, it would require modification if that's not the case, but it wasn't specified, so this is what was done.


Ruby:
# DECLARATIONS
declare upper;


#USER INPUTS
input barsBack = 5;
input extendRight = no;


#GLOBAL COLOR DEFINITIONS
DefineGlobalColor("Green", CreateColor(0, 155, 0));
DefineGlobalColor("Red", CreateColor(225, 105, 105));
DefineGlobalColor("Gray", CreateColor(192, 192, 192));


#DEFINITIONS
def isLastBar = !IsNaN(close) and IsNaN(close[-1]);
def isTargetPeriod = !IsNaN(close) and IsNaN(close[-barsBack]);

def avgHighs = if !IsNaN(close) and IsNaN(close[-1]) then Average(high, barsBack) else avgHighs[1];
def lastAvgHigh = if isNaN(close[-barsBack]) then avgHighs[-barsBack] else Double.NaN;

def avgLows = if !IsNaN(close) and IsNaN(close[-1]) then Average(low, barsBack) else avgLows[1];
def lastAvgLow = if isNaN(close[-barsBack]) then avgLows[-barsBack] else Double.NaN;



#PLOTS
plot highLine =
    if !extendRight and IsNaN(close) then Double.NaN
    else lastAvgHigh
;

plot lowLine =
    if !extendRight and IsNaN(close) then Double.NaN
    else lastAvgLow
;

plot midline = (highLine + lowLine) / 2;


#FORMATTING
highLine.SetDefaultColor(GlobalColor("Green"));
highLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
highLine.HideTitle();
highLine.HideBubble();

lowLine.SetDefaultColor(GlobalColor("Red"));
lowLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowLine.HideTitle();
lowLine.HideBubble();

midLine.SetDefaultColor(GlobalColor("Gray"));
midLine.SetPaintingStrategy(PaintingStrategy.DASHES);
midLine.HideTitle();
midLine.HideBubble();
 
Solution

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

How can I add label to this code

UPDATED:
This works...
AddLabel(yes, Concat("midline = ", close), color.white);
 
Last edited by a moderator:
Here's a version that will work.

I didn't know if you wanted the average highs and lows combined (i.e. Average(H + L / 2, 5) or if you wanted them separate. This version has them separate, but you could easily modify this to just calculate the combined value.

Additionally, I don't know if you mean "prior" bars when you say "last 5 bars", so this one will include the last bar as one of the 5. It would have to be modified if you wanted to exclude the last bar and show bars 2-6 instead of 1-5.

There are also some other assumptions made, like you only want it plotted on the last 5 bars. Again, it would require modification if that's not the case, but it wasn't specified, so this is what was done.


Ruby:
# DECLARATIONS
declare upper;


#USER INPUTS
input barsBack = 5;
input extendRight = no;


#GLOBAL COLOR DEFINITIONS
DefineGlobalColor("Green", CreateColor(0, 155, 0));
DefineGlobalColor("Red", CreateColor(225, 105, 105));
DefineGlobalColor("Gray", CreateColor(192, 192, 192));


#DEFINITIONS
def isLastBar = !IsNaN(close) and IsNaN(close[-1]);
def isTargetPeriod = !IsNaN(close) and IsNaN(close[-barsBack]);

def avgHighs = if !IsNaN(close) and IsNaN(close[-1]) then Average(high, barsBack) else avgHighs[1];
def lastAvgHigh = if isNaN(close[-barsBack]) then avgHighs[-barsBack] else Double.NaN;

def avgLows = if !IsNaN(close) and IsNaN(close[-1]) then Average(low, barsBack) else avgLows[1];
def lastAvgLow = if isNaN(close[-barsBack]) then avgLows[-barsBack] else Double.NaN;



#PLOTS
plot highLine =
    if !extendRight and IsNaN(close) then Double.NaN
    else lastAvgHigh
;

plot lowLine =
    if !extendRight and IsNaN(close) then Double.NaN
    else lastAvgLow
;

plot midline = (highLine + lowLine) / 2;


#FORMATTING
highLine.SetDefaultColor(GlobalColor("Green"));
highLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
highLine.HideTitle();
highLine.HideBubble();

lowLine.SetDefaultColor(GlobalColor("Red"));
lowLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowLine.HideTitle();
lowLine.HideBubble();

midLine.SetDefaultColor(GlobalColor("Gray"));
midLine.SetPaintingStrategy(PaintingStrategy.DASHES);
midLine.HideTitle();
midLine.HideBubble();
ok, thank you!
 

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
435 Online
Create Post

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