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

Newbie coding help query; Range Bar query

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

    Newbie coding help query; Range Bar query

    I am brand new to Ninja. Have some experience with Tradestation (many years) but am not the world's best programmer although have made some fairly complex systems with great difficulty (have never mastered loops and arrays for example).

    One thing I miss from Tradestation is a colored bar that reflects the internal volume. I use tick charts for day-trading but (at least in TS) you can also keep track of the volume of contracts up and down within that bar. Volume is not the same as ticks in that one tick might trade 1 contract and the next 100.

    In TS when you load a symbol and choose Ticks, there is an additional option to base volume on either ticks or volume. I am not sure if this exists in Ninja. If it doesn't, then forget about it. But I suspect it does since there are volume indicators.

    I have looked through the Ninja script help area but don't have a clue how to code this myself as simple as it is. Can't even figure out how to color a bar! However, once I see the code, that will help.

    I have 2 types in this one but really I ended up using the first one. If anyone would like to code that for me I would be grateful. Please forgive if it's already been done somewhere. I can't find it obviously.


    Here is the TS code for this very simple Paintbar indicator:

    {TickA PB by ASH}

    inputs: Type(1),UpColor(yellow),DnColor( cyan) ;
    variables: ColorLevel( 0), updown(0),avup(0),avdn(0),avupdown(0) ,upt(0),downt(0);

    upt = upticks; downt = downticks;
    updown = upticks-downticks;
    avup = waverage(upt,3);
    avdn = waverage(downt,3);
    avupdown = waverage(upt-downt,3);

    If Type = 1 then Begin
    If updown>0 then Plotpaintbar(h,l,"Tk",upcolor)
    else Plotpaintbar(h,l,"Tk",dncolor); End;

    If Type = 2 then Begin
    If avupdown > 0 then Plotpaintbar(h,l,"Tk",upcolor)
    else Plotpaintbar(h,l,"Tk",dncolor); End;


    End;


    +++++++++++++++++++++


    2. I notice an indicator that uses only 'range bars'. I can't find them in the help index and don't know what they are nor how to plot them whatever they are. But am curious! Would appreciate a little help there as well so I can explore them.
    Last edited by cclsys; 10-27-2008, 11:07 AM. Reason: checking to see if email verification on

    #2
    cclsys,

    Welcome to the NinjaTrader Support Forums.

    Are you talking about coloring a bar based on if the volume is up instead of based off of price? This is doable with a custom indicator you would need to program. To change bar colors you would use BarColor:
    Code:
    if (some condition...)
         BarColor = Color.Red;
    else if (some other condition...)
         BarColor = Color.Green;
    Range bars are bars generated based off of price ranges. So a range of 10 would be every bar having a close and open differential of 10 ticks.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Josh, thanks.

      yes, the logic is that if upvolume - downvolume > 0 then paint the bar bullish, else paint the bar bearish as expressed in the attached code of first post.

      Josh, any chance you could code it? That's why I posted the code here. Would love to have my usual volume bars back in Ninja!!

      I wouldn't have a clue where to start with Ninja script yet, simple as this is. Just don't understand at all how to write simple things out that can be verified. If you can't because of some sort of policy against coding, maybe somebody else visiting could code it. It's not a big secret or anything, just that I can't code Ninja and also can't view the videos so kindly provided since they are not downloadable and I am on rural dialup. Can only access the Help Menu.

      I will learn when I see things like this put into Ninja Script because I know what the base logic is and then when I see it 'translated' I will understand much better than going through the Wizards etc. And I wouldn't know how to go through a Wizard with this anyway.

      2. OK, I remember what a range bar is. But how do you choose to plot a chart in range bar mode? Couldn't find that option anywhere.


      Originally posted by NinjaTrader_Josh View Post
      cclsys,

      Welcome to the NinjaTrader Support Forums.

      Are you talking about coloring a bar based on if the volume is up instead of based off of price? This is doable with a custom indicator you would need to program. To change bar colors you would use BarColor:
      Code:
      if (some condition...)
           BarColor = Color.Red;
      else if (some other condition...)
           BarColor = Color.Green;
      Range bars are bars generated based off of price ranges. So a range of 10 would be every bar having a close and open differential of 10 ticks.

      Comment


        #4
        1. Unfortunately I cannot do custom coding. Hopefully a community member can chime in or you can try one of the 3rd party NinjaScript Consultants here: http://www.ninjatrader.com/webnew/pa...injaScript.htm

        2. It is a chart type you can select. Instead of choosing Minutes or Ticks you can choose Range.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          OK, josh, although disappointed I understand about 'custom coding' although I think this is so simple that it shouldn't qualify as such.

          In a previous msg you wrote:
          if (some condition...)
          BarColor = Color.Red;
          else if (some other condition...)
          BarColor = Color.Green;

          So my follow-up is:

          1. what do you have to write before that snippet above to satisfy various protocols in the script routines so that it is an indicator? You can't just write that little excerpt above can you? I can't use the Wizard on this because I don't understand what things to fill in or not with this particular case since it's not an indicator using a pre-existing function like RSI or something.

          2. In terms of my condition: How do you tell the program to identify up volume versus down volume? I can't find anything in the help index that explains this and don't understand the example codes in some of the (very good) volume histograms provided. Again, by 'up volume' here I mean nr of contracts up (at offer) (not the same as number of up ticks).

          3. Is there a reference list somewhere to pre-existing functions or do you just have to find them somehow in the index of everything? (In TS there is an 'Easy Language Dictionary' button that takes you into EL stuff only. Maybe you have one and I just haven't found it.

          So how would you write what you call 'some condition' in terms of up volume in a bar? The only reference I can find in the Help Menu is volume which is the total of all volume.

          Originally posted by NinjaTrader_Josh View Post
          1. Unfortunately I cannot do custom coding. Hopefully a community member can chime in or you can try one of the 3rd party NinjaScript Consultants here: http://www.ninjatrader.com/webnew/pa...injaScript.htm

          2. It is a chart type you can select. Instead of choosing Minutes or Ticks you can choose Range.

          Comment


            #6
            1. That is correct. The snippet is to be placed inside the OnBarUpdate() method that is created by the Indicator Wizard. I recommend you first start with these tutorials on how to make your own custom indicator. Things may start clicking after you give them a read: http://www.ninjatrader-support.com/H...verview23.html

            2. Not sure what you mean by "nr of contracts up". You can get the volume at various bid/ask prices, but this becomes more advanced programming. There is a reference sample available for this in the Reference Sample section of the forums, but I am not sure if that is what you wanted. It is called something along the lines of "Creating your own Level II book".

            3. The Index is the best place we have.

            If you wanted to see if volume is up based on if the price is up you need to program it.

            In Variables section of your code:
            Code:
            private int currVol = 0;
            private int prevVol = 0;
            private int upVol = 0;
            private int downVol = 0;
            In Initialize() method
            Code:
            CalculateOnBarClose = false;
            In OnBarUpdate() method
            Code:
            currVol = Volume[0] - prevVol;
            prevVol = Volume[0];
            if (Close[0] > Open[0])
                 upVol = upVol + currVol;
            else if (Close[0] < Open[0])
                 downVol = downVol + currVol;
            Unfortunately, cclsys, I cannot provide much more code than this. We just don't have enough bandwidth to custom code everybody's requests. Thank you for understanding.
            Last edited by NinjaTrader_JoshP; 10-27-2008, 03:22 PM.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Thanks. I have been through all the tutorials but have no idea how to even begin plotting this very simple thing using the Wizards.

              By up volume I mean nr. of shares/contracts up in a bar. I presume this is calculated by those trading at the offer. In TS this is quite automatic so I confess I am not sure, but that is probably it.

              In a tick chart, say 233 tick, you have 233 ticks each bar. But some ticks have 100 contracts traded and some only 1. It is helpful to see differentiate this volume in a tick chart. Often you find a bar closing lower with a lower low but in fact the internal net contract volume is positive. This is a sign that buying is coming into the market even as price declines.

              I have no idea if Ninja can see this (although I suspect it can given the volume histogram indicators included as defaults). But it seems that what you are saying is that unless someone else on the forum feels like coding it for fun that basically I am on my own. Also, if I can't find specific language I am on my own. For example, whether or not there is an existing function for upvolume and downvolume I am on my own to find that out somehow. Or if there is a way of getting that info but not with a pre-programmed function I am on my own.

              This is a very, very simple thing upvolume versus down volume. Seems like a) if it exists I won't be able to find it or
              b) if it doesn't exist I won't be able to figure out how to have it coded unless I first become an expert or
              d) pay someone to code this 1-2 lines of code simply because the Help Index doesn't really include all pre-existing functions.

              Forgive me for being snotty. Having got used to programming such simple things in EL in a matter of minutes without the slightest problem it is galling to suddenly be totally clueless and to find that the tutorials don't help me understand the basics at all.

              Knowing me, I won't give up just yet. But my first impression is that without access to the video resources you make available only via streaming that learning Ninja Script is going to be 10x harder than EL and that was hard enough almost 20 years ago!

              Ash.


              Originally posted by NinjaTrader_Josh View Post
              1. That is correct. The snippet is to be placed inside the OnBarUpdate() method that is created by the Indicator Wizard. I recommend you first start with these tutorials on how to make your own custom indicator. Things may start clicking after you give them a read: http://www.ninjatrader-support.com/H...verview23.html

              2. Not sure what you mean by "nr of contracts up". You can get the volume at various bid/ask prices, but this becomes more advanced programming. There is a reference sample available for this in the Reference Sample section of the forums, but I am not sure if that is what you wanted. It is called something along the lines of "Creating your own Level II book".

              3. The Index is the best place we have.

              If you wanted to see if volume is up based on if the price is up you need to program it.

              In Variables section of your code:
              Code:
              private int currVol = 0;
              private int prevVol = 0;
              private int upVol = 0;
              private int downVol = 0;
              In Initialize() method
              Code:
              CalculateOnBarClose = false;
              In OnBarUpdate() method
              Code:
              currVol = Volume[0] - prevVol;
              prevVol = Volume[0];
              if (Close[0] > Open[0])
                   upVol = upVol + currVol;
              else if (Close[0] < Open[0])
                   downVol = downVol + currVol;
              Unfortunately, cclsys, I cannot provide much more code than this. We just don't have enough bandwidth to custom code everybody's requests. Thank you for understanding.

              Comment


                #8
                The code I've provided should do up volume and down volume exactly how you have specified.

                Maybe you are misunderstanding the purpose of the Indicator Wizard. The Indicator Wizard does NOT allow you to input any logic. It is just there to help you setup the initial parameters and variables you may want to use later. After you finish the wizard you need to go over to the actual code and input in your logic there. This is why on all of the Indicator tutorials they have a section called "Entering Calculation Logic".

                Unfortunately there are no more resources than the ones already provided. It is best for you to review the tutorials and just try opening up various pre-installed NinjaTrader indicators. NinjaScript is not a scripting language and as such it does inevitablly include some of the complexities of the underlying programming language, C#. The benefits of this are vast since there are no bounds to what can be done in C#.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Where in the Help Menus can you find some sort of introduction to the Ninja Script language.

                  For example below you mention the 'variables section'. Where is this explained as such, i.e. what a variable is and how it is introduced?

                  Like how do I learn what the 'private int' business is?

                  When you do a Wizard it is done for you but I don't know what is being done so that when I try to do something myself I am totally clueless.

                  Having gone through all the tutorials twice I still don't have the faintest idea how to even code in getting a moving average to change color when it crosses over another. I couldn't figure out how to change the colour of a bar because that wasn't in a tutorial either. I have no idea how to write the very simplest of code because although there are many examples, there is nothing explaining how the language works, which bits have to be there in the beginning, which parts are functions or not, what a variable is in your language and so forth. Have I just missed a whole chapter somewhere that you could point me to? Please don't point to the tutorials again since I have done those and they don't help on this level. ( I couldn't get my first one to verify because I typed in 'If' instead of 'if' but the error helper kept saying it wanted to see a ' ; ' )

                  Truly, I am absolutely flummoxed by the Ninja Script interface and don't have the faintest idea how to proceed or learn it at all.

                  The volume code you supplied below is, I believe, has something to do with adding up volume if the close is up and downvolume if close is down. That has nothing to do with what I am asking which is simply to record the net up or down volume per bar, volume being nr shares/contracts not nr ticks.

                  In Variables section of your code:
                  Code:
                  private int currVol = 0;
                  private int prevVol = 0;
                  private int upVol = 0;
                  private int downVol = 0;
                  In Initialize() method
                  Code:
                  CalculateOnBarClose = false;
                  In OnBarUpdate() method
                  Code:
                  currVol = Volume[0] - prevVol;
                  prevVol = Volume[0];
                  if (Close[0] > Open[0])
                       upVol = upVol + currVol;
                  else if (Close[0] < Open[0])
                       downVol = downVol + currVol;
                  Unfortunately, cclsys, I cannot provide much more code than this. We just don't have enough bandwidth to custom code everybody's requests. Thank you for understanding.[/quote]

                  Comment


                    #10
                    cclsys,

                    The tutorials is the section of the Help Guide related to NinjaScript. There are no other resources than what we have already provided you with.

                    Variables section is in the code itself. You need to open up your indicator file. You will see a section titled "Variables" with a + sign on the left. Press the + sign and it will expand the Variables section.

                    "private int" business is C#. Unfortunately this is standard C# procedure and it may be best if you picked up a C# book or try googling some resources.

                    What the Wizard does are generally all things you don't ever need to do by hand. Simply because you can always do them from the Wizard so you do not need to worry about this.

                    To code MA cross over change color is like how you would code anything else cclsys. You create your indicator then you go to the OnBarUpdate() method and input in your code. You input in your logic then you input in what you want the action to be. In a prior post I already told you how to do the action you wanted.
                    Code:
                    BarColor = Color.Red;
                    The code I provided does exactly what you want for up volume and down volume. It is aggregating the net up and net down for you on a per trade basis. Every single tick has an associated volume with it. If that tick went up it will add it to the net up volume. if that tick was a down tick it will add the volume to the net down volume.

                    Where you start learning NinjaScript is by opening up some indicators and looking at the code. Read the Tutorials over again. Try actually doing the tutorials yourself. You may even try using the Strategy Wizard. The Strategy Wizard allows you to add Conditions and Actions. Unfortunately there is no other way we can show you how to program. Again, try googling C# and maybe pick up a book from the library on C# programming.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      The code I provided does exactly what you want for up volume and down volume. It is aggregating the net up and net down for you on a per trade basis. Every single tick has an associated volume with it. If that tick went up it will add it to the net up volume. if that tick was a down tick it will add the volume to the net down volume.

                      if (Close[0] > Open[0])
                      upVol = upVol + currVol;
                      else if (Close[0] < Open[0])
                      downVol = downVol + currVol
                      Josh, obviously I am frustrated for which I apologise. Thanks for your other remarks. I will go through things several times until it starts to sink in.

                      However, from the code above viz. volume I just don't see how that has anything to do with what I am asking, leaving aside my coding issues.

                      What I am asking has nothing to do with whether the close > open etc. It is simply whether or not there is net up volume versus down volume and volume here is not the same as uptick versus downtick since a tick is a tick and volume of shares is not the same thing. If as you say Ninja automatically distinguishes up tick from up volume that is great. But how would your write that out without all the reference to close and open which has nothing to do with it?

                      Thanks.

                      Comment


                        #12
                        cclsys,

                        Then I do not know what you mean by volume. Volume only goes up; it never goes down except for when it is resetting itself for the next bar.

                        You need to redefine out what up volume is to you. Up volume to me is the associated volume with an upward moving price. Down volume is the associated volume with a downward moving price. The way you determine up or down price movement is by comparing Close against Open.

                        If you wanted to check this every single tick, the current tick's price against the previous tick's price, this is possible too.
                        Code:
                        if (Close[0] > prevTick)
                             upVol = upVol + currVol;
                        else if (Close[0] < prevTick)
                             downVol = downVol + currVol;
                        prevTick = Close[0];
                        Last edited by NinjaTrader_JoshP; 10-27-2008, 04:58 PM.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          Josh, I don't believe your code example will do what is desired here. Take up volume for example. One definition of up volume is what you used: Accumulate up volume if the current bar close is greater than the current bar open.

                          Rather, what cclsys wants is an implementation of TradeStation's UpTicks and DownTicks functions, which can return values in volume units on a per-tick basis per bar.

                          That is, if the current tick is higher than the previous tick in the same bar, add the current tick volume to upVol. If the current tick is lower than the previous tick in the same bar, then add the current tick volume to downVol.

                          So, tell me if this would do it in OnBarUpdate():
                          Code:
                          if (FirstTickOfBar) upVol = downVol = 0;
                          currVol = Volume[0] - prevVol;
                          prevVol = Volume[0];
                          if (Close[0] > prevPrice)
                               upVol += currVol;
                          else if (Close[0] < prevPrice)
                               downVol += currVol;
                          prevPrice = Close[0]; // save new prevPrice for next tick
                          I'm primarily an EOD trader so I'm unfamiliar with how things work on tick data. I'm assuming here that OnBarUpdate() is executed on every tick if CalculateOnBarClose=false, correct? Or should the code above be in OnMarkedData()?

                          -Alex
                          Last edited by anachronist; 10-27-2008, 05:05 PM.

                          Comment


                            #14
                            Alex,

                            Thanks for the clarification. I am not familiar with the TradeStation functions. Your code is the same code in my last post so there should be no problems here anymore.
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              Ah, you're right. I didn't notice the code in your last post; I just replied to an earlier one. Is the "reset" (first line) in my code the correct thing to do?
                              -Alex

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by CortexZenUSA, Today, 12:53 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post CortexZenUSA  
                              Started by CortexZenUSA, Today, 12:46 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post CortexZenUSA  
                              Started by usazencortex, Today, 12:43 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post usazencortex  
                              Started by sidlercom80, 10-28-2023, 08:49 AM
                              168 responses
                              2,265 views
                              0 likes
                              Last Post sidlercom80  
                              Started by Barry Milan, Yesterday, 10:35 PM
                              3 responses
                              11 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Working...
                              X