Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

If the last long trade was a stop out within x bars, then...

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

    If the last long trade was a stop out within x bars, then...

    Hi, could someone please point me in the direction if this has been previously answered, I would like to code the following:
    If the last long trade was a stop out within x bars, then...
    Thanks

    #2
    Hello,

    The following branching command will trigger if the last trade has less than 10 bars between the entry and exit.

    if (Performance.AllTrades.Count > 0 && Bars.GetBar(Performance.AllTrades[Performance.AllTrades.Count-1].Exit.Time)-
    Bars.GetBar(Performance.AllTrades[Performance.AllTrades.Count-1].Entry.Time) < 10)
    {
    // execute code
    }

    Below is a link to the help guide on Performance.AllTrades.
    http://www.ninjatrader.com/support/h.../alltrades.htm

    And a link to get bar.
    http://www.ninjatrader.com/support/h...nt7/getbar.htm
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks, I had a good read of the links plus related info, but am still unable to piece together what I want to achieve. I now see I need to rephrase my question:
      If the last trade was a long trade, and it was a loosing trade, and the exit of this loosing trade was less than X bars ago, then...
      Thanks

      Comment


        #4
        Hi Stem1,

        Thank you for clarifying that for me.

        While working this out I found two ways to accomplish this. I am going to provide both ways so that it is a little clearer of what I am trying to find.

        Method 1:
        Code:
        if (Performance.AllTrades[Performance.AllTrades.Count-1] == Performance.LongTrades.LosingTrades[Performance.LongTrades.LosingTrades.Count-1]
        && BarsSinceExit() < 10)
        {
        // execute code
        }
        This method looks to see if the last trade (last index of the all trades collection) is the same object as the last long losing trade (last index of the longtrades - losing trades collection) and that the last exit of any order is less than 10 bars ago.

        Method 2:
        Code:
        if (Performance.AllTrades[Performance.AllTrades.Count-1].Entry.MarketPosition == MarketPosition.Long
        && Performance.AllTrades[Performance.AllTrades.Count-1].ProfitCurrency < 0
        && BarsSinceExit() < 10)
        {
        // execute code
        }
        This method checks that the last trade has an entry order that made a long position and that the profit in currency is less than 0 and that the last exit of any order is less than 10 bars ago.

        I gave this a test with some simple trades and both methods return the exact same thing.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thanks. There's something not right with the code as it compiles ok but won't trade in the backtest when added to an otherwise trading strat. When it is deleted it trades again.
          I then added just the code given and had it change a variable unconnected to the rest of the program and it wouldn't trade.
          When removing line by line and compiling and testing at each step, it won't trade until the last line is removed.
          Both methods do the same.
          The code looks right and makes sense, is there something missing?
          Thanks

          Comment


            #6
            Hi Stem1,

            Are there errors in the Log tab of the Control Center?

            If so, what do these errors say?

            The code I provided does not have any actions, did you add actions to the action block of this branching command?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Error in log:
              "Error on calling 'onbarupdate' method, you are accessing an index with a value thats invalid since its out of range,IE accessing a series [Barsago] with a value of 5 when there are only 4 bars on the chart."
              This is the only type of message, but repeated many times.
              Yes added actions, then added actions that had nothing to do with the rest of the program to test if it was the action that was causing the problem, but no change. Just having the code in the program without it interacting with the rest of the program causes it not to trade. Delete the code and the strat trades without changing anything else.
              Its probably something simple we're missing.
              Thanks

              Comment


                #8
                Hi Stem1,

                Thanks for the error message. That definitely lets me know what's going on. The trades index is called without a check to see if there are trades.

                I've modified the code a bit:

                Method 1:
                Code:
                if ( Performance.LongTrades.Count > 0 && Performance.AllTrades[Performance.AllTrades.Count-1] == Performance.LongTrades.LosingTrades[Performance.LongTrades.LosingTrades.Count-1]
                && BarsSinceExit() < 10)
                {
                // execute code
                }
                Method 2:
                Code:
                if (Performance.AllTrades.Count > 0 && Performance.AllTrades[Performance.AllTrades.Count-1].Entry.MarketPosition == MarketPosition.Long
                && Performance.AllTrades[Performance.AllTrades.Count-1].ProfitCurrency < 0
                && BarsSinceExit() < 10)
                {
                // execute code
                }
                This should work. Let me know if it does not.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  That got it, thanks for your help!

                  Comment


                    #10
                    Hello again, the same error message pops up when running the genetic optimizer and it kicks out early, but not when back testing or using the default optimizer. Is this expected with this code?
                    Thanks
                    Last edited by Stem1; 05-12-2014, 09:50 AM.

                    Comment


                      #11
                      Hi Stem1,

                      I'm not able to reproduce.

                      I've got a script I'm testing this with, and I was able to optimize this strategy (using a variable that does nothing which I've called MyInt).

                      Attached is the strategy. Please give this a run on your end and let me know if you get errors running this.

                      Then take a look at the code within and compare to your own.
                      Attached Files
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Ok, what I have found is that in simple code like your sample and when added to the MA crossover sample, the problem does not reproduce. When I cut and paste the code from your test strategy into more complex strategies with many parameters to optimize it fails every time.
                        Is there another way to accomplish the task of counting the number of long or short successive losses as outlined below?
                        Thanks

                        Comment


                          #13
                          Hello Stem1,

                          If you are getting the same message "Error on calling 'OnBarUpdate' method, you are accessing an index with a value that's invalid since its out of range"

                          Then it may be that your first trade has not been stopped out. You may want to add another check for if the Performance.LongTrades.LosingTrades.Count > 0. For example:

                          if ( Performance.LongTrades.Count > 0 && Performance.LongTrades.LosingTrades.Count > 0 && Performance.AllTrades[Performance.AllTrades.Count-1] == Performance.LongTrades.LosingTrades[Performance.LongTrades.LosingTrades.Count-1]
                          && BarsSinceExit() < 10)
                          {
                          Print(Performance.AllTrades[Performance.AllTrades.Count-1].ProfitCurrency);
                          }
                          JCNinjaTrader Customer Service

                          Comment


                            #14
                            Fixed!
                            Thanks

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                            0 responses
                            580 views
                            0 likes
                            Last Post Geovanny Suaza  
                            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                            0 responses
                            335 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by Mindset, 02-09-2026, 11:44 AM
                            0 responses
                            102 views
                            0 likes
                            Last Post Mindset
                            by Mindset
                             
                            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                            0 responses
                            554 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by RFrosty, 01-28-2026, 06:49 PM
                            0 responses
                            552 views
                            1 like
                            Last Post RFrosty
                            by RFrosty
                             
                            Working...
                            X