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 TraderBCL, Today, 04:38 AM
        2 responses
        14 views
        0 likes
        Last Post TraderBCL  
        Started by martin70, 03-24-2023, 04:58 AM
        14 responses
        105 views
        0 likes
        Last Post martin70  
        Started by Radano, 06-10-2021, 01:40 AM
        19 responses
        607 views
        0 likes
        Last Post Radano
        by Radano
         
        Started by KenneGaray, Today, 03:48 AM
        0 responses
        5 views
        0 likes
        Last Post KenneGaray  
        Started by thanajo, 05-04-2021, 02:11 AM
        4 responses
        471 views
        0 likes
        Last Post tradingnasdaqprueba  
        Working...
        X