Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

End Strategy on Realized Profit

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

    #16
    lvaduva, I think the best way to do this is to set a variable named canEnterTrade to false instead of returning from the method.

    When return; is called, it breaks out of the current method call (usually OnBarUpdate()), so nothing else is evaluated in the code block. Using a variable instead of return is typically a better solution:
    Code:
    protected override void OnBarUpdate()
    {
    if ((ToTime(Time[0]) >= 90000 && ToTime(Time[0]) < 160000) && (Performance.AllTrades.TradesPerformance.Currency.CumProfit > 200 || Performance.AllTrades.TradesPerformance.Currency.CumProfit < -1350))
        canEnterTrade = true;
    else
        canEnterTrade = false;
    
    if (canEnterTrade == true)
    {
        // entry logic goes here
    }
    
    // then to exit instead of enter a new trade:
    if (canEnterTrade == false && Position.MarketPosition != MarketPosition.Flat)
    {
        ExitLong();
        ExitShort();
    }
    AustinNinjaTrader Customer Service

    Comment


      #17
      If the last code provided by Austin is used, and the current position is either long or short, will the trade exit immediately once the strategy is profitable by 200?

      Comment


        #18
        Baseheadz,

        There is nothing in Austin's snippet that checks the strategies unrealized PnL. For this, you can use GetProfitLoss()
        Ryan M.NinjaTrader Customer Service

        Comment


          #19
          Can GetProfitLoss() be used to find the PnL of the last trade? Prior trades?

          GetProfitLoss()[1] // Something like this?

          Comment


            #20
            Baseheadz,

            GetProfitLoss() is only going to work on the current position.

            You can use GetTrades() to get the PnL of a trade X bars ago or the most recent occurrence.

            Please see our Help Guide article on TradeCollection for more information:



            We also have a reference sample that uses and explains some of these concepts:

            MatthewNinjaTrader Product Management

            Comment


              #21
              How can you identify if

              a) The last trade was a losing trade?

              b) The last trade was a Short Position?

              Comment


                #22
                Baseheadz,

                You can check for both of these using code similar to below:

                Code:
                if (Performance.AllTrades[Performance.AllTrades.Count - 1].ProfitCurrency < 0 && Performance.AllTrades[Performance.AllTrades.Count - 1].Entry.MarketPosition == MarketPosition.Short)
                        {
                         // doSomething here
                        }
                The first condition is checking that the last trade was a loser (e..g, ProfitCurrency < 0) and the second condition is checking that the trade was a short position.
                MatthewNinjaTrader Product Management

                Comment


                  #23
                  Hi Josh,

                  I tried to use this and I keep getting "Strategy Error on calling 'OnBarUpdate' method for strategy 'ChamuelCDStat4c/8c8ef9af49c941bab6a08e91f4c6d07d': 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"


                  I used in the first line of OBU:

                  if (CurrentBars[0] < BarsRequired )
                  return;


                  if(Performance.AllTrades[Performance.AllTrades.Count - 1].ProfitCurrency < 0
                  && Performance.AllTrades[Performance.AllTrades.Count - 1].Entry.MarketPosition == MarketPosition.Short)
                  //this is causing the error

                  Comment


                    #24
                    Hello,

                    Please try adding the following before checking the Performance:

                    if (Performance.AllTrades.Count < 1)
                    return;

                    then add:

                    else if(Performance.AllTrades[Performance.AllTrades.Count - 1].ProfitCurrency < 0
                    && Performance.AllTrades[Performance.AllTrades.Count - 1].Entry.MarketPosition == MarketPosition.Short)
                    MatthewNinjaTrader Product Management

                    Comment


                      #25
                      Thanks Josh for the tip. I did it a little differently and I got it to work for me:


                      if(Performance.AllTrades.Count > 1
                      && Performance.AllTrades[Performance.AllTrades.Count - 1].ProfitCurrency < 0
                      && Performance.AllTrades[Performance.AllTrades.Count - 1].Entry.MarketPosition == MarketPosition.Short)

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      633 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      364 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      105 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      567 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      568 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X