Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

if(Historical) returns true on last 5 min bar

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

    if(Historical) returns true on last 5 min bar

    Hello. I'm trying to work some issues out of running a multi- series indicator in strategy analyzer to generate alerts. I've been getting unpredictable results, and the good suggestion was made to me to use if(Historical){return;} to avoid the possibility of alerts coming in from past data bars (where code logic no longer would trigger them on latest bars).

    I did this, and some alerts come in seeming accurate, and a few show technical conditions that shouldn't pass my logic. I added to my historical check:

    Code:
    if(Historical)
                {
                    if(Time[0].Date == DateTime.Today)
                    {
                        Print("Historical data: " + Time[0]);
                    }
                    
                    return;
                }
    and at 3:01, there were many 3:00 bars from today, then the 3:05s, etc. Any thoughts? Part of my question is why might alerts come through that shouldn't pass the logic on the latest data, so the whole indicator is below. Big thanks in advance.

    Code:
    protected override void Initialize()
            {
                Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
                Overlay    = false;
                Add(PeriodType.Day,1);
            }
            
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
    
            //2
            protected override void OnBarUpdate()
            {
                if(Historical)
                {
                    if(Time[0].Date == DateTime.Today)
                    {
                        Print("Historical data: " + Time[0]);
                    }
                    
                    return;
                }
                
                for (int index = 0; index < BarsArray.Length; index++) 
                {
                    if (CurrentBars[index] < (slowMA+12)) return;
                }
                
                if(BarsInProgress == 0)
                {
                    int retraceType = 0;
                    
                    //Is current intraday action favorable?
                    if((Close[0] > Open[0]) && (EMA(5)[0] > EMA(10)[0]))
                    {
                        int uptrendCheckCount = 0;
                        int countOfClosesOverSlowEma = 0;
                        double todaysEstimatedDailySlowMovAvg = (EMA(Closes[1],slowMA)[0] + Math.Abs(EMA(Closes[1],slowMA)[0] - EMA(Closes[1],slowMA)[1]));
                        double keyPriceViolationLevelPriorDay = EMA(Closes[1],slowMA)[0] * 1.025;
                        double keyPriceViolationLevelToday = todaysEstimatedDailySlowMovAvg * 1.025;
                        
                        //Make sure current price is > today's estimated slow daily MA.
                        if(Close[0] > todaysEstimatedDailySlowMovAvg)
                        {
                            //Check for uptrend on daily
                            for(int i = 0;i < 10;i++)
                            {
                                //Count up mov avg lines are stacked daily bars
                                if((EMA(Closes[1],fastMA)[i] >= EMA(Closes[1],medMA)[i]) && (EMA(Closes[1],medMA)[i] >= EMA(Closes[1],slowMA)[i]))
                                    uptrendCheckCount++;
                                
                                //Count up past closes >= slow ma line-small buffer (.1%) daily bars
                                if(Closes[1][i] >= EMA(Closes[1],slowMA)[i] * .999)
                                    countOfClosesOverSlowEma++;
                            }
                            
                            if((countOfClosesOverSlowEma == 10) && (uptrendCheckCount == 10))    //if it's an uptrend
                            {
                                //if close yesterday violated level, or 
                                if(Closes[1][0] <= keyPriceViolationLevelPriorDay) retraceType = 1;
                                
                                //if today's price level has been violated,
                                else if(Low[LowestBar(Low,Bars.BarsSinceSession-1)] < keyPriceViolationLevelToday) retraceType = 2;
                            }
                        }
                    }
                    Plot0.Set(retraceType);
                }
            }

    #2
    Originally posted by CSharpTrader View Post
    Hello. I'm trying to work some issues out of running a multi- series indicator in strategy analyzer to generate alerts. I've been getting unpredictable results, and the good suggestion was made to me to use if(Historical){return;} to avoid the possibility of alerts coming in from past data bars (where code logic no longer would trigger them on latest bars).

    I did this, and some alerts come in seeming accurate, and a few show technical conditions that shouldn't pass my logic. I added to my historical check:

    Code:
    if(Historical)
                {
                    if(Time[0].Date == DateTime.Today)
                    {
                        Print("Historical data: " + Time[0]);
                    }
     
                    return;
                }
    and at 3:01, there were many 3:00 bars from today, then the 3:05s, etc. Any thoughts? Part of my question is why might alerts come through that shouldn't pass the logic on the latest data, so the whole indicator is below. Big thanks in advance.

    Code:
    protected override void Initialize()
            {
                Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
                Overlay    = false;
                Add(PeriodType.Day,1);
            }
     
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
     
            //2
            protected override void OnBarUpdate()
            {
                if(Historical)
                {
                    if(Time[0].Date == DateTime.Today)
                    {
                        Print("Historical data: " + Time[0]);
                    }
     
                    return;
                }
     
                for (int index = 0; index < BarsArray.Length; index++) 
                {
                    if (CurrentBars[index] < (slowMA+12)) return;
                }
     
                if(BarsInProgress == 0)
                {
                    int retraceType = 0;
     
                    //Is current intraday action favorable?
                    if((Close[0] > Open[0]) && (EMA(5)[0] > EMA(10)[0]))
                    {
                        int uptrendCheckCount = 0;
                        int countOfClosesOverSlowEma = 0;
                        double [COLOR=red][B]todaysEstimatedDailySlowMovAvg = (EMA(Closes[1],slowMA)[0] + Math.Abs(EMA(Closes[1],slowMA)[0] - EMA(Closes[1],slowMA)[1]));[/B][/COLOR]
                        double keyPriceViolationLevelPriorDay = EMA(Closes[1],slowMA)[0] * 1.025;
                        double keyPriceViolationLevelToday = todaysEstimatedDailySlowMovAvg * 1.025;
     
                        //Make sure current price is > today's estimated slow daily MA.
                        if(Close[0] > todaysEstimatedDailySlowMovAvg)
                        {
                            //Check for uptrend on daily
                            for(int i = 0;i < 10;i++)
                            {
                                //Count up mov avg lines are stacked daily bars
                                if((EMA(Closes[1],fastMA)[i] >= EMA(Closes[1],medMA)[i]) && (EMA(Closes[1],medMA)[i] >= EMA(Closes[1],slowMA)[i]))
                                    uptrendCheckCount++;
     
                                //Count up past closes >= slow ma line-small buffer (.1%) daily bars
                                if(Closes[1][i] >= EMA(Closes[1],slowMA)[i] * .999)
                                    countOfClosesOverSlowEma++;
                            }
     
                            if((countOfClosesOverSlowEma == 10) && (uptrendCheckCount == 10))    //if it's an uptrend
                            {
                                //if close yesterday violated level, or 
                                if(Closes[1][0] <= keyPriceViolationLevelPriorDay) retraceType = 1;
     
                                //if today's price level has been violated,
                                else if(Low[LowestBar(Low,Bars.BarsSinceSession-1)] < keyPriceViolationLevelToday) retraceType = 2;
                            }
                        }
                    }
                    Plot0.Set(retraceType);
                }
            }
    You may be having a logic problem. The line that I have highlighted in red does not seem kosher to calculate any kind of moving average.

    Comment


      #3
      That's just a double value. An estimated mov avg for today, based on the daily data series, since we don't really have it yet. What's not kosher? Is the EMA syntax off somehow? The "if(Historical)" shouldn't pass, right from the start, for many of the prints I saw..

      Comment


        #4
        Originally posted by CSharpTrader View Post
        That's just a double value. An estimated mov avg for today, based on the daily data series, since we don't really have it yet. What's not kosher? Is the EMA syntax off somehow? The "if(Historical)" shouldn't pass, right from the start, for many of the prints I saw..
        So what are you seeking then? Your Historical filter says to Print for all bars that are historical that occurred today. That should return a Print for every bar that closed today before the time that you loaded the indicator. Is that not what you are seeking?

        That calculation for the estimated average is persistently above the previous high, regardless of whether price increased or decreased. It will, therefore, probably be consistently too high. Consequently, it will not be a valid calculation of any moving average, and could logically be throwing off what you are expecting to see, if your expectations are gated by the value of that moving average.

        Comment


          #5
          @koganam:

          The approximated mov avg is always higher than the prior mov avg that it is calculated from. I'm looking for an intraday (5 min bar) retracement to it. I'm not following your conclusions.

          The point of printing historical bar times was to see that the latest bars closing were returning "true" as I mentioned..

          Comment


            #6
            Originally posted by CSharpTrader View Post
            @koganam:

            The approximated mov avg is always higher than the prior mov avg that it is calculated from. I'm looking for an intraday (5 min bar) retracement to it. I'm not following your conclusions.
            Well, I cannot explain it any better, unfortunately. When I calculate an average, I expect mathematical validity. If you are satisfied with the logic, then that is all that there is to it, but according to your logic, even if the price were to decline everyday, your average would continue to go up?

            The point of printing historical bar times was to see that the latest bars closing were returning "true" as I mentioned..
            How is that not acting correctly then? A bar becomes historical once it closes. What are you expecting to see that is not consistent with that?

            Comment


              #7
              We're kind of off topic just a little, but... when a est mov avgs 10,20,50 are stacked in that order, the 50 is going up. Even if it was to flatten, i'm ok with the small "buffer" value that is included. I prefer it, actually, so its inclusion is deliberate.

              My main issue is determining how to reliably understand the alerts market analyzer is giving me. I'm hoping big time to get it up and running soon. Anyone more advanced than I that could take a whack at this, I'd be in their debt.

              Comment


                #8
                I missed the last part of your post, koganam. I might have misunderstood you earlier, then... isn't if(Historical) to return false on the latest bar to close? I took your earlier suggestion to add this after asking how to make sure only the last bar to close generates alerts. If any bar that closes is historical, when would my indicator logic be processed? *scratches head*

                Comment


                  #9
                  Originally posted by CSharpTrader View Post
                  We're kind of off topic just a little, but... when a est mov avgs 10,20,50 are stacked in that order, the 50 is going up. Even if it was to flatten, i'm ok with the small "buffer" value that is included. I prefer it, actually, so its inclusion is deliberate.

                  My main issue is determining how to reliably understand the alerts market analyzer is giving me. I'm hoping big time to get it up and running soon. Anyone more advanced than I that could take a whack at this, I'd be in their debt.
                  Could you maybe post some annotated pictures of what you are seeing, and what you expect to see that does not match?

                  Comment


                    #10
                    Thanks. I have to get alerts, pick out which are the kludges, then chart them, so I'll get images together when I can. If you have a thought on is historical, i might have enough to go on to trade tomorrow.

                    Comment


                      #11
                      PDF attached of charts

                      Here's an expected/ unexpected of what I was getting today, with a chart for each time interval the data series use. Hope it helps. Again, thanks for the time.
                      Attached Files

                      Comment


                        #12
                        CSharpTrader,

                        Alert functions such as Alert(), SendMail(), PlaySound() are already ignored on historical data.

                        They will only be called when the Historical property of the bar is false (i.e., a real- time bar).

                        If you're getting alerts where you're not expecting, it would be with the condition you're checking.

                        Where exactly in your code are you calling Alert()?
                        MatthewNinjaTrader Product Management

                        Comment


                          #13
                          Hi Matthew, i'm setting an alert condition in market analyzer.

                          Comment


                            #14
                            Originally posted by CSharpTrader View Post
                            I missed the last part of your post, koganam. I might have misunderstood you earlier, then... isn't if(Historical) to return false on the latest bar to close? I took your earlier suggestion to add this after asking how to make sure only the last bar to close generates alerts. If any bar that closes is historical, when would my indicator logic be processed? *scratches head*
                            The bar indexed as "0" is the only non-historical bar. So actions will only be triggered on the current bar: intrabar if COBC is false, and on close of the bar if COBC is true. If COBC is true, then when the next bar opens, it becomes realtime, and the bar that just closed (on which the last action occurred), is now historical.

                            Comment


                              #15
                              In my understanding, when a bar closes, another opens concurrently. So if when a new bar opens, the prior bar is historical, how can the bar indexed as 0 in my code be non- historical? I'm still not clear.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              576 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              334 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              101 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              553 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              551 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X