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

Storing and Accessing position attributes in OnBarUpdate()

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

    Storing and Accessing position attributes in OnBarUpdate()

    I have a strategy that can enter a long trade in different market regimes and has different exit rules based on what regime that trade was entered under.

    What is the recommended way of storing the regime attribute regarding this opening position and how do I access that in OnBarUpdate()?

    Is it through SignalName?
    Any issues I would have when starting a Strategy that I should consider?

    #2
    Hello RandyT,

    What are you referring to "market regimes" and "regime attribute"?

    Are you adding multiple series with different instruments to a script?

    You would need to assign orders placed by the strategy to a variable to reference this later. The OnOrderUpdate demonstrates in the help guide publicly linked below.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Chelsea,

      For example, long position is entered in "trending regime" which I identified by strategy analysis.

      Once entered, I need to apply rules as follows for the exit and I want to apply the rules based on the regime type that it was entered under since that regime type could change while the trade is open.

      Code:
      if (IsLong)
          if (traderegimetype == "trending" && rsi[0] > .9)
              ExitLong();
          else if (traderegimetype == "meanreverting" && rsi[0] < .3)
               ExitLong();
      Needing some way to store the regimetype when entering the trade. First thought is in signalname but wanted to get other input.

      Comment


        #4
        Hello RandyT,

        I am not understanding your terminology, however, it seems you are asking about custom logic and variable names in your script and not necessarily NinjaScript properties or methods.

        You can create a custom series to store custom information for each bar. (For example a series of strings)

        Below is a public link to the help guide on Series<T>.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I've tried using a Series to store this information and it seems that because several bars could pass before an exit, I cannot count on the previous regimeseries[1] to be the value for the type of market that the open position was entered under.

          Struggling with how to make this more clear...

          I want to store some information about the open position so that I know what exit rules to apply in OnBarUpdate(); That information is known when the trade is opened so I need to store a value when opening the trade that I can reference in each pass through OnBarUpdate() looking for a match on exit rules depending on that open trade metadata.

          Comment


            #6
            Hello RandyT,

            You only want to store information for the most recent position?

            Are you saving the information to a variable when the entry order fills?

            Is this information no longer saved when the position is open or after the exit order fills?

            Are you wanting to get this information for previous positions? (If so, save the current bar numbers for the entry and or exit in an array or list and use this to get a barsAgo value to call information from a series)
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ChelseaB View Post
              Hello RandyT,

              You only want to store information for the most recent position?
              I only care about the currently open position as I tried to show in the bit of pseudo code.

              Are you saving the information to a variable when the entry order fills?
              I am looking for some place to store this information when the order is filled.

              Is this information no longer saved when the position is open or after the exit order fills?
              I don't care about this information after the position is exited. I am applying different exit rules based on the value that I am storing as I tried to show in the pseudo code.

              Are you wanting to get this information for previous positions? (If so, save the current bar numbers for the entry and or exit in an array or list and use this to get a barsAgo value to call information from a series)
              I just need this info for the currently open position. This seems like a lot of work to go through to get that info, but if that is my best option... Is there not some value available that would tell me what bar index the position was entered on? Or a place in the position info that I could store some string or integer?

              Comment


                #8
                Hello RandyT,

                To confirm you want to store information for the open position only.

                Are you saving the information to a string variable?

                Is this information no longer saved when the position is open?

                What is the issue you are having trying to use the saved value after the position is opened?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_ChelseaB View Post
                  Hello RandyT,
                  To confirm you want to store information for the open position only.
                  Yes. I only want info for the open position.

                  Are you saving the information to a string variable?
                  My first attempt has been to store a 1 or 0 in a Series<int> to get the value of the open position to apply the appropriate exit rules. I'm realizing that unless this exit occurs on the next bar (which is rare), the value of traderegime[1] is not useful. I need the BarIndex that the position was opened on.

                  As mentioned in my first post, I could also use the SignalName to set a name that represents the opening regimetype, but was not sure that was a dependable option and not sure how to access that in OnBarUpdate() when I am looking for a match of my exit signal criteria.

                  Is this information no longer saved when the position is open?

                  What is the issue you are having trying to use the saved value after the position is opened?
                  As mentioned, it seems like an unreasonable amount of effort to conjure up the BarsIndex for the open position which leads me to think there is an easier way to do this. Thus, my question to the forum...

                  Comment


                    #10
                    I guess the other obvious approach would be to continue to update the regime info if there is an open position. Like so:

                    Code:
                    If (openposition)
                        if (long && (regime[1] == 1) && rsi[0] > .5)
                            ExitLong();
                        else if (long && (regime[1] == 0) && rsi[0] < .5)
                            ExitLong();
                    
                    regime[0] = regime[1]
                    Would be open to other suggestions.

                    Comment


                      #11
                      Hello RandyT,

                      Since you don't need information from previous positions, there isn't any need to use a Series which would allow information to be saved for each bar.

                      Instead, without using a series and just assigning the value you want to access later to a variable, what is the issue you are having that is preventing you from using this?

                      Is the value no longer saved?

                      Is the variable null?

                      I was not able to verify, what is the issue you are having trying to use the saved value after the position is opened?

                      Are you asking if there is an easier way to reference a stored value without using a variable? (No, you would need to save the value to a variable to reference the value later)
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Using a global variable for this just seems a bit of a hack and since there is a data structure associated with open positions, I thought there might be a logical place to store this info with the position data. And the SignalName seemed it might be the mechanism to get there.

                        Comment


                          #13
                          Hello RandyT,

                          A global variable should not be necessary.
                          (What are you referring to as a Global variable? One that is in a partial class of Strategy? One in a custom namespace?)

                          A Series would not be necessary if you are only dealing with one position (the open position).

                          What is the exact issue you are having?

                          Have you created a private variable within the scope of the class?

                          Are you assigning a value to this variable when the entry order fills (you could do this in OnExecution, OnOrderUpdate, OnPositionUpdate, or even in OnBarUpdate on the first bar update where the position has changed)?

                          Are you waiting until a later time and then checking the value of this variable?

                          Is the value an unexpected value?
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Chelsea,

                            Not sure where the disconnect is here. I'm not posting a problem. I am looking for advice as to how to do what I want to accomplish.

                            Comment


                              #15
                              Hello RandyT,

                              A variable is used to hold a value and retrieve the stored value at a later time (like on a later bar).

                              Create a private local variable within the scope of the Strategy class to hold the value. (Meaning within the curly braces of the class declaration only)

                              Assign the desired value to the private variable when the entry order fills in OnExecution.

                              Any where in the script you would like check the position to be open, if its open use the variable which has the stored value any way you would like.

                              To check if a position is open:

                              if (Position.MarketPosition != MarketPosition.Flat)
                              {
                              // a position is open, execute code here
                              }

                              As an example, below is a public link to a 3rd party educational site on integers.
                              Evaluate the int and uint types. Use the 4-byte integer type in a program.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by FrazMann, Today, 11:21 AM
                              2 responses
                              6 views
                              0 likes
                              Last Post NinjaTrader_ChristopherJ  
                              Started by rjbtrade1, 11-30-2023, 04:38 PM
                              2 responses
                              80 views
                              0 likes
                              Last Post DavidHP
                              by DavidHP
                               
                              Started by Spiderbird, Today, 12:15 PM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_ChristopherJ  
                              Started by lorem, Yesterday, 09:18 AM
                              5 responses
                              18 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by cmtjoancolmenero, Yesterday, 03:58 PM
                              12 responses
                              42 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X