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

Multi DataSeries indicator

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

    Multi DataSeries indicator

    Hello,
    I am working on an indicator that shows 2 plots on a tick chart (red and blue), and that works fine. I want to add a 15 minute dataseries to the indicator to get 2 more plots based on 15M (Magenta and green). After much trial and error I am now getting all 4 plots, but the 15MIN plot only for the last day and both Magenta and Green plots are broken in places, whilst the plots based on BarsArray[0] are fine - see image attached. Am I missing something in my coding for the plots?
    Code:
    #region Variables
    //Defines the DataSeries object
    			private DataSeries myDataSeriesBear;
    			private DataSeries myDataSeriesBull;
    			private DataSeries myDataSeriesBear2nd;
    			private DataSeries myDataSeriesBull2nd;
     #endregion
    
     protected override void Initialize()
            {
                Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Line, "BslShortPeriodBear"));
    			Add(new Plot(Color.FromKnownColor(KnownColor.DarkBlue), PlotStyle.Line, "BslShortPeriodBull"));
    			Add(new Plot(Color.FromKnownColor(KnownColor.Magenta), PlotStyle.Line, "BslShortPeriodBear2nd"));
    			Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "BslShortPeriodBull2nd"));
    
    // Add a 15 minute Bars object - Bars object index = 1, as the primary data the indicator is run against set by the UI takes the index of 0
        		Add(PeriodType.Minute, 15);
    			
    			// Create a new DataSeries object and assign it to the variable - for BarsArray[0]
    			//myDataSeries declared in the ‘Variables’ region above - synced to the primary bars object
    			myDataSeriesBear = new DataSeries(this);
    			myDataSeriesBull = new DataSeries(this);
    }
    
     protected override void OnBarUpdate()
            {
    if (CurrentBar < minLookBackPeriod || CurrentBars[1] < minLookBackPeriod) return; 
    			int i = 0; 
    /* Only need to sync DataSeries objects once. We couldn't do this sync in the Initialize() method because we
    			cannot access the BarsArray property there. */
    			if (myDataSeriesBear2nd == null || myDataSeriesBull2nd == 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.*/
    				myDataSeriesBear2nd = new DataSeries(SMA(BarsArray[1], 14));
    				myDataSeriesBull2nd = new DataSeries(SMA(BarsArray[1], 14));
    			}
    if (BarsInProgress == 0)
    			{
    			/* To set calculated values to our DataSeries we use the Set() method.*/
    			myDataSeriesBear.Set(Math.Round(bearVolPerTick / bullVolPerTick, 2)/Period);
    			myDataSeriesBull.Set(Math.Round(bullVolPerTick / bearVolPerTick, 2)/Period);
    			
    			/* Plotting the SMA of the intermediate calculation step stored in DataSeries object*/				
    			BslShortPeriodBear.Set(SUM(myDataSeriesBear, Period)[0]);
    			BslShortPeriodBull.Set(SUM(myDataSeriesBull, Period)[0]);
    			}
    			
    			if (BarsInProgress == 1)
    			{
    			myDataSeriesBear2nd.Set(Math.Round(bearVolPerTick / bullVolPerTick, 2)/Period);
    			myDataSeriesBull2nd.Set(Math.Round(bullVolPerTick / bearVolPerTick, 2)/Period);	
    			BslShortPeriodBear2nd.Set(SUM(myDataSeriesBear2nd, Period)[0]);
    			BslShortPeriodBull2nd.Set(SUM(myDataSeriesBull2nd, Period)[0]);
    			}
    }
    Thank you.
    Attached Files

    #2
    Hello GeorgeW,

    Thank you for writing in.

    You need to have your data series synced to the primary series if you’d like your plots to not have missing gaps. You should modify the BarsArray so that it syncs with the primary series too.

    From,

    myDataSeriesBear2nd = new DataSeries(SMA(BarsArray[1], 14));
    myDataSeriesBull2nd = new DataSeries(SMA(BarsArray[1], 14));

    To,

    myDataSeriesBear2nd = new DataSeries(SMA(BarsArray[0], 14));
    myDataSeriesBull2nd = new DataSeries(SMA(BarsArray[0], 14));

    You should also set each plot in BarsInProgress 0, such as,

    if (BarsInProgress == 0)
    {

    BslShortPeriodBear.Set(SUM(myDataSeriesBear, Period)[0]);
    BslShortPeriodBull.Set(SUM(myDataSeriesBull, Period)[0])
    BslShortPeriodBear2nd.Set(SUM(myDataSeriesBear2nd, Period)[0]);
    BslShortPeriodBull2nd.Set(SUM(myDataSeriesBull2nd, Period)[0]);
    }

    Please let us know if you need further assistance.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Thanks for your reply, Alan P.

      I am still not getting this to work. I have tried it step by step.
      1. Syncing to the primary also:
      Code:
      if (myDataSeriesBear2nd == null || myDataSeriesBull2nd == null)
      			{
      				myDataSeriesBear2nd = new DataSeries(SMA(BarsArray[0], 14));
      				myDataSeriesBull2nd = new DataSeries(SMA(BarsArray[0], 14));
      				myDataSeriesBear2nd = new DataSeries(SMA(BarsArray[1], 14));
      				myDataSeriesBull2nd = new DataSeries(SMA(BarsArray[1], 14));
      			}
      The end of the 15M plots are in the correct position, but still gaps in the plot. I have even tried removing the secondary, but that gives incorrect results.

      2. Set 2nd in BarsInProgress[0] and not in [1], both the secondary plots fall to 0.
      Code:
      if (BarsInProgress == 0)
      			{
      	myDataSeriesBear.Set(Math.Round(bearVolPerTick / bullVolPerTick, 2)/Period);
      	myDataSeriesBull.Set(Math.Round(bullVolPerTick / bearVolPerTick, 2)/Period);
      					
      			BslShortPeriodBear.Set(SUM(myDataSeriesBear, Period)[0]);
      			BslShortPeriodBull.Set(SUM(myDataSeriesBull, Period)[0]);
      			BslShortPeriodBear2nd.Set(SUM(myDataSeriesBear2nd, Period)[0]);
      			BslShortPeriodBull2nd.Set(SUM(myDataSeriesBull2nd, Period)[0]);
      			}
      Even if I move the myDataSeries part of the code above if (BarsInProgress == 0), the 15M plots are still 0, and the primary plots then become distorted.

      3. Set 2nd in BarsInProgress[1] also - I get a stepped type plot and the results are incorrect.

      Comment


        #4
        Hello GeorgeW,

        I have simplified your script down to demonstrate how to reference a specific data series and then assign that value to a plot. This plot will not have gaps and nor does it contain anything in BIP==1.

        For simplicity I used Open/High/Low/Close values in place of,(Math.Round(bearVolPerTick /bullVolPerTick, 2)/Period);

        Please let us know if you need further assistance.
        Attached Files
        Alan P.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by elderan, Today, 08:03 PM
        0 responses
        2 views
        0 likes
        Last Post elderan
        by elderan
         
        Started by algospoke, Today, 06:40 PM
        0 responses
        10 views
        0 likes
        Last Post algospoke  
        Started by maybeimnotrader, Today, 05:46 PM
        0 responses
        8 views
        0 likes
        Last Post maybeimnotrader  
        Started by quantismo, Today, 05:13 PM
        0 responses
        7 views
        0 likes
        Last Post quantismo  
        Started by AttiM, 02-14-2024, 05:20 PM
        8 responses
        169 views
        0 likes
        Last Post jeronymite  
        Working...
        X