NexusFi: Find Your Edge


Home Menu

 





Common code re-use


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one LostTrader with 4 posts (0 thanks)
    2. looks_two fluxsmith with 2 posts (0 thanks)
    3. looks_3 Big Mike with 1 posts (0 thanks)
    4. looks_4 Xeno with 1 posts (1 thanks)
    1. trending_up 7,361 views
    2. thumb_up 1 thanks given
    3. group 4 followers
    1. forum 12 posts
    2. attach_file 0 attachments




 
Search this Thread

Common code re-use

  #1 (permalink)
 
LostTrader's Avatar
 LostTrader 
Tucson, AZ
 
Experience: Intermediate
Platform: Ninjatrader
Broker: Mirus/Zen-Fire
Trading: CL, TF
Posts: 74 since Sep 2009
Thanks Given: 21
Thanks Received: 64

Does anyone have a preferred method of re-using common code (or code snippets) in NinjaScript?

Back when I used to write real code, I could embed a file which the compiler would pull in and place inline before compiling, so I could re-use common sections without replicating the same lines in multiple places. I would love to be able to do that in NinjaTrader.

I have written separate namespaces, with custom classes, and saved/compiled the script in the Indicator directory. Then added "Using name;" with the others at the top of the Indicator script that used the class. However, since the namespace isn't built off the Indicator code, I haven't figured out how to use Print() or any of the NinjaScript functions.

Likewise, I can write a static class that can be used by any other without forcing an instantiation, but it has similar limitations.

What solutions have others used?

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Trade idea based off three indicators.
Traders Hideout
MC PL editor upgrade
MultiCharts
Quant vue
Trading Reviews and Vendors
Cheap historycal L1 data for stocks
Stocks and ETFs
ZombieSqueeze
Platforms and Indicators
 
  #3 (permalink)
 fluxsmith 
Santa Maria
 
Experience: Advanced
Platform: NinjaTrader, ThinkOrSwim
Broker: Mirus/Zen-Fire
Trading: ES
Posts: 290 since May 2010
Thanks Given: 97
Thanks Received: 322


I ran into the problem yesterday of not being able to use Print, I'm sure there's some global object for the output window we need to get our hands on, but I don't know what it is.
My solution was intrusive but if I really need to communicate an error condition, I found that calling System.Windows.Forms.MessageBox.Show works.

Visit my NexusFi Trade Journal Reply With Quote
  #4 (permalink)
 aviat72 
San Francisco Bay Area
 
Experience: Intermediate
Platform: NT,TOS,IB
Trading: ES,CL,TF
Posts: 281 since Jun 2010
Thanks Given: 161
Thanks Received: 273

In your custom code accept the NT Strategy/Indicator object as a parameter to the constructors. You can then call the NT functions from your code via the handle you have to the NT data-structures.

 
Code
 
using NinjaTrader.Data;
using NinjaTrader.Cbi;
using NinjaTrader.Strategy;
...
class CustomCode{
...
StrategyBase parentStrat;
...
public CustomCode(StrategyBase pStat) { // constructor
                    parentStrat = pStat;
                    pStat.TraceOrders = true;
            }

// Now use parentStrat.Method in your code

Visit my NexusFi Trade Journal Reply With Quote
  #5 (permalink)
 
LostTrader's Avatar
 LostTrader 
Tucson, AZ
 
Experience: Intermediate
Platform: Ninjatrader
Broker: Mirus/Zen-Fire
Trading: CL, TF
Posts: 74 since Sep 2009
Thanks Given: 21
Thanks Received: 64

Thanks for the suggestions, keep 'em coming!

I have wondering about the resource consumption by instantiating (calling a constructor) a class simply to re-use common code. NT is not the most efficient compiler in the world and I wonder about excess memory consumption and performance impacts. Up til now, the only methods I moved to a separate class were those which could be static, so no instantiation (construction) is required to call them.

I know one person got around not having the NT Draw routines by overriding the chart plot routines and creating his own.

Started this thread Reply With Quote
  #6 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,463 since Jun 2009
Thanks Given: 33,239
Thanks Received: 101,661

You can profile NinjaScript C# code:



Mike

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #7 (permalink)
 
LostTrader's Avatar
 LostTrader 
Tucson, AZ
 
Experience: Intermediate
Platform: Ninjatrader
Broker: Mirus/Zen-Fire
Trading: CL, TF
Posts: 74 since Sep 2009
Thanks Given: 21
Thanks Received: 64

Thanks Mike. I was actually looking at some Visual Studio links earlier -- it would be nice but I don't have Visual Studio and haven't quite figured out how much it costs, which version do I need, etc. One day I plan to get there.

So I started this thread hoping to encourage folks to share techniques that work with just NT. :-) For poor folk like me!

Started this thread Reply With Quote
  #8 (permalink)
 fluxsmith 
Santa Maria
 
Experience: Advanced
Platform: NinjaTrader, ThinkOrSwim
Broker: Mirus/Zen-Fire
Trading: ES
Posts: 290 since May 2010
Thanks Given: 97
Thanks Received: 322

I don't know if it includes profiling or not, but you might be interested in the free version:
Microsoft Express Home - Visual Studio Express and SQL Server Express are free tools

Visit my NexusFi Trade Journal Reply With Quote
  #9 (permalink)
 MXASJ 
Asia
 
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 796 since Jun 2009
Thanks Given: 109
Thanks Received: 800

VS 2010 Pro has some of that functionality and costs between $0-$500 depending on your use. I'm on the road now but that will be the subject of the next update in my "NinjaTrader and VS2010 for Dummies" thread when I'm back.

Basically you can create and edit your own Code Snippets for custom methods, etc which saves typing. The IDE also knows your file-specific variables, etc and can auto-fill them for you as you type.

Reply With Quote
  #10 (permalink)
 
LostTrader's Avatar
 LostTrader 
Tucson, AZ
 
Experience: Intermediate
Platform: Ninjatrader
Broker: Mirus/Zen-Fire
Trading: CL, TF
Posts: 74 since Sep 2009
Thanks Given: 21
Thanks Received: 64


OK, I separated out the Visual Studio discussion to a separate thread because I do not want it to stop, but I do want to keep this thread on topic. So please bear with me.

If anyone has examples of C# common code re-use that they would liketo share, or more techniques that can be implemented in a NinjaScript cs file, please chime in!

I'll see if I can dig some out. I know that they are not "officially supported" so use at your own risk. But it is a great way to learn.

Started this thread Reply With Quote




Last Updated on October 5, 2011


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts