CloseD function not functioning as expected

Questions about MultiCharts and user contributed studies.
albevier
Posts: 24
Joined: 13 Nov 2005
Has thanked: 5 times
Been thanked: 3 times

CloseD function not functioning as expected

Postby albevier » 25 Dec 2020

MC v14.0 (Build 20935)

I expected CloseD to return the previous day's close on my intraday charts in order to plot Daily Moving Averages without inserting an invisible daily chart of the same instrument

Using just CloseD, however, all time frames plot the 20ema of that particular time frame. I did not expect that.

If CloseD is working as designed, what am I doing wrong?

Code: Select all

[IntrabarOrderGeneration = False] inputs: LELength( 20 ) ; variables: LEvar7( 0 ), // Plot the Daily 20ema LEvar7 = XAverage( closeD(1), LELength ) ; plot1(LEvar7, "20ema Daily");

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: CloseD function not functioning as expected

Postby TJ » 25 Dec 2020

The CloseD function's description:

CloseD (Series Function)

The CloseD function allows you to reference the daily Close of previous days in an intraday chart (minute, second, or tick-based) or a daily chart.

CloseD is a function in a family of functions that allows historical daily, weekly, monthly, and yearly references in intraday charts.

Syntax
CloseD(PeriodsAgo)

Returns (Double)
A numeric value for the current bar. If the PeriodsAgo parameter is out of range (> 50), or there is not enough data, the function will return –1.

Parameters
Name
PeriodsAgo

Type
Numeric

Description
The number of days/periods back to reference a previous day’s closing price. (50 days back maximum) (0 = Today’s current Close)

Remarks
You must have enough intraday data in the chart in order to look back and reference any previous close. For example, if you want to look back at the close of 25 days ago on a 5-minute chart, you must have 25 days of 5-minute bars in the chart.

The value for the PeriodsAgo input parameter should always be a whole number greater than or equal to 0, but less than 51. Setting PeriodsAgo to 0 equals today’s current Close.

Example
Assigns the Close of the previous day on an intraday chart to Value1, then plots Value1:

Value1 = CloseD(1);

Plot1(Value1, "PrevClose");

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: CloseD function not functioning as expected

Postby TJ » 25 Dec 2020

MC v14.0 (Build 20935)

I expected CloseD to return the previous day's close on my intraday charts in order to plot Daily Moving Averages without inserting an invisible daily chart of the same instrument

Using just CloseD, however, all time frames plot the 20ema of that particular time frame. I did not expect that.

If CloseD is working as designed, what am I doing wrong?

Code: Select all

[IntrabarOrderGeneration = False] inputs: LELength( 20 ) ; variables: LEvar7( 0 ), // Plot the Daily 20ema LEvar7 = XAverage( closeD(1), LELength ) ; plot1(LEvar7, "20ema Daily");
What is your chart resolution?

Please post a screenshot of your chart.

Please write notes on the chart to point out the "error".
Please write notes on the chart to illustrate your expectation.

albevier
Posts: 24
Joined: 13 Nov 2005
Has thanked: 5 times
Been thanked: 3 times

Re: CloseD function not functioning as expected

Postby albevier » 26 Dec 2020

Thanks TJ;
I see the page from the Easylanguage manual and that is what I expected -- closed to use the daily close. When I use closed on the 30 minute intraday, however, closed used the close from the 30 minutes bars. I figure there is more to getting closed to work but I can't find any info that directs me to anything more.

FYI, I edited the code to use standard nomenclature for variables etc and changed the closed(1) to closed(0) to make it easier to illustrate my question.

I've attached two annotated screen shots and a video. The screen shots should be enough but video can be much more precise. Below is the edited code. Ooooops! Nope...a video is not an acceptable file type. Hope the screen shots tell the story.

Code: Select all

[IntrabarOrderGeneration = False] inputs: Length( 20 ) ; variables: var1( 0 ); // Plot the Daily 20ema var1 = XAverage(closeD(0), Length ) ; plot1(var1, "20ema Daily");
Attachments
Daily Chart.PNG
(66.02 KiB) Not downloaded yet
30 Minute Chart.PNG
(93.74 KiB) Not downloaded yet

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: CloseD function not functioning as expected

Postby TJ » 26 Dec 2020

Have you tried plotting the CloseD on the 30 min chart?
(without the xavg)

Have you tried plotting the CloseD with average instead of xavg?

albevier
Posts: 24
Joined: 13 Nov 2005
Has thanked: 5 times
Been thanked: 3 times

Re: CloseD function not functioning as expected

Postby albevier » 26 Dec 2020

I have tried several variations of CloseD and Average. Plotting CloseD(1) on the 30min chart using the code from the example (see below) produces the expected stair step graph with the correct closes for the previous days.

Here's a table of the results.

I did not include AverageFC as they were the same as Average. Note that XAverage with CloseD(0) gave wildly different graph than XAverage with CloseD(1) -- the difference cannot be explained but the fact that the two plots are a day apart.
Attachments
CloseD Truth Table.pdf
(53.85 KiB) Downloaded 106 times

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: CloseD function not functioning as expected

Postby TJ » 27 Dec 2020

You pdf makes no sense to me.
You need to draw diagrams.

Anyway, your code is not going to give you daily averages,
because your calculation is based on 30 min bars.

You can try 600 as your average input. ( 30 bars in a day X 20 periods )

(I am not at my work station, I can't test this for you.)

albevier
Posts: 24
Joined: 13 Nov 2005
Has thanked: 5 times
Been thanked: 3 times

Re: CloseD function not functioning as expected

Postby albevier » 27 Dec 2020

It's a Holiday weekend, TJ, so I'm surprised (and very pleased) to have received any response. Nor would I expect you to test something like this for me. So no worries.

From your comment, that my use of CloseD in my "...code is not going to give you daily averages, because your calculation is based on 30 min bars." tells me that I don't understand the use of CloseD.

I expected CloseD() to allow me to assign the previous day's Close to a variable which I could then use to plot daily moving averages on a 30 min chart.

Apparently I'm not interpreting the description correctly. If you're aware of a function, native to MC or custom made, that will allow me to plot daily closes on a 30 min chart, let me know. If not, I suppose I can write my own DLL though my C skills are minimal. In the meantime I can use Tradingview.

Thanks again for responding over the holidays, TJ.


Return to “MultiCharts”