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

FirstTickofBar Variables - Incrementing Twice?

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

    FirstTickofBar Variables - Incrementing Twice?

    Hi all,

    I have a variable I'm attempting to increment from 0 when a certain condition is achieved. It seems to work, but since it's in the "if (FirstTickOfBar)" area, it increments it twice. I discovered this in the Log file after going through a market replay session.

    Here's what the code looks like. FYI, COBC is false.

    if (FirstTickOfBar)
    {

    if (volume > volMA_14 * 2)
    {
    volumeSpikes++;
    Print("volumeSpikes is now " + volumeSpikes + ".");
    }
    }

    When this condition is met, volumeSpikes shows up in two log entries.
    It first says "volumeSpikes is now 1" on the first line, with a timestamp of 9:57 AM.
    The second says "volumeSpikes is now 2" on the second line, with a timestamp of 9:57 AM.

    Any ideas on this one? Thanks in advance for any advice and your time!

    Yours,
    Spider

    #2
    Spider,

    Do you see this on historical data, or real-time data?

    I have ran this historically and only see one instance true at this time on the output window.

    What log are you referring to? Are only logging to the output window, or else where?

    Is it possible you have two instances of the same script running, resulting in two conditions at the same time?
    MatthewNinjaTrader Product Management

    Comment


      #3
      Hi Matthew,

      I'm running my strategy on recorded data taken during a trading session (I had my computer on, recording the day session with the Market Recorder). I had captured a full day of data tracking SPY.

      When I typed 'Log', I meant the output window. My mistake there. In fact, I modified the Print scripts to give more information and show what I mean. Here's the new text:

      The number for Bars.BarsSinceSession is 13.
      volumeSpikes is now 1 at the following time (7/17/2012 3:47:02 PM).
      ---------------------------------------------------------------------------------------
      For this volume spike, volume is 38000 and the 14 MA Volume * 2 is (31205).


      The number for Bars.BarsSinceSession is 13.
      volumeSpikes is now 2 at the following time (7/17/2012 3:47:02 PM).
      ---------------------------------------------------------------------------------------
      For this volume spike, volume is 38000 and the 14 MA Volume * 2 is (31205).

      And I checked. I'm not running two scripts at the same time. I did check to see if that volumeSpikes variable was increased in another part of the script, but it isn't.

      I also looked further down the output window to find the following:

      The number for Bars.BarsSinceSession is 14.
      volumeSpikes is now 3 at the following time (7/17/2012 3:47:03 PM).
      ---------------------------------------------------------------------------------------
      For this volume spike, volume is 49600 and the 14 MA Volume * 2 is (40271).


      The number for Bars.BarsSinceSession is 14.
      volumeSpikes is now 4 at the following time (7/17/2012 3:47:03 PM).
      ---------------------------------------------------------------------------------------
      For this volume spike, volume is 49600 and the 14 MA Volume * 2 is (40271).

      So it looks like it's definitely doubling, but why? Any ideas?

      Comment


        #4
        Originally posted by Spiderbird View Post
        Hi Matthew,

        I'm running my strategy on recorded data taken during a trading session (I had my computer on, recording the day session with the Market Recorder). I had captured a full day of data tracking SPY.

        When I typed 'Log', I meant the output window. My mistake there. In fact, I modified the Print scripts to give more information and show what I mean. Here's the new text:

        The number for Bars.BarsSinceSession is 13.
        volumeSpikes is now 1 at the following time (7/17/2012 3:47:02 PM).
        ---------------------------------------------------------------------------------------
        For this volume spike, volume is 38000 and the 14 MA Volume * 2 is (31205).


        The number for Bars.BarsSinceSession is 13.
        volumeSpikes is now 2 at the following time (7/17/2012 3:47:02 PM).
        ---------------------------------------------------------------------------------------
        For this volume spike, volume is 38000 and the 14 MA Volume * 2 is (31205).

        And I checked. I'm not running two scripts at the same time. I did check to see if that volumeSpikes variable was increased in another part of the script, but it isn't.

        I also looked further down the output window to find the following:

        The number for Bars.BarsSinceSession is 14.
        volumeSpikes is now 3 at the following time (7/17/2012 3:47:03 PM).
        ---------------------------------------------------------------------------------------
        For this volume spike, volume is 49600 and the 14 MA Volume * 2 is (40271).


        The number for Bars.BarsSinceSession is 14.
        volumeSpikes is now 4 at the following time (7/17/2012 3:47:03 PM).
        ---------------------------------------------------------------------------------------
        For this volume spike, volume is 49600 and the 14 MA Volume * 2 is (40271).

        So it looks like it's definitely doubling, but why? Any ideas?
        Is your indicator exposing a double or int as a public variable?

        Comment


          #5
          Is this a multi series script? Or do you only run on one data series?
          MatthewNinjaTrader Product Management

          Comment


            #6
            Originally posted by koganam View Post
            Is your indicator exposing a double or int as a public variable?
            Hi Koganam,

            Your post prompted me to try the following variable declaration variations:

            double volumeSpikes;
            double volumeMFI;

            public double volumeSpikes;
            public double volumeMFI;

            public int volumeSpikes;
            public int volumeMFI;

            int volumeSpikes;
            int volumeMFI;

            I compiled the code and re-applied the strategy to the replay chart, watching the output window. Same dynamic happens (volumeSpikes is incremented twice)

            I read elsewhere in the forums that it could be an inherent problem with FirstTickOfBar, but I would have to look up that thread I found (I'm holding a newborn and typing with one hand.)

            @Matthew - Good thought. I'm running the strategy on only one data series.

            Comment


              #7
              Here's another thought... FirstTickOfBar is within a couple of other "if" sections. Here's the complete rundown:

              protected override void OnBarUpdate()
              {

              (...)
              if (ToTime(Time[0]) >= 83500 && ToTime(Time[0]) <= 144500)

              (...)
              // Note: else if below is part of a market position check (flat, short, long)
              else if (Position.MarketPosition == MarketPosition.Short)
              {

              (...)
              if (FirstTickOfBar)
              {

              (...)
              if (volume > volMA_14 * 2)
              {
              volumeSpikes++;

              Comment


                #8
                Is this the only area you're using volumeSpikes++ - you don't check this if the position is flat or long either?

                Can you try printing market position as well to ensure that you're only incrementing this variable when the strategy is short?

                Ultimately, I'd suggest taking it out of any block other than 'FirstTickOfBar' to ensure it only gets called once as expect and then build it back from there.
                MatthewNinjaTrader Product Management

                Comment


                  #9
                  Thanks!

                  Originally posted by NinjaTrader_Matthew View Post
                  Is this the only area you're using volumeSpikes++ - you don't check this if the position is flat or long either?

                  Can you try printing market position as well to ensure that you're only incrementing this variable when the strategy is short?

                  Ultimately, I'd suggest taking it out of any block other than 'FirstTickOfBar' to ensure it only gets called once as expect and then build it back from there.
                  Hi Matthew,

                  The code actually checks to see if the position is long, short or flat before checking volume, so that's in there. I did as you suggested and printed out my market position for visual reference and to verify where I was in the market.

                  Plus, all aspects of volumeSpikes++ are in those FirstTickOfBar areas, so that's also nixed.

                  The code below ended up being my final solution that works. I put a few more volume checks in there, but it does the trick by comparing a saved Time[0] variable and rechecking it in the "if" loop illustrated below.

                  Thanks to everyone for their help!

                  Yours,
                  Robert

                  Code:
                  else if (Position.MarketPosition == MarketPosition.Long)
                  				{
                  					if (FirstTickOfBar)
                  					{
                  						highPriceLine = High[HighestBar(High, barLookBack_inTrade)];
                  						lowPriceLine = Low[LowestBar(Low, barLookBack_inTrade)];
                  						
                  						DrawHorizontalLine("highPriceLine", false, highPriceLine, Color.DarkSeaGreen, DashStyle.Solid, 4);
                  						DrawHorizontalLine("lowPriceLine", false, lowPriceLine, Color.DarkRed, DashStyle.Solid, 4);
                  						
                  						/*  Calibration:  Check bar for Volume / MFI and set appropriate variables
                  							---------------------------------------------------------------------- */
                  						
                  						if (volumeTime != ToTime(Time[0]))
                  						{	volumeCheck = true;  }
                  						
                  						if (volumeCheck == true)
                  						{	
                  							if (mfi_5 > 95 && volume > volMA_14 * 3)	
                  							{
                  								volumeMFI++;
                  								BackColorSeries[1] = Color.CadetBlue;
                  								Print("MFI Hit");
                  							}
                  							
                  							else if (volume > volMA_14 * 3)
                  							{  
                  								volumeSpike3x++;
                  								BackColorSeries[1] = Color.CornflowerBlue;
                  								Print("3x Hit");
                  							}
                  							
                  							else if (volume > volMA_14 * 2)
                  							{  
                  								volumeSpike2x++;
                  								BackColorSeries[1] = Color.AliceBlue;
                  								Print("2x Hit");
                  							}
                  							
                  							volumeCheck = false;
                  							volumeTime = ToTime(Time[0]);
                  						}

                  Comment


                    #10
                    Glad you got it working and I appreciate you following up with your solution.
                    MatthewNinjaTrader Product Management

                    Comment


                      #11
                      Hi Matthew,

                      I still ran into multiple order problems, BUT, I did figure out that I had a Add(PeriodType.Minute) in my initialize section that caused the double run discussed above.

                      The offending code was this:
                      Code:
                      protected override void Initialize()
                      {
                      	CalculateOnBarClose = false;
                      	Unmanaged = true;
                      	TraceOrders = true;
                      	ExitOnClose = true;
                      			
                      	BarsRequired = 5;
                      	Add(PeriodType.Minute, 1); // This line was the culprit
                      			
                      	EntriesPerDirection = 2; 
                      	EntryHandling = EntryHandling.AllEntries;
                      }
                      I'll test tomorrow to see if that is also causing multiple orders to be submitted.

                      Comment


                        #12
                        Spider,

                        Please feel free to contact us should you require additional assistance.
                        Adam P.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by ETFVoyageur, Today, 02:15 AM
                        1 response
                        13 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by lorem, 04-25-2024, 09:18 AM
                        21 responses
                        89 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by mlprice12, 12-21-2021, 04:55 PM
                        5 responses
                        307 views
                        0 likes
                        Last Post paypachaysa  
                        Started by Human#102, 05-08-2024, 09:54 AM
                        4 responses
                        17 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by ETFVoyageur, Today, 06:11 AM
                        1 response
                        8 views
                        0 likes
                        Last Post NinjaTrader_BrandonH  
                        Working...
                        X