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

How to get string length

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

    How to get string length

    Hi
    I developed a simple indicator here that shows the high and the low of the current bar or any other prior bar.

    I have had a request to reduce the footprint of this indicator.

    so.. if I have a price of say 4006.5 what I want to do is to show 06.5.

    How do I measure the string length please and how do I access the correct characters?

    I use the following in a DrawText line curently.
    Code:
    string decstr = "N" + Decimal.ToString();//Dec places
    string lowstr = "   " + (Low[NBar] - TickSize).ToString(decstr);
    I have found the word MeasureString but somehow I can't seem to get it to work!?

    #2
    Hi Mindset,

    This is C# string manipulation. Google "C# string manipulation". You will find several links showing you different methods of how to get what you want. Good luck.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      so slow

      Hi Josh

      Ok found what I wanted and incorporated it into my simple indicator.
      it now loads up like glue - I am talking 25seconds to change an input per chart. Ironically the msdn says this string manipulation is particularly fast so is it the way it's coded?
      [ protected override void OnBarUpdate()
      {
      if (Historical) return;


      string decstr = "N" + Decimal.ToString();//Dec places
      string finallow = (( Low[NBar]- TickSize).ToString(decstr)).Substring(Chars);// Chars is an Input int variable
      string finalhigh = ( (High[NBar] + TickSize).ToString(decstr).Substring(Chars));



      //SizeF size = MeasureString(lowstr.ToString(decstr), txtFont);

      DrawText("HighText", false, " " + finalhigh , 0, High[0]+ (TickSize * Math.Abs( Offset)) ,Color.Blue, txtFont, StringAlignment.Near, Color.Transparent, Color.Transparent, 50);
      DrawText("LowText", false, " " + finallow,0, Low[0] - (TickSize*Math.Abs( Offset)),Color.Red, txtFont, StringAlignment.Near, Color.Transparent, Color.Transparent, 50);

      }
      Last edited by Mindset; 11-11-2008, 02:31 PM. Reason: quote not code

      Comment


        #4
        Unfortunately I do not know. You will just have to play with it. You could just try trimming off the beginning part of the string instead.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          mindset,

          I recall reading something about string manipulation being slow.

          It was recommended to me during my masters to use StringBuilder if string manipulation speed is critical.

          I don't know to much to help you, but you can look in that direction.

          hope this helps.
          mrlogik
          NinjaTrader Ecosystem Vendor - Purelogik Trading

          Comment


            #6
            thanks mrlogic I shall try and work through that and see if I can improve the speed - I did reload NT however and it improved considerably but there is definitely a drop off in performance now.

            Comment


              #7
              MeasureString() is a complete resource hog, I highly advise that you use it sparingly. In some of our system indicators such as BarTimer, this method is called once with the exception if the user changes the font in which case it needs to be called again.
              RayNinjaTrader Customer Service

              Comment


                #8
                Hi Ray

                Yes I found it in Bar timer strangely.
                It seems using operands like "+" in strings causes lots of memory pressure or some such technical stuff that is not good.
                I have got round it by doing the calculations separately

                double l = Low[NBar] - TickSize;
                string finallow = h.ToString(decstr).Substring(3);
                I can't seem to get the hang of stringbuilder yet - I eagerly await the arrival of my book on C#.

                Comment


                  #9
                  up the learning curve

                  I paid for a consultant to do some work for me (not this indicator) and this little bit of formatting popped up which I have used here - does away with a whole variable and much more elegant.


                  Code:
                  string finallow = String.Format("{0}",( Low[NBar]- TickSize)).Substring(chars);

                  Comment


                    #10
                    Thanks for posting this snippet Mindset!
                    BertrandNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by chbruno, Today, 04:10 PM
                    0 responses
                    3 views
                    0 likes
                    Last Post chbruno
                    by chbruno
                     
                    Started by josh18955, 03-25-2023, 11:16 AM
                    6 responses
                    436 views
                    0 likes
                    Last Post Delerium  
                    Started by FAQtrader, Today, 03:35 PM
                    0 responses
                    6 views
                    0 likes
                    Last Post FAQtrader  
                    Started by rocketman7, Today, 09:41 AM
                    5 responses
                    19 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Started by frslvr, 04-11-2024, 07:26 AM
                    9 responses
                    127 views
                    1 like
                    Last Post caryc123  
                    Working...
                    X