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

IDataSeries Usage In Indicator Methods

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

    IDataSeries Usage In Indicator Methods

    I'd like to locate a programming code example using IDataSeries in indicators in multiple time frames.

    Many NT indicator methods provide for using the IDataSeries interface. As example:

    MACD(IDataSeries inputData, int fast, int slow, int smooth).Avg[int barsAgo]

    The Verison 6 Help Guide page 'DataSeries Class' states:

    "DataSeries objects can be used on supplementary series in a multi-time frame and instrument strategy. Please see our support forum NinjaScript reference samples section for further information."

    After reading this comment, I looked at the reference samples here, but found no specific examples using of IDataSeries in indicator methods. I did find code on changing IDataSeries data using BarsInProgress, but this is not relavent.

    Are there any examples elsewhere?

    Thanks for the support.
    Last edited by borland; 02-04-2010, 06:27 PM.

    #2
    borland, you could for example review the SampleMultiInstrument / SampleMultiTimeFrame Strategies we install per default with NinjaTrader.

    As dataseries you can also pass in directly the needed BarsArray to point to which series the indicator / method to calculate from -

    BertrandNinjaTrader Customer Service

    Comment


      #3
      Bertrand,

      Those bundled NT examples use BarsArray[], so its not the same. Using BarsArray[] would work, but the task of keeping track of several index markers will not work. I need to assign variable names to the bars array or DataSeries in order to keep track of what's going on with the programming trade logic.

      How's this look for a multi-timeframe, multi-instrument coding framework?

      Code:
      // NinjaScript Framework for Multi-Timeframe & Multi-Instrument(1-traded), 3-timeframes/2-instruments
      #region Variables 
      //Standard declarations..
          using System;
          using System.ComponentModel;
          using System.Diagnostics;
          using System.Drawing;
          using System.Drawing.Drawing2D;
          using System.Xml.Serialization;
          using NinjaTrader.Cbi;
          using NinjaTrader.Data;
          using NinjaTrader.Indicator;
          using NinjaTrader.Strategy;
      //declare multi-Instrument(1-traded), multi-timeframe DataSeries...
      //trading off 5m bar, but need longer timeframes too..
        private DataSeries my_60m_bar ; 
        private DataSeries my_1d_bar ;
        private DataSeries my_$compq_5m_bar ;
        private DataSeries my_$compq_60m_bar ;    
        private DataSeries my_$compq_1d_bar ;
       
      #endregion
      protected override void Initialize() 
      { 
      //Initialize multi-Instrument, multi-timeframe DataSeries...
        my_60m_bar = new DataSeries(Add(PeriodType.Minute, 60));
        my_daily_bar = new DataSeries(Add(PeriodType.Day, 1));
        my_$compq_5m_bar = new DataSeries(Add("$COMPQ", PeriodType.Minute, 5));
        my_$compq_60m_bar = new DataSeries(Add("$COMPQ", PeriodType.Minute, 60));
        my_$compq_1d_bar = new DataSeries(Add("$COMPQ", PeriodType.Day, 1));
       
      //Detect trade triggers intra-bar ...
        CalculateOnBarClose = false;
      }
      protected override void OnBarUpdate() 
      { 
        if (BarsInProgress != 0)
        {
          return;
        }
        if (Position.MarketPosition == MarketPosition.Flat)
        {
          if
            (
              // Entry trade trigger logic here....
              MACD(12,26,9)[0] > 0
              &&
              MACD(my_60m_bar,12,26,9)[0] > 0
              &&
              MACD(my_1d_bar,12,26,9)[0] > 0
              &&
              MACD(my_$compq_5m_bar,12,26,9)[0] > 0
              &&
              {
                MACD(my_$compq_60m_bar,12,226,9)[0] > 0
                ||
                MACD(my_$compq_1d_bar,12,26,9)[0] > 0
              }
            )
          {
            EnterLong();
          }
        }
        if (Position.MarketPosition == MarketPosition.Long)
        {
          if
          (
            // Exit trade trigger logic here.....
            MACD(12, 26, 9)[0] > 0
            &&
            MACD(my_60m_bar,12,26,9)[0] > 0
            &&
            MACD(my_1d_bar,12,26,9)[0] > 0
            &&
            MACD(my_$compq_5m_bar,12,26,9)[0] > 0
            &&
            {
              MACD(my_$compq_60m_bar,12,26,9)[0] > 0
              ||
              MACD(my_$compq_1d_bar,12,26,9)[0] > 0
            }
          )
          {
            ExitLong();    
          }
        }
      }

      Comment


        #4
        borland, as long as your not running into any issues with that code go ahead and use it. I haven't personally tried anything like my_60m_bar = new DataSeries(Add(PeriodType.Minute, 60)), nor do I know if that is a supported way to create and initialize a DataSeries.

        If it doesn't work, you will need to work with BarsArray[] per Bertrand's posted sample.
        AustinNinjaTrader Customer Service

        Comment


          #5
          Austin,

          OK, BarsArray[]..... not what I wanted to do.

          Here's the revised code, zero complilation errors, 'Strategy successfully generated'.

          Code:
          // NinjaScript Framework for Multi-Timeframe & Multi-Instrument(1-traded), example: 3-timeframes/2-instruments
          #region Variables 
          //Standard declarations..
              using System;
              using System.ComponentModel;
              using System.Diagnostics;
              using System.Drawing;
              using System.Drawing.Drawing2D;
              using System.Xml.Serialization;
              using NinjaTrader.Cbi;
              using NinjaTrader.Data;
              using NinjaTrader.Indicator;
              using NinjaTrader.Strategy;
          #endregion
          namespace NinjaTrader.Strategy
          {  
            public class SampleMultiTimeFrameMultiInstrument : Strategy
            {
              #region Variables
           //declare multi-Instrument(1-traded), multi-timeframe DataSeries...
              //trading off 5m bar, but need longer timeframes too..
              private int my_60m_bar; 
              private int my_1d_bar;
              private int my_compq_5m_bar;
              private int my_compq_60m_bar;    
              private int my_compq_1d_bar;
              private bool LookForLongEntryTrigger = false;
              private bool LookForLongExitTrigger = true;
           
              #endregion
              protected override void Initialize() 
              { 
                //Initialize multi-Instrument, multi-timeframe BarsArray[] DataSeries...
                Add(PeriodType.Minute,60);
                my_60m_bar = 1;
                Add(PeriodType.Day,1);
                my_1d_bar = 2;
                Add("$COMPQ", PeriodType.Minute, 5);
                my_compq_5m_bar = 3;
                Add("$COMPQ", PeriodType.Minute, 60);
                my_compq_60m_bar = 4;
                Add("$COMPQ", PeriodType.Day, 1);
                my_compq_60m_bar = 5;
           
                //Detect trade triggers intra-bar ...
                CalculateOnBarClose = false;
              }
              protected override void OnBarUpdate() 
              { 
                if (BarsInProgress != 0)
                {
                  return;
                }
                if (BarsInProgress == 0)
                {
                  if (Position.MarketPosition == MarketPosition.Flat)
                  { 
                    // Trade Entry trigger logic goes here...
              LookForLongEntryTrigger =
                    MACD(12,26,9)[0] > 0
              &&
              MACD(BarsArray[my_60m_bar],12,26,9)[0] > 0
              &&
              MACD(BarsArray[my_1d_bar],12,26,9)[0] > 0
                    &&
              MACD(BarsArray[my_compq_5m_bar],12,26,9)[0] > 0
              &&
              (
                MACD(BarsArray[my_compq_60m_bar],12,226,9)[0] > 0
                      ||
                      MACD(BarsArray[my_compq_1d_bar],12,26,9)[0] > 0
              );
           
                    // If Entry Triggered, Make the Trade..
                 if (LookForLongEntryTrigger == true)
                    {
                      EnterLong();
                      return;
                    }
                  }
                  if (Position.MarketPosition == MarketPosition.Long)
                  {
                    // Trade Exit trigger logic goes here.....
                 LookForLongExitTrigger =
                    MACD(12, 26, 9)[0] > 0
                    &&
                    MACD(BarsArray[my_60m_bar],12,26,9)[0] > 0
                    &&
                    MACD(BarsArray[my_1d_bar],12,26,9)[0] > 0
                    &&
                    MACD(BarsArray[my_compq_5m_bar],12,26,9)[0] > 0
                    &&
                    (
                      MACD(BarsArray[my_compq_60m_bar],12,26,9)[0] > 0
                      ||
                      MACD(BarsArray[my_compq_1d_bar],12,26,9)[0] > 0
                    );
                    // If Exit Triggered, Make the Trade..
                 if (LookForLongExitTrigger == true)
                 {
                      ExitLong();
                      return;            
                    }
                  }
                }
              }
            }
          }
          Do you know a way to assign an array variable name to BarsArray[]? That would get me to where I wanted to go.

          Comment


            #6
            borland, I'm not sure of a way to assign a variable name to BarsArray[]. I'll leave this ticket open so a coworker can get back to you with a definitive answer tomorrow.
            AustinNinjaTrader Customer Service

            Comment


              #7
              borland, this should work, as your variables just contain the index given to the added series by the Add method, spotted a small type you would need to correct -
              Code:
               
               
                Add("$COMPQ", PeriodType.Minute, 60);
                    my_compq_60m_bar = 4;
                    Add("$COMPQ", PeriodType.Day, 1);
                    my_compq_60m_bar = 5;
              BertrandNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by GussJ, 03-04-2020, 03:11 PM
              11 responses
              3,221 views
              0 likes
              Last Post xiinteractive  
              Started by andrewtrades, Today, 04:57 PM
              1 response
              10 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Started by chbruno, Today, 04:10 PM
              0 responses
              6 views
              0 likes
              Last Post chbruno
              by chbruno
               
              Started by josh18955, 03-25-2023, 11:16 AM
              6 responses
              436 views
              0 likes
              Last Post Delerium  
              Started by FAQtrader, Today, 03:35 PM
              0 responses
              9 views
              0 likes
              Last Post FAQtrader  
              Working...
              X