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

Exit short if new low hasn't been made for 20 bars

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

    Exit short if new low hasn't been made for 20 bars

    Hey, it's my first post here.

    Can somebody help me out with a code, that would exit short trades, if new low hasn't been made for, let's say 20 bars. The logic is to give some room for the trade to pan out, but if it doesn't continue (make new low), exit.

    Having this example code, I could backtest and optimize what is the best bar count setting and also modify code for exit long.

    Thanks in advance.

    #2
    Hello UltraNIX,

    Thanks for your post and welcome to the NinjaTrader Forums!

    What you can do is create an int type variable to save the current bar number from your starting point and a double variable to hold low price level that is the reference level. For example create an int variable called startBar and a double called reference_Level. You will also need a bool variable to prevent the exit if a new low has been found. For example call the bool myshortBool which is initial set to false.

    if (your conditions to enter short)
    {
    // enter short order
    startBar = CurrentBar; // save the bar number as a starting point to count from
    reference_Level = Low[0]; // save the low of the bar as the reference to test against
    myshortBool = true; // set the bool true to enable the short exit when conditions are no new low and 20 bars after entry.
    }

    if (Position.MarketPosition == MarketPosition.Short )
    {
    if (Low[0] < reference_Level)
    {
    myshortBool = false; // if a new low is found then no longer need to exit this way
    }
    if (CurrentBar - savedBar == 20 && myshortBool == true) // bool must be true (no new low found) and bar count must be 20
    {
    exitShort();
    }
    }



    Reference: https://ninjatrader.com/support/help...currentbar.htm

    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks for your reply, PaulH!

      I just want to get clarification on 2 things:
      1) I cannot found initial setting of savedBar, should it be named startBar? (in: CurrentBar - savedBar == 20)
      2) Is it counting 20 bars from initial bar (startBar), or from any bar that had a new high? I mean, my intention is to not just measure whether it never made a new high after initial bar, but, let's say the trade is running for 84 bars, and then it doesn't make a higher high above 84th bar for 20 bars, so I want to exit on 104th bar (in this example, 84+20). Will your script achieve that?

      Originally posted by NinjaTrader_PaulH View Post
      Hello UltraNIX,

      Thanks for your post and welcome to the NinjaTrader Forums!

      What you can do is create an int type variable to save the current bar number from your starting point and a double variable to hold low price level that is the reference level. For example create an int variable called startBar and a double called reference_Level. You will also need a bool variable to prevent the exit if a new low has been found. For example call the bool myshortBool which is initial set to false.

      if (your conditions to enter short)
      {
      // enter short order
      startBar = CurrentBar; // save the bar number as a starting point to count from
      reference_Level = Low[0]; // save the low of the bar as the reference to test against
      myshortBool = true; // set the bool true to enable the short exit when conditions are no new low and 20 bars after entry.
      }

      if (Position.MarketPosition == MarketPosition.Short )
      {
      if (Low[0] < reference_Level)
      {
      myshortBool = false; // if a new low is found then no longer need to exit this way
      }
      if (CurrentBar - savedBar == 20 && myshortBool == true) // bool must be true (no new low found) and bar count must be 20
      {
      exitShort();
      }
      }



      Reference: https://ninjatrader.com/support/help...currentbar.htm

      Comment


        #4
        Hello UltraNIX,

        Thanks for your reply.

        "I cannot found initial setting of savedBar, should it be named startBar? (in: CurrentBar - savedBar == 20)" The example script was provided as an example that would meet the specific requirements of your first post. As you would ultimately need to write this into your code, you can call the variable what ever you want, I used startBar as the name of the variable. The variable name should reflect what the variable is used for when possible (for good code reading practices). You can declare and assign an initial value at the class level, for example private int savedBar = 0;

        "Is it counting 20 bars from initial bar (startBar), or from any bar that had a new high?
        The example script was provided as an example that would meet the specific requirements of your first post which was based on a new low scenario.

        I mean, my intention is to not just measure whether it never made a new high after initial bar, but, let's say the trade is running for 84 bars, and then it doesn't make a higher high above 84th bar for 20 bars, so I want to exit on 104th bar (in this example, 84+20). Will your script achieve that?" The example script was provided as an example that would meet the specific requirements of your first post.

        I provide a specific example for you based on your statement of "Having this example code, I could backtest and optimize what is the best bar count setting and also modify code for exit long."

        I would suggest testing the example code and using print statements to help understand how it works as you will need to understand how the code works in order to change its application. Here is a link to our debugging tips that provide information on how to use the print statement: https://ninjatrader.com/support/help...script_cod.htm
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          EDIT: After testing code in NinjaScript Editor and applying some changes, I got it to work, thanks!
          Last edited by UltraNIX; 06-03-2020, 03:10 AM.

          Comment


            #6
            However, it does not work properly. Here I am attaching a screenshot, where I explained what failed in this attempt.

            I am also posting the strategy I am using here, so it would be easier to get into the code.

            Comment


              #7
              Hello UltraNIX,

              Thanks for your reply.

              In taking a quick look at your code, it appears that your exit condition is not contained within the Position check.

              Please check the braces {} of my example compared to what you have. For example, the } at line 126 should be below the brace on line 130.





              Paul H.NinjaTrader Customer Service

              Comment


                #8
                I updated braces for both Long (instead of at line 126 - I put int on line 130) and Short. However, no changes in performance.

                Here's an updated screenshot and updated strategy file:

                Comment


                  #9
                  Hello UltraNIX,

                  Thanks for your reply.

                  If your strategy is not working as expected and you do not see any errors in the "log" tab of the control center, your next step would be to begin the debugging process to determine why the logic is not performing as expected. Typically this is done by print statements that send their output to the New>ninjascript output window. You would use the print statement to print out what the values were that would go into the conditions that make the trade exit.

                  To get you started with the process, here is a link to our debugging tips: https://ninjatrader.com/support/help...script_cod.htm

                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    PaulH,

                    It has to be something with a code.

                    Because. It works fine, when:
                    1) it triggers ->
                    2) trigger bar is the lowest low ->
                    3) it exits after preset number of bars not making new low.

                    However, startBar or savedBar, should update on each bar update and re-evaluate, whether the current bar's low is the lowest in the sequence. And then, if 20 (or any other preset) bars passes without making a new low, it triggers a stop.

                    My idea is like this:
                    If (low [0] < low [1])
                    {
                    savedbar = CurrentBar;
                    reference_Level = Low[0];
                    }

                    Comment


                      #11
                      Hello UltraNIX,

                      Thanks for your reply.

                      Using the Print statement and printing out the values of the variables prior to the condition evaluation would help to clarify this for you.
                      Paul H.NinjaTrader Customer Service

                      Comment


                        #12
                        Yes, I modified the code I wrote in my latest reply and it worked! Thank you for assistance, PaulH!

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by manitshah915, Today, 12:59 PM
                        0 responses
                        2 views
                        0 likes
                        Last Post manitshah915  
                        Started by ursavent, Today, 12:54 PM
                        0 responses
                        2 views
                        0 likes
                        Last Post ursavent  
                        Started by Mizzouman1, Today, 07:35 AM
                        3 responses
                        17 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Started by RubenCazorla, Today, 09:07 AM
                        2 responses
                        13 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by i019945nj, 12-14-2023, 06:41 AM
                        7 responses
                        82 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Working...
                        X