• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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.
                            Patrick H.NinjaTrader Customer Service

                            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 Bouzayen, Yesterday, 08:31 PM
                              0 responses
                              14 views
                              0 likes
                              Last Post Bouzayen  
                              Started by IanC28, 03-27-2020, 10:20 AM
                              6 responses
                              30 views
                              0 likes
                              Last Post jim1234
                              by jim1234
                               
                              Started by aramtrader, 04-01-2020, 03:05 PM
                              7 responses
                              40 views
                              0 likes
                              Last Post woodie1
                              by woodie1
                               
                              Started by renehtx, Yesterday, 03:51 PM
                              2 responses
                              16 views
                              0 likes
                              Last Post renehtx
                              by renehtx
                               
                              Started by eidog2020, Yesterday, 03:31 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post eidog2020  
                              Working...
                              X