Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

how to check last trade = loser and was a long

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

    how to check last trade = loser and was a long

    trying to put a condition in my strategy to check if the last completed trade was
    1) a loser
    2) and a long trade

    im using this code :
    if (Performance.AllTrades[0].ProfitCurrency<0 && Performance.AllTrades[0].Entry.MarketPosition==MarketPosition.Long)

    Ran into problems so I tested each condition separately

    Performance.AllTrades[0].ProfitCurrency<0 -- This seems to work

    Performance.AllTrades[0].Entry.MarketPosition==MarketPosition.Long -- this seems to be never true even though my strategy does take long trades in the backtest. I dont know why it does not work.

    Is this the right way or should this be done another way ? how do I check for my original condition ?

    Thanks in advance

    #2
    qewcool, the MarketPosition.Long property will only return true if you are in a long position when the property was called. So, if you are flat, it will return false.

    Here is a bit of code I whipped together from the help documents that should get you going in the right direction:
    Code:
    // this is in OnBarUpdate somewhere
    if (Performance.AllTrades.Count > 0)
    {
        Trade mostRecentTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
        Print(mostRecentTrade.Entry.Time.ToString() + " " + mostRecentTrade.Entry.MarketPosition.ToString());
    }
    AustinNinjaTrader Customer Service

    Comment


      #3
      Thanks for the reply

      Isnt MarketPosition.Long a valid value for the Performance.AllTrades[0].Entry.MarketPosition property ?

      see the guide here :


      and here :




      Are you telling me the Performance.AllTrades[0].Entry.MarketPosition will only contain the value MarketPosition.Long if I am in a long position when I call/read that property ?

      Can you explain this because this is confusing me now.
      I thought the Performance.AllTrades[0].Entry.MarketPosition property would give me the info whether the last completed trade was a long or a short.


      Thanks for your help

      Comment


        #4
        Originally posted by NinjaTrader_Austin View Post

        Here is a bit of code I whipped together from the help documents that should get you going in the right direction:
        Code:
        // this is in OnBarUpdate somewhere
        if (Performance.AllTrades.Count > 0)
        {
            Trade mostRecentTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
            Print(mostRecentTrade.Entry.Time.ToString() + " " + mostRecentTrade.Entry.MarketPosition.ToString());
        }
        from your suggestion :
        Is Performance.AllTrades[Performance.AllTrades.Count - 1] referring to the last trade or should it be Performance.AllTrades[0] ?

        I thought Performance.AllTrades[0] would refer to the last trade like Close[0] refers to the last close ?

        Comment


          #5
          Originally posted by qewcool View Post
          Are you telling me the Performance.AllTrades[0].Entry.MarketPosition will only contain the value MarketPosition.Long if I am in a long position when I call/read that property?
          No I am not. I apologize, I misread your question the first time around. I believe the issue lies in which trade you're getting data from because the trades are not reverse indexed like bars are.

          This is the correct code for getting the most recent trade:
          Code:
          Trade mostRecentTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
          So, your code should look like this:
          Code:
          if (Performance.AllTrades[Performance.AllTrades.Count - 1].ProfitCurrency < 0 && Performance.AllTrades[Performance.AllTrades.Count - 1].Entry.MarketPosition == MarketPosition.Long)
          {
           // most recent trade was long and it lost money, do something
          }
          AustinNinjaTrader Customer Service

          Comment


            #6
            Ok thanks
            will try that.

            Comment


              #7
              I'm trying to use the suggested code in my strategy, it does compile fine, however strategy is not activated. I do click on enabled=true and its not active. If I take this part of the code out, the strategy is activating. Please advise.

              if (Close[1] > Open[1]
              && High[1]-Close[0] > 1 * TickSize
              && Performance.AllTrades[Performance.AllTrades.Count - 1].ProfitCurrency > 0
              && Performance.AllTrades[Performance.AllTrades.Count - 1].Entry.MarketPosition == MarketPosition.Short
              && Low[1]-Low[2] < 6 * TickSize)

              {

              EnterLongStop(DefaultQuantity, High[1], "long");
              }

              Comment


                #8
                Hello meowflying,

                Are you receiving an error message in the Log tab of the Control Center when you enable the script?

                If so, what does this error message say?

                When checking the performance, are you making sure there has been a completed trade before trying to use the index of the last trade?

                if (Performance.AllTrades.Count > 0)
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  You right I'm getting error like that:

                  **NT** Error on calling 'OnBarUpdate' method for strategy 'MyCustomStrategy25/48067a5f0c894fc4aa9fd9f12260a335': You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

                  Also you right about previous completed trade that there are non on the strategy start up. Please advise how i can go around the error and the lack of previous completed trade. Thanks

                  Originally posted by NinjaTrader_ChelseaB View Post
                  Hello meowflying,

                  Are you receiving an error message in the Log tab of the Control Center when you enable the script?

                  If so, what does this error message say?

                  When checking the performance, are you making sure there has been a completed trade before trying to use the index of the last trade?

                  if (Performance.AllTrades.Count > 0)

                  Comment


                    #10
                    Hello meowflying,

                    Before calling an index that does not exist, check to see if it exists first.

                    if (Performance.AllTrades.Count > 0)
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      When activating strategy, there are no past trades, so if I will put as a condition (Performance.AllTrades.Count > 0) it will never enter new trade as well? How to go around it?

                      Originally posted by NinjaTrader_ChelseaB View Post
                      Hello meowflying,

                      Before calling an index that does not exist, check to see if it exists first.

                      if (Performance.AllTrades.Count > 0)

                      Comment


                        #12
                        Originally posted by meowflying View Post
                        You right I'm getting error like that:

                        **NT** Error on calling 'OnBarUpdate' method for strategy 'MyCustomStrategy25/48067a5f0c894fc4aa9fd9f12260a335': You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

                        Also you right about previous completed trade that there are non on the strategy start up. Please advise how i can go around the error and the lack of previous completed trade. Thanks
                        if (Close[1] > Open[1]
                        && High[1]-Close[0] > 1 * TickSize
                        && Performance.AllTrades[Performance.AllTrades.Count - 1].ProfitCurrency > 0
                        && Performance.AllTrades[Performance.AllTrades.Count - 1].Entry.MarketPosition == MarketPosition.Short
                        && Low[1]-Low[2] < 6 * TickSize)
                        means that you are trying to access bar 1 when you are on bar 0.

                        Check that the bar that you are trying to access is valid, or escape.

                        Code:
                        if (CurrentBar < 2) return;
                        placed before the first time that you try to access bar 2, should do it.

                        Comment


                          #13
                          Added what you suggested to the code, still getting same error in the log. What is wrong now?


                          if (CurrentBar < 2) return;


                          // Condition set 1
                          if (Close[1] > Open[1]
                          && High[1]-Close[0] > 1 * TickSize
                          &&Position.MarketPosition == MarketPosition.Flat
                          && Performance.AllTrades[Performance.AllTrades.Count - 1].Entry.MarketPosition != MarketPosition.Long
                          &&Performance.AllTrades.Count > 0
                          && Low[1]-Low[2] < 6 * TickSize )
                          {

                          Originally posted by NinjaTrader_ChelseaB View Post
                          Hello meowflying,

                          Before calling an index that does not exist, check to see if it exists first.

                          if (Performance.AllTrades.Count > 0)

                          Comment


                            #14
                            Originally posted by meowflying View Post
                            Added what you suggested to the code, still getting same error in the log. What is wrong now?


                            if (CurrentBar < 2) return;


                            // Condition set 1
                            if (Close[1] > Open[1]
                            && High[1]-Close[0] > 1 * TickSize
                            &&Position.MarketPosition == MarketPosition.Flat
                            && Performance.AllTrades[Performance.AllTrades.Count - 1].Entry.MarketPosition != MarketPosition.Long
                            &&Performance.AllTrades.Count > 0

                            && Low[1]-Low[2] < 6 * TickSize )
                            {
                            A single code statement will be evaluated as written. You cannot query a member of a collection before validating that such a member exists. Verify that the count is valid before you query the member. Reverse the order of the 2 expressions in red.

                            Comment


                              #15
                              Originally posted by koganam View Post
                              A single code statement will be evaluated as written. You cannot query a member of a collection before validating that such a member exists. Verify that the count is valid before you query the member. Reverse the order of the 2 expressions in red.
                              Thank you, no more errors, I can activate the strategy, however it does not take trades, I guess due to &&Performance.AllTrades.Count > 0 requirement for the entry.


                              if (CurrentBar < 2) return;

                              // Condition set 1
                              if (Close[1] > Open[1]
                              && High[1]-Close[0] > 1 * TickSize
                              &&Position.MarketPosition == MarketPosition.Flat
                              &&Performance.AllTrades.Count > 0
                              && Performance.AllTrades[Performance.AllTrades.Count - 1].Entry.MarketPosition != MarketPosition.Long

                              && Low[1]-Low[2] < 6 * TickSize )

                              {


                              EnterLongStopLimit(DefaultQuantity, High[1]-1 * TickSize,High[1]-1 * TickSize, "long");

                              }




                              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