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

Understanding start behavior

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

    Understanding start behavior

    Please help me to understand Start Behavior for a strategy. I currently have the behavior set to "Wait until flat, synchronize account". But I just activated a strategy, and on the chart it shows that there is an active short on the chart. I believe currently the strategy will wait until that trade finishes, and then it will start initiating trades once the strategy is flat. How can I get the strategy to activate a trade on the NEXT signal, without having it activate immediately based on the last signal?

    #2
    Hello Bionian,

    The strategy will process historical data before real-time data.
    Historical orders will appear on the chart.
    https://ninjatrader.com/support/foru...442#post782442

    With Wait until flat, if the strategy calculates a historical position open, it will wait until a virtual order closes the historical position before placing any real-time orders.
    Once the strategy has become flat or crossed through flat, the strategy name will become green indicating the strategy is now sending real-time orders.
    https://ninjatrader.com/support/foru...541#post811541

    To prevent a strategy from processing any historical data use:
    if (State == State.Historical)
    return;

    To exit a position on the last historical bar use:
    if (State == State.Historical && CurrentBar == Count - 2)
    {
    if (Position.MarketPosition == MarketPosition.Long)
    ExitLong();
    if (Position.MarketPosition == MarketPosition.Short)
    ExitShort();
    }
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I have been trying to study C#, but since this will be by first time unlocking code, where should I place this? And I assume I begin at the "if" statement?

      Comment


        #4
        Hello Bionian,

        This logic would be in OnBarUpdate().


        Below I am providing a public link to a forum post with helpful information about getting started with NinjaScript.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Chelsea:

          I inserted your code into the strategy. I don't necessarily want to prevent the strategy from processing historical data, but I do want to be able to apply the script and begin executing trades immediately, without waiting for the virtual order to close the historical position, when I see signs from price action that the market has temporarily converted into a sideways market. Can you tell me if I inserted the script properly? The strategy appear to still function, but I don't know if it will accurately begin generating signals on live data immediately.

          OneMinSwingIndScalpCloseOnRevC345StartNow.zip

          Thanks-
          Rick

          Comment


            #6
            Hello Bionian,

            Yes, at the top of OnBarUpdate(), this would cause any historical position to be exited on the last historical bar.

            Are you finding that the strategy still has a virtual historical position (showing yellow on the Strategies tab of the Control Center) once real-time data is being received when you are testing?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Yes Chelsea.

              Since I have had an opportunity to test the strategy today, it appears that it is doing just want I wanted. I it is exiting any open positions, but still showing historical trades, so I can continue to monitor the effectiveness of the strategy. This will help me learn to program. I saved a copy of the strategy which is still available in Chart Builder, so I can continue to modify it. Then I can compare the two, before and after then copy and paste the new code into the strategy you see here.

              Thanks for you help, as always. Hopefully before long I will be able to code them myself.

              Rick

              Comment


                #8
                Originally posted by NinjaTrader_ChelseaB View Post
                Hello Bionian,

                Yes, at the top of OnBarUpdate(), this would cause any historical position to be exited on the last historical bar.

                Are you finding that the strategy still has a virtual historical position (showing yellow on the Strategies tab of the Control Center) once real-time data is being received when you are testing?
                Just to chime in here, this sounds like what my strategy is doing. After my reboot during market close, when I reactivate the strategy using the "wait until flat" the strategy never turns green when the market opens, it remains yellow because it still has a virtual position. Would changing the start behavior to "immediately submit, synchronize account" be the correct setting? My strategy exits all positions at market close, then are supposed to reenter upon market open but if I'm rebooting my computer during market close, I think the "wait until flat" is the wrong setting.

                Comment


                  #9
                  Hello trader14,

                  Are you only enabling the strategy before the market opens?

                  If not, the historical position may not be flat if there is data process.

                  See below about resuming a strategy.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_ChelseaB View Post
                    Hello trader14,

                    Are you only enabling the strategy before the market opens?

                    If not, the historical position may not be flat if there is data process.

                    See below about resuming a strategy.
                    http://ninjatrader.com/support/forum...332#post499332
                    My goal was to restart my computer during the 1 hr break each day. The problem however, is that when I do this, I have to restart the strategy. When I restart the strategy using the "wait until flat" behavior, the strategy remains yellow and never turns green, even when the market opens back up. (It doesn't realize it SHOULD be flat during the close, as my strategy is set to exit all positions upon close, then reenter upon the open.) Is "immediately submit, synchronize account" the appropriate start behavior to use then? Is there a way for the strategy to realize it should be in a flat state during market close while using the "wait until flat" start behavior? Thanks for the help on this!

                    Comment


                      #11
                      Hello trader14,

                      If you restart a strategy after the market is open, then even if it was flat when the market opened that is not going to prevent new orders from being placed after the market is open.

                      Are you trying to prevent all new orders after the market is open?

                      Resuming a strategy would require Immediately Submit. This is outlined in the forum post I have linked.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by NinjaTrader_ChelseaB View Post
                        Hello trader14,

                        If you restart a strategy after the market is open, then even if it was flat when the market opened that is not going to prevent new orders from being placed after the market is open.

                        Are you trying to prevent all new orders after the market is open?

                        Resuming a strategy would require Immediately Submit. This is outlined in the forum post I have linked.
                        No, not at all, you're not hearing me. My strategy exits on session close each day and also utilizes on bar close as part of the strategy. I initially liked the idea of rebooting my PC during the 1 hr break to keep things clean but I noticed that rebooting messes with the strategy behavior and there isn't a start behavior to solve the problem. "Wait until flat" doesn't work because even though the position was exited just prior to close, when I enable the strategy during the close in preparation for the open, it puts on a virtual position. It shouldn't do this, it should be flat, because the market is closed but apparently the strategy isn't aware of this. Using "immediately submit synchronize position" isn't necessarily any better as it will place the order upon the open even though the bar hasn't completed yet. Again, my strategy needs a full bar to complete to know what to do. So, basically, I can't every turn my computer off, ever. There's gotta be a better solution for this.

                        Comment


                          #13
                          Hello trader14,

                          I'm not clear on how Exit on session close is involved.

                          The strategy is calculating a historical position with historical data every time it is enabled.

                          This includes historical data from the new session.

                          It sounds like you are wanting to disable a strategy and re-enable a strategy and have the position be resumed as described in the forum post I have linked. Is this not what you are wanting to do?
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Originally posted by NinjaTrader_ChelseaB View Post
                            Hello trader14,

                            I'm not clear on how Exit on session close is involved.

                            The strategy is calculating a historical position with historical data every time it is enabled.

                            This includes historical data from the new session.

                            It sounds like you are wanting to disable a strategy and re-enable a strategy and have the position be resumed as described in the forum post I have linked. Is this not what you are wanting to do?
                            You guys still aren't getting it, this is simple, even Bionian in this post list understands lol. I'll explain again. Obviously, I have to initially start your strategy at some point using a particular start behavior, "wait until flat" for example. Once it starts, my strategy will function normally assuming you NEVER need to restart it again (this is not realistic obviously). Assuming the strategy is running normally as it should without being restarted, it exits all positions just before close, then remains GREEN during the 1 hour close because the strategy is flat and the account is flat. It's then ready to trade upon the open, wait for the first bar to complete, and then make a trade.

                            However, if I attempt to restart my strategy DURING THE 1 HOUR BREAK using "wait until flat" as I want to do to reboot/refresh my PC, I initially I expected the strategy to turn GREEN during the 1 hour close. I wrongly expected it to know that it should be in a flat state (as the strategy dictates all positions are exited just before market close) and be ready to trade at the open. However, I now know this isn't the case. When the my strategy is restarted during the close using the "wait until flat" start behavior, it ALWAYS enters a virtual position (turns and stays yellow) and does not realize that it really should be flat according to the strategy rules. This is a problem because now, my strategy won't trade the way it normally should had I been leaving it running as the above paragraph mentions. The only alternative is to use "immediately submit" but that doesn't work either because it violates my strategy rules but not waiting for a bar to fully close.

                            So, Ninjatrader doesn't have a start behavior that works my strategy. I can't reboot my PC during the close because neither "wait until flat" or "immediately submit" will allow my strategy to trade the way it should. The only alternative for me, is to start the strategy once and leave it running forever.

                            Comment


                              #15
                              Hello trader14,

                              I may still not be understanding.

                              It still sounds like you want to disable and re-enable a strategy and have it resume the position (the strategy would start green not yellow).

                              Can you clarify further if this is not what you are wanting to do?


                              Yes, a strategy with wait until flat that starts with a historical position will ALWAYS wait until flat before it turns green.
                              Your strategy logic is what is creating the historical position.

                              When you mention: "The only alternative is to use "immediately submit" but that doesn't work either because it violates my strategy rules but not waiting for a bar to fully close."
                              I do not understand your meaning by 'not waiting for a bar to fully close'.
                              You can wait for a bar to fully close. You can do something before a bar fully closes. You can do either. What is the issue?
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by TraderBCL, Today, 04:38 AM
                              2 responses
                              17 views
                              0 likes
                              Last Post TraderBCL  
                              Started by martin70, 03-24-2023, 04:58 AM
                              14 responses
                              106 views
                              0 likes
                              Last Post martin70  
                              Started by Radano, 06-10-2021, 01:40 AM
                              19 responses
                              609 views
                              0 likes
                              Last Post Radano
                              by Radano
                               
                              Started by thanajo, 05-04-2021, 02:11 AM
                              4 responses
                              471 views
                              0 likes
                              Last Post tradingnasdaqprueba  
                              Working...
                              X