![]() |
This website will be down for maintenance from Friday May 24th at 6PM MDT until Sunday May 26th at 12PM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com
|
|||||||
| General Programming General NinjaScript programming questions. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
Join Date: Jul 2008
Posts: 527
Thanks: 0
Thanked 9 times in 6 posts
|
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:
[Description("Data transfer base class.")]
public class DataTransfer
{...}
Code:
[Description("Data transfer application class - Profit/Loss.")]
public class DataTransferPL : DataTransfer
{...}
Code:
[Description("Test strategy for the DataTransfer class.")]
public class DataTransferTest : Strategy
{...}
Code:
...// Retrieve the total profit/loss until now
doubleValue = dataTransferPL.TotalProfitLoss(Time[0]);
Print("# Startegy: " + strategyName + " Total PnL until " + Time[0] + ": " + doubleValue);
...
Code:
//#define DEBUG
...
public DataTransfer()
{
memberID = memberCount++;
memberDict.Add(memberID, new MemberData(string.Empty, DateTime.MinValue));
#if DEBUG
if (memberInstance != null) memberInstance.Print("DataTransfer.DataTransfer memberID: " + memberID + " new member count: " + memberDict.Count);
#endif
...
}
Have fun Ralph
Last edited by Ralph; 09-06-2009 at 05:30 PM.
|
|
|
|
|
The following 3 users say thank you to Ralph for this post: |
|
|
|
#2 |
|
Senior Member
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
|
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, |
|
|
|
|
|
#3 |
|
Senior Member
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
|
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. |
|
|
|
|
|
#4 |
|
Senior Member
Join Date: Jul 2008
Posts: 527
Thanks: 0
Thanked 9 times in 6 posts
|
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 at 01:21 PM.
|
|
|
|
|
|
#5 | |
|
Senior Member
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
|
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 Quote:
|
|
|
|
|
|
|
#6 |
|
Senior Member
Join Date: Jul 2008
Posts: 527
Thanks: 0
Thanked 9 times in 6 posts
|
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:
Regards Ralph |
|
|
|
|
|
#7 |
|
Senior Member
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
|
Ralph,
I'm re-reading this thread and catching up. Your thread is fantastic! Thank you again, |
|
|
|
|
|
#8 |
|
Senior Member
Join Date: Oct 2007
Location: Sydney
Posts: 311
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#9 |
|
Senior Member
Join Date: Jul 2008
Posts: 527
Thanks: 0
Thanked 9 times in 6 posts
|
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 |
|
|
|
|
|
#10 | |
|
Senior Member
Join Date: Oct 2007
Location: Sydney
Posts: 311
Thanks: 0
Thanked 0 times in 0 posts
|
Quote:
I am learning a lot just from going through your code with a C# manual, and trying to understand it. thanks again. |
|
|
|
|
|
|
#11 | |
|
Senior Member
Join Date: Jul 2008
Posts: 527
Thanks: 0
Thanked 9 times in 6 posts
|
Quote:
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 |
|
|
|
|
|
|
#12 |
|
Senior Member
Join Date: Oct 2007
Location: Sydney
Posts: 311
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#13 |
|
Senior Member
Join Date: Jul 2008
Posts: 527
Thanks: 0
Thanked 9 times in 6 posts
|
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 |
|
|
|
|
|
#14 | |
|
Senior Member
Join Date: Mar 2009
Posts: 441
Thanks: 0
Thanked 25 times in 16 posts
|
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 Quote:
How do I make them all be in the same Memberlist? Regards, Baruch |
|
|
|
|
|
|
#15 |
|
Senior Member
Join Date: Jul 2008
Posts: 527
Thanks: 0
Thanked 9 times in 6 posts
|
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 at 08:01 AM.
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Transfer Updated Instrument List | trend | General Programming | 3 | 07-23-2009 11:33 AM |
| How to transfer info to Excel | Cryptorchid | General Programming | 1 | 06-28-2009 11:08 PM |
| how to transfer the workspace | inventor1949 | General Programming | 3 | 06-29-2008 10:33 PM |
| Performance manager transfer | tonchi | Miscellaneous Support | 1 | 12-03-2006 04:20 PM |
| Transfer files and setting to new computer | markr413 | Miscellaneous Support | 1 | 03-27-2005 12:37 PM |