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?
Number of Bars and Sum of Close Prices within a Time Range
- Henry MultiСharts
- Posts: 9165
- Joined: 25 Aug 2011
- Has thanked: 1264 times
- Been thanked: 2958 times
Re: Number of Bars and Sum of Close Prices within a Time Ran
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.
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.
Re: Number of Bars and Sum of Close Prices within a Time Ran
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?
Re: Number of Bars and Sum of Close Prices within a Time Ran
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];
}
}
- Henry MultiСharts
- Posts: 9165
- Joined: 25 Aug 2011
- Has thanked: 1264 times
- Been thanked: 2958 times
Re: Number of Bars and Sum of Close Prices within a Time Ran
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.
Re: Number of Bars and Sum of Close Prices within a Time Ran
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]"..
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])
}
}
- Henry MultiСharts
- Posts: 9165
- Joined: 25 Aug 2011
- Has thanked: 1264 times
- Been thanked: 2958 times
Re: Number of Bars and Sum of Close Prices within a Time Ran
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];
Re: Number of Bars and Sum of Close Prices within a Time Ran
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".
- Henry MultiСharts
- Posts: 9165
- Joined: 25 Aug 2011
- Has thanked: 1264 times
- Been thanked: 2958 times
Re: Number of Bars and Sum of Close Prices within a Time Ran
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).
Further in your code the length value is modified (you already have this logic implemented).
Re: Number of Bars and Sum of Close Prices within a Time Ran
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] );
}
Re: Number of Bars and Sum of Close Prices within a Time Ran
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] );
}