Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Check file content before proceding with indicator calculation

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

    Check file content before proceding with indicator calculation

    Hi,
    Please can you help me providing a sample of code to do that:

    I want to check if into my txt there is "OK" before calculate the indicator on the chart. If != OK ninjatrader indicator has to read again the file until found "OK". When OK, the indicator has to start your process.

    I tried with the code below but the indicator doesn't exit the loop and all crashes (i've put the code into state.dataloaded):

    Code:
    while(Testo5 != "OK"){
    
    //                if (!File.Exists(path5))
    //                {
    //                    // If file does not exist, let the user know
    //                    Print("File does not exist.");
    //                    return;
    //                }
    
    //                /* The try-catch block is used for error handling.
    //                In this case it is used to ensure you only have one stream object operating on the same file at any given moment. */
    //                try
    //                {
    //                    // Sets the file the StreamReader will read from
    //                    sr5 = new System.IO.StreamReader(path5);
    
    //                    string line5;
    
    //                    // Read lines and calculate the current day's OHLC from the file. While loop will go through the whole file.
    //                    while ((line5 = sr5.ReadLine()) != null)
    //                    {
    //                        Testo5 = File.ReadAllText(path5);
    
    //                        }
    //                    }
    
    //                    catch (Exception e)
    //                    {
    //                        // Outputs the error to the log
    //                        Log("You cannot write and read from the same file at the same time. Please remove SampleStreamWriter.", NinjaTrader.Cbi.LogLevel.Error);
    //                        Print(e.ToString());
    //                    }
    
    //                    if (sr5 != null)
    //                    {
    //                        sr5.Dispose();
    //                        sr5 = null;
    //                    }​
    Thank you

    #2
    Hello Davide999,

    Thanks for your notes.

    We have received your question and are investigating this matter so we may provide you with the best information possible.

    Once we are finished investigating this inquiry, we will update this forum thread with our findings.

    Thanks for your patience.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Hello Davide999,

      Thanks for your patience.

      You may try implementing this logic at the top of the OnBarUpdate() section of the script instead of State.DataLoaded so that the indicator calculates values when the condition checking if in your text there is "OK" before calculating logic.

      See the reference sample linked below demonstrating the use of StreamReader.

      SampleStreamReader: https://ninjatrader.com/support/help...b=streamreader
      <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

      Comment


        #4
        Hi,
        Thanks for your response. I've already done this thing (put the code in on bar update, at the start of the function); this not works. The code read OK from the file (i've checked with a print) but do not exit from the while loop. It is an infinite loop...it is strange in my opinion.

        Let me know.

        Comment


          #5
          Hello Davide999,

          Thanks for your notes.

          Further debugging would need to be done by reducing code and adding pritns to understand exactly how your logic is evaluating and behaving when running the script.

          It is likely the loops in your script causing this behavior.

          Below is a link to a forum post that demonstrates how to use prints to understand behavior.
          https://ninjatrader.com/support/foru...121#post791121

          Using C# loops in a script is considered C# education and would go beyond the support we would be able to provide you with in the Support Department at NinjaTrader. We do not provide C# education in our support.

          You could research using C# loops further to understand how to work with them in your script. For example, a quick Google search for something like 'using C# loops' could be done to research this topic further.

          This forum thread will also be open for other community members to share their insights on the matter.
          <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          597 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          343 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          103 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          556 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          555 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X