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

Data Transfer between Strategies

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

    Data Transfer between Strategies

    Hi r2kTrader and Hi all traders,

    In this thread I present a basic concept how to design a class (DataTransfer) maintaining basic informations transfered from multiple strategy instantiations.
    Code:
    [SIZE=2][SIZE=2][FONT=Courier New][Description([/FONT][/SIZE][/SIZE][FONT=Courier New][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]"Data transfer base class."[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2])][/SIZE][/SIZE][/FONT]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][FONT=Courier New]public [/FONT][/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]class[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] DataTransfer[/SIZE][/FONT]
    [SIZE=2][FONT=Courier New][FONT=Courier New]{...}[/FONT][/FONT][/SIZE][/SIZE][/FONT]
    An application class derived from DataTransfer (DataTransferPL) serves as an example how to maintain the profit & loss values provided by these strategies.
    Code:
    [SIZE=2][SIZE=2][FONT=Courier New][Description([/FONT][/SIZE][/SIZE][FONT=Courier New][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]"Data transfer application class - Profit/Loss."[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2])][/SIZE][/SIZE][/FONT]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][FONT=Courier New]public [/FONT][/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]class[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] DataTransferPL : DataTransfer[/SIZE][/FONT]
    [SIZE=2][FONT=Courier New][FONT=Courier New]{...}[/FONT][/FONT][/SIZE][/SIZE][/FONT]
    The example strategy (DataTransferTest) instantiates the DataTransferPL class and transfers profit & loss date to this instance and retrieves condensed informations from this instance.
    Code:
    [SIZE=2][SIZE=2][FONT=Courier New][Description([/FONT][/SIZE][/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"Test strategy for the DataTransfer class."[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2][FONT=Courier New])][/FONT][/SIZE][/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/FONT]
    [FONT=Courier New][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public [/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]class[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] DataTransferTest : Strategy[/SIZE][/FONT]
    [SIZE=2][FONT=Courier New][FONT=Courier New]{...}[/FONT][/FONT][/SIZE][/SIZE][/FONT]
    You could/should start several DataTransferTest strategies on the same chart and/or on different charts. They do all interact with each other by the instantiation of DataTransferPL. The strategies transmit a randomly generated profit & loss and retrieve the profit & loss of this trading day and the total profit & loss generated by all strategies with every first session bar . The results are printed to the output window (open this window before starting this application).
    Code:
    [FONT=Courier New]...[/FONT][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000][FONT=Courier New]// Retrieve the total profit/loss until now[/FONT][/COLOR][/SIZE]
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2][FONT=Courier New]doubleValue = dataTransferPL.TotalProfitLoss(Time[[/FONT][/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2][FONT=Courier New]]);[/FONT][/SIZE]
    [SIZE=2][FONT=Courier New]Print([/FONT][/SIZE][/SIZE][FONT=Courier New][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]"# Startegy: "[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] + strategyName + [/SIZE][/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]" Total PnL until "[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] + Time[[/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2]] + [/SIZE][/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]": "[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] + doubleValue);[/SIZE][/SIZE][/FONT]
    [FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]...[/SIZE][/FONT]
    [/SIZE][/FONT]
    There is an debug mode implemented. If activated, status statements for nearly all methods of the transfer classes (DataTransfer, DataTransferPL) are printed to the output window. The debug mode is activated by uncommenting the "#define DEBUG" definition in the first line of both source files. Don't forget to comment both statement when the debugging is finished.
    Code:
    [SIZE=2][FONT=Courier New][COLOR=#008000][SIZE=2][FONT=Courier New][COLOR=#008000][SIZE=2][FONT=Courier New][COLOR=#008000][FONT=Courier New]//#define DEBUG[/FONT][/COLOR][/FONT][/SIZE]
    [FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New]...[/FONT][/COLOR][/SIZE]
    [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2][FONT=Courier New] DataTransfer()[/FONT][/SIZE]
    [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
    [SIZE=2][FONT=Courier New] memberID = memberCount++;[/FONT][/SIZE]
    [SIZE=2][FONT=Courier New] memberDict.Add(memberID, [/FONT][/SIZE][/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] MemberData([/SIZE][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2].Empty, DateTime.MinValue));[/SIZE][/SIZE][/FONT]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][FONT=Courier New]#if[/FONT][/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2][FONT=Courier New][COLOR=#000000] DEBUG[/COLOR][/FONT][/SIZE]
    [/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff] if[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] (memberInstance != [/SIZE][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2]) memberInstance.Print([/SIZE][/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]"DataTransfer.DataTransfer memberID: "[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] + memberID + [/SIZE][/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]" new member count: "[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] + memberDict.Count);[/SIZE][/SIZE][/FONT]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][FONT=Courier New]#endif[/FONT][/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff][FONT=Courier New]...[/FONT][/COLOR][/SIZE]
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][FONT=Courier New][SIZE=2]}[/SIZE][/FONT]
    [/SIZE][/COLOR][/SIZE][/FONT][/COLOR][/FONT][/SIZE][/COLOR][/FONT][/SIZE]
    You can find the source code strategy installation containing two files in the attachment.

    Have fun
    Ralph
    Attached Files
    Last edited by Ralph; 09-06-2009, 05:30 PM.

    #2
    Ralph,

    Thank you so much for your time and contribution. I'm going to carefully read your thread and looking forward to learning all I can to get the most from the development process.

    Sincerely,

    Comment


      #3
      Ralph,

      I'm looking over your post and I can't help but thank you again. What you are doing here is so badly needed by NT that I can't say enough.

      More specifically, it is very nice to get a play by play for developing your own base class and incorporating it within your strategies. I want to get out from under the world of cut and paste everytime I build a new strategy and better manage my code so that it is centralized. Write once, use many.

      This is a great template and I am going to read this post with my C# books right next to me and look forward to sharing my first class with the board.


      Really, thanks again. Hopefully, NT will begin to pay more attention to this area.

      Comment


        #4
        Hi r2kTrader,

        your comment is highly appreciated, thank you very much.

        It may be a good idea to execute this code example with the debug mode. It helps to understand the chronology of execution. The two code files have the debug mode disabled/commented. So, don't forget to uncomment as described.

        The test evaluation is worth the effort for a different reason in addition: It provides you with a detailed inside of how competing strategies are executed with the data already available on the chart, especially if you start several strategies on the same chart or if you add an additional strategy to a chart later on. I don't know, if you do such things, but I did learn something about strategy execution with Ninja and it affected also the implemetation of the concept idea.

        And in the end I support your view of how to organise code in the object oriented era. And I assure you not to use something outside the area of what NT supports. So far, only Initialize(), OnBarUpdate() and Dispose() is used as an interface to NT (these are all documented features). The rest is pure C# coding independend from the NT-interface.

        Regards
        Ralph
        Last edited by Ralph; 09-07-2009, 01:21 PM.

        Comment


          #5
          Ralph,

          I will do both.

          I am really excited about the skeleton you provided for building custom namespaces. I've been reading C# books, but of course NT is its own animal so your homework is a great example.

          Thank you again for sharing.



          Justin

          Originally posted by Ralph View Post
          Hi r2kTrader,

          your comment is highly appreciated, thank you very much.

          It may be a good idea to execute this code example with the debug mode. It helps to understand the chronology of execution. The two code files have the debug mode disabled/commented. So, don't forget to uncomment as described.

          The test evaluation is worth the effort for a different reason in addition: It provides you with a detailed inside of how competing strategies are executed with the data already available on the chart, especially if you start several strategies on the same chart or if you add an additional strategy to a chart later on. I don't know, if you do such things, but I did learn something about strategy execution with Ninja and it affected also the implemetation of the concept idea.

          And in the end I support your view of how to organise code in the object oriented era. And I assure you not to use something outside the area of what NT supports. So far, only Initialize(), OnBarUpdate() and Dispose() is used as an interface to NT (these are all documented features). The rest is pure C# coding independend from the NT-interface.

          Regards
          Ralph

          Comment


            #6
            Memberlist

            Until now the DataTransfer base class handles construction, destruction and registration of instantiations only. I implemented a little more fuctionality: a memberlist. The memberlist stores a collection of DataTransfer instances based on pattern matching for the name strings. The instance holding the search pattern has access to the members of the list. The search pattern is a regular expression. For example "Test[A-Z]1" matches all name strings starting with "Test", followed by a capital letter A-Z, followed by the number 1.

            The methods of the DataTransferPL example implementation are now operating with the memberlist (instead with all DataTransfer instances).

            On the attached screenshot you see a total of 5 instantiated strategies distributed over 3 charts. Two strategies (named TestB1 and TestC1) contain a search pattern and they create their memberlists accordingly.
            The memberlist activities are reported in the Ninja log tab:
            • TestB1 strategy instance creates a memberlist based on search pattern "Test[A-Z]1": TestB1, TestA1
            • TestC1 strategy instance creates a memberlist based on search pattern "Test[A-Z]1": TestC1, TestA1, TestB1
            • TestB1 extends its memberlist with TestC1 because it is an additional match.
            A strategy instance is always the first member of its own memberlist, no matter if it matches its own search pattern.

            Regards
            Ralph
            Attached Files

            Comment


              #7
              Ralph,

              I'm re-reading this thread and catching up.

              Your thread is fantastic!


              Thank you again,

              Comment


                #8
                Hi Ralph,
                Thanks very much for this code. The functionality is just what I need.
                Please excuse the silly question, but should these C# files be compiled into a dll?

                thanks
                Grant

                Comment


                  #9
                  Many thanks for the feedback, Grant.
                  I did compile it just the "standard way".
                  Is there a certain reason to put it into a dll?

                  Regards
                  Ralph

                  Comment


                    #10
                    Originally posted by Ralph View Post
                    Many thanks for the feedback, Grant.
                    I did compile it just the "standard way".
                    Is there a certain reason to put it into a dll?

                    Regards
                    Ralph
                    I have no specific reason to put it in a dll, this is just my lack of depth in C# coming through. I thought any custom code that was not an indicator or strategy needed to be in a dll. So you learn something every day

                    I am learning a lot just from going through your code with a C# manual, and trying to understand it.

                    thanks again.

                    Comment


                      #11
                      Originally posted by astrolobe View Post
                      ...I thought any custom code that was not an indicator or strategy needed to be in a dll...
                      Hello Grant,

                      now I understand. As you have seen already I use an own namespace. That does not matter for the compiler. It structures code and implements access permissions by the "using"-functionallity. The NT software does not know anything about this code, unless you use it in an indicator or strategy or both. You even could connect a strategy with an indicator by this transfer concept, it is no difference. But I need to decide for obvious reason where to locate the code and there are only 2 choices, indicator or strategy.

                      I use this kind of namespacing for library development for my NT applications.

                      If you extend your C# skills by reading the code also pay attention to how the code is structured. The base class contains the transfer functionality and the derived class implements an application. That is an OOP-concept. And that is where I spent the most time (for thinking) because a proper interface is valuable in terms of application and re-usability. If you like programming, then it is worth to spend some efforts too in this field (OOP).

                      Regards
                      Ralph

                      Comment


                        #12
                        Thanks Ralph, you cleared up up my confusion about where to place the code. I find focusing on the C# features I need, i.e., your code, is a much easier way to learn C#, then trying to read through Andrew Troelsen's C# manual (1300 + pages !!)
                        After all, I need the time for trading. However, having really useful code base does save a lot of time that I would otherwise do manually.

                        cheers
                        Grant

                        Comment


                          #13
                          Agree Grant, trading has first priority. With this in mind I would follow the same path you descibed, finding a proper code example and expanding it when required.

                          Regards
                          Ralph

                          Comment


                            #14
                            Hi Ralph,
                            I'm trying to implement your datatransfer example, but it doesn't work in NT7.
                            When I add 2 strategies on 2 instruments, thats how my log looks like
                            17/01/11 6:08 AM Default DataTransfer instance: Test2 Memberlist created: Test2
                            17/01/11 6:08 AM Default DataTransfer instance: Test1 Memberlist created: Test1
                            As you see each member is for itself.
                            How do I make them all be in the same Memberlist?

                            Regards,
                            Baruch

                            Comment


                              #15
                              Hello Baruch,

                              Each strategy requesting access to the (or to a) member list has to define a member list name search pattern in the property grid field "Member search pattern". The search pattern must be a regular expression.

                              You defined Test1 and Test2 as the names for your 2 strategies. You could define a regular expression like Test[1-9] or Test[1-9]+ for larger numbers. And you need to define a regular expression for each strategy which should get access to the member list.

                              Regards
                              Ralph
                              Last edited by Ralph; 01-17-2011, 09:01 AM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by merzo, 06-25-2023, 02:19 AM
                              10 responses
                              823 views
                              1 like
                              Last Post NinjaTrader_ChristopherJ  
                              Started by frankthearm, Today, 09:08 AM
                              5 responses
                              15 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Started by jeronymite, 04-12-2024, 04:26 PM
                              3 responses
                              43 views
                              0 likes
                              Last Post jeronymite  
                              Started by yertle, Today, 08:38 AM
                              5 responses
                              16 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by adeelshahzad, Today, 03:54 AM
                              3 responses
                              19 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Working...
                              X