NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 04-11-2008, 01:14 PM   #1
adrian
Member
 
Join Date: Mar 2008
Posts: 78
Thanks: 0
Thanked 0 times in 0 posts
Default Utility functions & code organization

Hey gurus,

As I develop some strategies, I'm accumulating a growing collection of methods that I would like to share with all of my strategies. I wonder if someone can help with ways of doing this without cut-and-paste...

Can I use polymorphism, so something like:

public class MySubStrategy : MyParentStrategy

where I provide default implementations of OnBarUpdate or OnPositionUpdate? Is there some way of templating so that the OnPositionUpdate comes from one source and OnBarUpdate comes from another? I can create helper methods of course, as long as I pass all necessary properties but it's messy.

And is there some good location to stuff all of the other helper methods and utilities I create? Right now I have a sort of dummy Indicator. It doesn't do anything indicator-esqe, but it's the only way I've found to create a new class which can be instantiated and referenced in Strategies. Feels like a hack. Is there any better way?
adrian is offline  
Reply With Quote
Old 04-11-2008, 01:41 PM   #2
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Default

>> public class MySubStrategy : MyParentStrategy
We have not tried that and this would be beyond what we provide support for. You would need to try by yourself.
NinjaTrader_Dierk is offline  
Reply With Quote
Old 10-26-2008, 07:24 AM   #3
cunparis
Senior Member
 
Join Date: Feb 2008
Location: Paris
Posts: 713
Thanks: 10
Thanked 10 times in 7 posts
Default

Can you suggest another method of sharing code between strategies? I'm having the same issue.
cunparis is offline  
Reply With Quote
Old 10-26-2008, 07:48 AM   #4
mrlogik
Certified NinjaScript Consultant
 
mrlogik's Avatar
 
Join Date: Sep 2006
Location: New York, USA
Posts: 774
Thanks: 1
Thanked 7 times in 5 posts
Default

Since NT strategies / indicators / functions are all "partial" classes, they all compile to one project. So, you can basically create an empty indicator, make it a "partial" class, and write "public" functions.

I have attached the framework with some comments to help.

I hope this is helpful
Attached Files
File Type: zip GlobalShare.zip (1.7 KB, 150 views)
"You look closely enough, you can find everything has a ... weak spot where it can break, sooner or later"

PureLogikTrading
mrlogik is offline  
Reply With Quote
Old 10-26-2008, 08:01 AM   #5
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default

Our UserDefinedMethods is a partial class that does just this as well although mrlogik approach might work better since if you export your custom scripts, this global indicator will be included in the export file were the UserDefinedMethods will not.
NinjaTrader_Ray is offline  
Reply With Quote
Old 10-26-2008, 08:04 AM   #6
mrlogik
Certified NinjaScript Consultant
 
mrlogik's Avatar
 
Join Date: Sep 2006
Location: New York, USA
Posts: 774
Thanks: 1
Thanked 7 times in 5 posts
Default

Hey Ray,

Thanks, i forgot to mention that. This is the reason I created my own indicator / class to do it; I export to a few computers for trading whenever I create something.
"You look closely enough, you can find everything has a ... weak spot where it can break, sooner or later"

PureLogikTrading
mrlogik is offline  
Reply With Quote
Old 11-14-2008, 11:50 AM   #7
ActiveHawk
Junior Member
 
Join Date: Feb 2008
Posts: 8
Thanks: 0
Thanked 0 times in 0 posts
Default

Thanks mrlogik,

I tried your GV module and it works great for public functions but do I have to create a public static variable to access the global variable examples in your file? or is there another way to use them?
ActiveHawk is offline  
Reply With Quote
Old 11-14-2008, 12:18 PM   #8
mrlogik
Certified NinjaScript Consultant
 
mrlogik's Avatar
 
Join Date: Sep 2006
Location: New York, USA
Posts: 774
Thanks: 1
Thanked 7 times in 5 posts
Default

You have ot make a public static variable within the Gv global indicator.

For example, with the code I gave you.

Code:
public static BData gBData;
you can then access gBData from any indicator.

Make sure you don't run into any race conditions with reading / writing out of sequence, or writing from multiple indicators at the same time.
"You look closely enough, you can find everything has a ... weak spot where it can break, sooner or later"

PureLogikTrading
mrlogik is offline  
Reply With Quote
Old 02-04-2009, 03:54 PM   #9
michaelangela
Junior Member
 
Join Date: Nov 2008
Posts: 11
Thanks: 0
Thanked 0 times in 0 posts
Default

I know this is an old thread but it seems very useful. However I seem to be missing something very simple. After defining global functions in the GV indicator, the functions do not seem to be available, including the original GetDateFunction. Intellisense isn't picking it up and there is a "not found" error from the compiler.

What am I missing?

Using Ninjatrader 6.5.1000.8

Thanks!
michaelangela is offline  
Reply With Quote
Old 02-04-2009, 04:02 PM   #10
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

You will need to fix the error before you will see anything. Unfortunately this is outside the scope we can offer support for. Maybe a forum member familiar with what is being requested could help you out.
NinjaTrader_Josh is offline  
Reply With Quote
Old 02-04-2009, 04:04 PM   #11
michaelangela
Junior Member
 
Join Date: Nov 2008
Posts: 11
Thanks: 0
Thanked 0 times in 0 posts
Default

That was fast!

Oh sorry, I wasn't clear. If I try typing the global function, it doesn't show in intellisense. But if I do go ahead and type it manually anyway, the compiler isn't able to locate the method. Is there anything special I have to add, say in the Using section? For example, I can't just do GetDateFunction(...). Of course, GV.GetDateFunction doesn't work either. How would I call that one from my strategy?

Thanks!
michaelangela is offline  
Reply With Quote
Old 02-04-2009, 04:07 PM   #12
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

As mentioned, this is outside the scope of what we can support. I have no idea what this "GV" thing you refer to is.

Likely you need to do GV().GetDateFunction. Not sure. You may want to contact whoever's code you are duplicating to try and get help.
NinjaTrader_Josh is offline  
Reply With Quote
Old 02-04-2009, 04:11 PM   #13
michaelangela
Junior Member
 
Join Date: Nov 2008
Posts: 11
Thanks: 0
Thanked 0 times in 0 posts
Default

The GV is in the GlobalShare.zip file above where the example is provided. I'll see if mrlogik can be of assistance though. But thanks for writing. The compiler doesn't complain so that's a step in the right direction. I'll start testing it out now. Thank you!

When writing partials like this, is there a way to refresh the intellisense cache?
Last edited by michaelangela; 02-04-2009 at 04:19 PM. Reason: new to C# and still learning!
michaelangela is offline  
Reply With Quote
Old 02-05-2009, 01:33 AM   #14
cunparis
Senior Member
 
Join Date: Feb 2008
Location: Paris
Posts: 713
Thanks: 10
Thanked 10 times in 7 posts
Default

Here is what I did: I created a class to facilitate the use of these Global Variable methods. I'm attaching it to this post.

BTW I'm not sure if you're trying to coordinate between two strategies or two different applications. If two strategies, you don't need GV. You can just use a static variable. The GV is useful for having two different processes communicate, like Ninjatrader and Tradestation for example.
Attached Files
File Type: zip ToolGlobalVariable.zip (750 Bytes, 95 views)
cunparis is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Organization of Indicators.............. redrum28 Miscellaneous Support 1 02-27-2008 06:55 AM
Standard Return Code for Functions arbifox Automated Trading 1 12-13-2007 08:21 PM
renaming functions z32000 General Programming 3 12-03-2007 10:47 AM
Code->test/debug->change code->retest ... cycle process bbarroux Strategy Development 3 10-02-2007 12:44 PM
Unrealized PnL for ATI Functions whitmark Automated Trading 1 09-19-2007 03:47 PM


All times are GMT -6. The time now is 04:49 AM.