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

BarsSinceEntry() overrides the other exits

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

    BarsSinceEntry() overrides the other exits

    Hi Everybody,

    I have some simple code to exit if it is not profitable after 9 bars, but it seems to override all the other exit rules and during the back test, the strategy generated only one buy in half an year, lasting from 1.1 to 5.30.

    Code:
    if (BarsSinceEntry()>9)
    {
       if (Position.GetProfitLoss(Close[0],PerformanceUnit.Points)<=0*TickSize) ExitLong();
    }
    Can anybody figure out why is this happening ?

    #2
    Hello wolfcuring, and thank you for your question.

    I noticed in your code you do not have any checks to ensure you are in a long position before checking your unrealized PnL. I believe the following sequence of events is therefore possible :

    • You enter a long position
    • 9 ticks go by, and you have not made a profit
    • You exit your long position
    • More than 9 ticks have now gone by for the remainder of the life of your strategy, and your unrealized PnL is always 0, which prevents you from ever entering a long trade again, since even if you call EnterLong you call ExitLong as well every OnBarUpdate.

    To prevent this, I recommend adding a check for


    if (Position.MarketPosition == MarketPosition.Long)


    Please let me know if there is any other way we can help.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Thank you Jessica,

      I checked, before using the exit method. However, when the BarsSinceEntry() method is used, all the other exit methods are not working, and the strategy stays in Long for ever. A long trade stayed the whole period of the test.

      Code:
      if (Position.MarketPosition==MarketPosition.Long)
      { 
      	if (Position.GetProfitLoss(Close[0],PerformanceUnit.Points)<=-27*TickSize) ExitLong();                        
      				
      	if (Close[0]<=Envelopes(BarsArray[1],0.618,0.381,3,26).InnerLower[0])  ExitLong();          
      				
      	if (CrossBelow(Stochastics(BarsArray[1],5,8,3).K,Stochastics(BarsArray[1],6,12,3).D,1)) ExitLong(); 
      	if (Close[0]>=Envelopes(BarsArray[1],0.618,0.381,3,26).Upper[0]) ExitLong();          
      				
      	if (BarsSinceEntry()>9)
      				{
      					if (Position.GetProfitLoss(Close[0],PerformanceUnit.Points)<=0*TickSize) ExitLong();
      				}
      				 
      }
      So, I guess it is reasonable if the position is profitable, and I have no other exit conditions. But, how could this override the other is quite confusing.

      And, I think, the code following BarsSinceEntry() is not executed at all.
      Last edited by wolfcuring; 06-01-2016, 02:41 AM.

      Comment


        #4
        Hello wolfcuring,

        I have added some trace code to the code sample you have provided me. Could I ask you to replace the code you have by this sample, run this strategy, and then send (My) Documents\NinjaTrader 7\log and (My) Documents\NinjaTrader 7\trace files for today to platformsupport[at]ninjatrader[dot]com, referencing this unique number in the body of your e-mail?

        1522698

        This will make it obvious what is occurring with your code.

        Code:
        [FONT=Courier New]if (Position.MarketPosition == MarketPosition.Long)
        {
            double pNl = Position.GetProfitLoss(Close[0],PerformanceUnit.Points);
            double eil = Envelopes(BarsArray[1],0.618,0.381,3,26).InnerLower[0];
            double eu = Envelopes(BarsArray[1],0.618,0.381,3,26).Upper[0];
            IDataSeries k = Stochastics(BarsArray[1],5,8,3).K;
            IDataSeries d = Stochastics(BarsArray[1],6,12,3).D;
        
            Log(String.Format("GetProfitLoss : {0}", pNl), LogLevel.Information);
            Log(String.Format("Envelopes.InnerLower : {0}", eil), LogLevel.Information);
            Log(String.Format("Envelopes.Upper : {0}", eu), LogLevel.Information);
            Log(String.Format("k[0] : {0}", k[0]), LogLevel.Information);
            Log(String.Format("d[0] : {0}", d[0]), LogLevel.Information);
        
            if (pNl <= -27*TickSize)
            {
                Log("(1) pNl <= -27*TickSize met", LogLevel.Information);
                ExitLong();
            }
        
            if (Close[0] <= eil)
            {
                Log("(2) Close[0] <= eil met", LogLevel.Information);
                ExitLong();
            }
        
            if (CrossBelow(k, d, 1))
            {
                Log("(3) CrossBelow(k, d, 1) met", LogLevel.Information);
                ExitLong();
            }
        
            if (Close[0] >= eu)
            {
                Log("(4) Close[0] >= eu met", LogLevel.Information);
                ExitLong();
            }
        
            if ( (BarsSinceEntry()>9) && (pNl <= 0*TickSize) )
            {
                Log("(5) (BarsSinceEntry()>9) && (pNl <= 0*TickSize) met", LogLevel.Information);
                ExitLong();
            }
        
        }[/FONT]
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          How can we know moving price

          OnBarUpdate can we detect moving price by using Open or Input? or which property can we use to detect currently price

          Comment


            #6
            Hello Wolfcuring,

            Thank you, I have received your log and trace files. After reviewing them, I believe I will need to see your (My) Documents\NinjaTrader 7\Config.xml file as well. Could you send it to the same address, again referencing this unique number :

            1522698

            As well, so that I may test your strategy on my end if need be, can you remove your references to the Envelope indicator, and let me know if this resolves your query or if you still experience the same behavior?
            Jessica P.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by john.pham View Post
              OnBarUpdate can we detect moving price by using Open or Input? or which property can we use to detect currently price
              Hello John,

              If this does not satisfy your query, could I ask that you start a new thread for this question?

              If you set the property CalculateOnBarClose to false in Initialize, OnBarUpdate will occur once every price change (or in other words, once every price change)
              Jessica P.NinjaTrader Customer Service

              Comment


                #8
                Dear Jessica,

                I have sent the log and trace file to the platform support. Does the strategy must generates at least one real time order ?

                Besides, when disabled the BarsSinceEntry() method, I tried other exit condition as follows

                Code:
                if (BoolHiLow(BarsArray[1],5).FlipDown[0])  ExitLong("FlipDown","KD Up");
                The FlipDown is a bool value which I have tested within the indicator and it works fine. But, here, the exit condition is not triggered when backtesting.

                Thank you so much for your help.

                Comment


                  #9
                  Hello Wolfcuring,

                  I did in fact receive your log and trace files.

                  It appears what is happening on your system may be outside of our code.

                  Could you send your Config.xml file to the same place you sent your log and trace files, and also remove the references to the Envelope indicator on your end?
                  Jessica P.NinjaTrader Customer Service

                  Comment


                    #10
                    Hello Wolfcuring,

                    Thank you for sending me your Config.xml file. I noticed the reference you had to NinjaTrader.Vendor.dll had some unprintable characters in the path. I would like to recommend you use the following procedure to repair this path.

                    • Close NinjaTrader
                    • Copy your Config.xml file to Config.xml.backup
                    • Open up your Config.xml file in your favorite text editor
                    • Search for NinjaTrader.Vendor.dll
                    • Replace this file path with the full path to your (My) Documents\NinjaTrader 7\bin\Custom\NinjaTrader.Vendor.dll
                    • Save your Config.xml file
                    • Open NinjaTrader
                    • Recompile your strategy

                    I am attaching the version of your program that I used for testing on my end. Once you have done the above, please run your strategy, and let me know if none of the trace code I asked you to add appears in your logs, or if your strategy is still not behaving as expected, or if there are any other questions I could answer.
                    Jessica P.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi Jessica,

                      Thank you so much for your effort.

                      I did exactly as your last post indicated. The full path seems to be all right, it is only that the "My Documents" part is in Chinese character.

                      I ran as well as back tested the strategy, very thing is all the same. And the strategy generated only trade during the half year period.

                      Comment


                        #12
                        Hello Wolfcuring,

                        I believe we are making progress in determining exactly why your strategy is never enabling. In the log you sent me, we now see this message :
                        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

                        If we look at BarsSince Entry's documentation, we see an overload with a BarsInProgress parameter :
                        https://ninjatrader.com/support/help...sinceentry.htm
                        The following method signature should be used when working with multi-time frame and instrument strategies:

                        BarsSinceEntry(intbarsInProgressIndex, stringsignalName, intentriesAgo)

                        Note: 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.
                        Therefore, in order to progress to a place where the code we were discussing is ever reached, we will first need to look for where BarsSinceEntry appears in your code, and send 3 arguments in to this method.

                        I am including some more documentation on multi-time frame strategies and BarsInProgress for more information.



                        Please don't hesitate to let us know when we can assist further.
                        Jessica P.NinjaTrader Customer Service

                        Comment


                          #13
                          Thank you Jessica, for all these days effort. It works fine now, after I add the multi time series parameter.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Haiasi, Today, 06:53 PM
                          1 response
                          3 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by ScottWalsh, Today, 06:52 PM
                          1 response
                          6 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by ScottW, Today, 06:09 PM
                          1 response
                          4 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by ftsc2022, 10-25-2022, 12:03 PM
                          5 responses
                          256 views
                          0 likes
                          Last Post KeyonMatthews  
                          Started by Board game geek, 10-29-2023, 12:00 PM
                          14 responses
                          244 views
                          0 likes
                          Last Post DJ888
                          by DJ888
                           
                          Working...
                          X