Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90


Multiple Orders filled when i only want one.

  • Filter
  • Time
  • Show
Clear All
new posts

    Multiple Orders filled when i only want one.


    I'm using a script that shows up two buttons on the chart. One is a BUY button, the other is a SHORT button (code attached to the post).
    When clicked, an "Open" button is changed into a "Close" button.
    The buttons do what's expected : Buy when clicked, then close when clicked again.

    My issue is that sometimes, I get more than one order filled, when only 1 is supposed to be.
    As you can see in the linked screenshot : it should be only 1 order filled, but you can see 3 orders filled.

    Can you help me understanding why I have those unexpected orders filled ?

    Thank you in advance for your reply.
    Attached Files

    Hello thanajo,

    Thanks for your post.

    The strategy is being run with Calculate.OnEachTick. This means the code is executed for every incoming tick which can be very fast executions. The code block for entering an order is only checking 2 conditions, 1) for a button click bool and 2) the position being flat. Once the button is clicked the bool remains true until the button is clicked again. The other entry condition is checking to see if the position is flat, the position object cannot be updated until the order has been sent to the exchange, received, filled, and acknowledged back which takes physical time to perform, meanwhile your code continues to execute as each new tick comes in and when the button click is true and the position remains flat, orders will continue to be submitted as fast as the ticks come in.

    To prevent multiple unintended orders, you would need to add another entry condition.

    You can add code to limit the order entry to no more than once per bar. This can be done by using the systems CurrentBar which is the bar number that is being processed and by using an int variable (that you create, likely one for long and one for short). In the entry condition, you would check to see if the CurrentBar is NOT equal to the int variable. In the action part of the if statement where you submit the order you would then assign CurrentBar to the int variable. The first time the button is clicked (which sets a bool true) and you are flat the current bar number will not be equal to the int variable so the order will be sent and the current bar number is then saved to the int variable. On the very next tick, the button bool is still true, the position is still flat however now the CurrentBar is equal to the int variable so the condition is no longer true and this will prevent further orders from being placed.


    If you need the ability to place an order multiple times within the same bar then you would need to come up with logic that would allow you to do that again adding a condition into the entry block.
    Paul H.NinjaTrader Customer Service


      Thank you very much Paul.
      That helped me perfectly !



      Latest Posts


      Topics Statistics Last Post
      Started by ATMtrader0001, 02-10-2021, 01:20 PM
      14 responses
      Last Post bltdavid  
      Started by junkone, Today, 03:42 PM
      0 responses
      Last Post junkone
      by junkone
      Started by SampaioCabezas, Today, 01:58 PM
      1 response
      Last Post NinjaTrader_Jesse  
      Started by bourasrafik, Today, 07:06 AM
      3 responses
      Last Post NinjaTrader_Emily  
      Started by guillembm, Today, 10:33 AM
      3 responses
      Last Post NinjaTrader_Spencer