Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

BarsSinceEntry

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

    BarsSinceEntry

    Hi Guys,

    Just playing around with a little code here, to help myself understand how to program in NT.

    The strategy works fine on a backtest until I add the BarsSinceEntry command. I added BarsSinceEntry b/c in the back test, it enters a long position just after getting stopped out.

    I'm coding something wrong, any idea what it is?

    Here is my code:

    protected override void OnBarUpdate()
    {
    if
    // Check that the bond pit is open
    (ToTime(Time[0]) > ToTime(8, 30, 0)
    && ToTime(Time[0]) < ToTime(14, 45, 0)

    // Check that current position in this strategy is flat
    && Position.MarketPosition == MarketPosition.Flat

    // Check for four consecutive down bars
    && Open[3] > Close[3]
    && Open[2] > Close[2]
    && Open[1] > Close[1]
    && Open[0] > Close[0])
    {
    // Check that we haven't entered a trade in the last two bars.
    if (BarsSinceEntry(0, "FourBarStrategy", 0) >2)

    // Buy!
    EnterLongLimit(Contracts, Close[0], "FourBarStrategy");

    #2
    Hi heyligerb,

    The reason your code doesn't work is because on the very first condition it is never > 2. You never entered before. Instead you should use BarsSinceExit().

    Code:
    if (BarsSinceExit() > 2 || BarsSinceExit() == -1)
         EnterLong();
    You only need to use the syntaxing you chose if you are in a multi-series strategy.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Great! That works.

      One other quick question...

      The statement "Position.MarketPosition == MarketPosition.Flat"

      Will that check the market position for the strategy, or across all positions?

      I might have another trade on, for the same instrument, but I only want to hold off entering a new position in this strategy, only if, the same strategy has a current position. Not if a different strategy, or a manual trade has a position.

      Brian

      Comment


        #4
        It is for the strategy position only. It does not know what other strategies are doing or what manual trades are doing.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          What would you use on an indicator since BarsSinceExit/Entry don't apply to indicators?

          Comment


            #6
            What are you trying to do? An indicator does not know anything about strategy positions, entries, or exits. If you are trying to do something based off of trades you need to do it in a strategy.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Just trying to make an alert similar to the strategy...

              Four consecutive down bars would print/play what I tell it to.

              I have that part working, but if I get a fifth bar, it print/plays again. I only want it to to it on the first four bars.

              Brian

              Comment


                #8
                Brian,

                You would have to do it over in the strategy. You can fire off alerts from there.
                Josh P.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by cmtjoancolmenero, Yesterday, 03:58 PM
                8 responses
                31 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by helpwanted, Today, 03:06 AM
                2 responses
                21 views
                0 likes
                Last Post NinjaTrader_LuisH  
                Started by DayTradingDEMON, Today, 09:28 AM
                0 responses
                9 views
                0 likes
                Last Post DayTradingDEMON  
                Started by navyguy06, Today, 09:28 AM
                0 responses
                2 views
                0 likes
                Last Post navyguy06  
                Started by rjbtrade1, 11-30-2023, 04:38 PM
                2 responses
                77 views
                0 likes
                Last Post DavidHP
                by DavidHP
                 
                Working...
                X