Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Question About NinjaScript Function

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

    Question About NinjaScript Function

    Hello staff,

    Is there a way in the NinjaScript to prevent a strategy from entering a new trade if the prior one was closed with a profit?

    Let's say that trade A is active and running a profit when it suddenly receives a signal to exit and reverse position into trade B; but instead ignores trade B and waits for the signal that would reverse trade B and enter into trade C.

    I'm looking forward to your reply, thank you in advance for your time.

    LG

    #2
    Hello GLFX005,

    Thanks for your post.

    "Is there a way in the NinjaScript to prevent a strategy from entering a new trade if the prior one was closed with a profit?" Yes, you would need to check the last trades PNL and if positive then use that to set a bool variable that you create and then use as part of the entry conditions to prevent the entry of the new trade.

    Please see the help guide link here: https://ninjatrader.com/support/help...collection.htm The first example shows how to get the the PNL of the last trade that you can check to see if profitable.

    Comment


      #3
      Hello Paul,

      I tried the following method but it doesn't allow me to even enable the strategy;

      protected override void OnBarUpdate()
      {
      bool xembel = false;

      if (lastTrade.ProfitPercent > 1)
      {
      xembel = true;
      }

      // Buy
      if ((Close[0] > Close[1])
      && (Close[1] > Close[2])
      && xembel == false)
      {
      EnterLong(Convert.ToInt32(DefaultQuantity), @"L");
      }

      // Sell
      if ((Close[0] < Close[1])
      && (Close[1] < Close[2])
      && xembel == false)
      {
      EnterShort(Convert.ToInt32(DefaultQuantity), @"S");
      }
      }


      Are you able to find any mistakes I'm making?

      Thank you for your time, I appreciate it a lot

      LG
      Last edited by GLFX005; 11-21-2019, 10:00 AM.

      Comment


        #4
        Hello GLFX005,

        Thanks for your reply.

        Please review the example in the help guide again. This is what you would be missing

        if (SystemPerformance.AllTrades.Count > 1)
        {
        Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];

        if (lastTrade.ProfitPercent > 1)
        {
        xembel = true;
        }

        }

        Note, you may want to add the print statement from the help guide example so you can see how the percent is expressed. Print("The last trade profit is " + lastTrade.ProfitPercent);

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        116 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        61 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        40 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        44 views
        0 likes
        Last Post TheRealMorford  
        Started by Mindset, 02-28-2026, 06:16 AM
        0 responses
        82 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Working...
        X