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

Hold Order open

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

    Hold Order open

    I'm not sure how to ask this question. I'm not necessarily looking for code, but more logic.
    Current Setup Logic:
    If (entryCondition == correct)
    EnterLong/Short

    The issue I run into is if the entryCondition turns False and the order hasn't filled yet, it will cancel the order. That's a nice feature to have as default, but how to I override that.

    New Setup:
    If (entryCondition == Correct)
    EnterLong/Short and hold for X bars/time to try and get the fill.


    I'm just not sure what kind of logic I would need to bypass the default behavior of canceling the order when entryCondition turns false.

    Any help would be fantastic.


    #2
    Hello alphatango,

    Thank you for your post.

    Are you creating this strategy in the Strategy Builder or is it an unlocked script in the NinjaScript Editor? Depending on which way you are making the strategy, I would be glad to provide you with some options.

    I look forward to your reply.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Great question. I've really just coded my things by hand because the logic layout in the Strategy Builder is out of order which makes it somewhat confusing.

      I just free code at this point.

      Comment


        #4
        Hello alphatango,

        Thank you for your reply.

        You have a couple of options. One option is to toggle a bool when the entry conditions are true, and then have your order submitted only when that bool becomes true. Once the position is flat again (or at whichever point you would like to check for the entry conditions for a new entry) you can reset the bool back to false. Here is an example of what that might look like:
        Code:
        if (Position.MarketPosition == MarketPosition.Flat)
        activeEntry = false;
        
        if (EntryConditions == true)
        activeEntry = true;
        
        if (activeEntry == true)
        EnterLongLImit(____);
        This way, even when the EntryConditions aren't true anymore, the bool activeEntry will stay true and allow the EnterLongLImit order to "stay alive" rather than canceling once the entry conditions are not true.

        Another option would be to utilize Live Until Canceled orders, which is covered as an advanced order concept. We have more info at the following links, including reference samples that demonstrate how to "keep orders alive" as well as how to use CancelOrder() to explicitly cancel an order:Please let us know if we may be of further assistance.
        Emily C.NinjaTrader Customer Service

        Comment


          #5

          I like the multiple If statements. Gives me the ability to queue the order without having it cancel.

          The issue with the code is that it cycles around and cancels the order once "EntryConditions != True" anymore. Still having the same issue.

          With other programming I can step through my code and see how it processes. I don't know that I can do that with Ninjascript so any help with how the script will process is extremely helpful.

          I thought of doing a While loop, but it looks like NS doesn't allow While loops unless I'm wrong.

          Would this work?
          Code:
          Same logic 
          if (Entryconditions == True)
              EnterLongLimit
              WaitingPeriod()
          
          
          Private Void WaitingPeriod()
          {
          TimeToBeOpen = 3
          
          If TimeToBeOpen < BarsSinceEntry (is that a method?)
          {
              return;
          }
          else
          {
             CancelOrder
          }
          The question is, would "return;" send the code back to the beginning of the whole code or just the current method? I feel like it's all a matter of how the code executes at this point.


          Tertiary Question: Had to reinstall NT8 and can't seem to find the setting to turn the background black in NS Editor. Any ideas where that is?

          Comment


            #6
            Hello alphatango,

            Thank you for your reply.

            It seems you are submitting the order when the entry conditions are true; once those conditions are no longer true, it is expected that the order will be canceled. You need to follow the example I provided that toggles a bool to true when the entry conditions are true, then set up another condition that submits the order when that bool is true or you will need to consider using IsLIveUntilCanceled orders. If you want the order to only stay "alive" for a certain number of bars, you may simply toggle the bool to false after that many bars have passed. BarsSinceEntryExecution() is a method, but that won't apply to when an order was submitted and only applies to executions (when an order is filled):


            Otherwise, if you use IsLiveUntilCanceled orders, the SampleCancelOrder script that I shared the link to shows how to cancel an order if it has not been filled within 3 bars.

            "return" will stop processing OnBarUpdate() when it is called and the logic for OnBarUpdate() will not be evaluated again until the next update (on bar close, on price change, or on each tick).
            To change the skin color of the platform, go to Control Center > Tools > Options. Select the desired skin color from the dropdown menu and then click OK to save the change. Restart NinjaTrader for the change to take effect.

            Please let us know if we may be of further assistance.
            Emily C.NinjaTrader Customer Service

            Comment


              #7
              Sort of a breakthrough.

              This seems to work, but the update calculates on each Tick. How do I make it calculate on a bar? I imagine I can do a timestamp hhmm, but then I'd have to code in rollover on a new hour. Not impossible just work...

              Code:
              If (Entrycondition == True || WaitTime > 0)
              {
                   If (WaitTime == 0)
                   {
                        WaitTime = 3; //arbitrary number at this point. 
                   }
                   EnterLongLimit([stuff]);
                   WaitTime--;
              }
              The only issue I have is that WaitTime will update on each tick, but I feel like just adding the OR for a wait time will give it 2 chances of being True.

              Any tips on how to get WaitTime-- to process on a bar by bar basis?

              Comment


                #8
                Hello alphatango,

                Thank you for your reply.

                To separate logic so that it only calculates on bar close, you may check if IsFirstTickOfBar is true:


                There is a reference sample that demonstrates how to separate logic between calculating on each tick and on bar close here:


                That said, you could also consider saving the CurrentBar index on the bar where the order is submitted, then make a comparison if the CurrentBar is greater than the saved index + x number of bars. This is done in the SampleCancelOrder script. It saves barNumberOfOrder and checks if CurrentBar is greater than barNumberOfOrder + 3 and if so, calls CancelOrder():


                Thank you for your time and patience.
                Emily C.NinjaTrader Customer Service

                Comment


                  #9
                  Hey Emily,
                  Did I miss the "SampleCancelOrder" script? I feel like I should be able to see it here, but I just don't. Any help would be greatly appreciated!

                  Comment


                    #10
                    Hello alphatango,

                    Thank you for your reply.

                    The script may be found at the following link:


                    Here is a screenshot showing which part of the page will take you to download the sample:


                    Another sample script I shared previously in this thread is the SampleIsLIveUntilCanceled script and I suggest reviewing this one as well in case you are interested in using IsLiveUntilCanceled orders:


                    Thank you for your time and patience.
                    Emily C.NinjaTrader Customer Service

                    Comment


                      #11
                      Thank you! This might work.

                      Quick Syntax question:
                      ExitLongLimit(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double limitPrice, string signalName, string fromEntrySignal)

                      What is the difference between "string signalName," and "string fromEntrySignal"?

                      Comment


                        #12
                        Originally posted by alphatango View Post
                        Thank you! This might work.

                        Quick Syntax question:
                        ExitLongLimit(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double limitPrice, string signalName, string fromEntrySignal)

                        What is the difference between "string signalName," and "string fromEntrySignal"?
                        The signalName is defined as "User defined signal name identifying the order generated. Max 50 characters."
                        The fromEntrySignal definition is "The entry signal name. This ties the exit to the entry and exits the position quantity represented by the actual entry. Note: Using an empty string will attach the exit order to all entries."

                        These definitions may be found on the help guide page for this method:


                        Essentially, the signalName is a string to give a name to that particular order. The fromEntrySignal is used to tie the exit order to an entry order and match the quantity for that entry.

                        Please let us know if we may be of further assistance.
                        Emily C.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by fx.practic, 10-15-2013, 12:53 AM
                        5 responses
                        5,403 views
                        0 likes
                        Last Post Bidder
                        by Bidder
                         
                        Started by Shai Samuel, 07-02-2022, 02:46 PM
                        4 responses
                        94 views
                        0 likes
                        Last Post Bidder
                        by Bidder
                         
                        Started by DJ888, Yesterday, 10:57 PM
                        0 responses
                        6 views
                        0 likes
                        Last Post DJ888
                        by DJ888
                         
                        Started by MacDad, 02-25-2024, 11:48 PM
                        7 responses
                        158 views
                        0 likes
                        Last Post loganjarosz123  
                        Started by Belfortbucks, Yesterday, 09:29 PM
                        0 responses
                        8 views
                        0 likes
                        Last Post Belfortbucks  
                        Working...
                        X