Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

no orders submitted when using BarsSinceEntry

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

    no orders submitted when using BarsSinceEntry

    Hi,

    When I use the condition BarsSinceEntry() no orders will submit. Without it, the strategy works fine but I like to avoid submit orders when there was already an order filled within 3 bars ago. I wonder what I do wrong or how I can achieve this (in maybe another way)?

    Thank you in advance.

    Here below is my condition:

    {

    // Condition set 2
    if (BarsSinceEntry() > 3
    && orderId.Length == 0
    && atmStrategyId.Length == 0
    && CrossBelow(Stochastics(PeriodD, PeriodK, Smooth).K, Stochastics(PeriodD, PeriodK, Smooth).D, 1)
    && Stochastics(PeriodD, PeriodK, Smooth).K[0] <= 25
    && ADX(ADXperiode)[0] <= ADXlevel)


    {
    atmStrategyId = GetAtmStrategyUniqueId();
    orderId = GetAtmStrategyUniqueId();
    AtmStrategyCreate(Cbi.OrderAction.Sell, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "Rood", atmStrategyId);
    }
    // Check for a pending entry order
    if (orderId.Length > 0)
    {
    string[] status = GetAtmStrategyEntryOrderStatus(orderId);

    // If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
    if (status.GetLength(0) > 0)
    {
    // Print out some information about the order to the output window
    Print("The entry order average fill price is: " + status[0]);
    Print("The entry order filled amount is: " + status[1]);
    Print("The entry order order state is: " + status[2]);

    // If the order state is terminal, reset the order id value
    if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
    orderId = string.Empty;
    }
    } // If the strategy has terminated reset the strategy id
    else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
    atmStrategyId = string.Empty;
    }

    #2
    Hi Toroso, welcome to our forums - I would not expect this combination to work (BarsSinceEntry with ATMStrategyCreate), however you could custom track your last entry (CurrentBar) in a variable and then compare against this saved variable in your entry conditions to ensure potential entries are your desired bars part from the script.

    Comment


      #3
      thank you for the reply.
      I doubt what you exactly mean (i'm not really a script goeroe yet)
      Can you give an example?

      I don't think you mean something like below or do you?

      {
      // Make sure this strategy does not execute against historical data
      if (Historical)
      return;
      if (CurrentBar < 3)
      return;
      {
      // Condition set 1

      Comment


        #4
        Hi Bertrand,
        I tried it today but it didn't work.
        So I think you mean something else, can you give a more specific example?

        Comment


          #5
          Hello Toroso,

          Using ATM Strategies inside of NinjaScript will only be placed in Real-Time so the if(Historical) can be left their.

          I believe what Bertrand was referring to is something like the following:

          Code:
          private int barEntryNumber 	= -1;
          
          protected override void OnBarUpdate()
          {
               if ( CurrentBar >= (CurrentBar - barEntryNumber ) )
               {
                     Print("Number of Bars Since ATM Strategy has been place is : "+(CurrentBar-barEntryNumber) );
               }    
          
               if ( /*Your Entry Condition*/ )
               {
                      // Enter Order
                      barEntryNumber = CurrentBar;
               }
          
          }
          JCNinjaTrader Customer Service

          Comment


            #6
            Hi,

            I implemented your suggestions as below but the result was that there were no entries at all.
            maybe I did something wrong or in the wrong place?


            private int barEntryNumber = -3;

            protected override void Initialize()
            {

            CalculateOnBarClose = true;
            }

            protected override void OnBarUpdate()
            {
            if (CurrentBar >= (CurrentBar - barEntryNumber ))
            {
            Print("Number of Bars Since ATM Strategy has been place is : "+(CurrentBar-barEntryNumber) );
            }

            {
            // Condition set 1
            if (orderId.Length == 0
            && atmStrategyId.Length == 0
            && CrossAbove(Stochastics(PeriodD, PeriodK, Smooth).K, Stochastics(PeriodD, PeriodK, Smooth).D, 1)
            && Stochastics(PeriodD, PeriodK, Smooth).K[0] <= 25
            && ADX(ADXperiode)[0] <= ADXlevel)


            {
            atmStrategyId = GetAtmStrategyUniqueId();
            orderId = GetAtmStrategyUniqueId();
            AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "Lichtgroen", atmStrategyId);
            barEntryNumber = CurrentBar;
            }

            // Check for a pending entry order
            if (orderId.Length > 0)
            {
            string[] status = GetAtmStrategyEntryOrderStatus(orderId);


            // If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
            if (status.GetLength(0) > 0)
            {
            // Print out some information about the order to the output window
            Print("The entry order average fill price is: " + status[0]);
            Print("The entry order filled amount is: " + status[1]);
            Print("The entry order order state is: " + status[2]);

            // If the order state is terminal, reset the order id value
            if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
            orderId = string.Empty;
            }
            } // If the strategy has terminated reset the strategy id
            else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
            atmStrategyId = string.Empty;
            }

            Comment


              #7
              Hello Toroso,

              The code in Bold is not a part of your Entry Condition so it would not effect if your Strategy enters a Position.

              You may want to use the Print() statement before you condition to check to make sure that you are getting the values that you expect for your Entry Condition. Here is a helpful thread that goes over how to debug your strategy that you may view.
              JCNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              637 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              366 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              107 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              569 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              571 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X