Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Embedded indicators within a strategy - Calculate property

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Embedded indicators within a strategy - Calculate property

    NT Forum,

    As stated in the NT8 help guide (Calculate):
    Embedded indicators within a strategy should not use a Calculate property since it is already utilizing the Calculate property of the strategy.
    How should the calculate property of an indicator be written to allow for the indicator to be used in isolation and as an embedded indicator within a Strategy?
    Noting NT Forum - CalculateOnBarClose of an indicator w/in an indicator, should such an indicator only set its calculation mode after ascertaining if it was called by a Strategy (or Indicator)?

    By extension, where a Strategy calculates .OnPriceChange, is it possible to have an embedded indicator set its calculation property to be the same (i.e. .OnPriceChange). Thus, if the Strategy were changed to calculate .OnBarClose, the embedded indicator would change its calculation property to .OnBarClose.

    Regards
    Shannon
    Last edited by Shansen; 03-26-2016, 12:40 AM.

    #2
    Hello Shansen, and thank you for your question.

    I am providing a sample indicator which will not have its Calculate property set by default, but which can optionally have its property set if used independently. Setting its CalculationMode property to 1, 2, or 3 will set its Calculate property to OnBarUpdate, OnPriceChange, and OnEachTick, respectively.

    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class Example1489961 : Indicator
        {
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                            = @"Enter the description for your new custom Indicator here.";
                    Name                                = "Example1489961";
                    //Calculate                            = Calculate.OnBarClose;
                    switch(CalculationMode)
                    {
                        case 1 :
                            Calculate = Calculate.OnBarClose;
                            break;
                        case 2 :
                            Calculate = Calculate.OnPriceChange;
                            break;
                        case 3 :
                            Calculate = Calculate.OnEachTick;
                            break;
                    }
                    IsOverlay                            = false;
                    DisplayInDataBox                    = true;
                    DrawOnPricePanel                    = true;
                    DrawHorizontalGridLines                = true;
                    DrawVerticalGridLines                = true;
                    PaintPriceMarkers                    = true;
                    ScaleJustification                    = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event. 
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive            = true;
                    CalculationMode                    = 1;
                }
                else if (State == State.Configure)
                {
                }
            }
    
            protected override void OnBarUpdate()
            {
                Print("Indicator " + Name + " has a calculation mode " + Calculate);
            }
    
            #region Properties
            [Range(0, int.MaxValue)]
            [NinjaScriptProperty]
            [Display (
                    Name="CalculationMode",
                    Description="Allows setting calculation mode externally - 1 = Bar Close, 2 = Price Change, 3 = Each Tick",
                    Order=1,
                    GroupName="Parameters"
                    )]
            public int CalculationMode
            { get; set; }
    
            [NinjaScriptProperty]
             [Display(Name="Name", Description="Lets you identify this indicator  in nested print statements", Order=1, GroupName="Parameters")]
            public string Name
            { get; set; }
            #endregion
    
        }
    }
    Please let us know if there is any other way we can help.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Jesse,

      Thanks for sending this script through.

      As I understand it,
      1) when used in isolation the indicator Example1489961 takes the setting for NinjaScriptBase.Calculate from the Indictators Properties window : Calculate parameter. Thus, the CalculationMode parameter in the Indicators Properties window is ignored.
      2) when used in conjunction with a Strategy (example below), the indicator Example1489961 takes the setting for NinjaScriptBase.Calculate from the indicator constructor in the Strategy.

      Code:
      public class Strategy1489961 : Strategy
      {
        private Example1489961 _example1489961;
        
        protected override void OnStateChange()
        {
          if (State == State.SetDefaults)
      	{
      	Description							= @"Enter the description for your new custom Strategy here.";
      	Name								= "Strategy1489961";
      	Calculate							= Calculate.OnBarClose;
      	EntriesPerDirection					= 1;
      	EntryHandling						= EntryHandling.AllEntries;
      	IsExitOnSessionCloseStrategy		= true;
      	ExitOnSessionCloseSeconds			= 30;
      	IsFillLimitOnTouch					= false;
      	MaximumBarsLookBack					= MaximumBarsLookBack.TwoHundredFiftySix;
      	OrderFillResolution					= OrderFillResolution.Standard;
      	Slippage							= 0;
      	StartBehavior						= StartBehavior.WaitUntilFlat;
      	TimeInForce							= TimeInForce.Gtc;
      	TraceOrders							= false;
      	RealtimeErrorHandling				= RealtimeErrorHandling.StopCancelClose;
      	StopTargetHandling					= StopTargetHandling.PerEntryExecution;
      	BarsRequiredToTrade					= 20;
          }
          else if (State == State.Configure)
          {
            _example1489961 = Example1489961(2,"example");
          }
        }
      
        protected override void OnBarUpdate()
        {
          _example1489961.Update();
        }
      }
      As it stands:
      - as a standalone Indicator, the CalculationMode input can be set but has no effect.
      - as part of a Strategy, the indicator constructor requires 1,2, or 3 to be set in the constructor. So, while set in the Strategy, it is still possible for the Indicator Calculate value to differ from the Strategy Calculate value.

      Is there a better solution? Is it possible to have the Strategy Calculate variable as the only argument in the Indicator constructor? Is it possible for the Indicator when used standalone to "test" whether this argument is null and thus revert to the usual process of setting the Calculate value?

      Alternatively, is there an approach similar to Mathew's approach in the post NT8 (B6) : Strategy 'OnStateChange' Object reference error?

      Again, thank you for your help
      Shannnon

      Comment


        #4
        Originally posted by Shansen View Post
        Jesse,

        Thanks for sending this script through.

        As I understand it,
        1) when used in isolation the indicator Example1489961 takes the setting for NinjaScriptBase.Calculate from the Indictators Properties window : Calculate parameter. Thus, the CalculationMode parameter in the Indicators Properties window is ignored.
        2) when used in conjunction with a Strategy (example below), the indicator Example1489961 takes the setting for NinjaScriptBase.Calculate from the indicator constructor in the Strategy.

        Code:
        public class Strategy1489961 : Strategy
        {
          private Example1489961 _example1489961;
          
          protected override void OnStateChange()
          {
            if (State == State.SetDefaults)
        	{
        	Description							= @"Enter the description for your new custom Strategy here.";
        	Name								= "Strategy1489961";
        	Calculate							= Calculate.OnBarClose;
        	EntriesPerDirection					= 1;
        	EntryHandling						= EntryHandling.AllEntries;
        	IsExitOnSessionCloseStrategy		= true;
        	ExitOnSessionCloseSeconds			= 30;
        	IsFillLimitOnTouch					= false;
        	MaximumBarsLookBack					= MaximumBarsLookBack.TwoHundredFiftySix;
        	OrderFillResolution					= OrderFillResolution.Standard;
        	Slippage							= 0;
        	StartBehavior						= StartBehavior.WaitUntilFlat;
        	TimeInForce							= TimeInForce.Gtc;
        	TraceOrders							= false;
        	RealtimeErrorHandling				= RealtimeErrorHandling.StopCancelClose;
        	StopTargetHandling					= StopTargetHandling.PerEntryExecution;
        	BarsRequiredToTrade					= 20;
            }
            else if (State == State.Configure)
            {
              _example1489961 = Example1489961(2,"example");
            }
          }
        
          protected override void OnBarUpdate()
          {
            _example1489961.Update();
          }
        }
        As it stands:
        - as a standalone Indicator, the CalculationMode input can be set but has no effect.
        - as part of a Strategy, the indicator constructor requires 1,2, or 3 to be set in the constructor. So, while set in the Strategy, it is still possible for the Indicator Calculate value to differ from the Strategy Calculate value.

        Is there a better solution? Is it possible to have the Strategy Calculate variable as the only argument in the Indicator constructor? Is it possible for the Indicator when used standalone to "test" whether this argument is null and thus revert to the usual process of setting the Calculate value?

        Alternatively, is there an approach similar to Mathew's approach in the post NT8 (B6) : Strategy 'OnStateChange' Object reference error?

        Again, thank you for your help
        Shannnon
        Calculate is a property of the indicator. Just set it to what you want it to be. Standard OOP.

        Comment


          #5
          Koganam,

          Brilliant! To confirm, within the Strategy, would you simply include the line (as below) setting the Indicator Calculate Property equal to the Strategy Calculate value?

          Code:
          private Example1489961 _example1489961;
                  
          protected override void OnStateChange()
          {
            ...			
            else if (State == State.Configure)
            {
               _example1489961 = Example1489961(<<parameters>>);
          [B]     _example1489961.Calculate = Calculate;[/B]
            }
          }
          Again, thanks
          Shannon

          Comment


            #6
            Originally posted by Shansen View Post
            Koganam,

            Brilliant! To confirm, within the Strategy, would you simply include the line (as below) setting the Indicator Calculate Property equal to the Strategy Calculate value?

            Code:
            private Example1489961 _example1489961;
                    
            protected override void OnStateChange()
            {
              ...			
              else if (State == State.Configure)
              {
                 _example1489961 = Example1489961(<<parameters>>);
            [B]     _example1489961.Calculate = Calculate;[/B]
              }
            }
            Again, thanks
            Shannon
            That should do it, though I would probably tie it down with:
            Code:
            _example1489961.Calculate = this.Calculate;
            I have also found that sometimes things do not quite work in State.Config, so I move them to State.DataLoaded or even OnBarUpdate().

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by bortz, 11-06-2023, 08:04 AM
            47 responses
            1,607 views
            0 likes
            Last Post aligator  
            Started by jaybedreamin, Today, 05:56 PM
            0 responses
            9 views
            0 likes
            Last Post jaybedreamin  
            Started by DJ888, 04-16-2024, 06:09 PM
            6 responses
            19 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by Jon17, Today, 04:33 PM
            0 responses
            6 views
            0 likes
            Last Post Jon17
            by Jon17
             
            Started by Javierw.ok, Today, 04:12 PM
            0 responses
            15 views
            0 likes
            Last Post Javierw.ok  
            Working...
            X