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

Reading from a text file

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

    Reading from a text file

    Hi All,

    I'm having a bit of a problem reading from a text file and converting to a double. Here is my partial script:

    #region Variables
    private double textDouble = 0;
    private string path = @"C:\NTFiles\textfile.txt";
    private System.IO.StreamWriter sw;
    #endregion

    protected override void OnBarUpdate( )
    {
    if (Bars.FirstBarOfSession)
    {
    string[ ] tempText = File.ReadAllText (path);
    foreach (string value in tempText)
    {
    textDouble = Convert.ToDouble(value);
    }
    File.WriteAllText (path, String.Empty);
    }
    }

    protected override void OnTermination ( )
    {
    sw = File.AppendText (path);
    sw.WriteLine (textDouble);
    if (sw != null)
    {
    sw.Dispose ( );
    sw = null;
    }
    }

    Additional Info:
    1. The text file will always have only one line
    2. The script reads that one line, and deletes the text file contents
    3. On termination, the script writes one value to the text file leaving the text file with just one line for the next time it runs.
    4. As is, the content clearing and writing part work as they should. The problem is reading the text file and converting it to a double. The text file will have 1453.45 and the script will make the value of textDouble = 0.00.

    All help is greatly appreciated.
    Last edited by GeoOrtiz; 03-09-2015, 08:31 PM.

    #2
    This code has a significant error and should not even compile.

    In your post, the definition of tempText looks mangled,

    if (Bars.FirstBarOfSession)
    string{ ] tempText = File.ReadAllText (path);

    saying "string{ ]" is incorrect syntax, to my mind.

    Can you attach your script?

    Comment


      #3
      Sorry! That was a typo. The script is ok and it compiles.

      I edited my original post to fix that problem.
      Last edited by GeoOrtiz; 03-09-2015, 08:29 PM.

      Comment


        #4
        Hi All,

        My apologies for posting this prematurely. There is actually nothing wrong with the partial script I posted. It is working as it should. The issue was that somewhere else in my script, that variable is reset to 0.00 and I didn't realize it until now.

        Thank you!

        Comment


          #5
          Originally posted by GeoOrtiz View Post
          Hi All,

          My apologies for posting this prematurely. There is actually nothing wrong with the partial script I posted. It is working as it should. The issue was that somewhere else in my script, that variable is reset to 0.00 and I didn't realize it until now.

          Thank you!
          looking at your code its not robust..
          you should use this
          Converts the string representation of a number to its double-precision floating-point number equivalent. A return value indicates whether the conversion succeeded or failed.
          MicroTrends
          NinjaTrader Ecosystem Vendor - micro-trends.co.uk

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by RubenCazorla, 08-30-2022, 06:36 AM
          3 responses
          77 views
          0 likes
          Last Post PaulMohn  
          Started by f.saeidi, Yesterday, 12:14 PM
          9 responses
          23 views
          0 likes
          Last Post f.saeidi  
          Started by Tim-c, Today, 03:54 AM
          0 responses
          3 views
          0 likes
          Last Post Tim-c
          by Tim-c
           
          Started by FrancisMorro, Today, 03:24 AM
          0 responses
          4 views
          0 likes
          Last Post FrancisMorro  
          Started by Segwin, 05-07-2018, 02:15 PM
          10 responses
          1,772 views
          0 likes
          Last Post Leafcutter  
          Working...
          X