Announcement

Collapse

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

Collapse

OnBarUpdate vs exit point vs 1 minute delay

Collapse
X
 
  • 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.

    FYI:
    - 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.

    #2
    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

    Comment


      #3
      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!

      Comment


        #4
        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

        Comment


          #5
          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. :-)

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by algospoke, Today, 06:40 PM
          0 responses
          9 views
          0 likes
          Last Post algospoke  
          Started by maybeimnotrader, Today, 05:46 PM
          0 responses
          7 views
          0 likes
          Last Post maybeimnotrader  
          Started by quantismo, Today, 05:13 PM
          0 responses
          7 views
          0 likes
          Last Post quantismo  
          Started by AttiM, 02-14-2024, 05:20 PM
          8 responses
          168 views
          0 likes
          Last Post jeronymite  
          Started by cre8able, Today, 04:22 PM
          0 responses
          9 views
          0 likes
          Last Post cre8able  
          Working...
          X