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

Writing to a text file with a fixed width between characters

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

    Writing to a text file with a fixed width between characters

    Hi,

    I would like to know if it is at all possible to have a fixed alignment in terms of space between characters when writing to a text file I don't mean + " " + I am talking about for example instead of displaying the following like so;

    1
    10

    but instead have a space in front of the 1 so that the right hand side of the column (last character) ends at the same point, like Excel for example.

    Regards,
    suprsnipes
    Last edited by suprsnipes; 01-21-2013, 06:14 PM.

    #2
    suprsnipes, as this more C# targetted this would not be something we can directly support here, but I would suggest checking C#'s String Formatting options available - http://www.dotnetperls.com/string-format
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Hi,

      Thanks for your response, I looked at the link would you care to give us a small example in relation to my first post on the subject?

      Thanks,
      suprsnipes

      Comment


        #4
        Ok,

        I found this link here, this is more specific to my problem if anyone else would like to know.



        suprsnipes

        Comment


          #5
          To clarify would I need to convert a double ToString for this to work?

          Comment


            #6
            Yes, as you would need to use the ToString offered formatting options to realize your alignments.
            BertrandNinjaTrader Customer Service

            Comment


              #7
              I would like some help with the following. I am having some trouble trying to format a string so that it prints to the output screen in the format I am looking for.

              Here is my code;

              Code:
              const string format = "|TG {0,6} |TO {1,4} |PO {2,4}": 
              string result = string.Format(format, net, total, price);  
              Print(result);
              For some reason even when trying the string format above using the File.AppendAllText() method creates the same output as the Print() function, please see sample attached, it shows a) what it looks like and b) what I am trying to achieve.

              The problem seems to occur when one of the objects changes from X number of digits to another number of digits ie. 9 to 10 or 0 to -1 etc.

              Is there anything I could be doing wrong? Is this normal? Should I expect the output I am looking for?

              suprsnipes
              Attached Files
              Last edited by suprsnipes; 03-03-2013, 11:55 PM. Reason: Text file as sample was incorrect updated image of a) and b) instead

              Comment


                #8
                Originally posted by suprsnipes View Post
                I would like some help with the following. I am having some trouble trying to format a string so that it prints to the output screen in the format I am looking for.

                Here is my code;

                Code:
                const string format = "|TG {0,6} |TO {1,4} |PO {2,4}": 
                string result = string.Format(format, net, total, price);  
                Print(result);
                For some reason even when trying the string format above using the File.AppendAllText() method creates the same output as the Print() function, please see sample attached, it shows a) what it looks like and b) what I am trying to achieve.

                The problem seems to occur when one of the objects changes from X number of digits to another number of digits ie. 9 to 10 or 0 to -1 etc.

                Is there anything I could be doing wrong? Is this normal? Should I expect the output I am looking for?

                suprsnipes


                You will need some custom hacking on that. B) can't be expected with regular formatting...

                Comment


                  #9
                  Custom hacking? Do you have any suggestions?

                  Comment


                    #10
                    Originally posted by suprsnipes View Post
                    Custom hacking? Do you have any suggestions?
                    why do you need 2 spaces when >= 100?

                    why isn't 1 acceptable?

                    Comment


                      #11
                      Originally posted by suprsnipes View Post
                      Custom hacking? Do you have any suggestions?
                      Combine the string result into the print statement...

                      but you'll need a condition


                      psuedo code:

                      Code:
                      if >= 100 
                      then
                         your value *100
                      else
                         do nothing
                      end if
                      
                      Print result;

                      Here's a sample of my "FORMATTED" code to line up my output...

                      Print ( " ["+BarsInProgress+"]=" + String.Format( "{0,7}" , String.Format( "{0:####.00}" ,Close[0])) + " ," + String.Format( "{0:MM/dd HH:mm:ss}",Time[0]) +
                      " [ES][0]=" + String.Format( "{0:0.00}" ,Closes[0][0]) + " ," + String.Format( "{0:MM/dd HH:mm:ss}",Times[0][0]) +
                      " [ES1][0]=" + String.Format( "{0:0.00}" ,Closes[ES1][0]) + " ," + String.Format( "{0:MM/dd HH:mm:ss}",Times[ES1][0]) +
                      " Lows[ES1][0]=" + String.Format( "{0:0.00}" ,Lows[ES1][0]) + " Highs[ES1][0]=" + String.Format( "{0:0.00}" ,Highs[ES1][0])+
                      " ZLEMA[ES1][0]=" + String.Format( "{0:0.00}" ,Math.Round (ZLEMA(BarsArray[ES1],3)[0],2) )) ;

                      Comment


                        #12
                        Just in case the number is -100. I don't quite understand why something that seems quite simple is proving to be such a pain in the butt. All I want to do be able to do is read the Output Window print statements as easy as possible and the best way for this to happen is if the 'columns' line up with each other...surely someone has figured out a way to achieve this??

                        Comment


                          #13
                          Originally posted by suprsnipes View Post
                          Just in case the number is -100. I don't quite understand why something that seems quite simple is proving to be such a pain in the butt. All I want to do be able to do is read the Output Window print statements as easy as possible and the best way for this to happen is if the 'columns' line up with each other...surely someone has figured out a way to achieve this??


                          I am looking in your text file in Notepad...

                          the A) part lines up perfect
                          the b) part - Your "what I want" is off centered by *100

                          Code:
                          a) what it looks like
                          |TG     98 |TO   41 |PO   42
                          |TG    101 |TO   42 |PO   42
                          
                          b) what I want it to loook like
                          |TG      98 |TO   41 |PO   42
                          |TG    101 |TO   42 |PO   42
                          Which is strange... I wrapped in "code" tags here, and A) lines up , and B) does not.

                          Good grief...

                          Comment


                            #14
                            Originally posted by suprsnipes View Post
                            Just in case the number is -100. I don't quite understand why something that seems quite simple is proving to be such a pain in the butt. All I want to do be able to do is read the Output Window print statements as easy as possible and the best way for this to happen is if the 'columns' line up with each other...surely someone has figured out a way to achieve this??
                            Here is what I see in TextPad and Notepad...
                            Attached Files

                            Comment


                              #15
                              Originally posted by suprsnipes View Post
                              Just in case the number is -100. I don't quite understand why something that seems quite simple is proving to be such a pain in the butt. All I want to do be able to do is read the Output Window print statements as easy as possible and the best way for this to happen is if the 'columns' line up with each other...surely someone has figured out a way to achieve this??
                              Read up here (or other c# formatting information)

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Jonker, Today, 01:19 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post Jonker
                              by Jonker
                               
                              Started by futtrader, Today, 01:16 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post futtrader  
                              Started by Segwin, 05-07-2018, 02:15 PM
                              14 responses
                              1,790 views
                              0 likes
                              Last Post aligator  
                              Started by Jimmyk, 01-26-2018, 05:19 AM
                              6 responses
                              838 views
                              0 likes
                              Last Post emuns
                              by emuns
                               
                              Started by jxs_xrj, 01-12-2020, 09:49 AM
                              6 responses
                              3,294 views
                              1 like
                              Last Post jgualdronc  
                              Working...
                              X