Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Strategy Indicator on Different Calculate Method then Strategy does not work

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

    Strategy Indicator on Different Calculate Method then Strategy does not work

    When using a different Calculate method for an indicator in your strategy then what your strategy is running is not working. I uploaded a test that can duplicate my results. I made a copy of the EMA and set its
    PHP Code:
    (State ==State.HistoricalCalculate =Calculate.OnBarClose
    I set my strategy to Calculate =Calculate.OnEachTick.

    As you can see from the below print statements, the prints from the indicator are calculating correctly on the bar close but the strategy does not see these results unless the strategy is using the same Calculate method as the indicator. The results for the emas from the strategies perspective is ZERO.

    The strategy works perfectly well using historical data (for obvious reasons that it only calculates OnBarClose) but what I mean is that it enters orders in historical but doesn't enter orders in Real Time, meaning how I am calling the indicator to get its values is correct.

    <REALTIME>
    From Indicator- Calculate.OnBarClose1 Min Heiken-Ashi 10:29:00 PM Current Bar #: 6087 EMA: 1.13698849230083
    From Strategy - Calculate.OnBarClose 1 Min Heiken-Ashi 10:29:00 PM Current Bar #: 6087 ema_a: 1.13698849230083
    From Indicator- Calculate.OnBarClose1 Min Heiken-Ashi 10:29:00 PM Current Bar #: 6087 EMA: 1.13697405473776
    From Strategy - Calculate.OnBarClose1 Min Heiken-Ashi 10:29:00 PM Current Bar #: 6087 ema_b: 1.13697405473776
    From Indicator- Calculate.OnBarClose1 Min Heiken-Ashi 10:30:00 PM Current Bar #: 6088 EMA: 1.13698949743361
    From Strategy - Calculate.OnBarClose 1 Min Heiken-Ashi 10:30:00 PM Current Bar #: 6088 ema_a: 1.13698949743361
    From Indicator- Calculate.OnBarClose1 Min Heiken-Ashi 10:30:00 PM Current Bar #: 6088 EMA: 1.13697936982517
    From Strategy - Calculate.OnBarClose1 Min Heiken-Ashi 10:30:00 PM Current Bar #: 6088 ema_b: 1.13697936982517
    <REALTIME>
    From Strategy - Calculate.OnEachTick1 Min Heiken-Ashi 10:33:00 PM Current Bar #: 6091 ema_b: 0
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 10:33:00 PM Current Bar #: 6091 ema_a: 0
    From Strategy - Calculate.OnEachTick1 Min Heiken-Ashi 10:33:00 PM Current Bar #: 6091 ema_b: 0
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 10:33:00 PM Current Bar #: 6091 ema_a: 0
    From Strategy - Calculate.OnEachTick1 Min Heiken-Ashi 10:33:00 PM Current Bar #: 6091 ema_b: 0
    From Indicator- Calculate.OnBarClose1 Min Heiken-Ashi 10:33:00 PM Current Bar #: 6091 EMA: 1.13702627768273
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 10:34:00 PM Current Bar #: 6092 ema_a: 0
    From Indicator- Calculate.OnBarClose1 Min Heiken-Ashi 10:33:00 PM Current Bar #: 6091 EMA: 1.13700722068894
    From Strategy - Calculate.OnEachTick1 Min Heiken-Ashi 10:34:00 PM Current Bar #: 6092 ema_b: 0
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 10:34:00 PM Current Bar #: 6092 ema_a: 0
    From Strategy - Calculate.OnEachTick1 Min Heiken-Ashi 10:34:00 PM Current Bar #: 6092 ema_b: 0
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 10:34:00 PM Current Bar #: 6092 ema_a: 0
    <REALTIME>
    From Indicator- Calculate.OnEachTick1 Min Heiken-Ashi 10:39:00 PM Current Bar #: 6097 EMA: 1.13694239544264
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 10:39:00 PM Current Bar #: 6097 ema_a: 1.13694239544264
    From Indicator- Calculate.OnEachTick1 Min Heiken-Ashi 10:39:00 PM Current Bar #: 6097 EMA: 1.13697587396995
    From Strategy - Calculate.OnEachTick1 Min Heiken-Ashi 10:39:00 PM Current Bar #: 6097 ema_b: 1.13697587396995
    From Indicator- Calculate.OnEachTick1 Min Heiken-Ashi 10:39:00 PM Current Bar #: 6097 EMA: 1.1369490621093
    From Strategy - Calculate.OnEachTick 1 Min Heiken-Ashi 10:39:00 PM Current Bar #: 6097 ema_a: 1.1369490621093
    From Indicator- Calculate.OnEachTick1 Min Heiken-Ashi 10:39:00 PM Current Bar #: 6097 EMA: 1.13697920730328
    From Strategy - Calculate.OnEachTick1 Min Heiken-Ashi 10:39:00 PM Current Bar #: 6097 ema_b: 1.13697920730328




    Attached Files

    #2
    Hello cutzpr,

    Thanks for your post.

    I notice your are using BarsAgo 0 references in your script. This will show differences when calculating OnEachTick with your strategy and OnBarClose with the indicator.

    When using Calculate.OnEachTick, we constantly recalculate the developing bar (BarsAgo 0.) When Calculating.OnBarClose, we are not calculating the developing bar, but referencing that bar when the bar closes.

    If you would like to make OnBarClose referencing when using OnEachTick/OnPriceChange, you will have to check BarsAgo 1 of the opening tick of the next bar. This can be done by checking:

    if (IsFirstTickOfBar) Print(Indi[1]); // Print the value of the bar after it has closed.

    I've attached an example that can show using IsFirstTickOfBar with BarsAgo 1 references to mimic OnBarClose behavior with BarsAgo 0. This example also demonstrates accounting for calculating historical data with and without using Tick Replay where the NinjaScript may not be forced to calculate OnBarClose.

    Publicly available documentation on IsFirstTickOfBar can be found here - https://ninjatrader.com/support/help...ttickofbar.htm

    Please let me know if you have any additional questions.
    Attached Files
    JimNinjaTrader Customer Service

    Comment


      #3
      Jim,

      Thank you for the clarification. I thought since the indicator is Set to OnBarClose, that its last value it calculated would still be at position[0] until a new bar was formed. This makes sense now.

      Comment


        #4
        Jim,

        Thank you very much, that seems to work. But I have one question, how could I take advantage of IsRising and IsFalling using this method since I cant specify which bar to look at (by default it will look at the current [0])

        Would I be forced to do something like - if(a[1] > a[2] && a[2] > a[3]) while using usePrevBarsAgo

        Comment


          #5
          Hello cutzpr,

          Yes, you would have to use logical statements or a loop to check the Series from a specified number of bars ago.

          for example:

          if (Close[i] > Close[i+1]) where "i" is the bars ago you wanted to check or to see if you are rising at that bar.

          Let me know if there is anything else I can do to help.
          JimNinjaTrader Customer Service

          Comment


            #6
            Thanks Jim

            I will place this here just incase anyone else finds it helpful

            PHP Code:
            public bool zIsFalling(ISeries<doubleseries ,int startBar,int barsBack)
                        {

                            for(
            int i startBarbarsBacki++)
                            {
                                if(
            series[i] > series[i+1]) return false;
                            }

                            return 
            true;

                        }

            public 
            bool zIsRising(ISeries<doubleseries ,int startBar,int barsBack)
                        {

                            for(
            int i startBarbarsBacki++)
                            {
                                if(
            series[i] < series[i+1]) return false;
                            }
                            return 
            true;
                        } 

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by DanielTynera, Today, 01:14 AM
            0 responses
            2 views
            0 likes
            Last Post DanielTynera  
            Started by yertle, 04-18-2024, 08:38 AM
            9 responses
            40 views
            0 likes
            Last Post yertle
            by yertle
             
            Started by techgetgame, Yesterday, 11:42 PM
            0 responses
            12 views
            0 likes
            Last Post techgetgame  
            Started by sephichapdson, Yesterday, 11:36 PM
            0 responses
            2 views
            0 likes
            Last Post sephichapdson  
            Started by bortz, 11-06-2023, 08:04 AM
            47 responses
            1,615 views
            0 likes
            Last Post aligator  
            Working...
            X