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

triggering order while bar in progress

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

    triggering order while bar in progress

    Hi,

    Let's say the chart is setup on hourly candle and the buy order should trigger when price crosses above 10 ema. In the strategy builder if I code Close > 10 ema, then it will trigger the buy order only when the hourly bar is closed (on the next bar), right? But I want to trigger the buy order as soon as the price crosses 10 ema not to wait until the bar is closed.How do I do this? Thanks in advance.

    On the close of 9th number, the value of 10 ema would be know, if that value is x, then when current price is greater then x, the buy order should trigger.
    Last edited by dkbyond; 12-09-2019, 10:01 AM.

    #2
    Hello dkbyond,

    Thanks for your post.

    For your strategy code to run "Intrabar" on live data then you would need to use the calculate mode of Calculate.OnEachTick or Calculate.OnPriceChange, Calculate.OnPriceChange will cause your code to run every time there is a change in price on the live bar while On each tick would be every incoming tick even if price does not change, for that reason we would suggest OnPriceChange as it is less resource intensive (but way more so that Calculate.OnBarClose) and a cross would only occur when price changes.

    Notes:
    1) The calculate mode of OnPriceChange would only be effective on Live data (or Market replay) as historical data would only be calculated as if Calculate.OnBarClose was used.
    2) With Calculate.OnPriceChange (or Calculate.OnEachTick) it is entirely possible that on one tick the cross condition would be true and on the next tick, if price pulls back, the cross condition could be false and this could occur many times within the same bar. Ultimately, when the bars closes it may be that no cross actually occurred. This means you could place an entry order for a cross that historically would not have occurred. This is the risk.
    3) Because the cross condition may oscillate many times within the bar you will need additional logic to prevent further orders once the first order is placed. Here is an example you can use to prevent more than one entry per bar, assume the int variable savedBar is previously created at the class level.

    if (Your conditions to enter && CurrentBar != savedBar) // only enter if your conditions are true and the savedBar is not equal to the CurrentBar (being processed)
    {
    // your entry order here
    savedBar = CurrentBar; // save the current bar number so we only enter once per bar.
    }

    For historical trades, you could add a faster data series (such as 1 minute bars) and check the value of the hourly EMA on each 1 minute bar and place the order. Please see: https://ninjatrader.com/support/help...ipt_strate.htm
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks Paul,

      Lot of information there. Will go through it and come back if I still need any clarifications. My strategy is ultimately based on 5 minute, like lets say, if price crosses 10 ema on a hourly chart and the price also crosses or is greater than 10 ema on a 5 minute chart then buy order trigger.

      Now if I use OnPriceChange for the 1 hour chart and then check, if price > 10 ema on 5 minute chart (this can be on bar close) and exit if price < 10 ema on 5 minute chart (on bar close).

      So this should avoid the whipsaws on the 1 hour OnPriceChange, as once the buy order is triggered, the exit for it is based only on, price < 10 ema on a 5 min chart (on bar close). Hope I got it correct?

      Comment


        #4
        Hello dkbyond,

        Thanks for your reply.

        The calculate mode you select would apply to all data series that the strategy is attached to. It would be possible to segment your code so that some of it runs once per bar and other parts run on each price change. Here is a link to an example of a strategy configured like that: https://ninjatrader.com/support/help...either_cal.htm

        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Thanks. That was my next question. Will try.

          Comment


            #6
            Hi Paul,

            Gone through the documentation, but it seems I understood may be only 10%.

            What I need is, if Script1 ema1 > ema2 on daily chart (Calculate.OnEachTick) and if Script2 ema1 > ema2 on hourly chart (Calculate.OnEachTick) and if Script2 ema1 > ema2 on 5 min chart (Calculate.OnbarClose) enter buy

            for exit - if Script2 ema1 crosses below ema2 on 5 min chart (Calculate.OnbarCose) exit buy.

            Would really apprecate if you could help me with what should be written where in the code sample in the link you provided. Thanks. https://ninjatrader.com/support/help...either_cal.htm

            Comment


              #7
              Hello dkbyond,

              Thanks for your reply.

              We are a small team and cannot provide code writing or debugging services. I recommend that you review this complete section of the help guide which should fill in the gaps of a deep subject which is multi time frame coding: https://ninjatrader.com/support/help...nstruments.htm This help guide is a complete reference to multi time frame coding and covers all aspects so we recommened reading in its entirety for the best overview as each section builds on the previous.

              If you would like your strategy created for you, an alternative would be to hire a 3rd party programmer and we can provide links to thoe if that would help.
              Paul H.NinjaTrader Customer Service

              Comment


                #8
                Hi Paul

                Im also looking into the entire backtesting more "correct" - and I believe i understand bit about how it needs to be setup.

                So - just to understand it all a bit better can you please explain a bit more about what you wrote here:

                "For historical trades, you could add a faster data series (such as 1 minute bars) and check the value of the hourly EMA on each 1 minute bar and place the order. Please see: https://ninjatrader.com/support/help...ipt_strate.htm"

                So - how do i check if the hourly ema is doing from within the 1 minute bar?

                I understand that EnterShort(1, 1, "") is entering on the secondary bars - but how do i access the one hour primary EMA values from the secondary bars?


                Thank you very much.

                Comment


                  #9
                  Hello KarstenKafl,

                  Thanks for your post.

                  When you create the EMA in your script, you are assigning to the primary bars which in this case is 60 minute bars.

                  You can then access that value at any time. For example lets assume you created the EMA as myEMA(14);

                  In the OnBarUpdate(), when the 1 minute bars call OnBarUpdate() You could access the EMA like:

                  if (BarsInProgress == 1)
                  {
                  double theEMA = myEMA[0]; // get the current bar value of the 60 minute EMA
                  }

                  Note: Historically, the CurrentBar value on a 60 minute basis is the last closed 60 minute bar so in each 1 minute bar you are obtaining the same value for the 60 minutes until the 60 minute bar updates. Please see the help guide here: https://ninjatrader.com/support/help...taIsReferenced

                  Paul H.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by maybeimnotrader, Yesterday, 05:46 PM
                  3 responses
                  23 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by adeelshahzad, Today, 03:54 AM
                  5 responses
                  32 views
                  0 likes
                  Last Post NinjaTrader_BrandonH  
                  Started by stafe, 04-15-2024, 08:34 PM
                  7 responses
                  32 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by merzo, 06-25-2023, 02:19 AM
                  10 responses
                  823 views
                  1 like
                  Last Post NinjaTrader_ChristopherJ  
                  Started by frankthearm, Today, 09:08 AM
                  5 responses
                  22 views
                  0 likes
                  Last Post NinjaTrader_Clayton  
                  Working...
                  X