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

Unwanted mutual class access with several instances in parallel

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

    Unwanted mutual class access with several instances in parallel

    Hello everyone,


    as I switch from MetaTrader to NinjaTrader, I'm new to Object Oriented Programming. I have an issue which puzzles me currently and I run out of ideas to solve it by myself:


    In my strategy there's a class which is intended to export trading data (e.g. entry and exit price and date etc.) in a CSV file:


    Code:
    internal class JournalData
            {
                private static string
                    strSymbol,
                    strPositionSize,
                    strOrderOpen,
                    strDateOpen,
                    strDirection,
                    strPriceOpen,
                    strPriceStopLossInitial,
                    strDateBreakeven,
                    strDateClose,
                    strPriceClose;
    
                public void WriteResults (string FileName)
                {
                    FileInfo f = new FileInfo (NinjaTrader.Core.Globals.UserDataDir + FileName);
    
                    StreamWriter w = f.AppendText();
                    w.WriteLine (strSymbol+";"+strPositionSize+";"+strOrderOpen+";"+strDateOpen+";"+strDirection+";"+strPriceOpen+";"+strPriceStopLossInitial+";"+strDateBreakeven+";"+strDateClose+";"+strPriceClose);
                    w.Close();
                    
                    // Reset variables for possible subsequent opening in opposite direction
                    strSymbol = null;
                    strPositionSize = null;
                    strOrderOpen = null;
                    strDateOpen = null;
                    strDirection = null;
                    strPriceOpen = null;
                    strPriceStopLossInitial = null;
                    strDateBreakeven = null;
                    strDateClose = null;
                    strPriceClose = null;        
                }
                
                public void SetOrderOpen (Order OrderOpen)
                {
                    strSymbol = OrderOpen.Instrument.FullName;
                    strPositionSize = OrderOpen.Quantity.ToString().Replace (".", ",");
                    strOrderOpen = OrderOpen.OrderId;
                    strDateOpen = OrderOpen.Time.ToString ("G", CultureInfo.CreateSpecificCulture ("de-DE"));
                    strDirection = OrderOpen.OrderAction.ToString();
                    strPriceOpen = OrderOpen.AverageFillPrice.ToString().Replace (".", ",");
                }
                
                public void SetPriceStopLossInitial (double Price)
                {
                    if (String.IsNullOrEmpty (strPriceStopLossInitial))
                    {
                         strPriceStopLossInitial = Price.ToString().Replace (".", ",");
                    }
                }
                
                public void SetDateBreakeven()
                {
                     strDateBreakeven = DateTime.Now.ToString ("G", CultureInfo.CreateSpecificCulture ("de-DE"));
                }            
                
                public void SetOrderClose (Order OrderClose)
                {
                    strDateClose = OrderClose.Time.ToString ("G", CultureInfo.CreateSpecificCulture ("de-DE"));
                    strPriceClose = OrderClose.AverageFillPrice.ToString().Replace (".", ",");
                }
            }
            
            JournalData JournalDataTmp = new JournalData();
    In NT8 there are several (e.g. 3...4) instances of this strategy trading different underlyings. As long as there's one position opend at a time the class works as expected. When several positions are opened in parallel there seems to be an interaction between these instances leading to values from underlying B appearing in data set of underlying A while most of underlying B's values are reset:


    YM 09-18;1;448856229;06.07.2018 17:19:05;Buy;24451;2744,5;06.07.2018 17:35:00;06.07.2018 17:57:21;24452
    ;;;;;;;;06.07.2018 20:37:29;2761
    I already defined the class as internal which seems to have no effect. How can I prevent this mutual access? Can anyone help me?


    Thanks in advance,
    Frederik

    #2
    Hello Frederik,

    Thank you for your note.

    Are all these strategies writing to the same file?

    I look forward to your reply.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Hello Alan,


      yes, these strategies write to the same file.


      Sincerely,
      Frederik

      Comment


        #4
        Hello LeeDeForest,

        If you write to separate files are you seeing the same issue?

        I look forward to your reply.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          Hi Alan,
          thanks for your reply. I will figure that out and give feedback as soon as possible (this could take several days).



          Frederik

          Comment


            #6
            Hi Alan,
            I figured out that with using individual files the behaviour remains the same. In order to clarify this issue, I'd like to go into details. The following entries are written per line (i.e. per position):


            strSymbol
            strPositionSize
            strOrderOpen
            strDateOpen
            strDirection
            strPriceOpen
            strPriceStopLossInitial
            strDateBreakeven
            strDateClose
            strPriceClose


            I hope the variable names explain their usage sufficiently.


            Let's assume, position A is opened first then position B. When position A is closed while B remains open the values are written into the file via the method WriteResults(). Obviously the variables set by the method SetOrderOpen() contain values of position B (because this method is called by position B after the call by position A). After writing into the file all variables are reset. When position B is closed eventually, all variables are reset (i.e. null) despite those set via the method SetOrderClose() called before the call of WriteResults() by position B.


            In my opinion it looks pretty much like both instances of the Strategy share the same variable / memory space. As I mentioned I am new to object oriented programming. Has this issue something to do with it?


            Thanks in advance,
            Frederik

            Comment


              #7
              Hello LeeDeForest

              In my opinion it looks pretty much like both instances of the Strategy share the same variable/memory space
              You are correct about your statement in contrast to your sample specifically. You have used the modifier static on your variables.

              Code:
              private static string
              This is a C# concept which we don't document in NinjaScript, but essentially you are creating one instance of that variable and each of the instances of that class would see it. Removing static will make the variables apply to the class instance level or be separate. You can search online for C# static for further information, there is a decent example in the following stackoverflow page: https://stackoverflow.com/a/10796792

              Changing the variable to:

              Code:
              private string
              would make the variable apply to each strategy individually and could then have separate values for each script.

              If you see static used in C# examples online, please be hesitant to use that modifier in NinjaScript. Often static is used when using a console application in an example but generally is not used in NinjaScript unless you have a specific purpose to do so.



              I look forward to being of further assistance.
              JesseNinjaTrader Customer Service

              Comment


                #8
                Hello Jesse,


                thanks for your reply. I will test that change next week and report the results.


                Regards,
                Frederik

                Comment


                  #9
                  Jesse, you're the man.
                  It works how desired even with three instances in parallel ;o)


                  Great job, thanks a lot!


                  Regards,

                  Frederik

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Jon17, Today, 04:33 PM
                  0 responses
                  1 view
                  0 likes
                  Last Post Jon17
                  by Jon17
                   
                  Started by Javierw.ok, Today, 04:12 PM
                  0 responses
                  4 views
                  0 likes
                  Last Post Javierw.ok  
                  Started by timmbbo, Today, 08:59 AM
                  2 responses
                  10 views
                  0 likes
                  Last Post bltdavid  
                  Started by alifarahani, Today, 09:40 AM
                  6 responses
                  41 views
                  0 likes
                  Last Post alifarahani  
                  Started by Waxavi, Today, 02:10 AM
                  1 response
                  20 views
                  0 likes
                  Last Post NinjaTrader_LuisH  
                  Working...
                  X