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

strategies with one market and one stop market entry orders.

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

    #16


    people with nt,



    i have continued to work with these methods.


    i created this strategy that i include as an attachment so that it could be optimized. the initial short and long positions seem to work fine and i have also included all the position management code that i have been able to write to this moment. however, the strategy will never open the pullback positions which are the very reason for all these efforts.


    i have optimized this strategy only on the ema period variable both on the nq and cl contract and there are zero pullback entries ever. i'm not sure that the position management is doing much either.


    ¿could you please take a look at this code? i have described my objectives extensively all throughout this thread.



    OnOrderUpdatetests.zip

    Comment


      #17
      Hello rtwave,

      Thank you for your note.

      I see you are using a BarsInProgress == 1 check in your script but you do not have a secondary data series added. This means that this section of code may not execute since there is not secondary bars object.

      Ultimately, debugging steps should be taken 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.

      In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar. Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

      Below is a link to a forum post that demonstrates using prints to understand behavior and including a link to a video recorded using the Strategy Builder.
      https://ninjatrader.com/support/foru...121#post791121

      Please let me know if I may further assist
      Brandon H.NinjaTrader Customer Service

      Comment


        #18
        people with nt,



        i am back working with order and execution update methods again.


        in my previous post i included a strategy that would not generate secondary positions which is the entire point for what i am doing. i have now included a 1 minute additional data series and changed entry handling to unique entries and the strategy now does open secondary positions when there is a pullback as intended.


        however, i have not been able to get the stop orders to work at all. not for the initial positions, not for the secondary positions, nothing.


        i have been working on the sample pieces of code for order - execution updates that are available and just replaced some components with my own and made other minor changes to the logic but the strategy will never place exitstopmarket positions as intended.



        please take a look at this strategy, specially the order - execution update methods which are the parts that are not performing as intended. i'm working on m2k 20 minute bars, but the instrument or interval should not make any difference.



        very well, thanks, regards.


        20210630OnOrderUpdatetests.zip

        Comment


          #19
          Hello rtwave,

          Thanks for your note.

          When testing the attached code on our end, I am seeing an entry order and stop order placed. See the attached screenshot.

          If the expected trade(s) are not appearing, this would indicate that the condition to place the order is not evaluating as true or the order is being ignored for other reasons.

          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.

          In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar. Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

          Below is a link to a forum post that demonstrates using prints to understand behavior.
          https://ninjatrader.com/support/foru...121#post791121

          Please let me know if I may further assist
          Attached Files
          Brandon H.NinjaTrader Customer Service

          Comment


            #20




            people with nt,



            i have continued to work on this position management code but i haven't been able to get stop orders to perform as intended.


            i have some trend following strategies in nt, the most elementary version does not have any day - time filters and it works correctly. i then created a version that liquidates all positions on friday before the close and restores them on sunday at the open, it also works well. i then created a version that only opens and holds positions during regular session hours and it works correctly. i also have a version that uses a stop loss order and converts it to a breakeven stop and it works acceptably. and this version where i want to open a second position will indeed open secondary positions, but the stop orders that should help manage the positions will not work.


            i want nt support to help me with a sample strategy where two positions in the same direction each include conditional stop loss orders.


            Code:
            protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
            {
            /* We advise monitoring OnExecution to trigger submission of stop/target orders instead of OnOrderUpdate() since OnExecution() is called after OnOrderUpdate()
            which ensures your strategy has received the execution which is used for internal signal tracking. */
            if (orshpoor != null && orshpoor == execution.Order)
            {
            if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
            {
            // We sum the quantities of each execution making up the entry order
            sumFilled += execution.Quantity;
            
            // Submit exit orders for partial fills
            if (execution.Order.OrderState == OrderState.PartFilled && marketPosition == MarketPosition.Short)
            {
            orshpostor = ExitShortStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + ( Instlonuti * TickSize ), "orstorsp01", "orsp01");
            
            }
            // Update our exit order quantities once orderstate turns to filled and we have seen execution quantities match order quantities
            else if (execution.Order.OrderState == OrderState.Filled && sumFilled == execution.Order.Filled && marketPosition == MarketPosition.Short)
            {
            // Stop-Loss order for OrderState.Filled
            orshpostor = ExitShortStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + ( Instlonuti * TickSize ), "orstorsp01", "orsp01");
            
            }
            
            // Resets the orshpoor object and the sumFilled counter to null / 0 after the order has been filled
            if (execution.Order.OrderState != OrderState.PartFilled && sumFilled == execution.Order.Filled)
            {
            orshpoor = null;
            sumFilled = 0;
            }
            }
            }
            
            if (pushpoor != null && pushpoor == execution.Order)
            {
            if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
            {
            sumFilled += execution.Quantity;
            
            if (execution.Order.OrderState == OrderState.PartFilled && marketPosition == MarketPosition.Short)
            {
            pushpostor = ExitShortStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + ( Costlonuti * TickSize ), "pustpusp01", "pusp01");
            orshpostor = ExitShortStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + ( Costlonuti * TickSize ), "pustorsp01", "orsp01");
            }
            
            else if (execution.Order.OrderState == OrderState.Filled && sumFilled == execution.Order.Filled && marketPosition == MarketPosition.Short)
            {
            pushpostor = ExitShortStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + ( Costlonuti * TickSize ), "pustpusp01", "pusp01");
            orshpostor = ExitShortStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + ( Costlonuti * TickSize ), "pustorsp01", "orsp01");
            }
            
            if (execution.Order.OrderState != OrderState.PartFilled && sumFilled == execution.Order.Filled)
            {
            pushpoor = null;
            sumFilled = 0;
            }
            }
            }
            
            }

            i think that the problem could be with the arguments for position size and position entry price.


            the stop order for the initial position is expressed like this:

            orshpostor = ExitShortStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + ( Instlonuti * TickSize ), "orstorsp01", "orsp01");


            execution.Order.Filled and execution.Order.AverageFillPrice correspond to the values for the initial position "orsp01" and all this looks good.


            however, when a conditional secondary pullback position is opened, there should now be two stop orders that i want to manage in parallel at all times.


            pushpostor = ExitShortStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + ( Costlonuti * TickSize ), "pustpusp01", "pusp01");
            orshpostor = ExitShortStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + ( Costlonuti * TickSize ), "pustorsp01", "orsp01");


            it seems to me like execution.Order.Filled and execution.Order.AverageFillPrice in this second case correspond in both cases to "pusp01" which would not work. the position size for "orsp01" would not necessarily be the same as for "pusp01" and neither of these values would ever be the same as the sum of these two numbers. also, it doesn't seem to me like the values for execution.Order.Filled and execution.Order.AverageFillPrice are updated or reset anywhere when that could be necessary.


            i think that these discrepancies could be the reason why the stop orders won't work as intended when the rest of the code will. if the people with nt can take a look at this and provide a solution and a working sample of a strategy with two positions to be managed simultaneously with order and execution update methods that would be very positive.


            very well, regards.

            Comment


              #21
              Hello rtwave,

              Thanks for your note.

              I do not see anything specific in your shared code that would cause an issue. That said, if the expected trade(s) are not appearing, this would indicate that the condition to place the order is not evaluating as true or the order is being ignored for other reasons.

              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.

              In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar. Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

              Please see the attached example script, SampleOnOrderUpdateModified, which demonstrates how to submit 2 entry orders and manage the stops and targets of those entry orders by using OnOrderUpdate() and OnExecutionUpdate().

              Also, see the help guide documentation below for more information.
              OnOrderUpdate(): https://ninjatrader.com/support/help...tionupdate.htm
              OnExecutionUpdate(): https://ninjatrader.com/support/help...rderupdate.htm

              Let us know if we may assist further.
              Attached Files
              Brandon H.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by ScottWalsh, Today, 04:52 PM
              0 responses
              1 view
              0 likes
              Last Post ScottWalsh  
              Started by ScottWalsh, Today, 04:29 PM
              0 responses
              5 views
              0 likes
              Last Post ScottWalsh  
              Started by rtwave, 04-12-2024, 09:30 AM
              2 responses
              22 views
              0 likes
              Last Post rtwave
              by rtwave
               
              Started by tsantospinto, 04-12-2024, 07:04 PM
              5 responses
              70 views
              0 likes
              Last Post tsantospinto  
              Started by cre8able, Today, 03:20 PM
              0 responses
              7 views
              0 likes
              Last Post cre8able  
              Working...
              X