Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Cannot get the right dataseries into Upper / Lower Bollinger

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Cannot get the right dataseries into Upper / Lower Bollinger

    Hi,

    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:

    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	25.2 KB
ID:	905067

    This is the code I am using.
    #region Variables
    // 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]);
    }
    Thanks to you all.

    #2
    Hello ZAG2107,

    Thank you for your post and welcome to the NinjaTrader Support Forum.

    I am not sure why you are seeing those values. Although I would recommend calling Closes[0][0] and Closes[1][0] when calling those BarsInProgress directly. Below is my example that uses this idea:
    Code:
            #region Variables
            private DataSeries prime;
    		private DataSeries second;
    		private DataSeries result;
            #endregion
    		
            protected override void Initialize()
            {
    			Add(new Plot(Color.Blue, "Up"));
    			Add(new Plot(Color.Red, "Dn"));
    			Add(new Plot(Color.Black, "Md"));
    			
    			Add("ES 09-16", PeriodType.Minute, 1);
    			
    			prime = new DataSeries(this);
    			result = new DataSeries(this);
    			
                Overlay				= false;
            }
    		
            protected override void OnBarUpdate()
            {
                if (second == null)
    			{
    				second = new DataSeries(SMA(BarsArray[1], 50));
    			}
    			
    			if (CurrentBars[0] <= BarsRequired|| CurrentBars[1] <= BarsRequired)
    				return;
    			
    			if (BarsInProgress == 0)
    				prime[0] = Closes[0][0];
    			if (BarsInProgress == 1)
    				second[0] = Closes[1][0];
    			
    			if (prime.ContainsValue(0) && second.ContainsValue(0))
    			{
    				result[0] = Math.Abs(prime[0] - second[0]);
    				Values[0][0] = result[0];
    				Values[1][0] = Bollinger(result, 2, 20).Upper[0];
    				Values[2][0] = Bollinger(result, 2, 20).Lower[0];
    			}
            }

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by ScottWalsh, Today, 06:52 PM
    4 responses
    32 views
    0 likes
    Last Post ScottWalsh  
    Started by olisav57, Today, 07:39 PM
    0 responses
    4 views
    0 likes
    Last Post olisav57  
    Started by trilliantrader, Today, 03:01 PM
    2 responses
    19 views
    0 likes
    Last Post helpwanted  
    Started by cre8able, Today, 07:24 PM
    0 responses
    6 views
    0 likes
    Last Post cre8able  
    Started by Haiasi, Today, 06:53 PM
    1 response
    5 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Working...
    X