Number of Bars and Sum of Close Prices within a Time Range

Questions about MultiCharts .NET and user contributed studies.
Jobauma
Posts: 113
Joined: 16 Apr 2013
Has thanked: 25 times
Been thanked: 6 times

Number of Bars and Sum of Close Prices within a Time Range

Postby Jobauma » 31 May 2013

I have been trying to figure out how you get the number of bars and sum of close prices within a time range, but I'm pretty much clueless about where to start.

I stumbled up on the "TimeSpan" key word, so I'm sure it has to be part of the code I guess.

Where's a good place to start?

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Number of Bars and Sum of Close Prices within a Time Ran

Postby Henry MultiСharts » 03 Jun 2013

Hello Jobauma,

Please describe in more details what exactly you are trying to do and what is your final goal.
Provide an example of the calculations you want to have done.

Jobauma
Posts: 113
Joined: 16 Apr 2013
Has thanked: 25 times
Been thanked: 6 times

Re: Number of Bars and Sum of Close Prices within a Time Ran

Postby Jobauma » 03 Jun 2013

I realized that I had to use Bars.CurrentBar, combine that with TimeSpan and some subtraction to get the number of bars within a set time frame. But what about the average? On last bar use the number of bars and carry it on to AverageFC.length, or so I though.. I would love it if the time frame decides. Lambda seemed to be the answer, but it doesn't look like AverageFC handles that very well. Is a function the answer I'm looking for?

Jobauma
Posts: 113
Joined: 16 Apr 2013
Has thanked: 25 times
Been thanked: 6 times

Re: Number of Bars and Sum of Close Prices within a Time Ran

Postby Jobauma » 03 Jun 2013

Here's an example..

Code: Select all

private TimeSpan begTime = new TimeSpan(12,0,0);
private TimeSpan endTime = new TimeSpan(13,0,0);

protected override void CalcBar()
{
TimeSpan curTOD = Bars.Time[0].TimeOfDay;

if ( curTOD == begTime ) { m_1stBar.Value = Bars.CurrentBar; }
if ( curTOD == endTime )
{
m_LstBar.Value = Bars.CurrentBar;
m_Length.Value = m_LstBar[0] - m_1stBar[0];
}
}

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Number of Bars and Sum of Close Prices within a Time Ran

Postby Henry MultiСharts » 04 Jun 2013

Number of bars should not be used as AverageFC.length. In this case your study will be in a loop and will never be calculated. That is still not clear what your final goal is and what exactly you are trying to calculate. Please provide an example of the calculations you want to have done. You can also draw arrows and annotations on a chart to show what you want.

Jobauma
Posts: 113
Joined: 16 Apr 2013
Has thanked: 25 times
Been thanked: 6 times

Re: Number of Bars and Sum of Close Prices within a Time Ran

Postby Jobauma » 04 Jun 2013

This is basically what I'm trying to achieve. :)

The length is calculated by endTime and begTime, and the average value is plotted at endTime. Later on I want to be able to adjust the time with inputs. The main issue is "m_Average.length = m_Length[0]"..

Code: Select all

private AverageFC m_Average;

public VariableSeries<int> m_1stBar;
public VariableSeries<int> m_LstBar;
public VariableSeries<int> m_Length;

private IPlotObject p_Average;

private TimeSpan begTime1 = new TimeSpan(1,0,0);
private TimeSpan endTime1 = new TimeSpan(2,0,0);
private TimeSpan begTime2 = new TimeSpan(13,0,0);
private TimeSpan endTime2 = new TimeSpan(14,0,0);

protected override void Create()
{
m_Average = new AverageFC(this);

m_1stBar = new VariableSeries<int>(this);
m_LstBar = new VariableSeries<int>(this);
m_Length = new VariableSeries<int>(this);

p_Average = AddPlot( new PlotAttributes( "Average", EPlotShapes.Point, Color.Black, Color.Empty, 1, 0, false ) );
}

protected override void StartCalc()
{
m_Average.price = new Lambda<double>( barIndex => Bars.Close[barIndex] );
m_Average.length = m_Length[0];
}

protected override void CalcBar()
{
TimeSpan curTOD = Bars.Time[0].TimeOfDay;

if ( curTOD == begTime1 ) { m_1stBar.Value = Bars.CurrentBar; }
else if ( curTOD == endTime1 )
{
m_LstBar.Value = Bars.CurrentBar;
m_Length.Value = m_LstBar[0] - m_1stBar[0];
p_Average.Set(m_Average[0])
}
else if ( curTOD == begTime2 ) { m_1stBar.Value = Bars.CurrentBar; }
else if ( curTOD == endTime2 )
{
m_LstBar.Value = Bars.CurrentBar;
m_Length.Value = m_LstBar[0] - m_1stBar[0];
p_Average.Set(m_Average[0])
}
}

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Number of Bars and Sum of Close Prices within a Time Ran

Postby Henry MultiСharts » 05 Jun 2013

Please modify your StartCalc method the following way:

Code: Select all

protected override void StartCalc()
{
m_Average.price = new Lambda<double>( barIndex => Bars.Close[barIndex] );
m_Length.Value = 10; // Start Length for Average
m_Average.length = m_Length[0];

Jobauma
Posts: 113
Joined: 16 Apr 2013
Has thanked: 25 times
Been thanked: 6 times

Re: Number of Bars and Sum of Close Prices within a Time Ran

Postby Jobauma » 05 Jun 2013

Hmm.. This setup ensures m_Average.length to always be 10 I think. As far as I noticed "m_Length.Value = m_LstBar[0] - m_1stBar[0]" did not end up in "m_Average.length".

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Number of Bars and Sum of Close Prices within a Time Ran

Postby Henry MultiСharts » 06 Jun 2013

Length =10 is required for initial study calculcation to avoid division by zero, it is not always 10.
Further in your code the length value is modified (you already have this logic implemented).

Jobauma
Posts: 113
Joined: 16 Apr 2013
Has thanked: 25 times
Been thanked: 6 times

Re: Number of Bars and Sum of Close Prices within a Time Ran

Postby Jobauma » 06 Jun 2013

At this point m_Length[0] shows the desired output, but m_Average.length is not, and it doesn't make any sense! :) I've really tried to make this work, but m_Length seems to have no impact on m_Average.. :/

Code: Select all

else if ( curTOD == endTime1 )
{
m_LstBar.Value = Bars.CurrentBar;
m_Length.Value = m_LstBar[0] - m_1stBar[0];
Output.WriteLine( "{0}, {1}, {2}", m_Length[0], m_Average.length, m_Average[0] );
}

Jobauma
Posts: 113
Joined: 16 Apr 2013
Has thanked: 25 times
Been thanked: 6 times

Re: Number of Bars and Sum of Close Prices within a Time Ran

Postby Jobauma » 07 Jun 2013

Code: Select all

else if ( curTOD == endTime1 )
{
m_LstBar.Value = Bars.CurrentBar;
m_Length.Value = m_LstBar[0] - m_1stBar[0];
m_Average.length = m_Length[0];
Output.WriteLine( "{0}", m_Average[0] );
}
The first and second value assigned to m_Average.length is kind of mixed this way. Both "10" and m_Length.Value has impact on m_Average[0].


Return to “MultiCharts .NET”