Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Avoiding trading at the opening

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

    Avoiding trading at the opening

    Haya Support and Community,

    how to set a condiyion in a strategy to only trade after x bars or x time after the opening,to avoid the opening gaps?

    thank you

    #2
    Hello Cachevery,

    Thank you for your post.

    You can use Bars.FirstBarOfSession to get that opening bar.
    You could then use CurrentBar at that time to store a value and then compare it to when your bars past is met

    Sample code -
    Code:
    if(Bars.FirstBarOfSession)
        startingBar = CurrentBar
    
    if(CurrentBar - startingBar >= mySetValue)
    {
         // calculate here
    }
    Let me know if I can be of further assistance.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Cal View Post
      Hello Cachevery,

      Thank you for your post.

      You can use Bars.FirstBarOfSession to get that opening bar.
      You could then use CurrentBar at that time to store a value and then compare it to when your bars past is met

      Sample code -
      Code:
      if(Bars.FirstBarOfSession)
          startingBar = CurrentBar
      
      if(CurrentBar - startingBar >= mySetValue)
      {
           // calculate here
      }
      Let me know if I can be of further assistance.
      I get errors when trying to paste the snippet,Cal.Where do i stick it in?" >=mySetValue" is a numbering value,meaning 1-2 or 3 etc... bars from the opening?

      Comment


        #4
        CacheEvery,

        The startingBar and mySetValue were examples of local variables to store values and then use later to test those values.

        You will need to declare these in the variables section of the script.
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Cal View Post
          CacheEvery,

          The startingBar and mySetValue were examples of local variables to store values and then use later to test those values.

          You will need to declare these in the variables section of the script.
          I don`t get it Cal.Can you please privde the snippt code?

          Comment


            #6
            Cachevery,

            Please see attached file
            Attached Files
            Cal H.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Cal View Post
              Cachevery,

              Please see attached file
              Thanks Cal,where do i paste this part:


              }
              protected override void Initialize()
              {
              Overlay = true;
              CalculateOnBarClose = true;
              }


              protected override void OnBarUpdate()
              {
              //This will set the CurrentBar value to the startingBar variable
              if(Bars.FirstBarOfSession)
              startingBar = CurrentBar;

              //We then check the CurrentBar minus the startingBar value and ensure that it is greater than or equal to the set
              //Value that is set, this case 7 bars
              if(CurrentBar - startingBar >= mySetValue)
              {
              Print("Called when 7 or more bars is past the session open");
              }
              }

              When i try i get erros as well.

              Comment


                #8
                Cachevery,

                Are you including the Variables region as well then pasting the code?

                What is the error message you are getting?

                Keep in mind this is an example how to do this
                Cal H.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Cal View Post
                  Cachevery,

                  Are you including the Variables region as well then pasting the code?

                  What is the error message you are getting?

                  Keep in mind this is an example how to do this
                  Yes,i`ve included the Variable region.But i already have this statements in the code:

                  protected override void Initialize() ;and

                  protected override void OnBarUpdate().

                  Maybe that is the problem

                  Comment


                    #10
                    Correct, you don't want to duplicate those
                    Cal H.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_Cal View Post
                      Correct, you don't want to duplicate those
                      Now i`m getting the message "Overlay name does not exist".Should i put it in the Variable region as well?If so,how?

                      Comment


                        #12
                        Cachevery,

                        Lets try this.

                        Take a couple steps back and only add what is in the OnBarUpdate() and the two variables that are declared in the Variables Region.

                        You would not need to add anything else from my sample script that I supplied
                        Cal H.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_Cal View Post
                          Cachevery,

                          Lets try this.

                          Take a couple steps back and only add what is in the OnBarUpdate() and the two variables that are declared in the Variables Region.

                          You would not need to add anything else from my sample script that I supplied
                          No Cal,though i get no errors now,it still initiates a trade on the opening

                          Comment


                            #14
                            Hello cachevery,

                            Thank you for your response.

                            Please go to the following directory on your PC: (My) Documents\NinjaTrader 7\bin\Custom\Strategy --> then attach the strategy's .cs file to your response so I may investigate this matter further.

                            Comment


                              #15
                              Originally posted by NinjaTrader_PatrickH View Post
                              Hello cachevery,

                              Thank you for your response.

                              Please go to the following directory on your PC: (My) Documents\NinjaTrader 7\bin\Custom\Strategy --> then attach the strategy's .cs file to your response so I may investigate this matter further.
                              So?It`s already there

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by funk10101, Today, 08:14 AM
                              3 responses
                              5 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by frankthearm, Today, 09:08 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post frankthearm  
                              Started by samish18, Yesterday, 08:57 AM
                              11 responses
                              28 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by yertle, Today, 08:38 AM
                              1 response
                              6 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by love2code2trade, Yesterday, 01:45 PM
                              3 responses
                              22 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Working...
                              X