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

NT7 write to file

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

    NT7 write to file

    Hello,

    I want to write to a txt file with close of bar but I can´t get the solution (even when knowing the sample for stream writer) What am I doing wrong please?

    if(BarsInProgress==3 && FirstTickOfBar)
    {private double LongALevel;
    {
    set
    {
    using (StreamWriter outputFile = new StreamWriter(docPath + @"\" + longALevelFileName,false))
    outputFile.WriteLine(value.ToString());
    }}}

    What do I have to change or add please?

    Thank you!
    Tony

    #2
    Hello tonynt,

    Without the full code I cannot confirm if you will be running into a file access error with the path supplied, but I do see some syntax/compiler errors.

    You are trying to declare a private class member variable from within an if statement within OnBarUpdate(). This is not valid. Additionally, this class member variable is terminated with a semicolon before a setter is assigned to it.

    You will need to declare your private double directly within the class. This will be at the same level of depth in the class as other private/public variables that we create. You will also need to remove the semicolon after declaring this variable so you can attach your own getter/setter.

    For example:

    Code:
    protected override void OnBarUpdate()
    {
    
    }
    		
    private double MyDouble
    {
    	set{}
    }
    Please let me know if you have any questions.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hello Jim,

      thank you for your reply. Yes this I have for the moment and its working (as you replied above).

      But it seems - from my understanding - that it is writing or reading all the time/with every tick when the script runs COBCfalse.

      Therefore, I need to bring it into onbarupdate so that I can code when it is writing (or reading) so that I have one script eg with 5 RangeBar writing and other script with 3 RangeBar reading so that it is
      a.) not reading and writing with every tick
      b.) to avoid read and write the same time to the file

      Thank you for your hint what to add so that its working in onbarupdate as well.

      Thank you!
      Tony

      Comment


        #4
        Hello tonynt,

        You have the file writing within the setter of that variable. Whenever you set a value to this variable as you normally would, the setter will write to your file. If you are setting the value of this variable every tick, it will happen every tick.

        If you assign this variable a value on the first tick of a bar within a BarsInProgress check, the file will be written to on the first tick of the bar of that data series.

        OnBarUpdate() is called sequentially for each iterating data series and does not iterate at the same time. You may encounter file access issues if you are writing a large enough chunk of data to file while the other data series iterates and attempts to write to it. In such a case, you could add your own custom programming to buffer your file writes in case the file access is denied.

        Please let me know if I may be of further assistance.
        JimNinjaTrader Customer Service

        Comment


          #5
          Hello Jim,

          for writing this seems logical but how for reading? I have thought about another aproach and its working fine in onstartup but NT7 is freezing when its in onbarupdate. Is this correct what I have done for reading from web when I need it with close of bar[dataseries2]?

          if (!Historical && BarsInProgress==2 && FirstTickOfBar)
          {
          try{
          string uri = @"https://www.dropbox.com/s/" + filePath;
          HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
          HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
          using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
          {
          string line;
          while ((line=sr.ReadLine())!=null)
          {
          string[] parts = line.Split(',');
          if (parts[1]==Instrument.MasterInstrument.Name)
          {
          string symbol = parts[0];
          string name = parts[1];
          double stopprice = 0;
          if (double.TryParse(parts[2].Trim(),out optstopprice))
          {
          tkData d = new tkData(symbol,name,stopprice);
          lines.Add(d);
          stopset=true;
          }}}}
          }catch{}
          }

          Because even when I set dataseries2 eg to 5 min in Initialize then its reading immediately with enabling the script and my line which I use for debugging shows immediattely value stopprice, but it should do so only after close of 5-min-bar.

          Thank you!
          Tony
          Last edited by tonynt; 10-20-2017, 06:55 AM. Reason: tarnslation error, clearifying

          Comment


            #6
            Hello tonynt,

            Thanks for your additional question.

            This inquiry is beginning to escape the scope of services that we may provide. We support our product and are willing to provide direction for how NinjaScript can be used to accomplish something, but we are not C# educators, and we do not offer services to review implementations or educate others on C# concepts that are outside of the NinjaScript that is built on top of C#.

            For using StreamReader and StreamWriter, I recommend to review the samples we have on the forums, as well as any other publicly available resources online for further examples and understanding.

            We do not have any available samples that incorporate an HttpWebRequest in NinjaScript, and you will be on your own for creating an implementation that works for you. Publicly available resources may provide further direction for writing a proper implementation.

            NinjaScript Reference samples can be found here: https://ninjatrader.com/support/foru...splay.php?f=30

            If you have any questions on sample code that we have, please don't hesitate to ask.
            JimNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Christopher_R, Today, 12:29 AM
            0 responses
            8 views
            0 likes
            Last Post Christopher_R  
            Started by sidlercom80, 10-28-2023, 08:49 AM
            166 responses
            2,235 views
            0 likes
            Last Post sidlercom80  
            Started by thread, Yesterday, 11:58 PM
            0 responses
            3 views
            0 likes
            Last Post thread
            by thread
             
            Started by jclose, Yesterday, 09:37 PM
            0 responses
            7 views
            0 likes
            Last Post jclose
            by jclose
             
            Started by WeyldFalcon, 08-07-2020, 06:13 AM
            10 responses
            1,415 views
            0 likes
            Last Post Traderontheroad  
            Working...
            X