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

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.

    Paul H.NinjaTrader Customer Service

    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);
        Paul H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Segwin, 05-07-2018, 02:15 PM
        10 responses
        1,768 views
        0 likes
        Last Post Leafcutter  
        Started by Rapine Heihei, 04-23-2024, 07:51 PM
        2 responses
        30 views
        0 likes
        Last Post Max238
        by Max238
         
        Started by Shansen, 08-30-2019, 10:18 PM
        24 responses
        943 views
        0 likes
        Last Post spwizard  
        Started by Max238, Today, 01:28 AM
        0 responses
        10 views
        0 likes
        Last Post Max238
        by Max238
         
        Started by rocketman7, Today, 01:00 AM
        0 responses
        7 views
        0 likes
        Last Post rocketman7  
        Working...
        X