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

Multiple timeframes in indicator does not seem to work... should it?

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

    Multiple timeframes in indicator does not seem to work... should it?

    public class MyCustomIndicator2 : Indicator
    {
    #region Variables
    // Wizard generated variables
    private int myInput0 = 1; // Default setting for MyInput0
    private int m2 = 1; // Default setting for M2
    private int m3 = 1; // Default setting for M3
    private int m8 = 1; // Default setting for M4
    // 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.Orange), PlotStyle.Line, "Plot0"));
    Overlay = false;
    Add(PeriodType.Minute, 5);
    //Add (PeriodType.Minute, 10);
    //Add (PeriodType.Minute, 20);
    CalculateOnBarClose = false;

    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Use this method for calculating your indicator values. Assign a value to each
    // plot below by replacing 'Close[0]' with your own formula.
    //Print(CurrentBars.Length);
    if (CurrentBar<40)

    if (BarsInProgress == 0)
    {
    Plot0.Set(Close[0]);
    Print("0 Code has entered If statement. Close: "+ Close[0] + " Open: " + Open[0]
    + " Open2: " + Opens[1][0]
    + " "+ CurrentBars[0] + " "+CurrentBars[1]);
    }
    else
    Print("1 Code has entered If statement. Close: "+ Close[0] + " Open: " + Open[0]);
    }

    Output:
    0 Code has entered If statement. Close: 2607,35 Open: 2607,35 Open2: 0 0 -1
    0 Code has entered If statement. Close: 2604,65 Open: 2607,55 Open2: 0 1 -1
    0 Code has entered If statement. Close: 2606,15 Open: 2604,85 Open2: 0 2 -1
    0 Code has entered If statement. Close: 2606,65 Open: 2606,05 Open2: 0 3 -1
    0 Code has entered If statement. Close: 2605,65 Open: 2606,35 Open2: 0 4 -1
    0 Code has entered If statement. Close: 2606,35 Open: 2605,65 Open2: 0 5 -1
    0 Code has entered If statement. Close: 2606,65 Open: 2606,65 Open2: 0 6 -1
    0 Code has entered If statement. Close: 2605,35 Open: 2606,35 Open2: 0 7 -1
    0 Code has entered If statement. Close: 2604,15 Open: 2605,15 Open2: 0 8 -1

    Thanks

    #2
    Hello trader4_,

    Thanks for your post.

    When creating a multi series or multi time frame script, you would want to check that you have a bar loaded for each series and this is done by using the CurrentBars[] check.
    Reference: https://ninjatrader.com/support/help...urrentbars.htm

    If you have not reviewed the Multi series, multi-time help guide, please see: as the current bars check and other critical concepts are covered there.
    Reference: https://ninjatrader.com/support/help...nstruments.htm

    Paul H.NinjaTrader Customer Service

    Comment


      #3
      INot sure I understand... how will the bars be loaded?

      Comment


        #4
        ok they are not loaded.. how will they be loaded?

        Comment


          #5
          hello trader4_ ,

          Thanks for your replies.

          I am currently working on other requests, please be patient and I will be able to reply later today.


          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Hello trader4_ ,

            Thanks for your patience and sorry for the delay.

            You would use the check of the current bar of both series before proceeding further into your code.

            Please try the following in place of your current bar check.

            if (CurrentBars[0] < 1 || CurrentBars[1] < 1) return; // wait for 1 bar in each series before further processing

            In a multi time frame script, you would want to check to ensure that you had at least 1 bar from each series before trying to access it.

            Paul H.NinjaTrader Customer Service

            Comment


              #7
              if I do that... The code never goes past that line.
              It means currentbar 0 and current 1 will never be greater than 0
              How can I make sure ninja trader can returns bars.?

              Comment


                #8
                Hello trader4_,

                Thanks for your reply.

                CurrentBars[] is a pointer to the different data, series specified in the index. [0] reference to the chart bars, [1] refers to the added data series.

                What happens when a script loads is that it will process the first bar of the first data series to call OnBarUpdate(), Each added data series will call OnBarUpdate(). The check of CurrentBars[0] < 1 || CurrentBars[1] < 1) return; is to ensure that no further processing (below that line) can occur, until 1 bar of each data series has called OnBarUpdate(). Typically it will be the longest time frame that will be the delaying factor, so in order for you to see that the code is processing multiple time frames, you need to wait until you have at least 1 bar of each data series.


                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  This is the code
                  protected override void OnBarUpdate()
                  {
                  if (CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired)
                  return;


                  if ( Opens[1][0]>=0)
                  {

                  Print("0 Code has entered If statement. Close: "+ Close[0] + " Open: " + Open[0]
                  + " Open2: " + Opens[1][0]
                  + " "+ CurrentBars[0] + " "+CurrentBars[1]);
                  }
                  }


                  This code does not print anything... How can I make it print the second series?







                  Comment


                    #10
                    Hello trader4_,

                    Thanks for your post.

                    Your code would not print anything as the condition that allows it to print is if ( Opens[1][0]>=0) is likely never true.

                    Paul H.NinjaTrader Customer Service

                    Comment


                      #11
                      Why would not that be true? open prices should be above zero. but it should be true...as I added another bar by Add(PeriodType.Minute, 5);
                      Last edited by trader4_; 05-29-2019, 08:35 AM.

                      Comment


                        #12
                        hello trader4_,

                        Thanks for your reply.

                        You are correct, I was mistaken in reading it as = 0 and not >=0.

                        Here is an example that may help you to see what is happening.


                        if (CurrentBars[0] < 1 || CurrentBars[1] < 1) return; // wait for 1 bar in both series

                        Print ("BIP: "+BarsInProgress+" CurrentBar[0]: "+CurrentBars[0]+" Closes[0][0]: "+Closes[0][0]
                        +" CurrentBar[1]: "+CurrentBars[1]+" Closes[1][0]: "+Closes[1][0]);


                        Here is an example of the historical data from a 1-minute chart and where the indicator added a 5 minute data series as BarsInProgress = 1

                        BIP: 0 CurrentBar[0]: 4915 Closes[0][0]: 2778 CurrentBar[1]: 983 Closes[1][0]: 2777
                        BIP: 0 CurrentBar[0]: 4916 Closes[0][0]: 2777.25 CurrentBar[1]: 983 Closes[1][0]: 2777
                        BIP: 0 CurrentBar[0]: 4917 Closes[0][0]: 2776.25 CurrentBar[1]: 983 Closes[1][0]: 2777
                        BIP: 0 CurrentBar[0]: 4918 Closes[0][0]: 2776 CurrentBar[1]: 983 Closes[1][0]: 2777
                        BIP: 0 CurrentBar[0]: 4919 Closes[0][0]: 2777.75 CurrentBar[1]: 984 Closes[1][0]: 2777.75
                        BIP: 1 CurrentBar[0]: 4919 Closes[0][0]: 2777.75 CurrentBar[1]: 984 Closes[1][0]: 2777.75
                        BIP: 0 CurrentBar[0]: 4920 Closes[0][0]: 2777.5 CurrentBar[1]: 984 Closes[1][0]: 2777.75
                        BIP: 0 CurrentBar[0]: 4921 Closes[0][0]: 2775.5 CurrentBar[1]: 984 Closes[1][0]: 2777.75
                        BIP: 0 CurrentBar[0]: 4922 Closes[0][0]: 2775.5 CurrentBar[1]: 984 Closes[1][0]: 2777.75
                        BIP: 0 CurrentBar[0]: 4923 Closes[0][0]: 2774.75 CurrentBar[1]: 984 Closes[1][0]: 2777.75
                        BIP: 0 CurrentBar[0]: 4924 Closes[0][0]: 2772.75 CurrentBar[1]: 985 Closes[1][0]: 2772.75
                        BIP: 1 CurrentBar[0]: 4924 Closes[0][0]: 2772.75 CurrentBar[1]: 985 Closes[1][0]: 2772.75
                        BIP: 0 CurrentBar[0]: 4925 Closes[0][0]: 2772.25 CurrentBar[1]: 985 Closes[1][0]: 2772.75
                        BIP: 0 CurrentBar[0]: 4926 Closes[0][0]: 2770.75 CurrentBar[1]: 985 Closes[1][0]: 2772.75
                        BIP: 0 CurrentBar[0]: 4927 Closes[0][0]: 2769.5 CurrentBar[1]: 985 Closes[1][0]: 2772.75
                        BIP: 0 CurrentBar[0]: 4928 Closes[0][0]: 2770.25 CurrentBar[1]: 985 Closes[1][0]: 2772.75
                        BIP: 0 CurrentBar[0]: 4929 Closes[0][0]: 2770.5 CurrentBar[1]: 986 Closes[1][0]: 2770.5
                        BIP: 1 CurrentBar[0]: 4929 Closes[0][0]: 2770.5 CurrentBar[1]: 986 Closes[1][0]: 2770.5
                        BIP: 0 CurrentBar[0]: 4930 Closes[0][0]: 2770.75 CurrentBar[1]: 986 Closes[1][0]: 2770.5
                        BIP: 0 CurrentBar[0]: 4931 Closes[0][0]: 2771.5 CurrentBar[1]: 986 Closes[1][0]: 2770.5
                        BIP: 0 CurrentBar[0]: 4932 Closes[0][0]: 2771.75 CurrentBar[1]: 986 Closes[1][0]: 2770.5

                        As you can see by the BIP (BarsInProgress) when BIP is 0 that is the 1-minute chart bars calling the OnBarUpdate(). When BIP = 1, this is the 5 minute bar calling the OnBarUpdate(). I've added the CurrentBars[0] and CurrentBars[1] so that you can see the bar count for each series.

                        Paul H.NinjaTrader Customer Service

                        Comment


                          #13
                          I do not get these lines.. what could be the reason? Is it possible that Ninja trader does not have 5 minute bar data but it has 1 minute bar data only?
                          Last edited by trader4_; 05-29-2019, 10:22 AM.

                          Comment


                            #14
                            Hello trader4_,

                            Thanks for your reply.

                            Just to be clear in the example I am adding the data series in the Initialize() method, with Add(PeriodType.Minute, 5); which was not shown.

                            What data feed are you connected to?
                            Paul H.NinjaTrader Customer Service

                            Comment


                              #15
                              I imported 1 minute bars from a ninja 7 csv file

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by fx.practic, 10-15-2013, 12:53 AM
                              5 responses
                              5,406 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by Shai Samuel, 07-02-2022, 02:46 PM
                              4 responses
                              98 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by DJ888, Yesterday, 10:57 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by MacDad, 02-25-2024, 11:48 PM
                              7 responses
                              160 views
                              0 likes
                              Last Post loganjarosz123  
                              Started by Belfortbucks, Yesterday, 09:29 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post Belfortbucks  
                              Working...
                              X