• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.


No announcement yet.

Partner 728x90


OnBarUpdate vs exit point vs 1 minute delay

  • Filter
  • Time
  • Show
Clear All
new posts

    OnBarUpdate vs exit point vs 1 minute delay

    Hello and thanks for reading,
    First, I have some knowledge of coding, so, I'm not a fresh beginner, even if sometimes like now, I feel like one, hahaha. As the Strategy Builder offers limited way to calculate things, I had no choice to unlock a code to do what I had in mind, supported by the Strategy Builder.

    I'm almost done with my code but there is a problem I havent resolved yet.

    - I work in 1 minute chart
    - My strategy is based on different indicators and my code is filled with calculation
    - The Strategy setup is done on calculation at bar close

    Here's my problem: When a signal is triggered, the strategy immediately create the resulting order but refuse to create the opposite order at the same time (Buy/Sell limit or Sell/Buy limit). This opposite order is only created 1 minute later (because of the calculation on bar close). You understand than in this minute, this opposite order can be reached and because of that, I miss the train.

    I first worked with the position.AveragePrice command but I realized the order is not filled when the code line is read. I changed it to set up an order based on open[0] but it doesnt work either, even with Close[1].

    Here's the code I tried without success.
    ExitLongStopLimit(Convert.ToInt32(2), (Close[1] + (25 * TickSize)) , 0, @"LongExit", @"LongEntry");
    This line is in the same group of code as the BUY order that is working fine. I only have problem to set the sell limit order at the same time of the Buy.

    Here's the code working but 1 minute after the triggered order:
    ExitLongLimit(Convert.ToInt32(Position.Quantity), (Position.AveragePrice + (25 * TickSize)) , @"ExitLong", @"LongEntry");
    This line is in another group of code that is triggered by this code: if ((Position.Quantity != 0)...

    I think everything is there.

    Thanks in advance for the follow up.

    Hello Bricolico,

    Thanks for your post and welcome to the NinjaTrader forums!

    When placing orders, the order processing will be done asynchronously to the execution of your code. The order processing takes more time than it does for your code to execute. Using Calculate.OnBarClose means your code executes once per bar when the bar has closed. If the code conditions place an order, the order simply cannot be filled until the next bar.

    Typically, you would use SetStopLoss() and SetProfitTarget() methods as these will be applied immediately upon the entry order fill. You certainly can use other order methods however you would want to wait until the entry order has filled before placing those orders if they are dependant upon the actual entry price (position.AveragePrice). A typical way to do this is to check the market position. Reference: https://ninjatrader.com/support/help...etposition.htm

    Here is a link to one of our educational examples that may be helpful: https://ninjatrader.com/support/help...of_stop_lo.htm

    Also, for a good overview please see this link: https://ninjatrader.com/support/help...d_approach.htm

    Paul H.NinjaTrader Customer Service


      Thanks Paul for this response. I'll have a look at this in the next days.

      The SetProfitTarget seems to be what I want, at first.

      The MarketPosition command is not good, as it works with OnBarUpdate, so, 1 minute later.

      I'm at the first steps of coding NinjaScript and I'm learning a lot. I must tell that I find it difficult to learn another language because simple things take very long to find.

      What do you think if you (Ninjatrader) would do a series of videos/classes/webinars (1hr long) that could help people getting started with Strategy Builder (1-2 webinars) and 8-9 webinars with a little bit more complex knowledge using NinjaScript Editor with variables, math calculation, class types, etc.. that would certainly help people being more independant, that would probably decrease the number of questions here and would probably attract more people to use Ninjatrader.

      I think you have a very nice plateform. It could be a good thing to promote it that way, :-)

      Have a nice weekend!


        Hello Bricolico,

        Thanks for your reply and suggestions, appreciated.

        You can attend any of the NinjaTrader free webinars at this link. The Strategy Builder and Ninjascript (introductory) are shown at the same time on alternating weeks: https://ninjatrader.com/PlatformTraining

        You can find previous recordings as well as tips video here: https://www.youtube.com/user/NinjaTraderLLC

        You may be able to find other classes and courses by searching on the internet, offered by others.

        Here is a link to the NinjaTrader8 educational resource page: https://ninjatrader.com/support/help..._resources.htm
        Paul H.NinjaTrader Customer Service


          I confirmed that the SetProfitTarget is exactly what I wanted.

          2 orders has just been filled in seconds in 2 both side and the only thing I had to do is watch and being happy :-)

          Thanks a bunch. :-)


          Latest Posts


          Topics Statistics Last Post
          Started by mcdfgrtymml, Today, 07:52 AM
          0 responses
          Last Post mcdfgrtymml  
          Started by lju45, Today, 07:38 AM
          2 responses
          Last Post lju45
          by lju45
          Started by morrnel, Yesterday, 11:35 PM
          1 response
          Last Post NinjaTrader_PaulH  
          Started by evanp90, Today, 06:30 AM
          1 response
          Last Post NinjaTrader_Manfred  
          Started by sandman, Today, 07:13 AM
          1 response
          Last Post NinjaTrader_PaulH