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

CrossAbove/Below strange results

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

    CrossAbove/Below strange results

    Hello,

    I am working on an multi-timeframe indicator, and part of its calculation involves looking for values within a dynamic time range given by the current trend as defined by a double-crossover method.

    Here is part of the code:

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0) // Logic applies to primary series only
    return;

    IDataSeries dataSeries = BarsArray[Series -
    1];
    IDataSeries Indicator = RSI(Period,
    1); // *** CUSTOMISABLE ***

    if (CurrentBar - 1 < Slow
    || Indicator.IsValidPlot(CurrentBar -
    1) == false
    || CurrentBars[1] < 0) // Sufficient data checks
    return;

    if (CrossAbove(EMA(dataSeries, Fast), EMA(dataSeries, Slow), 1)
    || CrossBelow(EMA(dataSeries, Fast), EMA(dataSeries, Slow),
    1)) // Resets lookBack at the start of a new trend
    lookBack = 1;
    else
    lookBack++;

    However, the code is producing the wrong results when the primary data series is Daily and the secondary data series is Weekly...

    For some reason, whenever there is crossover based on the Weekly (secondary) series, I get a whole week of Daily (primary) bars where lookBack equals 1 (when values should have ranged from 1 to 5). It is only after week 2 when the Daily bars update correctly, with lookBack increasing on Daily intervals.

    Is there anything wrong in my code? Or could this be a bug?

    Thanks for your help,

    Stopped
    Last edited by Stopped; 07-06-2011, 10:50 AM.

    #2
    Stopped, this is expected. Since you are running all calculations on the secondary series, it should show a whole week of daily crossovers because the weekly series hasn't updated yet.
    AustinNinjaTrader Customer Service

    Comment


      #3
      Hi Austin,

      I am sorry but I disagree.

      I am talking about situations where the Weekly series has already confirmed a crossover (on a Friday close), but the Daily bars of the following week fail to capture that.

      Please find attached files related to a workspace and a number of NinjaScript files that I have created to illustrate this problem.

      Could you please run them at your end and then we can discuss the results?

      Thanks,

      Stopped
      Attached Files

      Comment


        #4
        Another file. See picture attached, thanks!
        Attached Files

        Comment


          #5
          Stopped, you are running all of your logic on the daily series (if BIP != 0 return), but checking for crossovers on the weekly series (dataSeries). Thus, for the entire first week, the results would point to the same exact crossover for five days in a row, resetting the lookBack every time OnBarUpdate() is called (every day).

          Then, the next week, the crossover isn't true anymore, so the counter correctly increases by 1 every day because you're still checking for the weekly crossover every day.

          I ran the test on TNA, but the results would be the same as ES ##-##.
          Attached Files
          AustinNinjaTrader Customer Service

          Comment


            #6
            Hi Austin,

            You are right, sorry I failed to see this.

            Can I ask you for a hint/tip about how you would solve this little problem?

            Thanks in advance!

            Stopped

            Comment


              #7
              Hello,

              This is Brett responding for austin.

              Anytime I work with Multi Time Frame I tend to set flags as this helps me conceptualize what I'm trying to do instead of accessing indicator values directly.

              Let me know if I can be of further assistance.

              Comment


                #8
                Hi Brett,

                As you say, I will need to rethink the way I tackle multi-timeframe problems... Using flags it is a good suggestion, thank you.

                In the meantime I have applied the following "patch" to my code. It is not pretty but it works!

                double prevFast = curFast;
                double prevSlow = curSlow;
                curFast = EMA(dataSeries, Fast)[
                0];
                curSlow = EMA(dataSeries, Slow)[
                0];

                if (prevFast != 0 && prevSlow != 0
                && (curFast > curSlow && prevFast <= prevSlow)
                || (curFast < curSlow && prevFast >= prevSlow))
                lookBack =
                1;
                else
                lookBack++;

                Thanks again to both of you for helping out with this problem.

                Stopped

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by CortexZenUSA, Today, 12:53 AM
                0 responses
                1 view
                0 likes
                Last Post CortexZenUSA  
                Started by CortexZenUSA, Today, 12:46 AM
                0 responses
                1 view
                0 likes
                Last Post CortexZenUSA  
                Started by usazencortex, Today, 12:43 AM
                0 responses
                5 views
                0 likes
                Last Post usazencortex  
                Started by sidlercom80, 10-28-2023, 08:49 AM
                168 responses
                2,266 views
                0 likes
                Last Post sidlercom80  
                Started by Barry Milan, Yesterday, 10:35 PM
                3 responses
                13 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Working...
                X