Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Only One Entry per Minute

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

    Only One Entry per Minute

    Hello

    I'm developing an automated momentum-based strategy on a 1 minute candle chart with the CalculateOnBarClose parameter set on false. The problem I'm encountering is that if my Profit Target is reached within a few seconds and the buy/sell criteria are still valid, the system will trigger a second, third, etc order in a row. I'd like to add a simple parameter that allows the code to trigger ONLY ONE order per minute.

    How could this be achieved? Thanks a lot for any suggestions.

    #2
    laocoon, you could work with a bool flag here to prevent those reentries in the candle. You would set it if your entry triggers and check it off as part of your entry conditions. Then reset to allow a trade only on FirstTickOfBar. So with pseudocode something like the below logic :

    if (FirstTickOfBar)
    tradeFlag = true;

    if (myConditions && tradeFlag)
    EnterLong()
    tradeFlag = false;
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Thanks a lot for your reply Bertrand. The Bool Flag is definitely the way to go, I have a question regarding the nature of the one you suggest however: does "FirstTickOfBar" allow an entry ONLY on the first tick of the bar (as the name obviously implies)? In this case it wouldn't work for me because my signal can be triggered at any moment within the 60 second timespan. The crucial point is that once it has been triggered it should not be triggered again within the same bar.

      Thanks

      Comment


        #4
        Correct, that's the reason why you do the reset of the flag on FirstTickOfBar. This way the entry condition can fire anytime within the bar unfolding, but the flag would be set to false then once fired preventing a further entry in this specific bar.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Excellent, that's exactly what I was looking for.
          I'm testing the strategy in ATM mode right now and everything works smoothly, the only issue I still have is that although COBC is set to false in the code and in the "Strategies" window, the order is still only executed at the open of the new bar, ie much too late.
          Is there another element besides the COBC parameter that could be responsible for that?

          Thanks

          Here's the code:


          CalculateOnBarClose = false;

          protected override void OnBarUpdate()
          {

          if (CurrentBar < activeBar)
          {
          return;
          }
          else if (CurrentBar != activeBar)
          {


          if (FirstTickOfBar)
          tradeFlag = true;


          if (my Conditions here && tradeFlag)
          {
          DrawDiamond("" + CurrentBar, false, 0, Low[0]- 2*(TickSize), Color.Black);
          EnterLong();
          tradeFlag = false;
          atmStrategyId = GetAtmStrategyUniqueId();
          orderId = GetAtmStrategyUniqueId();
          AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Market,0,0, TimeInForce.Day, orderId, "MyStrat", atmStrategyId);



          }
          else if(my Conditions here && tradeFlag)
          {
          DrawDiamond("" + CurrentBar, false, 0, High[0]+ 2*(TickSize), Color.Black);
          EnterShort();
          tradeFlag = false;
          atmStrategyId = GetAtmStrategyUniqueId();
          orderId = GetAtmStrategyUniqueId();
          AtmStrategyCreate(Cbi.OrderAction.Sell, OrderType.Market, 0,0, TimeInForce.Day, orderId, "MyStrat", atmStrategyId);


          }

          activeBar = CurrentBar;
          }
          Last edited by laocoon; 03-21-2013, 05:51 AM.

          Comment


            #6
            Originally posted by laocoon View Post
            Excellent, that's exactly what I was looking for.
            I'm testing the strategy in ATM mode right now and everything works smoothly, the only issue I still have is that although COBC is set to false in the code and in the "Strategies" window, the order is still only executed at the open of the new bar, ie much too late.
            Is there another element besides the COBC parameter that could be responsible for that?

            Thanks

            Here's the code:


            CalculateOnBarClose = false;

            protected override void OnBarUpdate()
            {

            if (CurrentBar < activeBar)
            {
            return;
            }
            else if (CurrentBar != activeBar)
            {


            if (FirstTickOfBar)
            tradeFlag = true;


            if (my Conditions here && tradeFlag)
            {
            DrawDiamond("" + CurrentBar, false, 0, Low[0]- 2*(TickSize), Color.Black);
            EnterLong();
            tradeFlag = false;
            atmStrategyId = GetAtmStrategyUniqueId();
            orderId = GetAtmStrategyUniqueId();
            AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Market,0,0, TimeInForce.Day, orderId, "MyStrat", atmStrategyId);



            }
            else if(my Conditions here && tradeFlag)
            {
            DrawDiamond("" + CurrentBar, false, 0, High[0]+ 2*(TickSize), Color.Black);
            EnterShort();
            tradeFlag = false;
            atmStrategyId = GetAtmStrategyUniqueId();
            orderId = GetAtmStrategyUniqueId();
            AtmStrategyCreate(Cbi.OrderAction.Sell, OrderType.Market, 0,0, TimeInForce.Day, orderId, "MyStrat", atmStrategyId);


            }

            activeBar = CurrentBar;
            }
            Look at the 2 lines that I have emphasized in red, and you will see that you are ensuring that the block can only be entered once per bar, so your trade can only happen when the next bar starts.

            Comment


              #7
              Hello laocoon,

              That would because you are only having your entry conditions called when "CurrentBar != activeBar" which is only going to be on the first tick of the bar since "activeBar" is being set to "CurrentBar" at the end of your OnBarUpdate().

              You may want to change that condition so that it can be called intrabar if you wish for an intrabar fill.
              JCNinjaTrader Customer Service

              Comment


                #8
                Thanks a lot koganam and JC for your help, I'm currently correcting my mistake.

                Thanks again for your help, much appreciated.

                Comment


                  #9
                  One last (unrelated) thing: the code below triggers an ATM called "MyStrat" with a predefined Order Quantity of 1, but every signal automatically buys/sells 2 contracts. The Stop Profit / Stop Loss values are also executed for one contract, which means that once either one is hit, there's still a second lot in the market with no stops. How can that be?

                  Thanks

                  Comment


                    #10
                    Hello laocoon,

                    If your ATM Strategy is saved with an Order Quantity of 1, are both of your Conditions being hit so that you would have them both enter a position?
                    JCNinjaTrader Customer Service

                    Comment


                      #11
                      Thanks for your reply JC. I indeed have two condition sets (one for Longs, one for Shorts).
                      This can't be the cause of the problem though because the strategy triggers a two lot Long or a two lot Short position (instead of a one lot position). If both my conditions were hit at the same time, this would result in one Long and one Short.

                      Thanks

                      Comment


                        #12
                        Hello laocoon,

                        Over looked that you have EnterLong() and AtmStrategyCreate()

                        Code:
                        if (my Conditions here && tradeFlag)
                        {
                        DrawDiamond("" + CurrentBar, false, 0, Low[0]- 2*(TickSize), Color.Black);
                        [COLOR="Magenta"]EnterLong();[/COLOR]
                        tradeFlag = false; 
                        atmStrategyId = GetAtmStrategyUniqueId();
                        orderId = GetAtmStrategyUniqueId();
                        [COLOR="Magenta"]AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Market,0,0, TimeInForce.Day, orderId, "MyStrat", atmStrategyId);[/COLOR]
                        So EnterLong() is entering without a Stop or Target and AtmStrategyCreate with the Stops and Targets.
                        JCNinjaTrader Customer Service

                        Comment


                          #13
                          Hello JC,

                          Thanks a lot for your reply. I should have seen that myself, but I overlooked it as well.

                          Thanks again.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Tim-c, Today, 02:10 PM
                          1 response
                          7 views
                          0 likes
                          Last Post NinjaTrader_ChelseaB  
                          Started by Taddypole, Today, 02:47 PM
                          0 responses
                          2 views
                          0 likes
                          Last Post Taddypole  
                          Started by chbruno, 04-24-2024, 04:10 PM
                          4 responses
                          50 views
                          0 likes
                          Last Post chbruno
                          by chbruno
                           
                          Started by TraderG23, 12-08-2023, 07:56 AM
                          10 responses
                          399 views
                          1 like
                          Last Post beobast
                          by beobast
                           
                          Started by lorem, Yesterday, 09:18 AM
                          5 responses
                          25 views
                          0 likes
                          Last Post NinjaTrader_ChelseaB  
                          Working...
                          X