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

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.
    Brandon H.NinjaTrader Customer Service

    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
      Brandon H.NinjaTrader Customer Service

      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.
          Brandon H.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by lightsun47, Today, 03:51 PM
          0 responses
          2 views
          0 likes
          Last Post lightsun47  
          Started by 00nevest, Today, 02:27 PM
          1 response
          8 views
          0 likes
          Last Post 00nevest  
          Started by futtrader, 04-21-2024, 01:50 AM
          4 responses
          41 views
          0 likes
          Last Post futtrader  
          Started by Option Whisperer, Today, 09:55 AM
          1 response
          13 views
          0 likes
          Last Post bltdavid  
          Started by port119, Today, 02:43 PM
          0 responses
          8 views
          0 likes
          Last Post port119
          by port119
           
          Working...
          X