Converting Moving Average line into bar/candle for the time period


New member
There's a few applications I would like to apply this too but I'm not sure that it's possible in TOS.

Basically I want TOS to remember the HLCO of a plotted moving average. So the MA is displayed as a bar or candle chart instead of just as a line chart.

I'm thinking it could be done with the addchart function which I use for a few studies, however I don't know how to have it store the HLCO of a number/study.

I haven't tried it yet, but I'm thinking of having it grab the data at certain times for the Open and Close and then maybe have it grab the highest high and the lowest low for a time period for the addchart function.

Do you think that would work, or is there a better way?


@BlueRaven I'm not sure what you mean by 'add chart function' and If such a thing exists, I would love to chart some indicators as candles.

As for storing the HLCO, if you are talking about intraday charts, here is a way to store the HLCO of each bar of a moving average:
def HI = high;
def LO = low;
def OP = open;
def CL = close;

input Length = 20;
input Average_Type = AverageType.SIMPLE;

plot LinRegH = MovingAverage(Average_Type,HI, Length);
plot LinRegL = MovingAverage(Average_Type,LO,Length);
plot LinRegC = MovingAverage(Average_Type,CL,Length);
plot LinRegO = MovingAverage(Average_Type,OP,Length);

I would like to hear more about the 'addchart' function, if it exists, that would be awesome!

Just realized this post is from 2019 LOL


Staff member
Here is some bare-bones informaton about the undocumented AddChart() function.
Here is an indicator that uses the AddChart() function.
A basic example:
14:54 Mobius: Example of AddChart()

declare lower;
# Add Chart
# Mobius
# Chat Room Request

def o = open(getUnderlyingSymbol());
def h = high(getUnderlyingSymbol());
def l = low(getUnderlyingSymbol());
def c = close(getUnderlyingSymbol());
Hope these give you some ideas of how this function can be useful. You can't break anything by playing w/it.
Last edited:


@BlueRaven I finished and it appears to be working properly😌
The following indicator charts a Moving Average's OHLC as candles, with customizable Length, and type:

#hint: Charts a Moving Average's OHLC as candles, with customizable Length, and type
#Assembled by @adii8000
#Idea by @BlueRaven
#Thanks to @MerryDay and @RobertPayne

input L_Length = 20;
input Average_Type = AverageType.SIMPLE;
input Chart_Type = ChartType.CANDLE;

def MAO = MovingAverage(Average_Type,open,L_Length);
def MAC = MovingAverage(Average_Type,close,L_Length);
def MAH = MovingAverage(Average_Type,high,L_Length);
def MAL = MovingAverage(Average_Type,low,L_Length);

def UpO;
def UpC;
def UpH;
def UpL;

#Up Bars
if MAC > MAO then {
UpO = MAO;
UpC = MAC;
UpH = MAH;
UpL = MAL;
} else {
UpO = Double.NaN;
UpC = Double.NaN;
UpH = Double.NaN;
UpL = Double.NaN;

def DownO;
def DownC;
def DownH;
def DownL;

#Down Bars
if MAC < MAO then {
DownO = MAO;
DownC = MAC;
DownH = MAH;
DownL = MAL;
} else {
DownO = Double.NaN;
DownC = Double.NaN;
DownH = Double.NaN;
DownL = Double.NaN;

#Doji Bars
def DojiO;
def DojiC;
def DojiH;
def DojiL;

if MAC == MAO then {
DojiO = MAO;
DojiC = MAC;
DojiH = MAH;
DojiL = MAL;
} else {
DojiO = Double.NaN;
DojiC = Double.NaN;
DojiH = Double.NaN;
DojiL = Double.NaN;

#Green Bars
AddChart(UpH, UpL, UpO, UpC, Chart_Type, Color.UPTICK);

#Red Bars
AddChart(DownH, DownL, DownO, DownC, Chart_Type, Color.DOWNTICK);

#Gray Bars
AddChart(DojiH, DojiL, DojiO, DojiC, Chart_Type, Color.GRAY);

#Outlines: Don't seem to work, they color all candles fully black
#AddChart(UpH, UpL, UpO, UpC, Chart_Type, Color.BLACK);
#AddChart(DownH, DownL, DownO, DownC, Chart_Type, Color.BLACK);
#AddChart(DojiH, DojiL, DojiO, DojiC, Chart_Type, Color.BLACK);

#AddChart(MAH, MAL, MAO, MAC, Chart_Type, Color.BLACK);

Note that I wasn't able to make the outlines of the bars function properly, so just turned them on. (it's only superficial anyway)

@MerryDay, thanks 4 teaching me some cool stuff!

Edit: tiny bug in the code for customizing average length, but fixed now.
Last edited:

Similar threads