Instantiating custom indicator from strategy - NinjaTrader | futures.io
futures.io futures trading
 

Go Back   futures.io

> Futures Trading, News, Charts and Platforms > Platforms and Indicators > NinjaTrader


Instantiating custom indicator from strategy
Started:October 28th, 2016 (10:07 AM) by ahadari28 Views / Replies:113 / 4
Last Reply:October 30th, 2016 (05:43 PM) Attachments:0

Welcome to futures.io.

Welcome, Guest!

This forum was established to help traders (especially futures traders) by openly sharing indicators, strategies, methods, trading journals and discussing the psychology of trading.

We are fundamentally different than most other trading forums:
  • We work extremely hard to keep things positive on our forums.
  • We do not tolerate rude behavior, trolling, or vendor advertising in posts.
  • We firmly believe in openness and encourage sharing. The holy grail is within you, it is not something tangible you can download.
  • 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, and we will never resell your private information.

-- Big Mike
     

Reply
 
Thread Tools Search this Thread

Instantiating custom indicator from strategy

Old October 28th, 2016, 10:07 AM   #1 (permalink)
Trading Apprentice
Tel Aviv
 
Futures Experience: None
Platform: NinjaTrader
Favorite Futures: es
 
Posts: 2 since Sep 2016
Thanks: 2 given, 0 received

Instantiating custom indicator from strategy

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
     

Old October 28th, 2016, 12:33 PM   #2 (permalink)
Elite Member
Omer עומר / Israel י
 
Futures Experience: Master
Platform: NinjaTrader, Proprietary,
Broker/Data: Ninjabrokerage/IQfeed + Synthetic datafeed
Favorite Futures: 6A, 6B, 6C, 6E, 6J, 6S, ES, NQ, YM, CL, ZB, ZN, ZC, ZS, GC
 
rleplae's Avatar
 
Posts: 1,560 since Sep 2013
Thanks: 1,132 given, 2,367 received
Forum Reputation: Legendary

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....
       }
   }

Reply With Quote
     
The following user says Thank You to rleplae for this post:
     

Old October 30th, 2016, 02:40 PM   #3 (permalink)
Trading Apprentice
Denver CO USA
 
Futures Experience: None
Platform: NT, JForex, Mql4 & 5
Broker/Data: FXCM
Favorite Futures: Forex
 
Posts: 23 since Sep 2016
Thanks: 0 given, 9 received


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


Last edited by maryfromcolorado; October 30th, 2016 at 02:46 PM. Reason: Another solution
Reply With Quote
     
The following 2 users say Thank You to maryfromcolorado for this post:
     

Old October 30th, 2016, 03:57 PM   #4 (permalink)
Trading Apprentice
Tel Aviv
 
Futures Experience: None
Platform: NinjaTrader
Favorite Futures: es
 
Posts: 2 since Sep 2016
Thanks: 2 given, 0 received

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
     

Old October 30th, 2016, 05:43 PM   #5 (permalink)
Trading Apprentice
Denver CO USA
 
Futures Experience: None
Platform: NT, JForex, Mql4 & 5
Broker/Data: FXCM
Favorite Futures: Forex
 
Posts: 23 since Sep 2016
Thanks: 0 given, 9 received

Futures Edge on FIO
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
     

Reply



futures.io > Futures Trading, News, Charts and Platforms > Platforms and Indicators > NinjaTrader > Instantiating custom indicator from strategy

Thread Tools Search this Thread
Search this Thread:

Advanced Search



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

Al Brooks: Stop Losing when a Good Trade goes Bad, Correcting Mistakes

Elite only
 

Trading Technologies: Algo Design Lab hands-on

Dec 13

Normal webinar schedule resumes after the holidays

January
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom Strategy not compiling. Solar39 NinjaTrader 3 June 13th, 2016 10:18 PM
Dynamically instantiating an Indicator gomi The Elite Circle 31 April 18th, 2016 07:10 PM
Help with creating a strategy from custom indicator sra18376 NinjaTrader 1 July 5th, 2014 11:01 PM
Custom NT indicator has stopped auto-loading with strategy, cache? drwylde NinjaTrader 1 July 9th, 2011 09:11 PM


All times are GMT -4. The time now is 06:03 PM.

Copyright © 2016 by 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 2016-12-08 in 0.09 seconds with 19 queries on phoenix via your IP 54.159.129.152