First, this is my first post in this forum, I hope this is done in the correct manner. If not, feel free to let me know.
I am trying to create an indicator that subtracts one data series from another. I then tried to creat Bollinger Bands on that spread but for some reason both the upper / Lower bands takes in the value from the primary series.
Can anyone tell me how I should be approaching this? I wanted to test out the bands before I added an average on the spread.
This is how the chart looks like, it is the panel in the bottomn that shows the indicator:
This is the code I am using.
// Wizard generated variables
private double primary = 0; // Default setting for Primary
private double secondary = 1; // Default setting for Secondary
//create DataSeries to hold values
private DataSeries PrimarySeries;
private DataSeries SecondarySeries;
//to hold result dataseries
private DataSeries ResultSeries;
// User defined variables (add any user defined variables below)
#endregion
/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
Add(new Plot(Color.FromKnownColor(KnownColor.Desktop), PlotStyle.Line, "Result"));
Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "UpperBoil"));
Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Line, "LowerBoil"));
Overlay = false;
Add("CL 12-15", PeriodType.Second,1);
PrimarySeries = new DataSeries(this);
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Use this method for calculating your indicator values.
if (SecondarySeries == null)
{
/* Syncs another DataSeries object to the secondary bar object.
We use an arbitrary indicator overloaded with an IDataSeries input to achieve the sync.
The indicator can be any indicator. The DataSeries will be synced to whatever the
BarsArray[] is provided.*/
SecondarySeries = new DataSeries(SMA(BarsArray[1], 50));
}
if (ResultSeries == null)
{
ResultSeries = new DataSeries(this);
}
//Checks that there are enough bars
if (CurrentBars[0] < BarsRequired || CurrentBars[1] < BarsRequired)
return;
//Executed on primary bar updates only
if (BarsInProgress == 0)
{
// Set DataSeries object to store the trading range of the primary bar
PrimarySeries.Set(Close[0]);
}
if (BarsInProgress == 1)
{
// Set the DataSeries object to store the trading range of the secondary bar
SecondarySeries.Set(Close[0]);
}
if(SecondarySeries[0] > 0 && PrimarySeries[0] > 0)
{
ResultSeries.Set(SecondarySeries[0] - PrimarySeries[0]);
Result.Set(ResultSeries[0]);
UpperBoil.Set(Bollinger(ResultSeries[0],180).Upper[0]);
LowerBoil.Set(Bollinger(ResultSeries[0],180).Low[0]);
}
Comment