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

Doing a calculation within N bars where N is an user

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

    Doing a calculation within N bars where N is an user

    Hi, I saw a video where they said, you can create an strategy using strategy builder and then copy the conditions from there and put it inside of an indicator. I wanted to do that, but I got stuck at "Within N bars" I can't seem to do that using the NT strategy builder.



    Code:
    USER INPUT:
    N
    
    VARIABLES:
    HighOfCrossOver
    
    
    if PRICE crosses above KAMA
    {
        HighOfCrossOver = high
    }
    
    if within N bars
    {
        if price closes below
       {
          set HighOfCrossOver = 0
          cancel all calculations
       }
       else if price closes above or equal to HighOfCrossOver
       {
          print a dot/cross at the closing price that NT DataBox can read.
       }
    }

    #2
    Hello fawzanalim,

    Thanks for your post.

    Yes, this is a limit in the strategy builder to use a bar counter. This can be done but requires a custom series and some extra effort in the strategy builder that would not be needed in a Ninjascript.

    What you would do in Ninjascript is to create an int type variable that you would save the value of CurrentBar (this is the system bar counter) when you do HighOfCrossOver = high. Then to find out within N bars you would subtract CurrentBar minus the variable to determine the number of bars.

    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Correct me if I'm wrong.

      Code:
      for (i = 0; i < N, i++)
      {
          if (close of current bar > high of (currentBar - i))
              print a dot at the closing price of the current bar.
      }
      Though can you please provide an example? This would help me a lot.
      Last edited by fawzanalim; 12-03-2020, 09:07 AM.

      Comment


        #4
        Hello fawzanalim,

        Thanks for your reply.

        Sorry, I am not following your logic.

        Can you attach a screenshot of a chart that shows what you are looking to do? You can mark on the chart the key points.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          The code and screenshot is here. As I'm new in this sector, I'm having so much trouble. My only hope is the NT Forum. I will be glad if you let me know where the problem is in my code.

          Comment


            #6
            Hello fawzanalim,

            Thanks for your reply.

            The draw code will draw one and only one dot because the tag name is the same on every dot. Each draw object must use unique tag names, if they use the same tag name then as one is drawn the previous one is removed.

            The draw code will draw the dot at the value of "Entryoffset" which is set to 5, which means the one dot you could see would be drawn at a price level of 5.

            I would suggest something like: Draw.Dot(this, @"KAMA3rdSetup Dot_1"+CurrentBar, false, 0, Close[0], Brushes.CornflowerBlue); // print a dot on the close of every bar that meets the conditions.

            If you want to offset the dot from the bar then you could use something like: Draw.Dot(this, @"KAMA3rdSetup Dot_1"+CurrentBar, false, 0, High[0] + EntryOffset * TickSize, Brushes.CornflowerBlue); // draw the dot at Entryoffset ticks above the high of the bar

            Note that I added +CurrentBar to the tag name. This creates a unique tag name and it means that all of the drawn dots will now show on the chart. CurrentBar is the systems bar counter so its number will be different for every bar.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Hello fawzanalim,

              In addition to the above, you also need to add the following to properly initialize the KAMA indicator in your code

              else if (State == State.DataLoaded)
              {
              KAMA1 = KAMA(2, 10, 30); // you can apply any settings that you need for the KAMA
              }


              NOTE: Current your code shows:
              else if (State == Sttae.Configure)
              {
              }


              which is not used, so you can change .Configure to .DataLoaded then put the KAMA1 line between the { }
              Paul H.NinjaTrader Customer Service

              Comment


                #8
                Sir, I added an custom series instead of drawing dots. But why is it not showing up in the Data Box? Can you also please include the link to NT Help Guide where I can learn in details how to add a custom series that appears in the Data Box and also how to customize the design of it. Thank you.

                Comment


                  #9
                  Hello fawzanalim,

                  Thanks for your post.

                  Instead of a custom series, I recommend adding a plot that will show up in the data box. You can also set the plot style to plot Dots.

                  References:


                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    Thanks man, you are the BEST. <3

                    Comment


                      #11
                      I tried to use amaSuperTrendU11 from Lizard Indicator. I have a folder named LizardIndicators in my Indicators list. Inside of that, I have amaSuperTrendU11. I tried to use that in my indicator, but I am having this error. Line 48 and 95 is generated using Ninja Trader Strategy Builder.

                      Comment


                        #12
                        Hello fawzanalim,

                        Thanks for your reply.

                        I would suggest using the Strategy Builder and create a condition of close greater than the indicator. Then click on "view code" and it will show you the correct way to add/use the indicator in your strategy.
                        Paul H.NinjaTrader Customer Service

                        Comment


                          #13
                          I did, I did that. i created the a complete logic using strategy builder. when i got error, i erased everything and kept only 2 lines. but it still giving me the error. the error log is in the drive link. i am adding it here as well.

                          NinjaScript File: KAMA3rdSetupInd.cs

                          Error: The type or namespace name 'LizardIndicators' could not be found (are you missing a using directive or an assembly reference?)

                          Code: CS0246

                          Line: 48

                          Column: 11

                          Comment


                            #14
                            Hello fawzanalim,

                            Thanks for your reply.

                            Please make sure you are using the latest version of NinjaTrader 8.0.23.2. (Check under help about).

                            If not please update to the latest version and create a new strategy from the strategy builder accessing the same indicator.

                            If that does not work, or you are on the latest version, I would suggest contacting the 3rd party vendor to let them know the indicator is not working in the Strategy Builder.


                            ​​​​​​​
                            Paul H.NinjaTrader Customer Service

                            Comment


                              #15
                              How to check if today's Sunday? I actually need to know how do I get the today's day, at the moment, at my time zone. Thank you.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by terofs, Yesterday, 04:18 PM
                              1 response
                              21 views
                              0 likes
                              Last Post terofs
                              by terofs
                               
                              Started by CommonWhale, Today, 09:55 AM
                              1 response
                              3 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Started by Gerik, Today, 09:40 AM
                              2 responses
                              7 views
                              0 likes
                              Last Post Gerik
                              by Gerik
                               
                              Started by RookieTrader, Today, 09:37 AM
                              2 responses
                              13 views
                              0 likes
                              Last Post RookieTrader  
                              Started by alifarahani, Today, 09:40 AM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Working...
                              X