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

recent bar bigger then last x amount of bars and exit on crossing SMA

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

    #16
    Hello Paste1903,

    I don't see anything necessarily wrong with this code.

    We'll need the debugging information from prints and TraceOrders to say for certain what is going on with the script. As mentioned, in particular you should make sure to print out the values of the bar size and of the bars being compared. Then you can see if these bar sizes are actually 4x as large.

    In the strategy add prints (outside of any conditions) that print the date time of the bar and all values compared in every condition that places an order. You could print out the index of the bar, the size of the bar being compared, and the size of the current bar.

    The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.

    Further, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

    Please let me know if I may further assist with analyzing the output or if you need any assistance creating a print or enabling TraceOrders.​
    Gaby V.NinjaTrader Customer Service

    Comment


      #17
      ok thank you i will add prints the next days and let you know if im done.

      best,

      Comment


        #18
        hi gaby,

        the output is attached. i dont know if im done it right. ive used the method Print("step1"); trough my script for each step.

        best,
        Attached Files

        Comment


          #19
          Hello Paste1903,

          Thank you for your response.

          Unfortunately, these prints aren't descriptive enough to understand the strategy's behavior.

          The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.

          Below is a link to a forum post that demonstrates using prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.
          https://ninjatrader.com/support/foru...121#post791121

          Let me know if you need any assistance creating a print or enabling TraceOrders.

          If you would like assistance creating a print, please share your code or a portion of your code and I can provide an example print statement.

          Save the output from the output window to a text file and provide this with your reply. I'll be happy to assist with analyzing the output.​


          You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services.

          Please let me know if you would like a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.​
          Gaby V.NinjaTrader Customer Service

          Comment


            #20
            hi gaby

            ok thank you. i'll take another look at the post and try it out at the weekend. how much does a consultant cost? you can send me a list of the relevant consultants.
            also here is my code:

            protected override void OnBarUpdate()
            {

            if (CurrentBar < 30)
            return;

            //bool to keep track if the current bar fits the criteria for entering long
            bool isLarger = false;

            //loop throught the previous 30 bars
            for (int index = 1; index <= 30; index++)
            {
            //calculate the bar size. use absolute value to prevent negative values.
            double comparisonBarSize = Math.Abs(Open[index] - Close[index]);
            double currentBarSize = Math.Abs(Open[0] - Close[0]);

            //previous 2 bars
            if (index == 1 || index == 2)
            {
            //check if the size of the current bar is 4x larger than the bar we are currently comparing
            if (currentBarSize >= (comparisonBarSize * 4))
            {
            //set our bool to true if the current bar is 4x as big
            isLarger = true;
            }

            else
            {
            //else, set the bool to false. we will exit the loop since one of the bars does not fit the criteria,
            //we know that the current bar is not 4x larger than both of the previous 2 bars.
            isLarger = false;
            return;
            }

            }

            //3rd to the 30th bar
            if (index >= 3)
            {
            //check if the size of the current bar is 2x larger than the bar we are currently comparing
            if (currentBarSize >= (comparisonBarSize * 2))
            {
            //set our bool to true if it is
            isLarger = true;
            }

            else
            {
            //else, set the bool to false. we will exit the loop since once one of the bars does not fit the criteria,
            //we know that the current bar is not 2x larger than ALL of the last 30 bars.
            isLarger = false;
            return;
            }

            if (BarsInProgress != 0)
            return;

            if (CurrentBars[0] < 1
            || CurrentBars[2] < 0
            || CurrentBars[3] < 0)
            return;

            Print("set 1");
            // set 1
            if ((Close[0] > SMA1[0])
            && (Close[0] > SMA2[0])
            && (Close[0] > SMA3[0])
            && (isLarger == true))

            {
            Print("EnterLong");
            EnterLong(Convert.ToInt32(DefaultQuantity), "");

            }

            Print("Set 2");
            // set 2
            if ((Close[0] < SMA1[0])
            && (Close[0] < SMA3[0])
            && (Close[0] < SMA2[0])
            && (isLarger == true))

            {
            Print("EnterShort");
            EnterShort(Convert.ToInt32(DefaultQuantity), "");

            }
            Print("SMAcross below");
            if (CrossBelow(Close, SMA4, 1))
            {
            Print("SetStopLoss 1");
            SetStopLoss(CalculationMode.Price, Close[2]);
            }
            Print("SMAcross above");
            if (CrossAbove(Close, SMA4, 1))
            {
            Print("SetStopLoss 2");
            SetStopLoss(CalculationMode.Price, Close[2]);​
            thank you and have a nice weekend.

            Comment


              #21
              Hello,

              Here is an example print statement for your first entry condition:

              Code:
              if ((Close[0] > SMA1[0]) && (Close[0] > SMA2[0]) && (Close[0] > SMA3[0]) && (isLarger == true))
              Print(Times[0][0] + " Close[0]: " + Close[0] + " > SMA1[0]: " + SMA1[0] + " & Close[0]: " + Close[0] + " > SMA2[0]: " + SMA2[0] + " & Close[0]: " + Close[0] + " > SMA3[0]: " + SMA3[0] + " & isLarger: " + isLarger + " == true");


              You can search our extensive library of NinjaScript consultants through the link below. Simply enter a consultant name or search by using our filter categories. Once you have identified your consultants of choice, please visit each consultant's site for more information or contact them directly to learn more:

              https://ninjatraderecosystem.com/sea...mming-services

              You can locate the contact information for the consultants on their direct websites for any additional questions you may have. Since these consultants are third-party services for NinjaTrader, all pricing and support information will need to be obtained through the consultant.

              The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The companies and services listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem, LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.

              Let me know if I may be of further assistance.
              Last edited by NinjaTrader_Gaby; 03-22-2024, 08:47 AM.
              Gaby V.NinjaTrader Customer Service

              Comment


                #22
                hi gaby,

                when i use your code example i get the following output.

                its attached.

                best,
                Attached Files

                Comment


                  #23
                  Hello Paste,

                  This output only has the print I provided you, which was just an example you could use to create the rest of your prints.

                  We need prints for every for all conditions that place an order in your strategy.

                  If you would like to continue debugging the script yourself, I recommend watching this video that demonstrates debugging using prints in the Strategy Builder for guidance on debugging:

                  NT8 Strategy Builder - https://drive.google.com/file/d/1CCl...gEIwJKv6L/view

                  Please let me know if I can assist further.
                  Gaby V.NinjaTrader Customer Service

                  Comment


                    #24
                    sorry. i had uploaded the wrong file. here is the correct file.

                    i have not yet been able to find out how i can display "enterLong" as a print. but i don't think that's so important in this topic at the moment. it's just about why the strategy still has some errors. can you already do something with the output or have i overlooked something?​
                    Attached Files

                    Comment


                      #25
                      Hello Paste,

                      Thanks for the output.

                      However, I do not see the print statements for the conditions that submit your stop loss. I would also like to note, set methods cannot be unset, so it is important call Set methods with CalculationMode.Ticks when flat, and before calling a new entry.

                      Additionally, in particular what behavior in your strategy is not happening as expected? Are certain trades executing or not executing when you are expecting? If so, please provide the date and time/stamp of these trades so we can examine them in the output.

                      Thank you in advance. ​
                      Gaby V.NinjaTrader Customer Service

                      Comment


                        #26
                        hey gaby,

                        ok so i'll put this on oneachtick. thank you.

                        can you send me an example of a print that i can use with my stoploss? i haven't quite figured out how to set my stoploss at 2 min chart and have it take effect when my SMA4 crosses. 2nd possibility would be a stoploss when the low of the 2 bars before is broken.

                        i will send you a new output later and the false entrys etc with time etc. should i send it to you in europe time? or your timezone? but in my output is my timezone set

                        best,

                        Comment


                          #27
                          Hello,

                          Here is an example print you can use for your stop loss cross below condition.

                          Print("Close[1]: " + Close[1] + " > SMA4[1]: " + SMA4[1] + " & Close[0]: " + Close[0] + " < SMA4[0]: " + SMA4[0]);

                          Please let me know if you have any further questions.
                          Gaby V.NinjaTrader Customer Service

                          Comment


                            #28
                            hi gaby,

                            here is the new output from today. my timezone is UTC/GMT+1

                            here are 2 examples that show that the strategy is not yet working properly:


                            MNQ 26.03. 7:32am buy long position
                            the long position had to be exit at 8:14am crossing below SMA4 but the exit was at 10:40am

                            MNQ 26.03 01:36am buy long position was false because the bar was not 2x bigger then the previous 30 bars


                            best,
                            Attached Files

                            Comment


                              #29
                              the long position had to be exit at 8:14am crossing below SMA4 but the exit was at 10:40am
                              Do you have the print statements placed outside of the conditions? I am not seeing these prints happen on every bar. I see the order placed at 7:32AM but there are no further prints/updates until 8:40AM. If the prints were placed outside of the conditions, we would see a print for every bar update.

                              Please place the prints outside of the conditions so we can see what happens on every bar update, like the 8:14am bar.

                              MNQ 26.03 01:36am buy long position was false because the bar was not 2x bigger then the previous 30 bars
                              To figure out why isLarger was true on that bar, you will also need to add print statements to the code for all the calculations that determine if isLarger should be true.



                              You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services.

                              Please let me know if you would like a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.​​
                              Gaby V.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by fx.practic, 10-15-2013, 12:53 AM
                              5 responses
                              5,404 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by Shai Samuel, 07-02-2022, 02:46 PM
                              4 responses
                              95 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by DJ888, Yesterday, 10:57 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by MacDad, 02-25-2024, 11:48 PM
                              7 responses
                              159 views
                              0 likes
                              Last Post loganjarosz123  
                              Started by Belfortbucks, Yesterday, 09:29 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post Belfortbucks  
                              Working...
                              X