Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

StreamWriter with Live Strategy

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

    #16
    Thanks!
    It works for me as well

    Btw, can we force the strategy to save the file at a specific time instead of waiting for us to manually remove the strategy to save it?

    Thanks!
    Jim

    Comment


      #17
      Hi Mrlogik, Ninjaq, cunparis, tony900hwk,

      Would anyone of you mind to post a file of the code for use?

      I tried to copy and paste but it doesn't compile correctly and I cannot understand where the issue is (apparently it doesn't accept the overload sw = new StreamWriter(file)).

      Thank you very much

      Comment


        #18
        Stefy,

        I have created a class to handle file writing from a strategy. When I get in later this evening I will post it for you.
        Last edited by mrlogik; 08-27-2009, 11:14 AM.
        mrlogik
        NinjaTrader Ecosystem Vendor - Purelogik Trading

        Comment


          #19
          Attached is a file I wrote called GlobalStrategy.cs

          In it, I define a calss called DataLogType(). This class handles everything (at least for my use) you need for writing to a file.

          You first have to download the GlobalStrategy file, open any strategy, and compile (so the GlobalStrategy class is compiled as well). You then have to make a folder on the C:\ called NTLOGS ; this is where the files will be written to. This can be changed in code, but for this example I will use the method I do.

          Now, to use this in the strategy, you have to declare, initialize, and Dispose the DataLogType properly. Here's how.

          In the variable section of the strategy
          Code:
          private DataLogType LogFile;
          In the Initialize(), we want to instantiate our variable, with the following declaration / inputs:

          DataLogType(string iExtension, string iFolder, string iFileName)

          iExtension = the file extension
          iFolder = the folder to be written to
          iFileName = the file name (the date will be appended later)

          Code:
          LogFile = new DataLogType(".csv", "NTLOGS\\", "aGridEntryV1_LOG");
          Now, in the OnBarUpdate() region, we have to initialize our LogFile variable as such:

          InitLogFile(string iDate, string iSymbol)
          string iDate = The date in any format you wish
          string iSymbol = The symbol name string

          I also setup the csv header if this is the extension I am using.
          LogToFile(bool iHistorical, string iMsg, bool iNewLine)
          iHistorical = Historical flag -- keeps from writing historical data
          iMsg = the string to write to the file
          iNewLine = boolean indicating a new line or append to line.

          Code:
          if(!Historical)
          {
          int vDate = (Time[0].Year * 10000) + (Time[0].Month * 100) + (Time[0].Day);
          if(!LogFile.vInitialized)
               LogFile.InitLogFile(vDate.ToString(),Instrument.MasterInstrument.Name);
          
          //Setup .CSV header
          LogFile.LogToFile(Historical, "var1, var2, var3, var4,", true);
          }
          NOTE :: if you do not do a .csv extension in the Initialize() function, you do NOT have to setup the .CSV header.

          The file name should then be in the format
          yyyymmdd_strategyNameIndicatedInInitialize_symboln ame

          Whenever in the strategy I want to append some data to the file, I do this:


          CSV Extension
          Code:
          LogFile.LogToFile(Historical, variable1.ToString() + "," + variable2.ToString() + "," + variable3.ToString() + "," + variable4.ToString() + ",", true);
          You also have to add code to clean up the file DataLogType :
          [code]

          NON CSV Extension
          Code:
          LogFile.LogToFile(Historical, "SwingLow = " + sLow.ToString(), true);
          You also have to add code to clean up the file DataLogType :
          Code:
          public override void Dispose()
          {
              LogFile.CleanUp();
          
              //call base
              base.Dispose();
          }
          That's all. The file will not be readable until the strategy is removed from the chart, which closes the file handler.

          You may want to read through the code attached a little bit to get a better idea of how these functions work.

          I am writing this post without NT; I think everything here should compile fine. In the event it doesn't please let me know.

          goodLuck
          Attached Files
          mrlogik
          NinjaTrader Ecosystem Vendor - Purelogik Trading

          Comment


            #20
            mrlogik,

            Thank you very much for your help! I really appreciate it.

            I really don't understand why, but the code doesn't work for me: I tried to copy and paste it to a strategy but I was getting error messages, so I decided to clean up the strategy completely and just copy and paste and run the code, but yet it doesn't work: I get "No method for StreamWriter takes 1 argument" and "No definition for Write" and "Base.Close is a property but used as a method". Exactly the same errors I use in the previous version of your code.
            I'm clearly doing something wrong... but what?

            Thank you very much once again!

            Comment


              #21
              Check to make sure you have this line in the "Using declaration" region at the very top of the file.

              // Add this to your declarations to use StreamWriter
              using System.IO;

              It is often overlooked.

              Comment


                #22
                hi stefy,

                Did TAJAtrades's suggestion help?
                mrlogik
                NinjaTrader Ecosystem Vendor - Purelogik Trading

                Comment


                  #23
                  Hi mrlogik,

                  Many many thanks for your precious help and your concern!

                  It was completely my fault if your code was not compiling!
                  I made my own version of the SampleStreamWriter and, very stupidly, I called it StreamWriter (which obviously didn't have the overload need to run your code!).
                  I did that some time ago, so I completely forgot about it...
                  Well, this issue is solved... hopefully it brings me a step forward the initial issue I had: I needed to pull the PnL of one strategy into another strategy as a DataSeries (point to point, not just the final value), and being unsuccessful at creating a public DataSeries Class to contain it, I decided to resort to write the PnL into a file, import it as a security, and then use it for my purposes into the strategy.
                  Finally, I'm getting a step closer to the original plan... really many thanks for your code, it is useful and educational at the same time!

                  Till the next one!

                  Stefano

                  Comment


                    #24
                    Stefano,

                    I wouldn't recommend using file I/O to share data between strategies. You can easily add code to my GlobalStrategy.cs file to make a variable global between strategies. You simply need to add a partial class "Strategy" to the end of the file, and create in here any global variables you wish.

                    I have updated the GlobalStrategy.cs file and attached it. The update includes the following at the end of the file.

                    Code:
                        partial class Strategy
                        {
                            // use public static type
                            public static double GLO_PnN;
                        }
                    This variable can be accessed in any strategy you are using in NT. This is a very archaic method of sharing data, without get / set methods. Try it out and please let the community know of your experiences.

                    Thanks
                    Attached Files
                    mrlogik
                    NinjaTrader Ecosystem Vendor - Purelogik Trading

                    Comment


                      #25
                      Anthony,

                      First of all many thanks for your advice - I thought using I/O was not the best way to share data between strategies but it was everything I could figure out after many tries with public static dataseries that were always empty when I was calling them in the second strategy.

                      I tried your code, i.e. I added
                      Code:
                      partial class Strategy
                          {
                              // use public static type
                              public static double GLO_PnL;
                          }
                      in Strategy1, I set in OnBarUpdate()
                      double GLO_PnL = Performance.AllTrades.TradesPerformance.Currency.C umProfit;
                      and then I printed it out successfully
                      Print(ToDay(Time[0]) + " " + Math.Round(GLO_PnL, 2));

                      Then I opened Strategy2, and I just tried to print my global values, I tried both
                      Print(ToDay(Time[0]) + " " + Math.Round(GLO_PnL, 2));
                      and
                      Print(ToDay(Time[0]) + " " + Strategy1.GLO_PnL);
                      In both cases, the result is a long list of zeroes.

                      Many thanks in advance for any advice you wish to share!

                      Comment


                        #26
                        Stefy,

                        You don't have to initialize or declare the variable in strategy 1. It is already defined in the GlobalStrategy.cs file. You simply have to use it.

                        Try the simple case with number first (without using any Performance variables).

                        Strategy 1
                        Code:
                        OnBarUpdate()
                        {
                        GLO_PnL = 5;
                        }
                        ;

                        Strategy 2
                        Code:
                        OnBarUpdate()
                        {
                        Print(ToDay(Time[0]) + "  " + GLO_PnL.ToString());
                        }
                        Let me know.
                        mrlogik
                        NinjaTrader Ecosystem Vendor - Purelogik Trading

                        Comment


                          #27
                          Thank you once again, now it works, but it has a problem: when I take the PnL of the strategy, it just stores the last value of the PnL of Strategy1 and that's the only value available in second strategy, but I need the PnL day by day (I'm working on daily data), not just the last value.
                          I must be able to compare the PnL of Strategy1 on 01/02/09 with the PnL of Strategy2 on 01/02/09, and so on, not just the last value of PnL of Strategy1 (the value stored in the Global variable) with the day by day PnL of Strategy2.
                          That's why I thought I needed a global dataseries and not only a global variable...

                          Comment


                            #28
                            I gave that example for its simplicity.

                            You can use a global dataseries or array, or any type you wish.

                            I'm not 100% (no NT accessibility right now) if this will work, but try;

                            Code:
                            public static DataSeries gData = new DataSeries(this);
                            Thanks.
                            mrlogik
                            NinjaTrader Ecosystem Vendor - Purelogik Trading

                            Comment


                              #29
                              Thank you Anthony, but that's the issue I couldn't overcome tough.

                              I can only declare
                              Code:
                              public static DataSeries gData;
                              otherwise it won't compile.

                              Then in Strategy1, in Initialize() I write
                              gData = new DataSeries(this);
                              to synchronize it,
                              and I set it
                              gData.Set(Performance.AllTrades.TradesPerformance. Currency.CumProfit);

                              In Strategy2, I have to synchronize it again
                              gData = new DataSeries(this);
                              and then I try to print it out, but it's just zeroes.

                              I tried some variations, but it won't compile otherwise, and if it compiles, it gives zero values!

                              Comment


                                #30
                                Stefy,

                                You definitely cant' instantiate the gData variable in the strategies...

                                I would then use an array or something and see if that works for you.

                                If you can't get an array to work, let me know and I'll take a look when I get home.
                                mrlogik
                                NinjaTrader Ecosystem Vendor - Purelogik Trading

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                                0 responses
                                648 views
                                0 likes
                                Last Post Geovanny Suaza  
                                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                                0 responses
                                369 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by Mindset, 02-09-2026, 11:44 AM
                                0 responses
                                108 views
                                0 likes
                                Last Post Mindset
                                by Mindset
                                 
                                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                                0 responses
                                572 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by RFrosty, 01-28-2026, 06:49 PM
                                0 responses
                                573 views
                                1 like
                                Last Post RFrosty
                                by RFrosty
                                 
                                Working...
                                X