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

Public DataSeries or else - how to read the PNL of a strategy from another one

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

    Public DataSeries or else - how to read the PNL of a strategy from another one

    Hi everyone

    I have a problem: I need to use the PNL of a strategy into another strategy.
    I don't need simply the last value (I managed to access that through a global variable), but I need the whole dataseries (i.e., the PNL of Strategy 1 day by day).

    I read a lot the forum but couldn't find what I need, so I decided to take a stab at it and use a public DataSeries method.
    It seemed fine, because I could access the DataSeries of Strategy 1 (Strategy1_PNL) into Strategy 2, but when I tried to run Strategy 2 I get no trades and the following error in the log: "Error on calling OnBarUpdate method: Strategy1: ADX[BarsAgo]; barsAgo out of valid range 0 through -2, was 0".

    I don't really understand... I guess the DataSeries size doesn't match... so, the DataSeries are not synchronized? I don't understand this ADX[BarsAgo] message, since I'm not even using the ADX indicator in the strategy!

    I know this is out of the scope of NT, I would just really appreciate if someone more knowledgeable could point me to the right direction.

    Is there a way to fix this problem? Or should I try to go another way around?
    Any help is greatly appreciated!

    Thank you everyone!

    Stefy

    #2
    Hi stefy,

    I would place 3 print statements into the OnBarUpdate() of every involved strategy:

    The first right at the top of OnBarUpdate() to inidcate on wich bar we are just elaborating: Print("Startegy #1,#2,... CurrentBar: " + CurrentBar);

    The second one just before accessing the data series of another strategy: Print("Startegy #1,#2,... Before Access");

    The third one just after accessing the data series of another strategy: Print("Startegy #1,#2,... After Access");

    This way, you always know if your code is executing on the correct bar as expected and you can see which access causes the concerning exception (because you would see the "Before"-print-statement but not the "After"-print-statement.

    The print statements are printed to the output window if you open it before execution.

    Regards
    Ralph

    Comment


      #3
      Hi Ralph,

      Thank you for your advice.

      I tried to follow your advice, but I don't get any print out since the first Pint statement, and I get the same error message as below (Error on calling OnBarUpdate method: Strategy1: ADX[BarsAgo]; barsAgo out of valid range 0 through -2, was 0").

      I think, what I need to know, is: when I want to make a DataSeries public, so that it can be accessed by another strategy, what kind of syntax do I need to use?
      public DataSeries

      or

      public static DataSeries

      or

      something else?

      Many thanks everyone!

      Comment


        #4
        stefy, public dataseries should do it - you would first need to debug the error you got in the OnBarUpdate().
        BertrandNinjaTrader Customer Service

        Comment


          #5
          I would recommend to look at this example implementation provided by NT (Indicator: Using a DataSeries object to store calculations ):



          You don't declare anything as static as long as it is belonging to the indicator instance exclusively. How to setup the data series to be visible for the outside world is described in the example above.

          Regarding the exception error message: I think there is an additional issue in your code you are not aware of, because pointing to ADX[BarsAgo] has to have a reason.

          Regards
          Ralph

          Comment


            #6
            Hi Bertrand,

            I debugged the code, so I don't get error messages in the log anymore, but now it complies fine but if I print out the public DataSeries from second strategy, I get just zeros! If instead I print it out from the first strategy, it prints out the correct values.

            I know it's outside the scope of support, but I have been looking on Google for hours and didn't find an example, and I think to have a public DataSeries between strategies can be useful for most of the people in this forum.

            In Strategy_1
            in Variables:
            public static DataSeries xyz;

            in Initialize():
            xyz = new DataSeries(this);

            in OnBarUpdate():
            xyz.Set(any dataseries);

            Then, in Strategy_2:
            in Variables:
            public static DataSeries xyz;

            in Initialize():
            Strategy_1.xyz = new DataSeries(this);

            in OnBarUpdate():
            Print(Strategy_1.xyz[0]);

            Any idea why it doesn't work?

            I would really appreciate some help here since I couldn't find anything similar on the internet.

            Thank you




            Originally posted by NinjaTrader_Bertrand View Post
            stefy, public dataseries should do it - you would first need to debug the error you got in the OnBarUpdate().

            Comment


              #7
              Hi Ralph,

              Thank you for your response.

              I looked at the example in the link, but that is for a simple DataSeries inside the same strategy (indeed, it's a private DataSeries and not a public one).

              I guess my problem may be in the use of public static DataSeries? I'm taking the PNL of Strategy_1 and want to use it into Strategy_2, so does it make sense to use a static class?

              Many thanks for your help!

              Originally posted by Ralph View Post
              I would recommend to look at this example implementation provided by NT (Indicator: Using a DataSeries object to store calculations ):



              You don't declare anything as static as long as it is belonging to the indicator instance exclusively. How to setup the data series to be visible for the outside world is described in the example above.

              Regarding the exception error message: I think there is an additional issue in your code you are not aware of, because pointing to ADX[BarsAgo] has to have a reason.

              Regards
              Ralph

              Comment


                #8
                stefy, the issue here is there's no real method available to call a strategy as it is in an indicator for example...for the PnL the best might be to read / write this to a txt file for accessing it in a different strategy - http://www.ninjatrader-support2.com/...ead.php?t=3477
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by stefy View Post
                  I guess my problem may be in the use of public static DataSeries? I'm taking the PNL of Strategy_1 and want to use it into Strategy_2, so does it make sense to use a static class?
                  You can exchange data with a static class method generally. A static class is alive as long as NT is. This is to be considered in case you exchange a big amount of data like data series. Your strategy evaluation may be over, but the static data is still there. If you just exchange some PnL numbers, this shouldn't be an issue.

                  Regards
                  Ralph

                  Comment


                    #10
                    Originally posted by NinjaTrader_Bertrand View Post
                    stefy, the issue here is there's no real method available to call a strategy as it is in an indicator for example...for the PnL the best might be to read / write this to a txt file for accessing it in a different strategy - http://www.ninjatrader-support2.com/...ead.php?t=3477
                    Hi Bertrand,

                    Let's say I use the StreamWriter method to print out a file with the PnL of the first strategy (let's say I'm using SPY and I print out SPY_PnL_Strategy1).
                    If I use StreamWriter again in Strategy2, I can read the file SPY_PnL_Strategy1, but how can I access this DataSeries for my calculations? I mean, if I want to access DataSeries SPY_PnL_Strategy1, which syntax I need to use?

                    Comment


                      #11
                      stefy, not sure why you want to do this, since the previous values are already present the in the file you wrote this to, so you can just read in the double values you need in the 'calling' strategy.
                      BertrandNinjaTrader Customer Service

                      Comment


                        #12
                        Ralph,

                        I'm a C# noob, but from what I read and are learning, it seems perfectly reasonable to pass a public value from one strategy to the next without writing to a file. Granted, NT has it's own rules, but I would like to "understand" why this is not possible.

                        Reading and writing to a file seems more like a kludge.

                        I'm interested to learn more about how you would go about this as it is critical for intra-strategy development, particularly with spread trading or hedging.

                        I hope you haven't abandonded this thread yet ;-)


                        Thanks,



                        Originally posted by Ralph View Post
                        You can exchange data with a static class method generally. A static class is alive as long as NT is. This is to be considered in case you exchange a big amount of data like data series. Your strategy evaluation may be over, but the static data is still there. If you just exchange some PnL numbers, this shouldn't be an issue.

                        Regards
                        Ralph

                        Comment


                          #13
                          Hi r2kTrader,

                          no it is not abandoned.

                          Data transfer with a file would be an option if you would like to collect date over a longer time or save date in case of a crash. If that is not important then the fact that file access is slow may be an issue. But even coding the file access is complex because you need to parse text, handle access errors and syncronise the access.

                          A simple approach to transfer data without files would be with the file template provided by Ninja for both indicators and strategies: UserDefinedMethods. Variables defined there as public-static or protected-static are visible for every strategy or indicator accordingly. Just have a look into that, it's a very simple concept. The alternative would be to design a class which serves as a "data exchange" between the different instances of the strategies.

                          The advantage of using UserDefinedMethods is: it is simple and everybody can easily access and modify the static variables.
                          The dis-advantage of using UserDefinedMethods is: it is simple and everybody can easily access and modify the static variables.

                          I am personally tend to use a class for such kind of operation for two reasons:
                          1. I am paranoid.
                          2. Once having such a class ready to use, it still grows over time and finally I can't live without it anymore.
                          I'll present an idea at the weekend.

                          Regards
                          Ralph

                          Comment


                            #14
                            Ralph,

                            Great post. One of the areas I am really trying to gain insight and experience, is in regards to creating custom classes & namespaces for NT that will house my standard settings and methods for trade management, etc. As it stands now, I have to copy paste, copy paste and use a lot of region/endregion to keep everything organized. Not very elegant, lol.

                            I wanted to start with a very simple class or namespace and slowly let it evolve. My vision it to have a library that includes all the methods/members that I use over and over again, but write once use many, verses write many, change many places, and lose hair and debug, lol.

                            I welcome your experience and anything you contribute in this regard. I am will be happy to help in anyway.

                            Thank you again and I am glad to know this thread is active. Maybe we can start a new thread that relates to custom classes and intra-strategy integration or something.

                            I'm going to start with something very simple. Probably in regards to centralizing a trade notification/reporting class.

                            Also, is there a way to put properties that show up when you start your strategy without having to include them in the strategy every single time? I'm just trying to get away from cut and past, it's dirty and not very scalable.

                            Good stuff, thank you again!



                            r2kTrader

                            Comment


                              #15
                              r2kTrader, I inserted some comments below.

                              Regards
                              Ralph

                              Originally posted by r2kTrader View Post
                              Ralph,

                              Great post. One of the areas I am really trying to gain insight and experience, is in regards to creating custom classes & namespaces for NT that will house my standard settings and methods for trade management, etc. As it stands now, I have to copy paste, copy paste and use a lot of region/endregion to keep everything organized. Not very elegant, lol.

                              That's true. If you have to modify one spot only to add functionality or fix bugs in your libraries, then you have mastered the OOP.

                              I wanted to start with a very simple class or namespace and slowly let it evolve. My vision it to have a library that includes all the methods/members that I use over and over again, but write once use many, verses write many, change many places, and lose hair and debug, lol.

                              Yes, start with very basic functionality. And think very carefully about how to design. Proper implementations save you from a lot of additional efforts later.

                              I welcome your experience and anything you contribute in this regard. I am will be happy to help in anyway.

                              Thank you again and I am glad to know this thread is active. Maybe we can start a new thread that relates to custom classes and intra-strategy integration or something.

                              I'll start a new thread for the code example I was talking about.

                              I'm going to start with something very simple. Probably in regards to centralizing a trade notification/reporting class.

                              Also, is there a way to put properties that show up when you start your strategy without having to include them in the strategy every single time? I'm just trying to get away from cut and past, it's dirty and not very scalable.

                              You can accomplish that by inheritance:
                              class BaseStrat : Strategy
                              {public Property1, public Property2, ...}
                              class ApplicationStrat : BaseStrat
                              {...}
                              Property1 and Property2 are now visible in ApplicationStrat. In C# multiple inheritance is not available like in C++, only single inheritance. Since every strategy/indicator has to inherit from a NT base class you can't pass functionality from different sources that way. But you can instantiate custom classes and then access their public interfaces, which is a suitable (and the only alternative) concept too.

                              Good stuff, thank you again!



                              r2kTrader

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by PhillT, 04-19-2024, 02:16 PM
                              4 responses
                              31 views
                              0 likes
                              Last Post PhillT
                              by PhillT
                               
                              Started by ageeholdings, 05-01-2024, 05:22 AM
                              5 responses
                              36 views
                              0 likes
                              Last Post ageeholdings  
                              Started by reynoldsn, Today, 02:34 PM
                              0 responses
                              10 views
                              0 likes
                              Last Post reynoldsn  
                              Started by nightstalker, Today, 02:05 PM
                              0 responses
                              17 views
                              0 likes
                              Last Post nightstalker  
                              Started by llanqui, Yesterday, 09:59 AM
                              8 responses
                              30 views
                              0 likes
                              Last Post llanqui
                              by llanqui
                               
                              Working...
                              X