• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Flatten everything by conditions of from log

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

    Flatten everything by conditions of from log

    Hello,

    I want to ask please if it is possible to flatten everything (or close positions and cancel orders) when there is an error message or conflict message in log. Accurate example is that it is not possible to write and read same time in a file with stream writer/reader. (this is OK and clear, my question is not about this or why or how to avoid, no discussion about streamwriter/reader)

    Because it occurred that NT7-script-strategy did not react anymore suddenly I was trying to find the reason for it and I saw a message in log tab concerning file access.

    Therefore I made a workaround and now simply need to know please how one can close positions and cancel orders or flatten everything when there are issues showing up in log tab.

    Thank you!
    Tony
    Last edited by tonynt; 10-26-2017, 08:12 AM. Reason: translation error, more accurate title

    #2
    Hello tonynt,

    Thank you for your post.

    To be clear you have already got the Log text file being read in the script, correct?

    You would at that point then implement your logic to check the position and orders of the strategy and manage them accordingly. Or are you trying to close positions and manage orders outside of a strategy?

    I look forward to your response.
    Patrick H.NinjaTrader Customer Service

    Comment


      #3
      Hello Patrick,

      thank you for your reply. I do already check positions and prices from other scripts by writing and reading txt files to C:\ or dropbox. All working perfectly.

      But what I´m missing is when there is an error message in the log file to read it in the script.

      From messages in the log file I want to flatten or close positions and cancel orders. How can I read from log file please? Is the structure (Date, Category, Message) seperated by “,”?

      Eg
      Message_log = ???.Parse(parts[3]);
      If(Message_log == „ Error“) then do Flatten everything (or close positions and cancel orders)
      // To check for “Error” (or “Failed”) is enough, no need for more text

      Thank you!
      Tony

      Comment


        #4
        Hello tonynt,

        Thank you for your response.

        You would first go to Documents\NinjaTrader 7\log on your PC and open the Log files in a .txt file viewer such as Windows Notepad. Here you would be able to view the messages and their formats and thus format your code appropriately to the messages.

        The Format is 'Date|#|#|Message'. The two #s do not represent anything significant to the end user.

        Please let me know if you have any questions.
        Patrick H.NinjaTrader Customer Service

        Comment


          #5
          Hello Patrick,

          thank you for your reply. Concerning the reason for messages in the log tab I want to ask please: when using streamwriter in a script and streamreader in another script and both use the same file then there appears sometimes in the log tab "file can ot be accessed because used by another process" (translation by me).

          I have in my streamreader conditions "if(File.Exits(file))". From my understanding from threads in the forum this would avoid an error because when file is used for writing same time then the "if exists" returns false because there is no permission in this moment. Will this prevent the script continuing working correctly maybe? Or is the streamreader giving a next try to read eg with next bar (as I have FirstTickOfBar in my script working with COBCfalse)

          Or is there another way to avoid issues for writing and reading same time.

          Thank you!
          Tony
          Last edited by tonynt; 11-01-2017, 07:01 AM. Reason: translation error

          Comment


            #6
            Hello tonynt,

            Thank you for your response.

            I would recommend the use of the IO functions without StreamWriter or StreamReader which would allow for the File.AppendAllText and File.ReadAllText to occur without needing to release/dispose of the StreamReader or StreamWriter.

            We have an example of this at the following link: https://ninjatrader.com/support/foru...ead.php?t=3477

            Please let me know if you have any questions.
            Patrick H.NinjaTrader Customer Service

            Comment


              #7
              Hello Patrick,

              thank you for your reply. In the meantime I found in a thread about this topic a suggestion to use try/catch. Would this help to avoid this conflict by using try/catch with every stream reader/writer code like
              if(tradeclose==true)
              {try{
              using (StreamWriter outputFile = new StreamWriter(docPathT + @"\" + longAFile_J,false))
              outputFile.WriteLine(string.Format("{0},{1},{2},{3 },{4}",0,999,0,0,0));
              }catch {}
              }

              There was no more discussion about this idea in this thread.

              Thank you!
              Tony

              Comment


                #8
                Hello tonynt,

                Thank you for your response.

                While the Try Catch could prevent the code from stopping due to an error it would not correct the behavior as you intend to.

                Please let me know if you have any further questions.
                Patrick H.NinjaTrader Customer Service

                Comment


                  #9
                  Hello Patrick,

                  this additional question is caused by not understanding correctly when translating your "...it would not correct the behavior as you intend to". Do you mean with this A.) one can not avoid reading and writing same time (which I understand logically of course) or do you mean B.) the script would not try again with next bar to write or read when the bar before the try was not successful (this was my question and I can live with this if it writes or reads then the next bar - if there is not already another action, so it would try overnext bar).

                  Thank you!
                  Tony

                  Comment


                    #10
                    Hello tonynt,

                    Thank you for your response.

                    In large part this is going to depend on your code. The example you provide when running on the close of the bar (with the FirstTickOfBar logic) would mean that if this was running once per bar it would not attempt to run that logic until the next bar.

                    Please let me know if you have any questions.
                    Patrick H.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello Patrick,

                      ok so if reading was not successfully because the file was used for writing same time then it reads the next bar and script is working OK. So one can use different dataseries (eg 233 ticks for writing script and 377 ticks in the reading script) so that reading and writing with close of bars occurs at different time. But if it would occur same time (by any delay, not by accurate ticks) then it reads again the next close of 377 tick bar when using try/catch and the script continues working all the time. And this would not only be a good way for me but also for other users from what I read about problems with writing and reading.

                      Thank you!
                      Tony

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by maggie3, Today, 02:05 AM
                      0 responses
                      3 views
                      0 likes
                      Last Post maggie3
                      by maggie3
                       
                      Started by qewcool, Today, 01:14 AM
                      0 responses
                      3 views
                      0 likes
                      Last Post qewcool
                      by qewcool
                       
                      Started by fredrikn, 05-08-2019, 04:01 AM
                      17 responses
                      669 views
                      1 like
                      Last Post emtrader  
                      Started by zacharydw00, Yesterday, 10:19 PM
                      0 responses
                      3 views
                      0 likes
                      Last Post zacharydw00  
                      Started by chafan11, Yesterday, 09:28 PM
                      0 responses
                      7 views
                      0 likes
                      Last Post chafan11  
                      Working...
                      X