NexusFi: Find Your Edge


Home Menu

 





Instantiating custom indicator from strategy


Discussion in NinjaTrader

Updated
    1. trending_up 2,090 views
    2. thumb_up 3 thanks given
    3. group 5 followers
    1. forum 4 posts
    2. attach_file 0 attachments




 
Search this Thread

Instantiating custom indicator from strategy

  #1 (permalink)
ahadari28
Tel Aviv
 
Posts: 2 since Sep 2016
Thanks Given: 2
Thanks Received: 0

Hello everyone,

I'm new to this forum and first would like to thank everybody for the amazing work.

I'm a software developer but new to Ninja Trader.

I have a weird situation using Ninja Trader 7 in which I created my custom indicator which works perfeclty fine from
a chart.

Seeing that my indicator was working as expected I went on to create my strategy, consuming my custom indicator(s).

How happy was I to see that after some very basic coding, the strategy analyzer was indeed putting out results.

The following day I resumed to elaborate my trading logic in my strategy
And went to test the results on the strategy analyzer, only to see that "Object reference not set to an instance of an object"


Some debugging using visual studio an I see that the OnStartUp() of my custom indicators are not being hit anymore,
That is where I instantiate the Indicators public members (intended for use in my strategy)
resulting in an "object reference not set to an instance of an object" exception.

I've also noticed that when i write in the immediate window of visual studio
var x = new CustomIndicator();
I'm getting - "CustomIndicator exists both in x.dll and y.dll"

I'm getting back an instance of the class, but all of it's members which are not primitives types are
Not instantiated,

I'm very frustrated by that as I assume the problem is an internal NinjaTrader issue

I would love if you guys could help me out and re-assure me that I'm indeed working as intended in NinjaScript.

Basically what im doing is the following.

 
Code
public class CustomerIndicator1 : Indicator
 {
  #region Variables
        private SMA SMAArray;
        private Stochastics StochArray;
        #endregion

protected override void Initialize()
{
   Overlay = false;
   CalculateOnBarClose = true;
}

protected override void OnStartUp()
{
    SMAArray = SMA(50);
    StochArray = Stochastics(3, 5, 2);
}
protected override void OnBarUpdate()
{
//some logics.
}
}
//Relevant Properties to perform as getters to the SMAArray and StochArray.

and the strategies code:

 
Code
   #region Variables
        // Wizard generated variables
        
        private MyCustomIndicator1 ci;
        // User defined variables (add any user defined variables below)
#endregion



 protected override void Initialize()
        {
			
            CalculateOnBarClose = false;

            Add(PeriodType.Tick, 1);
            Add(BarsPeriod.Id, BarsPeriod.Value * 3);
            Add(BarsPeriod.Id, BarsPeriod.Value * 9);

        }


        protected override void OnStartUp()
        {
            ci = new Indicator.CustomIndicator1();

            
        }

       protected override void OnBarUpdate()
       {
          //trying to access public members of ct causing exception....
       }

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
MC PL editor upgrade
MultiCharts
ZombieSqueeze
Platforms and Indicators
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Exit Strategy
NinjaTrader
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Spoo-nalysis ES e-mini futures S&P 500
45 thanks
Just another trading journal: PA, Wyckoff & Trends
31 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
24 thanks
GFIs1 1 DAX trade per day journal
22 thanks
  #2 (permalink)
 
rleplae's Avatar
 rleplae 
Gits (Hooglede) Belgium
Legendary Market Wizard
 
Experience: Master
Platform: NinjaTrader, Proprietary,
Broker: Ninjabrokerage/IQfeed + Synthetic datafeed
Trading: 6A, 6B, 6C, 6E, 6J, 6S, ES, NQ, YM, AEX, CL, NG, ZB, ZN, ZC, ZS, GC
Posts: 3,003 since Sep 2013
Thanks Given: 2,442
Thanks Received: 5,863

Try something like this :

 
Code
public class YourStrategyName : Strategy
    {
       private CustomIndicator myCustomIndicator;

        protected override void Initialize()
        {
	    CalculateOnBarClose = false;
			
	    myCustomIndicator = CustomIndicator();
	    BarsRequired = 200;

            Add(PeriodType.Tick, 1);
            Add(BarsPeriod.Id, BarsPeriod.Value * 3);
            Add(BarsPeriod.Id, BarsPeriod.Value * 9);

        }

       protected override void OnBarUpdate()
       {
          //trying to access public members of ct causing exception....
       }
   }

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #3 (permalink)
maryfromcolorado
Denver CO USA
 
Posts: 32 since Sep 2016
Thanks Given: 0
Thanks Received: 13


Its telling you it doesn't know which one to instantiate as it is found in two dlls. Change the class name and then SaveAs this new class name, to keep it consistent. Then instantiate that new one.


namespace NinjaTrader.Indicator
{
/// <summary>
/// Calculates the range of a bar.
/// </summary>
[Description("Calculates the range of a bar.")]
public class MyStochasticIndicator: : Indicator

Note: You could also find the duplicate indicator and give it a different class name, or delete it. You would just need to make sure you have the right version. At some point you did save as, but didn't change the class name. NT compiles all indicators and strategies together. hence two with the same class name will cause issues. But I think giving it a real name is the best solution and then when you are sure delete the old ones with the generic name.
Cheers,
Mary

Reply With Quote
Thanked by:
  #4 (permalink)
ahadari28
Tel Aviv
 
Posts: 2 since Sep 2016
Thanks Given: 2
Thanks Received: 0

Hey rleplae, thanks for replying

I'm actually also from israel, but I'll respect the other members of the forum who might look into this thread,
And keep the discussion in english.

rleplae, creating an instance of the indicator from the strategy's Initialize didn't change the fact
that.

After going through ninja's source code I realized that I shouldn't instantiate an indicator withing a strategy with the
new keyword as if it was vanilla c#, but to use the auto generated method that bares the name of the class (indicator) that Returns the same type, sort of like their idea of a constructor, which still doesn't call OnBarUpdate btw,
But I've solved that with an explicit call to indicator.OnBarUpdate().

Mary, thanks for replying, if I understand you correctly, you mean that I've created two source file for the
Same indicator? I find it hard to believe, it doesn't show as a duplicate indicator anywhere on my custom indicator folder's files, I'm thinking maybe building the project from visual studio and then compiling an empty indicator (to make sure ninjatrader compiles on itself, I've read somewhere that this is a must, since ninja will not be aware of any changes
made to it's indicators or strategy's outside its ninjascript editor).
But I'll give it a go, If I change the name of the indicator and this error will not repeat itself then
You are right, otherwise - I'll have to find out where is the duplicate indicator hiding..

Reply With Quote
  #5 (permalink)
maryfromcolorado
Denver CO USA
 
Posts: 32 since Sep 2016
Thanks Given: 0
Thanks Received: 13

Thats because it doesn't determine duplicates by filename. Just by the class name in the indicator file. Thats where the duplicate is. Always name the class, the same as the file and there won't be an issue.

Shalom,
Mary

Reply With Quote




Last Updated on October 30, 2016


© 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