Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

closing trade prematurely

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

    closing trade prematurely

    Hello Ninjas

    How can I add a condition which would put a cap on how many bars the trade can have and close the trade prematurely if it lasts more than the specified X amount of bars?

    Thanks
    Jonas

    #2
    Hi Jonas, you can just check against the built in BarsSinceEntry - here you would get a count in terms of bars - https://www.ninjatrader.com/support/...sinceentry.htm

    You can compare that to a user input for example to set after which bars amount to trigger a 'premature' exit.

    Comment


      #3
      Hi Bertrand

      I struggle to make use of BarsSinceEntry - backtest doesn't return any results once I began using BarsSinceEntry in my multi-instrument strategy (attached).

      Looking into Log, the issue is
      Error on calling 'OnBarUpdate' method for strategy <...>: You must use the overload that has a 'BarsInProgress' parameter when calling the BarsSinceEntry() method in the context of a multi-time frame and instrument strategy.
      Where should I use BarsInProgress ? I tried solving it by doing one of the below or both of the below:
      1) By adding this line to OnBarUpdate()
      Code:
      BarsInProgress ==0 || BarsInProgress ==1 || BarsInProgress ==2
      2) By adding
      Code:
      if (BarsInProgress == 0 && BarsSinceEntry("Long 1") > 5)
                  	{
               		ExitLong("Long 1");
                      }

      Please advise how shall I use BarsInProgress so that the startegy works as normal and allows to specify max number of bars after which the trade would be closed?

      What am I doing wrong / missing?



      Jonas
      Attached Files
      Last edited by ionaz; 09-03-2014, 04:24 PM.

      Comment


        #4
        Hi Jonas, what you miss here actually is using the dedicated BarsSinceEntry overload that offers passing in the BarsInProgress parameter -

        BarsSinceEntry(int barsInProgressIndex, string signalName, int entriesAgo)

        Comment


          #5
          Thanks Bertrand

          The below compiles, but trades don't get closed even if > 14 days.
          Code:
          if (BarsSinceEntry(0,"Long 1",0) > 14 )
             {
             ExitLong("Long 1");
             }
          
          if (BarsSinceEntry(0,"Long 2",0) > 14 )
             {
             ExitLong("Long 1");
             }
          Why?

          Am I right thinking that each Add statement (attached) increments barsInProgressIndex by +1? And so the above code is only valid for the instrument in the 1st Add?
          Attached Files
          Last edited by ionaz; 09-04-2014, 02:59 PM.

          Comment


            #6
            That is correct Jonas, each add your do will increase your total # of bars arrays in the script available.

            So primary instrument / series is at BIP 0, first added one BIP 1 and so on.

            For the BarsSinceEntry in a MultiSeries / Instrument script, this hint from the helpguide would be important -

            When working with a multi-series strategy the BarsSinceEntry() will return you the elapsed bars as determined by the first Bars object for the instrument specified by the barsInProgressIndex.

            Comment


              #7
              Hi Bertrand

              I still cannot control max number of days a trade can last. Further to your confirmation that each Add increments the barsInProgressIndex, I've made sure that I increment BarsSinceEntry as well. Sorry for not super efficient code, but hope it all makes sense:

              Code:
                  		if (BarsSinceEntry(0,"Long 1",0) > 14 )
                          	        {
                       		ExitLong("Long 1");
              		        }
              
              		if (BarsSinceEntry(0,"Long 2",0) > 14 )
                          	        {
                       	        ExitLong("Long 2");
              		        }
              							
              		if (BarsSinceEntry(1,"Long 1",0) > 14 )
                          	        {
                       		ExitLong("Long 1");
              		        }
              
              		if (BarsSinceEntry(1,"Long 2",0) > 14 )
                          	        {
                       	        ExitLong("Long 2");
              		        }
              				
              		if (BarsSinceEntry(2,"Long 1",0) > 14 )
                          	        {
                       		ExitLong("Long 1");
              		        }
              
              		if (BarsSinceEntry(2,"Long 2",0) > 14 )
                          	        {
                       	        ExitLong("Long 2");
              		        }
              So I know my first 2 Adds are:
              Code:
              			Add("AAPL",PeriodType.Day, 1);
              			Add("ADBE",PeriodType.Day, 1);
              And so I expected at least AAPL and ADBE trades not to last more than 14 days. But it didn't work - didn't notice any changes whatsoever in the length of the AAPL and ADBE trades.


              I attach the short code just in case. Please advise what am I missing here?
              Attached Files
              Last edited by ionaz; 09-05-2014, 02:17 PM.

              Comment


                #8
                Hi ionaz,

                On what bars in progress are the orders being submitted on?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hi ChelseaB

                  I'm quite a novice with multi-instrument strategies, hence I think it might be best if you look into the code I attached to previous post, just don't want to mislead you.

                  I might be wrong, but as far as I understand the BarsInProgress is switched off. What I refer to is the line starting with
                  Code:
                  //BarsInProgress ==0 || BarsInProgress ==1 ||
                  . It is switched off because in the past I realized I don't need it, but this time around, when I try to make BarsSinceEntry work, I'm really not sure if I need it and where.

                  Nevertheless, do I really need to specify "what bars in progress are the orders being submitted on"??
                  Asking because if I do, and given bars in progress increments with each Add statement and given there will be 100s of Add statements in my code, how can I efficiently specify "what bars in progress are the orders being submitted on"? Would I need to use some sort of "Loop through" process? Unless required by BarsSinceEntry, I really don't see any issue with applying my orders below against ALL the add statements.

                  Code:
                    EnterLong("Long 1");
                    EnterLong("Long 2");
                  Perhaps this answers your question? If yes, then indeed, I want my orders above to be submitted against ALL the instruments added. Actually, the reason why I use multi-instrument strategy is solely because I wanted to get a single list of all the generated trades. Not because I wanted to separate trading logic etc. Hence I'm happy to submit the orders agains ALL the bars in progress.
                  This works, but now I want to be able to control the max length of these trades and here comes BarsSinceEntry...

                  Will appreciate your guidance!

                  Comment


                    #10
                    Hi ionaz,

                    Well, if this code is run in every bars in progress, then from what is in post #9 this would cause all orders on all bars in progress (on all instruments) to be exited in any bars in progress (when any bar is processing) as long as the entry on that bars in progress has 15 bars since the last entry.

                    Is this what you are intending to do?

                    The question becomes, are you submitting the orders on specific bars in progress or all at once? I notice you are not using a bars in progress for the entry orders. This means the entry order will placed on which ever bars in progress is running.

                    Have you turned on trace orders in your script to see how many entry orders are being placed and on what symbol?
                    TraceOrders = true;


                    If we know that the entry is on specific bars in progress then we can also know why the BarsSinceEntry for that bars in progress is not returning a value greater than 14.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello ChelseaB

                      Thank you for your post.

                      Have read your post carefully and I think we are on the same page when it comes to order placement - yes, can confirm I do not want to be selective when it comes to orders placement >> I want them placed on all instruments I've added via Add. If this is strange, then please note the reason I currently use multi-instrument is solely because only multi-instrument can output a list of all trades in single excel table - need it for my strategy analysis.

                      The question becomes, are you submitting the orders on specific bars in progress or all at once?
                      answer = all at once. obviously when a) the condition is met b) in-line with max orders per direction parameter in NT backtest interface. so yes, my intention is to submit those 2 orders whenever the condition is met in any of the added instruments. the condition is the same for all instruments (at least I want it to be and looks like works). just want to close any trades which last longer than I specify.

                      will make use of TraceOrders = true

                      So given the above, am I correct thinking that I cannot use BarsSinceEntry in my setup, because it needs bars in progress as an input? If so, I certainly need a workaround, especially if BarsSinceEntry is the only way to output days since entry/ and then control the length of a trade. Do you think I can still make use of BarsSinceEntry?

                      Comment


                        #12
                        Hi ionaz,

                        The best idea is to print out what is happening.

                        On each new bar you can print BarsSinceEntry/Exit for that BarsInProgress and keep track as things progress. This would eliminate uncertainty with the behavior.

                        For example:
                        Print("BIP: "+BarsInProgress+" | BarsSinceEntry: "+BarsSinceEntry(BarsInProgress, "", 0)+" BarsSinceExit: "+BarsSinceExit(BarsInProgress, "", 0));
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Hello ChelseaB

                          Thank you for your post.

                          Added the line, but struggle to interpret the output. Can you please help and tell why trades don't get closed at bar 14.

                          I've saved the brief code + output based on your print.
                          Attached Files

                          Comment


                            #14
                            Hello ionaz,

                            Thank you for your response.

                            Can you advise what bar series you are checking when you are referring to the following?
                            Originally posted by ionaz View Post
                            The below compiles, but trades don't get closed even if > 14 days.
                            If you are just entering on whatever bars object then how are you going to know how many bars have passed since the entry?

                            Your conditions for entry are not specific to any of the instruments you add in the Initialize() method and your conditions to exit are checking the primary bar series.

                            So to bring it back around full circle; are you trying to develop a strategy that looks at a list of stocks and then takes positions based on which ever one meets your criteria? Then exit when a set number of bars pass?

                            Comment


                              #15
                              Hi PatrickH, thanks for your post

                              If you are just entering on whatever bars object then how are you going to know how many bars have passed since the entry?
                              Well, I don't know. My thinking was that I could possibly close a trade in any instrument if the trade lasts longer than 14 days.

                              Your conditions for entry are not specific to any of the instruments you add in the Initialize() method and your conditions to exit are checking the primary bar series.
                              Yes, because I was suggested to use BarsSinceEntry() and just tried following its formula. What I want is exit regardless of the instrument, just like entry. exit if trade older than 14 days, how can I do this??? wonder if BarsSinceEntry() fit for this purpose. It needs instrument specified, correct? I don't quite want to do it, because don't know how to tell it check all he instruments, not just specific one. also, I hope there's an easier way, so trying to find one.

                              So to bring it back around full circle; are you trying to develop a strategy that looks at a list of stocks and then takes positions based on which ever one meets your criteria? Then exit when a set number of bars pass?
                              Absolutely! Thank you. So all works except controlling the trade length. Please advise how should I do this?
                              Last edited by ionaz; 09-15-2014, 04:22 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              656 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              371 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              109 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              574 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              579 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X