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

How to Force Exit the TP if not filled after x Seconds ?

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

    How to Force Exit the TP if not filled after x Seconds ?

    Dear all,

    Is there a way to change the TP (ExitLong using MARKET) to EnterShortLimit using BID to force the close to the nearest BID price, IF the TP order did not get filled after x Seconds ?

    Regards

    #2
    Hello PrQuantumH,

    Thanks for your post.

    It would not be possible to force the profit target (ExitLong using MARKET) to change to an EnterShortLimit() order.

    In this case, you would have to cancel the profit target order and submit your EnterShortLimit() order.

    Here is a link to a forum post that discusses having an action happen every 'x' seconds which you may find helpful: https://ninjatrader.com/support/foru...period-of-time

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

    Comment


      #3
      Hello Brandon,

      Thank you so much.

      What if the Study is related to Detecting that the TP is Filled Partially , can I still use the method you suggested to EnterShortLimit() for the remaining contracts that did not get filled ?

      Regards.
      Last edited by PrQuantumH; 02-17-2022, 11:38 AM.

      Comment


        #4
        Hello PrQuantumH,

        Thanks for your note.

        Yes, this would be possible. However, you should note that if the profit target order is partially filled then this means that the current market price is near the profit target price and an in-flight execution may occur when canceling the order.

        See this help guide page about In-Flight Executions: https://ninjatrader.com/support/help...ightExecutions

        And, see this forum thread which you may find to be helpful: https://ninjatrader.com/support/foru...ight-execution

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

        Comment


          #5
          Hi Brandon,

          Thanks a lot for guiding me. I have used the method you've suggested. ( To Force exit the TP if it is Still PartFill after 30 Seconds, by Entering a limit order ) But I'm not sure if I'm doing it correctly, because I didn't get a PartFill instance (Got Detected) to confirm that it is working.

          Please, I would appreciate it if you can tell me what you think about the following simplified code.

          Code:
          private int [B]Quant [/B]= 10;
          private Order [B]TP_Order [/B]= null;
          private bool [B]TP_Check [/B]= false;
          
          protected override void [B]OnStateChange[/B]()
          {[INDENT]if (State == State.SetDefaults)
          {[/INDENT][INDENT=2]...
          Calculate = Calculate.[B]OnBarClose[/B];
          ...[/INDENT][INDENT]}
          else if (State == State.Configure)
          {[/INDENT][INDENT=2]AddDataSeries(i.FullName,BarsPeriodType.Minute, 15);   //Trading Data Series
          AddDataSeries(i.FullName,BarsPeriodType.Second, 30);  //Additional Data Series for periodically 1 bar checks (Seconds)[/INDENT][INDENT]}[/INDENT]
           
          
            }
          
          protected override void [B]OnBarUpdate[/B]()
          {[INDENT]if (CurrentBars[1] > 50 || CurrentBars[2] > 50) return;
          
          if(BarsInProgress == 1){[/INDENT][INDENT=2]
          if( [I]LONG Conditions[/I] ) [B]EnterLong([/B]1, Quant, @"[B]Buy[/B]"[B])[/B]
          
          if( TP Conditions ) [B]ExitLong([/B]1, Quant, "[B]TP for Buy[/B]", "[B]Buy[/B]"[B])[/B][/INDENT][INDENT]}
          
          [B]//Checking the TP order if it still in a PartFill after 30 Sec to Cancel it and submit Limit order instead.[/B]
          if(BarsInProgress == 2){[/INDENT][INDENT=2]
          if(TP_Order!=null && (TP_Order[B].[/B]Name == "[B]TP for Buy[/B]" || TP_Order[B].[/B]Name == "[B]New BUY TP[/B]") ){[/INDENT][INDENT=3]if(TP_Check){[/INDENT][INDENT=4]if(Counter == 1){[/INDENT][INDENT=5]if(TP_Order[B].[/B]OrderState == OrderState[B].[/B]PartFilled){[/INDENT][INDENT=6][B]//Get Remaining Contracts[/B]
          int RemainingQuant = Quant - TP_Order[B].[/B]Quantity;
          
          [B]//Canceling the TP Order[/B]
          CancelOrder(TP_Order);
          
          [B]//Submitting EnterShortLimit[/B]
          EnterShortLimit(1, false, RemainingQuant, GetCurrentBid(1), "[B]New BUY TP[/B]");
          
          [B]//Reseting the Check & Counter[/B]
          TP_Check = false;
          Counter = 0;[/INDENT][INDENT=5]}[/INDENT][INDENT=4]
          }else{[/INDENT][INDENT=5]Counter++;[/INDENT][INDENT=4]}[/INDENT][INDENT=3]}[/INDENT][INDENT=2]}[/INDENT][INDENT]}[/INDENT]
           
          
           }
          
          protected override void [B]OnOrderUpdate[/B](){[INDENT]
          [B]//Detecting the TP order.[/B][/INDENT][INDENT]if(order.OrderAction == OrderAction.Sell && (order.Name == "[B]TP for Buy[/B]" || order.Name == "[B]New BUY TP[/B]")){[/INDENT][INDENT=2]
          TP_Order = order;
          TP_Check = true;
          
          [B]//Reset the TP Order object to null if order was cancelled without any fill[/B]
          if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
          {[/INDENT][INDENT=3]TP_Order = null;
          TP_Check = false;[/INDENT][INDENT=2]}[/INDENT][INDENT]}[/INDENT]
           
          
           }
          Regards,
          Last edited by PrQuantumH; 02-18-2022, 04:19 AM.

          Comment


            #6
            Hello PrQuantumH,

            Thanks for your note.

            I don't see anything specific in your code that is incorrect or would keep you from detecting if the OrderState is PartFilled.

            Ultimately, you would need to take debugging steps. 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).

            Also, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

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

            Note that in the support department at NinjaTrader it is against our policy to create, debug, or modify, code or logic for our clients. Further, we do not provide C# programming education services in our support. This is so that we can maintain a high level of service for all of our clients as well as our partners.

            You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. If you would like our NinjaTrader Ecosystem team follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request, please reach out to vendorsupport[AT]ninjatrader.com.

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

            Comment


              #7
              Thank you so much for your detailed answer.

              I think using Demo data feed is only simulating the PartFill the Entries and Completely Filling the exits. That's why I'm not able to detect the PartFills at TP to Force exit them.

              Click image for larger version

Name:	image_2022-02-18_173019.png
Views:	95
Size:	240.5 KB
ID:	1190595
              Last edited by PrQuantumH; 02-18-2022, 10:30 AM.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Karado58, 11-26-2012, 02:57 PM
              8 responses
              14,825 views
              0 likes
              Last Post Option Whisperer  
              Started by Option Whisperer, Today, 09:05 AM
              0 responses
              1 view
              0 likes
              Last Post Option Whisperer  
              Started by cre8able, Yesterday, 01:16 PM
              3 responses
              11 views
              0 likes
              Last Post cre8able  
              Started by Harry, 05-02-2018, 01:54 PM
              10 responses
              3,204 views
              0 likes
              Last Post tharton3  
              Started by ChartTourist, Today, 08:22 AM
              0 responses
              6 views
              0 likes
              Last Post ChartTourist  
              Working...
              X