Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

BarsSinceEntryExecution with AdoptAccountPosition

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

    BarsSinceEntryExecution with AdoptAccountPosition

    Ninja Team,

    I have a question concerning Adopt Account Position:

    Since I regularly restart NT8 while in a strategy-entered position, I’m required to use adopt account position so my strategies pick up where they've left off (as I understand it anyway). This has been working great. However, my issue is the strategy now starts with an adopted position (intended) but basically starts at day 1 with any int counters or anything else I’m updating using historical data (not intended).

    What I'm really needing to keep track of is historical daily close values, starting from the date the trade was placed. I’ve attempted using Close[i], where i = BarsSinceEntryExecution(), then decrement to reach current day via loop, but when the strategy adopts the account position, BarsSinceEntryExecution() is always 1, even though the actual execution occurred weeks before.

    Is there a way I can access the real execution date (# of bars ago) while still using adopt account position?

    Thanks,

    Ryan

    #2
    Update a variable to store the last execution bar in OnExecution() ?

    Comment


      #3
      Would said variable still be saved after disabling then re-enabling the strategy? Why OnExecution()?

      Comment


        #4
        If the main challenge is persisting variables between disabling and re-enabling the strategy, then you will not be able to do this natively with NinjaScript. Your best bet here would be to store any critical information outside of the strategy.

        As a rudimentary example, perhaps you could use StreamWriter to store variable values in a text file, then detect in your code whether or not you had adopted an account position, and if so, you could then parse the text in that file back to the variables in question.
        Dave I.NinjaTrader Product Management

        Comment


          #5
          Originally posted by ak5745 View Post
          Would said variable still be saved after disabling then re-enabling the strategy? Why OnExecution()?
          Yes, because your historical orders still show up, even if you are going to adopt account position.

          You use OnExecution() because that is the immediate place that you can query when the execution takes place, and what the characteristics may be of said execution..

          Comment


            #6
            koganam,

            I like your idea of OnExecution(), but am a little cloudy as to its implementation. As I understand it, OnExecution() is event-driven - called after an execution is filled. In my case, where the execution in question occurred potentially weeks ago, will this method be called each time I re-enable a strategy, given that strategy is adopting an account position? If so, would I then be able to access the execution's date using an Execution object? Do you have an example?

            Comment


              #7
              Originally posted by ak5745 View Post
              koganam,

              I like your idea of OnExecution(), but am a little cloudy as to its implementation. As I understand it, OnExecution() is event-driven - called after an execution is filled. In my case, where the execution in question occurred potentially weeks ago, will this method be called each time I re-enable a strategy, given that strategy is adopting an account position? If so, would I then be able to access the execution's date using an Execution object? Do you have an example?
              No real need for an example.
              1. Just stick in a Print() statement in OnExecution(), to print the barNumber.
              2. Compile.
              3. Clear the Output Window.
              4. Enable Strategy.
              5. Examine Output Window to see what got Print()ed.

              As I said every historical trade will be taken when you enable the strategy. So will all the events that happen for each trade. That is why we can examine what happens to our orders, if we use the TraceOrders directive.

              Comment


                #8
                Originally posted by koganam View Post
                No real need for an example.
                1. Just stick in a Print() statement in OnExecution(), to print the barNumber.
                2. Compile.
                3. Clear the Output Window.
                4. Enable Strategy.
                5. Examine Output Window to see what got Print()ed.

                As I said every historical trade will be taken when you enable the strategy. So will all the events that happen for each trade. That is why we can examine what happens to our orders, if we use the TraceOrders directive.
                Does it matter whether I have managed or unmanaged strategies in using your method above? I've always used the managed approach in the past, but lately have been educating myself on the unmanaged approach to order handling (I prefer more control). But as of this moment, all my strategies are managed.

                Comment


                  #9
                  I think what I'm trying to ask is whether or not I need to use Order objects in my code to achieve what you're describing koganam.

                  Comment


                    #10
                    Originally posted by ak5745 View Post
                    I think what I'm trying to ask is whether or not I need to use Order objects in my code to achieve what you're describing koganam.
                    As you seem to be saying that you are the same kind of anal-retentive code-control freak that I am, that would actually be easiest, but you do not have to: you can always query the execution that OnExecution() is handling, to find out if it is the execution that you want to handle, then go from there. Most likely, you want to look at execution.Order.OrderAction, to see if it is an entry.

                    Comment


                      #11
                      Originally posted by koganam View Post
                      As you seem to be saying that you are the same kind of anal-retentive code-control freak that I am, that would actually be easiest, but you do not have to: you can always query the execution that OnExecution() is handling, to find out if it is the execution that you want to handle, then go from there. Most likely, you want to look at execution.Order.OrderAction, to see if it is an entry.
                      Guilty as charged. Good to know there are others out there!

                      So after several days, I'm unable to get OnExecutionUpdate() to fire on a strategy that is re-enabled with AdoptAccountPosition as its starting behavior. Personally, I believe this result is to be expected. According to the documentation, OnExecutionUpdate() will only be called on positions that were strategy-entered. Given that these positions were strategy-entered, I believe that when we AdoptAccountPosition, this fact is "forgotten," or maybe only exists while the strategy is enabled. Also, I don't think NT can discern between positions manually-entered and strategy-entered once we re-enable. Hoping someone out there can either confirm or deny that belief.

                      I have StreamWriter working, but am looking for something fully contained in NT, if possible. Any ideas what I might be doing wrong here?

                      Thanks all.

                      Ryan

                      Comment


                        #12
                        Hello ak5745,

                        EDIT: It appears I was incorrect with what was said here in regards to historical calls of OnExecutionUpdate(). Koganam has provided an excellent example.

                        As a reminder, if you haven't already updated to the latest version of NinjaTrader, please note that all NinjaTrader users MUST update to NinjaTrader 7.0.1000.31 by Saturday, February 27th if they wish to continue using NinjaTrader; older versions of NinjaTrader will no longer work after the 27th. We encourage everyone to update now to avoid interruption in service.

                        None of your settings, work spaces, indicators, etc. will be affected if you follow the directions below.

                        To install the update please follow these instructions:
                        1. CRITICAL Shut Down NinjaTrader
                        2. Download the update HERE
                        3. Double click the file once it has completed downloading and follow the installation prompts on your screen

                        For more information about this critical update please click HERE.

                        Please, let us know if we may be of further assistance.
                        Last edited by NinjaTrader_ZacharyG; 02-26-2016, 03:26 PM.
                        Zachary G.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by ak5745 View Post
                          Guilty as charged. Good to know there are others out there!

                          So after several days, I'm unable to get OnExecutionUpdate() to fire on a strategy that is re-enabled with AdoptAccountPosition as its starting behavior. Personally, I believe this result is to be expected. According to the documentation, OnExecutionUpdate() will only be called on positions that were strategy-entered. Given that these positions were strategy-entered, I believe that when we AdoptAccountPosition, this fact is "forgotten," or maybe only exists while the strategy is enabled. Also, I don't think NT can discern between positions manually-entered and strategy-entered once we re-enable. Hoping someone out there can either confirm or deny that belief.

                          I have StreamWriter working, but am looking for something fully contained in NT, if possible. Any ideas what I might be doing wrong here?

                          Thanks all.

                          Ryan
                          As I needed to myself personally check this out, AdoptAccountPosition itself being MY suggestion in the first place, I finally stopped theorizing and wrote a strategy to test this out.

                          That strategy is attached to this post, and as you will see when you look in the Output Window, it perfectly tracks the number of bars of the last exit. If your code is not doing so, then you have a different problem: there is nothing wrong with what I suggested.
                          Attached Files
                          Last edited by koganam; 05-14-2016, 09:12 PM. Reason: Corrected spelling.

                          Comment


                            #14
                            Originally posted by NinjaTrader_ZacharyG View Post
                            Hello ak5745,

                            OnExecutionUpdate() will not be called as the order is not being re-submitted and filled again. The order already exists and the strategy is merely adopting the current account position with the "Adopt account position" start behavior chosen.

                            ...
                            Sorry, but it would appear that you are wrong. We can see what happened in historical executions. Please see the other reply: http://ninjatrader.com/support/forum...266#post450266

                            Comment


                              #15
                              Hello koganam,

                              Thank you for providing that sample.

                              It looks like I was testing this on my end incorrectly. You are, indeed, correct that historical executions will be caught through OnExecutionUpdate().
                              Zachary G.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by algospoke, Today, 06:40 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post algospoke  
                              Started by maybeimnotrader, Today, 05:46 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post maybeimnotrader  
                              Started by quantismo, Today, 05:13 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post quantismo  
                              Started by AttiM, 02-14-2024, 05:20 PM
                              8 responses
                              168 views
                              0 likes
                              Last Post jeronymite  
                              Started by cre8able, Today, 04:22 PM
                              0 responses
                              10 views
                              0 likes
                              Last Post cre8able  
                              Working...
                              X