Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Make trail stop value update intra bar without indicator update.

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

    #16
    I"m not talking about using the count function of ninja. Just define a varable in the variable section like

    private int myBars = 0;

    Then inside your onbarupdate,

    do something like

    myBars = myBars + 1;

    then

    if (myBars == 15) {
    do your order enty stuff and then
    myBars = 0;

    }

    Haven't tested this but it should work.

    Comment


      #17
      Woodside,

      This is what I got but it won't compile. Any ideas:

      #region Variables
      // Wizard generated variables
      private int sMAFast = 6; // Default setting for SMAFast
      private int trailStop = 4; // Default setting for TrailStop
      private int myBars = 0; // Woodside says to use this to count bars
      // User defined variables (add any user defined variables below)
      #endregion

      /// <summary>
      /// This method is used to configure the strategy and is called once before any strategy method is called.
      /// </summary>
      protected override void Initialize()

      {
      ///Add 1 Minute Bars to the strategy.

      Add(PeriodType.Minute, 15);


      // if (BarsInProgress == 1)

      SetTrailStop("", CalculationMode.Ticks, TrailStop, true);

      CalculateOnBarClose = true;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {

      // Woodside says use this code to count bars.
      myBars = myBars +1;

      then

      if(myBars == 15);

      //Call bars so that orders are made
      {
      if (BarsInProgress == 0)



      // Condition set 1
      if (ToTime(Time[0]) > ToTime(10, 0, 0)
      && ToTime(Time[0]) <= ToTime(16, 10, 0)
      && SMA(SMAFast)[0] > SMA(SMAFast)[1])


      {
      EnterLong(DefaultQuantity, "");
      // Woodside says use this code to reset the count to zero after entry is made.
      myBars = 0;
      }

      }
      }

      Comment


        #18
        Hi dendy,

        You don't need the line "then". Also if you don't feel like giving a signal name on your EnterLong() you can simply do EnterLong(DefaultQuantity). In fact since you are just using default quantity I believe you could even just go EnterLong().
        Last edited by NinjaTrader_JoshP; 10-19-2007, 01:46 PM.
        Josh P.NinjaTrader Customer Service

        Comment


          #19
          Thanks Josh,

          When I cut out the "then", the code compiles and the strategy runs. It's not happening right though. Either the code is not counting the bars or the reset is not occurring. I think the count is not restarting because the entries are happening every minute or so.

          Do you have any advice?

          dendy.

          Comment


            #20
            Right after the onbarupdate put:

            Print("myBar = " + myBars.ToString());

            then open an output window and see what you are getting.

            Comment


              #21
              Woodside,

              The output window shows the myBar count going high (at times into the hundreds). Afterward, the count will go to zero (sometimes for 3 to 5 zeros in a row). At times the count goes up to 2 or 3 and then back down to zero.

              An example is at the bottom of this post.

              Thanks again. I'm learning lots about Ninjascript with your help...

              myBar = 26
              myBar = 27
              myBar = 28
              myBar = 0
              myBar = 1
              myBar = 0
              myBar = 0
              myBar = 0
              myBar = 1
              myBar = 0
              myBar = 1
              myBar = 2
              myBar = 3
              myBar = 4
              myBar = 0
              myBar = 1
              myBar = 0
              myBar = 1
              myBar = 2
              myBar = 3
              myBar = 4
              myBar = 5
              myBar = 6
              myBar = 7
              myBar = 0
              myBar = 0
              myBar = 0
              myBar = 1
              myBar = 2
              myBar = 3
              myBar = 4
              myBar = 5
              myBar = 6
              myBar = 0
              myBar = 1
              myBar = 0
              myBar = 0
              myBar = 1
              myBar = 2
              myBar = 3
              myBar = 4
              myBar = 5
              myBar = 6
              myBar = 7
              myBar = 8
              myBar = 0
              myBar = 0
              myBar = 0
              myBar = 0
              myBar = 0
              myBar = 1
              myBar = 0
              myBar = 0
              myBar = 0
              myBar = 1
              myBar = 2
              myBar = 3
              myBar = 4
              myBar = 5
              myBar = 0
              myBar = 0
              myBar = 0
              myBar = 0
              myBar = 1
              myBar = 2
              myBar = 3
              myBar = 4
              myBar = 5
              myBar = 0
              myBar = 1
              myBar = 0
              myBar = 0
              myBar = 0
              myBar = 1
              myBar = 2
              myBar = 3
              myBar = 4
              myBar = 5

              Comment


                #22
                move the myBars = 0 to right below

                if(myBars == 15);
                myBars = 0;

                right now you have it inside your if conditions so it doesn't get reset unless your conditions are all true.

                Comment


                  #23
                  Hi dendy,

                  There are several errors in your current code.

                  Code:
                  if(myBars == 15);
                  There shouldn't be a ; at the end of the if statement.

                  Your myBar reset only happens if you entered long also. I'm not sure if this is intentional, but I would assume you would want it to reset every time it hits 15 regardless if you entered a trade or not. The way you have it currently, when your myBars becomes 15 and if, by chance, you don't enter the trade your myBars would never be reset and would just keep incrementing forever after that.

                  You also need curly brackets for the BarsInProgress if statement.

                  Code:
                  if(myBars == 15)
                  //Call bars so that orders are made
                  {
                      if (BarsInProgress == 0)
                      {
                          // Condition set 1
                          if (ToTime(Time[0]) > ToTime(10, 0, 0)
                          && ToTime(Time[0]) <= ToTime(16, 10, 0)
                          && SMA(SMAFast)[0] > SMA(SMAFast)[1])
                          {
                              EnterLong(DefaultQuantity, "");
                          }
                      }
                      // Woodside says use this code to reset the count to zero after entry is made.
                      myBars = 0;
                  }
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #24
                    I made all the suggested changes. The count goes from zero to 14 and then resets every time. To make the orders obey, I added: "&&myBars = 14" to the entry conditions. The code is below.

                    Again thanks for all the help...

                    I believe it is working now......................





                    protected override void OnBarUpdate()
                    {

                    // Woodside says, "Use this code to print the bar count to the output window.
                    Print("myBar = " + myBars.ToString());

                    {

                    // Woodside says use this code to count bars.
                    myBars = myBars +1;


                    if(myBars == 15)
                    myBars = 0;

                    //Call bars so that orders are made
                    {
                    if (BarsInProgress == 0)
                    {


                    // Condition set 1
                    if (ToTime(Time[0]) > ToTime(10, 0, 0)
                    && ToTime(Time[0]) <= ToTime(16, 10, 0)
                    && SMA(SMAFast)[0] > SMA(SMAFast)[1]
                    && myBars == 14)


                    {
                    EnterLong(DefaultQuantity, "");

                    }
                    }
                    }
                    }
                    }

                    Comment


                      #25
                      I don't think that is going to work properly.

                      You aren't going to see it hit 15 because when it's on 14, 1 gets added to it.

                      When the "if myBarrs ==15" condition is true and it gets reset back to 0.

                      For the same reason the && myBars == 14 will never be true because it's inside the if statement that only executes when myBars = 15 which then gets reset to 0. I'd remove the && myBars ==14 from your if and put another print statement near you "EnterLong" statement to see if if fires.

                      It doesn't appear to me that it will at this point.

                      When I'm developing/debugging I use lots of print statements to help me see what is going on.

                      -Erik

                      Comment


                        #26
                        For those of us that can't count; a suggestion...

                        Why don't you try something like this...

                        Code:
                        private bool wait_for_primary = false; // If set, remember to do logic on next primary time-frame bar.
                        
                        protected override void Initialize()
                        {            
                            Add(PeriodType.Minute, 15);     // Use a 15-Min bar object (for BarsInProgress==1).
                            CalculateOnBarClose = true;     // If set to 'false' then OnBarUpdate will be called for every tick.
                        }
                        
                        protected override void OnBarUpdate()
                        {
                            //  Secondary time frame -- Since trades must be issued from the primary instrument,
                            //                          just set a flag on the secondary time frame.
                            //
                            if (BarsInProgress == 1)        // This happens once every 15-min...
                            {
                                wait_for_primary  = true;   // Check for time to trade on next primary bar.
                                return;
                            }
                            
                            //  Primary time frame -- Since trades must be issued from the primary instrument,
                            //                        we do that here if the flag was set on the secondary time frame.
                            //
                            if (BarsInProgress == 0)        // This happens once every 1-min...
                            {
                                if (wait_for_primary)
                                {
                                    if (ToTime(Time[0]) > ToTime(10, 0, 0)
                                    && ToTime(Time[0]) <= ToTime(16, 10, 0)
                                    && SMA(SMAFast)[0] > SMA(SMAFast)[1])
                                    {
                                        EnterLong(DefaultQuantity, "");
                                    }
                                    wait_for_primary = false; // Only do this once; wait for another 15-min.
                                }
                            }
                        }

                        Comment


                          #27
                          Woodside and KBJ,

                          The count code works. I have not used the print on the EnterLong but I have looked at the backtest output to confirm that the EnterLong fires and only does so every 14 mins.

                          I also used KBJ's code and it works too. The only thing is that when I'm adding a second condition to exit the position at 4:15 PM this new condition was difficult for me to implement. I had to slide it in up top as such...

                          Thanks again guys!

                          protected override void OnBarUpdate()
                          {


                          // Condition set 2 (Exit all at 4:14 PM)
                          if (ToTime(Time[0]) == ToTime(16, 14, 0))
                          {
                          ExitLong("", "");
                          ExitShort("", "");
                          }

                          // Secondary time frame -- Since trades must be issued from the primary instrument,
                          // just set a flag on the secondary time frame.
                          //
                          if (BarsInProgress == 1) // This happens once every 15-min...
                          {
                          wait_for_primary = true; // Check for time to trade on next primary bar.
                          return;
                          }

                          // Primary time frame -- Since trades must be issued from the primary instrument,
                          // we do that here if the flag was set on the secondary time frame.
                          //
                          if (BarsInProgress == 0) // This happens once every 1-min...
                          {
                          if (wait_for_primary)
                          {
                          if (ToTime(Time[0]) > ToTime(13, 0, 0)
                          && ToTime(Time[0]) <= ToTime(16, 00, 0)
                          && SMA(SMAFast)[0] > SMA(SMAFast)[1])
                          {
                          EnterLong(DefaultQuantity, "");
                          }

                          wait_for_primary = false; // Only do this once; wait for another 15-min.



                          }


                          }
                          }

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          619 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          359 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by Mindset, 02-09-2026, 11:44 AM
                          0 responses
                          105 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                          0 responses
                          562 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          566 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X