Anyone have any hints for optimizing C# code? - NinjaTrader Programming | futures io social day trading
futures io futures trading


Anyone have any hints for optimizing C# code?
Updated: Views / Replies:24,813 / 111
Created: by ZTR Attachments:14

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors – all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you don’t need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

Reply
 14  
 
Thread Tools Search this Thread
 

Anyone have any hints for optimizing C# code?

  #101 (permalink)
Elite Member
Berlin, Europe
 
Futures Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker/Data: Interactive Brokers
Favorite Futures: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,651 since Mar 2010
Thanks: 4,226 given, 25,600 received
Forum Reputation: Legendary


kbeary33 View Post
How would you go about this "economization" of indicators if you are using a multi-instrument strategy?

ie, if you declared

 
Code
class MyIndicator : Indicator {
private SMA sma1;
private SMA sma2;
...
protected void override OnStartup() {
sma1 = SMA(Close,5);
sma2 = SMA(Close, 13);
...
}
How could you then call sma1 or sma2 for a BarsArray[index] item?


This should work:

 
Code
protected void override OnStartup() 
{
       sma1 = SMA(Closes[index],5);
       sma2 = SMA(Closes[index], 13);
       ...
}

Reply With Quote
The following 5 users say Thank You to Fat Tails for this post:
 
  #102 (permalink)
Elite Member
Calgary AB
 
Futures Experience: Intermediate
Platform: AmiBroker, IB
Broker/Data: Interactive Brokers
Favorite Futures: Stocks
 
Posts: 10 since Mar 2013
Thanks: 5 given, 2 received

Thanks very much for the reply. I still don't quite get it though... Just to clarify, I am setting this up inside a strategy, not inside an Indicator (I don't know if that makes a difference)

Say that I have 10 instruments, and that I want to maintain a ranked list based on their current SMA. Should I somehow specify:


 
Code
//Inside variables:
private SMA sma1;
int numInstruments = 10;
int smaPeriod = 10;
...

//Inside OnStartup()  -> I know this won't work, I just wasn't sure how else to communicate what I was thinking...
for(int i = 0; i < numInstruments; i++);
sma1[i] = SMA(i,smaPeriod);
...

Or, can you somehow declare sma1 with :
 
Code
sma1 = SMA(Index Placeholder, smaPeriod)
...

double smaInstrument1 = sma1(0)[0];
double smaInstrument2 = sma1(1)[0];
double smaInstrument3 = sma1(2)[0];

Or, lastly, do you have to hard code:
 
Code
private SMA sma1;
private SMA sma2;
private SMA sma3;
...

sma1 = SMA(Closes[0],smaPeriod);
sma2 = SMA(Closes[1],smaPeriod);
sma3 = SMA(Closes[2],smaPeriod);

Reply With Quote
 
  #103 (permalink)
Elite Member
Berlin, Europe
 
Futures Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker/Data: Interactive Brokers
Favorite Futures: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,651 since Mar 2010
Thanks: 4,226 given, 25,600 received
Forum Reputation: Legendary



kbeary33 View Post
Thanks very much for the reply. I still don't quite get it though... Just to clarify, I am setting this up inside a strategy, not inside an Indicator (I don't know if that makes a difference)

Say that I have 10 instruments, and that I want to maintain a ranked list based on their current SMA. Should I somehow specify:


 
Code
//Inside variables:
private SMA sma1;
int numInstruments = 10;
int smaPeriod = 10;
...

//Inside OnStartup()  -> I know this won't work, I just wasn't sure how else to communicate what I was thinking...
for(int i = 0; i < numInstruments; i++);
sma1[i] = SMA(i,smaPeriod);
...

Or, can you somehow declare sma1 with :
 
Code
sma1 = SMA(Index Placeholder, smaPeriod)
...

double smaInstrument1 = sma1(0)[0];
double smaInstrument2 = sma1(1)[0];
double smaInstrument3 = sma1(2)[0];

Or, lastly, do you have to hard code:
 
Code
private SMA sma1;
private SMA sma2;
private SMA sma3;
...

sma1 = SMA(Closes[0],smaPeriod);
sma2 = SMA(Closes[1],smaPeriod);
sma3 = SMA(Closes[2],smaPeriod);



If you want to use an indicator in a strategy, it is best to add an instance of the indicator to the strategy via the Add() method.

If you want to use your strategy with 10 different instruments, you would first add the 10 instruments in the Initialize() section of the strategy. Bar Index 0 denotes the primary bars, and bar indices 1 to 10 would denote the 10 added bar series (index follows the order in which they have been added).

Within the Initialize() section you can then add indicators to your strategy, also via Add(). This is required if you wish that the indicators are plotted, otherwise you can call indicators directly in OnBarUpdate().

Reply With Quote
The following 3 users say Thank You to Fat Tails for this post:
 
  #104 (permalink)
Elite Member
Calgary AB
 
Futures Experience: Intermediate
Platform: AmiBroker, IB
Broker/Data: Interactive Brokers
Favorite Futures: Stocks
 
Posts: 10 since Mar 2013
Thanks: 5 given, 2 received

OK, sounds good, thanks again.

Just so that I'm clear, what if I only need the indicator for calculations, but don't care about plotting it?
> If I want to apply this indicator to each of the 10 instruments, should I have to add a separate instance of the indicator for each of the instruments?
 
Code
Initialize()
{
Add(....all instruments...);

for(int i = 1; i <= numberOfAddedInstruments; i++)
{
Add(SMA(Closes[i],smaPeriod));
}
> Or, can I somehow add one instance of the indicator and then point that to various DataSeries later in the strategy?

Sorry if I'm not describing it quite clearly (Although, I would imagine that if I could just figure out a way to describe it, I probably wouldn't have any issue programming it...)

Thanks a million for helping with this, I'm very much lost in the dark right now

Reply With Quote
 
  #105 (permalink)
Elite Member
Berlin, Europe
 
Futures Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker/Data: Interactive Brokers
Favorite Futures: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,651 since Mar 2010
Thanks: 4,226 given, 25,600 received
Forum Reputation: Legendary


kbeary33 View Post
Just so that I'm clear, what if I only need the indicator for calculations, but don't care about plotting it?

You can call it directly from within OnBarUpdate() as SMA(Closes[i], period).


kbeary33 View Post
> If I want to apply this indicator to each of the 10 instruments, should I have to add a separate instance of the indicator for each of the instruments?

That is what I would try to do.


kbeary33 View Post
> Or, can I somehow add one instance of the indicator and then point that to various DataSeries later in the strategy?

No, this will not work.

Reply With Quote
The following 2 users say Thank You to Fat Tails for this post:
 
  #106 (permalink)
Elite Member
Calgary AB
 
Futures Experience: Intermediate
Platform: AmiBroker, IB
Broker/Data: Interactive Brokers
Favorite Futures: Stocks
 
Posts: 10 since Mar 2013
Thanks: 5 given, 2 received

Hi again,

Thanks very much for your input, but I still haven't been able to find an efficient way of implementing this.

First I tried:

 
Code
private override void Initialize()
for(int i = 0; i <= numInstruments; i++)
{
	Add(ATR(BarsArray[i],ATRPeriod));
	Add(ROC(BarsArray[i],RankingPeriod1));
}
... But NinjaTrader throws an error saying that you can't access BarsArray inside of the Initialize function

So, I then tried:
 
Code
private override void OnBarUpdate()
{
...
if(!init)
{
for(int i = 0; i <= numInstruments; i++) { Add(ATR(BarsArray[i],ATRPeriod)); Add(ROC(BarsArray[i],RankingPeriod1)); } init = true;
... But NinjaTrader then throws an error saying that you can't use "Add()" outside of the initialize function

I am actually working with a fairly large list of instruments (300+), so obviously I don't want to type a line manually for each of the indicators that I want to add. Is there some other way than what I've tried to do in the code above to iterate through the list of instruments and add their corresponding indicator?

Also, from a memory usage standpoint: If I'm running an optimization run, and NinjaTrader creates a new instance of each indicator for each set of parameters, will Ninja ever release that memory, or no? Right now, the memory usage gradually explodes (using all 12GB of RAM), and the only way to fix it is to close NT and restart.

I understand that the best way to handle this on a single instrument strategy is to create a unique instance of the indicator, and just reference that instance, but how can you hard code that to iterate automatically? Is there a way in C# to use a dynamic string when declaring variables? Not sure how to explain, but I'm thinking something along the lines of:

 
Code
for (int i = 0; i < numberInstruments; i++)
{
private SMA sma[i] = SMA(BarsArray[i], smaPeriod);
}

Reply With Quote
 
  #107 (permalink)
Elite Member
Wrocław, Poland
 
Futures Experience: Intermediate
Platform: NinjaTrader, Racket
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 651 since Aug 2009
Thanks: 321 given, 605 received


kbeary33 View Post
... But NinjaTrader throws an error saying that you can't access BarsArray inside of the Initialize function

Try using Closes instead:
 
Code
Add(ATR(Closes[i],ATRPeriod));
And see how this works instead.

Reply With Quote
The following 2 users say Thank You to gregid for this post:
 
  #108 (permalink)
Elite Member
Calgary AB
 
Futures Experience: Intermediate
Platform: AmiBroker, IB
Broker/Data: Interactive Brokers
Favorite Futures: Stocks
 
Posts: 10 since Mar 2013
Thanks: 5 given, 2 received

Thanks very much, this seemed to work. I had thought that because the ATR calc needed all of the high/low/close/open, it would have to be passed the BarsArray item. Passing Closes returned the same value though, so apparently that's ok

One last thing - Will adding the indicators to the strategy serve the same purpose as declaring a unique named instance of the indicator?

ie

Add(ATR(Closes[1],ATRPeriod)) --> same performance as: private ATR myATR1 = ATR(Closes[1],ATRPeriod)

Reply With Quote
The following user says Thank You to kbeary33 for this post:
 
  #109 (permalink)
Elite Member
Berlin, Europe
 
Futures Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker/Data: Interactive Brokers
Favorite Futures: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,651 since Mar 2010
Thanks: 4,226 given, 25,600 received
Forum Reputation: Legendary


kbeary33 View Post
Thanks very much, this seemed to work. I had thought that because the ATR calc needed all of the high/low/close/open, it would have to be passed the BarsArray item. Passing Closes returned the same value though, so apparently that's ok

If you look at the code of the ATR indicator, it does not use Input[0] anywhere. If it did, the output values would depend on the type of data passed. Therefore ATR(Closes[1], ATRPeriod) is the same as ATR(Highs[1], ATRPeriod). I would prefer to write ATR(Inputs[1], ATRPeriod) as this covers the general case.



kbeary33 View Post
One last thing - Will adding the indicators to the strategy serve the same purpose as declaring a unique named instance of the indicator?

ie

Add(ATR(Closes[1],ATRPeriod)) --> same performance as: private ATR myATR1 = ATR(Closes[1],ATRPeriod)

The main purpose of addingan indicator via the ADD() method is to have them directly plotted by the strategy. If this is not required, the code below is a simpler solution:

 
Code
#region Variables

private ATR   myATR1;

#endregion

protected override void OnStartUp()
{
     myATR1 = ATR(Inputs[1], ATRPeriod);
}

Reply With Quote
The following 2 users say Thank You to Fat Tails for this post:
 
  #110 (permalink)
Elite Member
Portland Oregon, United States
 
Futures Experience: Beginner
Platform: Ninjatrader®
Broker/Data: CQG, Kinetick
Favorite Futures: Gameplay Klownbine® Trading of Globex
 
Zondor's Avatar
 
Posts: 1,327 since Jul 2009
Thanks: 1,246 given, 2,635 received

Array Containing Ninjascript Data Series objects


An Array can store any type, including DataSeries objects. As an example, let's create an array to hold more than one DataSeries. The rank (number of elements) of the array does not need to be hard coded. It can be user selected or programmatically controlled, and the setting of the object references can be done in a For loop.

Declare the array. At this point the rank (number of elements) is not yet defined and the array is empty.

 
Code
private DataSeries [] multiSeries;
 //declare an array that will contain object references to DataSeries objects.
In the Initialize() method:
 
Code
//specify the number of elements in the array, which is the integer called rank 
multiSeries[]=new DataSeries[rank];           

//the array now contains the number rank of object references that need to be set to instances of objects

for (int count = 0; count<rank; count++)
multiSeries[count]=new DataSeries(this);           
Remember, an array is a ZERO BASED collection. So, for example, if an array contains 5 items, the subscripts of the items will be 0 through 4, not 1 through 5. Hence the start and end points of the For loop.

Each of these objects is accessed like any other DataSeries, but don't forget the first subscript that tells you which one is being accessed.

 
Code
double value_X_BarsBack_of_multiSeriesN = multiSeries[N][X];
That's it, we're almost done for today. Have a nice day!

But before I return to my undisclosed location, here is an example of how to use this technique, based on the Freedom of Movement Indicator in the April 2014 issue of TASC. By eliminating dependencies on external classes, the performance of the indicator has been improved. (I have other, more ambitious uses in mind for this technique, since for this particular indicator it is not really worth the trouble.)

"If we don't loosen up some money, this sucker is going down." -GW Bush, 2008
“Lack of proof that something is true does not prove that it is not true - when you want to believe.” -Humpty Dumpty, 2014
“The greatest shortcoming of the human race is our inability to understand the exponential function.”
Prof. Albert Bartlett
Attached Files
Register to download File Type: cs FoM.cs (17.5 KB, 27 views)

Last edited by Zondor; April 30th, 2014 at 03:32 PM.
Reply With Quote
The following 3 users say Thank You to Zondor for this post:

Reply



futures io > > > > > Anyone have any hints for optimizing C# code?

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Linda Bradford Raschke: Reading The Tape

Elite only

Adam Grimes: TBA

Elite only

NinjaTrader: TBA

January

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hints, Tips & Gocthas NW27 MultiCharts 2 August 7th, 2011 04:11 PM
Can someone code this ? skyfly NinjaTrader Programming 8 April 25th, 2011 01:40 PM
Japan's Intervention Hints Fail to Stall Yen's Strength Quick Summary News and Current Events 0 September 8th, 2010 03:20 AM
Optimizing Range Bar Settings? daedalus Traders Hideout 3 February 1st, 2010 01:00 AM
Does anyone esle find Ninja problematic for optimizing strategies? rsi77 NinjaTrader 3 June 17th, 2009 09:32 PM


All times are GMT -4. The time now is 12:42 AM.

Copyright © 2017 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
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.
no new posts
Page generated 2017-12-11 in 0.15 seconds with 40 queries on phoenix via your IP 107.20.115.174