Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Breakeven Indicator Backtest

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

    #16
    Hello tjames3245,

    Thanks for your notes.

    There would be no methods or properties available to increase the speed by which orders are filled.

    You could try unplugging your internet modem, and (if a separate device) router for at least 60 seconds, and also restart the Computer to see if the behavior you are reporting is due to internet connection.​

    If you are not already and the option is available to you, I highly recommend connecting to the internet using an ethernet cable. Wireless connections are significantly more unreliable

    NinjaTrader is sensitive to latency. While other applications are able to connect to the internet, it is still possible to have issues occur if the connection has slowed. Many other applications do not receive a steady stream of data and do not require a steady connection and are not affected by latency. Common reasons that contribute to connection stability are:
    • Distance from local computer to the connection servers
    • Connection through a mobile hotspot (high latency)
    • Connecting through Wifi (connecting wirelessly contributes to latency)
    • ISP issues such as unstable latency
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #17
      Originally posted by NinjaTrader_BrandonH View Post
      Hello tjames3245,

      Thanks for your notes.

      There would be no methods or properties available to increase the speed by which orders are filled.

      You could try unplugging your internet modem, and (if a separate device) router for at least 60 seconds, and also restart the Computer to see if the behavior you are reporting is due to internet connection.​

      If you are not already and the option is available to you, I highly recommend connecting to the internet using an ethernet cable. Wireless connections are significantly more unreliable

      NinjaTrader is sensitive to latency. While other applications are able to connect to the internet, it is still possible to have issues occur if the connection has slowed. Many other applications do not receive a steady stream of data and do not require a steady connection and are not affected by latency. Common reasons that contribute to connection stability are:
      • Distance from local computer to the connection servers
      • Connection through a mobile hotspot (high latency)
      • Connecting through Wifi (connecting wirelessly contributes to latency)
      • ISP issues such as unstable latency
      Hi Brandon, this is all on playback data that has been downloaded to local machine. So I don't think any of this would be related. Either way, internet speeds and ping are all very good. I had a friend test the code on his end and the same issue occurs.

      We are still in testing phase.

      I tried the fill immediately option for simulation but it made no difference.
      Last edited by tjames3245; 08-03-2023, 08:19 AM.

      Comment


        #18
        Hello tjames3245,

        Thanks for your notes.

        Does the behavior happen when testing the SampleAtmStrategy strategy that comes default with NinjaTrader?

        In the code you shared in post # 15, I suggest removing the condition that checks if State == State.Realtime in your condition to place the order and instead add the code below to the top of the OnBarUpdate() method as seen in the SampleAtmStrategy script.

        if(State == State.Historical)
        return;


        Also, you should add conditions to the script that checks if orderId.Length == 0 && atmStrategyId.Length == 0 in your condition to place the order. This can also be seen in the SampleAtmStrategy script.

        Reducing the script could be done to see if something specific in your code is causing the behavior to occur. Reducing a script is done by commenting out or removing lines or sections of code to see if a specific line or section of code in your script is causing the behavior to happen.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #19
          Originally posted by NinjaTrader_BrandonH View Post
          Hello tjames3245,

          Thanks for your notes.

          Does the behavior happen when testing the SampleAtmStrategy strategy that comes default with NinjaTrader?

          In the code you shared in post # 15, I suggest removing the condition that checks if State == State.Realtime in your condition to place the order and instead add the code below to the top of the OnBarUpdate() method as seen in the SampleAtmStrategy script.

          if(State == State.Historical)
          return;


          Also, you should add conditions to the script that checks if orderId.Length == 0 && atmStrategyId.Length == 0 in your condition to place the order. This can also be seen in the SampleAtmStrategy script.

          Reducing the script could be done to see if something specific in your code is causing the behavior to occur. Reducing a script is done by commenting out or removing lines or sections of code to see if a specific line or section of code in your script is causing the behavior to happen.
          So when testing the SampleAtmStrategy, on the first buy and sell, it stopped out within about 48 ticks - I had stop loss set to 45 ticks so this is completely reasonable.

          I am going to try your suggestions and report back to see what that does. Thank you once again! I feel like I'm getting closer to having this working with your help.



          On 2nd glance, it may be due to testing data from May and June - around rollover of NQ contract which caused a drop off in volume. So I am still investigating this.


          Last edited by tjames3245; 08-04-2023, 08:45 AM.

          Comment


            #20
            So everything looks ok now via testing on market replay.

            But...

            I went ahead and ran a live demo test during market hours and for some reason strategy at some point entered 10 quantity - it kept buying...

            Is there anything in the code I posted that may have caused it to enter multiple orders? I don't want it to keep adding to the position if it is already in play. Maybe I need to add an additional statement to the if statement about position quantity so it doesn't keep rebuying while in play?

            Code below...




            protected override void OnBarUpdate()
            { ​
            if (BarsInProgress != 0)
            return;

            if (CurrentBars[0] < 1)
            return;

            // Make sure this strategy does not execute against historical data
            if(State == State.Historical)
            return;

            /*/////////// This handles Longs below ////////////////*/

            // Set 1
            if(orderId.Length == 0
            && atmStrategyId.Length == 0
            && (DM1.DiPlus[0] > DM1.DiMinus[0])
            && (Position.MarketPosition == MarketPosition.Flat))

            {
            isAtmStrategyCreated = false; // reset atm strategy created check to false
            atmStrategyId = GetAtmStrategyUniqueId();
            orderId = GetAtmStrategyUniqueId();
            AtmStrategyCreate(OrderAction.Buy, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId, (atmCallbackErrorCode, atmCallBackId) => {
            //check that the atm strategy create did not result in error, and that the requested atm strategy matches the id in callback
            if (atmCallbackErrorCode == ErrorCode.NoError && atmCallBackId == atmStrategyId)
            isAtmStrategyCreated = true;
            });
            Draw.ArrowUp(this, @"ADXDMI2 Arrow up_1 " + Convert.ToString(CurrentBars[0]), true, 0, (High[0] + (20 * TickSize)) , Brushes.Green);
            }

            /*/////////// This handles shorts below ////////////////*/

            // Set 6
            if (orderId.Length == 0
            && atmStrategyId.Length == 0
            && (DM1.DiPlus[0] < DM1.DiMinus[0])
            && (Position.MarketPosition == MarketPosition.Flat))
            {
            atmStrategyId = GetAtmStrategyUniqueId();
            orderId = GetAtmStrategyUniqueId();
            AtmStrategyCreate(OrderAction.Sell, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId, (atmCallbackErrorCode, atmCallBackId) => {
            //check that the atm strategy create did not result in error, and that the requested atm strategy matches the id in callback
            if (atmCallbackErrorCode == ErrorCode.NoError && atmCallBackId == atmStrategyId)
            isAtmStrategyCreated = true;
            });
            Draw.ArrowDown(this, @"ADXDMI2 Arrow down_1 " + Convert.ToString(CurrentBars[0]), true, 0, (Low[0] - (20 * TickSize)) , Brushes.Red);
            }


            // Check that atm strategy was created before checking other properties
            if (!isAtmStrategyCreated)
            return;

            // Check for a pending entry order
            if (orderId.Length > 0)
            {
            string[] status = GetAtmStrategyEntryOrderStatus(orderId);

            // If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
            if (status.GetLength(0) > 0)
            {

            // If the order state is terminal, reset the order id value
            if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
            orderId = string.Empty;
            }
            } // If the strategy has terminated reset the strategy id
            else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
            atmStrategyId = string.Empty;

            /*//////////////// This handles any extra condtions below /////////////////*/

            if(new TimeSpan(13, 55, 0) == Times[0][0].TimeOfDay)
            {
            AtmStrategyClose(atmStrategyId);
            }
            }​


            I am also adding the log to show where the position size was increasing... I also noticed "market position flat = remove"
            Click image for larger version

Name:	image.png
Views:	187
Size:	86.8 KB
ID:	1263103
            Last edited by tjames3245; 08-04-2023, 11:15 AM.

            Comment


              #21
              Hello tjames3245,

              Thanks for your notes.

              I do not see anything specific in the code you shared that would cause the strategy to continue placing multiple orders.

              In the screenshot you shared the market position is flat on the MNQ SEP23 instrument since it states MarketPosition = Flat and the Quantity = 0.

              If you only want your strategy to submit 1 order, you should ensure that your Atm Strategy Template's 'Order quantity' is set to the number of entries you want to place. Also, ensure that the strategy's EntryHandling property is set to 'All entries' and the 'Entries per direction' property is set to the number of orders you want to be placed.

              That said, to understand why the script is behaving as it is, such as placing orders or not placing orders when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

              Below is a link to a forum post that demonstrates how to use prints to understand behavior.
              https://ninjatrader.com/support/foru...121#post791121
              <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

              Comment


                #22
                Originally posted by NinjaTrader_BrandonH View Post
                Hello tjames3245,

                Thanks for your notes.

                I do not see anything specific in the code you shared that would cause the strategy to continue placing multiple orders.

                In the screenshot you shared the market position is flat on the MNQ SEP23 instrument since it states MarketPosition = Flat and the Quantity = 0.

                If you only want your strategy to submit 1 order, you should ensure that your Atm Strategy Template's 'Order quantity' is set to the number of entries you want to place. Also, ensure that the strategy's EntryHandling property is set to 'All entries' and the 'Entries per direction' property is set to the number of orders you want to be placed.

                That said, to understand why the script is behaving as it is, such as placing orders or not placing orders when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

                Below is a link to a forum post that demonstrates how to use prints to understand behavior.
                https://ninjatrader.com/support/foru...121#post791121
                Thank you for the reply. I was able to get this issue resolved.


                Do you happen to know if there is any way to get ATM strategy to close a position and flip side from long to short or vise versa if conditions come into play based on IF statement?

                I know this is the case with short and long positions in ninja strategies, but I haven't been able to implement this when it comes to ATM strategy...

                Any ides?

                Comment


                  #23
                  Hello tjames3245,

                  Thanks for your notes.

                  The AtmStrategyClose() method would need to be used to close an Atm Strategy that is being used in a custom NinjaScript strategy. This method cancels any working orders and closes any open position of a strategy using the default ATM strategy close behavior.

                  Then, you could call AtmStrategyCreate() method to place an order in the opposite direction.

                  See this help guide page for more information about the AtmStrategyClose() method and sample code: https://ninjatrader.com/support/help...ategyclose.htm

                  See this help guide page for more information about the AtmStrategyCreate() method: https://ninjatrader.com/support/help...tegycreate.htm
                  <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                  Comment


                    #24
                    Originally posted by NinjaTrader_BrandonH View Post
                    Hello tjames3245,

                    Thanks for your notes.

                    The AtmStrategyClose() method would need to be used to close an Atm Strategy that is being used in a custom NinjaScript strategy. This method cancels any working orders and closes any open position of a strategy using the default ATM strategy close behavior.

                    Then, you could call AtmStrategyCreate() method to place an order in the opposite direction.

                    See this help guide page for more information about the AtmStrategyClose() method and sample code: https://ninjatrader.com/support/help...ategyclose.htm

                    See this help guide page for more information about the AtmStrategyCreate() method: https://ninjatrader.com/support/help...tegycreate.htm
                    So I am utilizing AtmstrategyClose command properly for some other aspects, but I am looking for a way to use my if statements in conjunction with account position.

                    Is there a command you might be aware of that will allow me to say if x,y,z + account position (long) , then AtmStrategyClose ? I am essentially trying to get my strategy to close long positions only when certain conditions are met and the account position is long.

                    Comment


                      #25
                      Hello tjames3245,

                      Thanks for your notes.

                      PositionAccount.MarketPosition could be used to see if you are in a flat, short, or long Account Position.

                      For example, the code below checks if your account position is long:

                      if (PositionAccount.MarketPosition == MarketPosition.Long)
                      {
                      //do something
                      }


                      See the help guide documentation below for more information.

                      PositionAccount.MarketPosition: https://ninjatrader.com/support/help...marketposition
                      <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                      Comment


                        #26
                        Originally posted by NinjaTrader_BrandonH View Post
                        Hello tjames3245,

                        Thanks for your notes.

                        PositionAccount.MarketPosition could be used to see if you are in a flat, short, or long Account Position.

                        For example, the code below checks if your account position is long:

                        if (PositionAccount.MarketPosition == MarketPosition.Long)
                        {
                        //do something
                        }


                        See the help guide documentation below for more information.

                        PositionAccount.MarketPosition: https://ninjatrader.com/support/help...marketposition
                        Thank you for this!

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by NullPointStrategies, Today, 05:17 AM
                        0 responses
                        39 views
                        0 likes
                        Last Post NullPointStrategies  
                        Started by argusthome, 03-08-2026, 10:06 AM
                        0 responses
                        124 views
                        0 likes
                        Last Post argusthome  
                        Started by NabilKhattabi, 03-06-2026, 11:18 AM
                        0 responses
                        64 views
                        0 likes
                        Last Post NabilKhattabi  
                        Started by Deep42, 03-06-2026, 12:28 AM
                        0 responses
                        41 views
                        0 likes
                        Last Post Deep42
                        by Deep42
                         
                        Started by TheRealMorford, 03-05-2026, 06:15 PM
                        0 responses
                        46 views
                        0 likes
                        Last Post TheRealMorford  
                        Working...
                        X