Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Question on "bars since entry" for multi timeframe strategy

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

    Question on "bars since entry" for multi timeframe strategy

    Hi. I'm working with a multi timeframe strategy, with the same symbol being used for each data series. I'm struggling to get the exit (it's an ExitLong() based on a condition), based on the daily data, to work. It seems on a few trades, the entry bar (added series) is signaling the sell signal, even though i'm using (BarsSinceEntry(1,"",0)>=2. The behavior I expect is that at earliest, the bar after the entry bar will be checked for the exit condition (again, referring to the added series for this). The primary series is a five minute bar and is used primarily for the entry. The one added is daily data.

    I attached a chart of one of the trades returned to show what I mean. My code is below. Any ideas, gurus? Thanks in advance...

    Code:
    protected override void OnBarUpdate()
    {
        if(CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired){return;}
        else
        {
            int countOf20EmaOver50Ema=0;
            int countOfClosesOver50Ema=0;
            int countOfLowsUnder50Ema=0;
            
            for(int i=0;i<=29;i++)
            {
                if(EMA(Closes[1],20)[i]>EMA(Closes[1],50)[i]){countOf20EmaOver50Ema++;}
                if(Closes[1][i]>=EMA(Closes[1],50)[i]){countOfClosesOver50Ema++;}
                if(Lows[1][i]<EMA(Closes[1],50)[i]){countOfLowsUnder50Ema++;}
            }
            
            if(countOf20EmaOver50Ema==30 && countOfClosesOver50Ema>=27 && 
                (countOfLowsUnder50Ema>=1 && countOfLowsUnder50Ema <= 5) &&
                (Lows[1][0]<EMA(Closes[1],50)[0] && Closes[1][0]>=EMA(Closes[1],50)[0])&&
                (Bars.FirstBarOfSession)&&
                (Close[0]>Lows[1][1]))    
                
            {
                EnterLong();    
            }
            
            //Exit if:
            if(BarsInProgress==1)
            {
                if(BarsSinceEntry(1,"",0)>=2&&Closes[1][0]<EMA(Closes[1],50)[0])
                {
                    ExitLong();    
                }
            }
        }
    }
    Attached Files
    Last edited by CSharpTrader; 10-15-2012, 02:28 PM.

    #2
    CSharpTrader,

    The way the backtesting engine works here is that it would calculate the BarsSinceEntry() based on where the signal occurred. So in this case, on the bar you showed the exit condition it would have BarsSinceEntry = 2 and thus it is exiting your trade. If you want to ensure 2 bars passed I would suggest trying to use "BarsSinceEntry > 2".
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Thanks Adam. Can you clarify a bit for me? I still don't get why an entry bar would be BarsSinceEntry #2. It's the entry itself. I'd expect 0 or even 1. What am I missing?

      Comment


        #4
        Actually I think I get it, it's just a little counter intuitive. But could you confirm this... it counts the bar that generated the signal... so it's actually counting the bar of entry, and the one before it, that generated the signal.. is that correct? Just my $.02, if this is right, taken literally, the name of the method implies it would count all bars after the entry bar.

        Comment


          #5
          Adam, a little more info here. I made the change you suggested, and I get the same results. Here's the new exit condition I tried:

          Code:
          //Exit if:
          if(BarsInProgress==1)
          {
              if(BarsSinceEntry(1,"",0)>2&&Closes[1][0]<EMA(Closes[1],50)[0])
              {
                  ExitLong();    
              }
          }
          And the trade shown on my image attachment is still there.

          Comment


            #6
            Originally posted by CSharpTrader View Post
            Adam, a little more info here. I made the change you suggested, and I get the same results. Here's the new exit condition I tried:

            Code:
            //Exit if:
            if(BarsInProgress==1)
            {
                if(BarsSinceEntry(1,"",0)>2&&Closes[1][0]<EMA(Closes[1],50)[0])
                {
                    ExitLong();    
                }
            }
            And the trade shown on my image attachment is still there.
            The problem is that your entry is being submitted against the primary barSeries, so your exit will be evaluated against it. If you want your entry to be submitted against your secondary barSeries, then you have to filter the entry to be matched against that barSeries.

            It does not matter that your entry conditions are evaluated against your secondary barSeries; the actual entry is being made on the tick update from the primary barSeries, because of the way that you have coded it.

            Comment


              #7
              Thanks for the response... do you have an instruction, or reference, on how I do this?

              Comment


                #8
                Originally posted by CSharpTrader View Post
                Thanks for the response... do you have an instruction, or reference, on how I do this?
                Just the standard way, using either a switch statement or an if statement.

                Code:
                if (BarsInProgress == 0)
                {
                // do stuff here that needs to be done against the primary barSeries
                }
                
                if (BarsInProgress == 1)
                {
                // do stuff here that needs to be done against the secondary barSeries
                }

                Comment


                  #9
                  This is totally new to me, so I appreciate the patience (seems like this aspect should be documented btw) So... you're saying the EnterLong() should be within the switch cond. for series 1 (the added series)?

                  Comment


                    #10
                    Originally posted by CSharpTrader View Post
                    This is totally new to me, so I appreciate the patience (seems like this aspect should be documented btw) So... you're saying the EnterLong() should be within the switch cond. for series 1 (the added series)?
                    Actually it is very well documented.

                    ref:



                    Those links answer your question better than I could.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    663 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    376 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    110 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    575 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    580 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X