Line connecting average of candle open and close

vasiche

New member
Hello, I hope somebody can help with a simple script. Please let me know if this is not the right place to ask.

I am looking to have a line on my chart that will connect the average of open/close of each candle. So it will take, for each candle, [open + close]/2 and connect those points for candles on the chart.

I intend to use this line to help me visualize magnitude and direction of an index (e.g., SPY, SPX) movement. This will ultimately assist in determining what options trading strategy I would like to apply (credit spreads, iron condors, iron flies, etc.).

Thank you!
 
Hello, I hope somebody can help with a simple script. Please let me know if this is not the right place to ask.

I am looking to have a line on my chart that will connect the average of open/close of each candle. So it will take, for each candle, [open + close]/2 and connect those points for candles on the chart.

I intend to use this line to help me visualize magnitude and direction of an index (e.g., SPY, SPX) movement. This will ultimately assist in determining what options trading strategy I would like to apply (credit spreads, iron condors, iron flies, etc.).

Thank you!
Try using a Heikin Ashi candle, Each candle opens with that same formula.
 

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

Hello, I hope somebody can help with a simple script. Please let me know if this is not the right place to ask.

I am looking to have a line on my chart that will connect the average of open/close of each candle. So it will take, for each candle, [open + close]/2 and connect those points for candles on the chart.

I intend to use this line to help me visualize magnitude and direction of an index (e.g., SPY, SPX) movement. This will ultimately assist in determining what options trading strategy I would like to apply (credit spreads, iron condors, iron flies, etc.).

Thank you!

how many bars for the average?
what kind of average ?
will post something in 30...
 
@Fluideng , good point but I completely forgot to mention that I am already using Heikin Ashi candles so I am looking for the averages line for such candles (not regular ones).
 
Last edited:
how many bars for the average?
what kind of average ?
will post something in 30...
@halcyonguy, just 1 bar for the average, but would appreciate if it's possible to change than in the code (from 1 bar to X bars) for testing as I don't know which number of bars may be best. If it's too much logic/coding, then 1 bar is good enough!

Also, I am using Heikin Ashi candles (forgot to mention that) so is it possible to do that based on Heikin Ashi candle open/close? I don't know if TOS automatically detects that when a study (i.e., this averages line) is added to the chart or if the script needs to indicate that these are Heikin Ashi candles?

For example (made up numbers):
Heikin Ashi candle 1: open = 100, close 102. Average (100 + 102) / 2 = 101
Hekin Ashi candle 2: open = 101, close 103. Average (101 + 103) / 2 = 102
Heikin Ashi candle 3: open = 102, close = 103. Average (102 + 103) / 2 = 102.5
...
So the line would connect points on candles 1-3: 101 to 102 to 102.5.

(I put example of 3 candles to save space here, but the line would cover the entire chart time frame and work on various time frames for candles, e.g., 1 min, 5 min, 10 min.)

I am not sure if this is asking too much coding. Please let me know if so!

Thank you!
 
EDIT
don't use this code. jump down to post #10 and use that
https://usethinkscript.com/threads...age-of-candle-open-and-close.7139/#post-68918


i don't use Heikin Ashi candles too often, but i imagine this will plot as you want.
you can go to study settings and
...change average length. default is 1
...change average type. default is simple
...show the line values , under the candles

Ruby:
# midline_01

def mid = (open + close)/2;

input avg1_len = 1;
input avg1_type =  AverageType.simple;
def ma1 = MovingAverage(avg1_type, mid, avg1_len);

plot avg = ma1;
avg.SetDefaultColor(Color.yellow);
# avg.setlineweight(1);
# avg.hidebubble();

input show_line_value = no;
plot v = ma1;
v.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
v.SetDefaultColor(Color.gray);
#

A0C5wBc.jpg
 
Last edited:
@halcyonguy , thank you for the script! It looks like it is not taking Heikin Ashi candles into account so it is painting the line using regular candle values. Is there a way, if you able to and have time, to convert open and close values to be those of Heikin Ashi candles?

Heikin Ashi candles are drawn as follows: https://corporatefinanceinstitute.com/resources/knowledge/trading-investing/heikin-ashi-technique/

Current Heikin Ashi (HA) candle open =[HA Open (-1) + HA Close (-1) ] / 2
Current Heikin Ashi (HA) candle close = [Open(0) + High(0) + Low(0) + Close(0)] / 4

From another website:

1. The Heikin-Ashi Close is simply an average of the open, high, low and close for the current period.

HA-Close = (Open(0) + High(0) + Low(0) + Close(0)) / 4

2. The Heikin-Ashi Open is the average of the prior Heikin-Ashi candlestick open plus the close of the prior Heikin-Ashi candlestick.

HA-Open = (HA-Open(-1) + HA-Close(-1)) / 2

My guess, some variables may need to be defined with the formulas above and then those variables used in the "def mid" statement in the script?
 
yes, i made it on a normal chart. i was hoping that if this study was used on a Heikin Ashi chart, it would work, that the open and close used for HA candles would be used in the study, and draw the line as desired. did you try it ?
 
@halcyonguy, I added and tried the script/study and on Heikin Ashi candles, it doesn't seem to be using their values and uses normal candle values. I am attaching a screen print (changed line color on it) and the line goes to the top of Heikin Ashi candles which is actually how the open of such candles is determined (using the formulas from above).

 
@halcyonguy, I added and tried the script/study and on Heikin Ashi candles, it doesn't seem to be using their values and uses normal candle values. I am attaching a screen print (changed line color on it) and the line goes to the top of Heikin Ashi candles which is actually how the open of such candles is determined (using the formulas from above).

Try this:



Code:
def haclose = (open + high + low + close) / 4;
def haopen = CompoundValue(1, (haopen[1] + haclose[1]) / 2, (open[1] + close[1]) / 2);

def mid = (haopen + haclose)/2;

input avg1_len = 1;
input avg1_type =  AverageType.simple;
def ma1 = MovingAverage(avg1_type, mid, avg1_len);

plot avg = ma1;
avg.SetDefaultColor(Color.yellow);
# avg.setlineweight(1);
# avg.hidebubble();

input show_line_value = no;
plot v = ma1;
v.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
v.SetDefaultColor(Color.gray);
#
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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