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

FirstTickOfBar entry issue

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

    FirstTickOfBar entry issue

    HI there,

    I am using the following logic

    CalculateOnBarClose = false;
    protected override void OnBarUpdate()
    {
    if (BarsInProgress == 0)
    {
    if(Bars.FirstBarOfSession)
    {
    Do something;
    }
    if (FirstTickOfBar)
    {
    if(SMA(sma)[1]>SMA(sma)[2])
    {
    smaRising = true;
    }
    }

    if(smaRising==true)
    {
    EnterLong();
    }

    My question is can you execute an order in OnBarUpdate outside of and after FirstTickofBar, or should you always execute the order inside of FirstTickofBar?

    My guess is if a variable is set inside of FirsttickofBar you would want the execution to happen inside that logic as well. I am just a bit confused about the order of calculation here.




    #2
    Hello stalt,

    Yes, you can submit an order any time during the bar.
    You can submit an order on the first tick only.
    Or you can submit orders on every tick in the bar.

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      OK so with the scrip written as is above, would the order being a market order execute on the second tick(inside OnBarUpdate) then as the condition would be evaluated on the first tick?

      Comment


        #4
        Hello stalt,

        There is not enough information provided.

        If smaRising is initialized as true, an order would be submitted on the very first tick and every subsequent tick of every new bar.

        If smaRising is initialized as false, the script would wait until the SMA on the previous bar is greater than the bar previous on the first tick of a bar and then would start placing orders on every subsequent tick after.
        Last edited by NinjaTrader_ChelseaB; 06-25-2019, 10:28 AM.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Right,
          So if initialized as false, The EnterLong would occur on the second tick of the bar, unless I placed the order logic inside the FirstTickofBar correct? or would it still execute on the second tick? It doesn't really matter to my script or results, just trying to make sure I understand the logic and the order of it.

          Comment


            #6
            Hello stalt,

            If smaRising is initialized false, the bool would only change on the first tick of a bar when the SMA on the previous bar is greater than 2 bars previous.

            After it was true, there is no code to set it back to false so it would remain true on all ticks after this.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Sorry I should have been more specific. If I included smaRising=false in the Bars.FirstBarsofSession as per below., and changed it to false, while long as per below, would the EnterLong happen on the First tickofbar or on the second tick?

              Is that clearer?

              CalculateOnBarClose = false;
              protected override void OnBarUpdate()
              {
              if (BarsInProgress == 0)
              {
              if(Bars.FirstBarOfSession)
              {
              Do something;
              smaRising=false;
              }
              if (FirstTickOfBar)
              {

              if(SMA(sma)[1]>SMA(sma)[2])
              {
              smaRising = true;
              }
              }

              if(smaRising==true)
              {
              EnterLong();
              }

              if(Position.MarketPosition==MarketPosition.Long)
              {
              smaRising=false
              }

              }

              Comment


                #8
                Hello stalt,

                With the new suggested code, smaRising would be set to false on the first bar of a session.

                After the first bar of a new session the script would wait until the SMA on the previous bar is greater than the bar previous on the first tick of a bar and would continue placing new orders on each new tick until after the position has changed and OnBarUpdate is triggered with the new position.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  I must not be understanding this correctly. When using a 5 minute chart, and using the code above, the orders placed in the

                  f (FirstTickOfBar)
                  {
                  if(SMA(sma)[1]>SMA(sma)[2])
                  {
                  smaRising = true;
                  }
                  }

                  if(smaRising==true)
                  {
                  EnterLong();
                  }

                  Are entering on the bar after the SMA rising on the first tick are indicated Please see image attached
                  CalculateonBarCLose is set to false.

                  Is this a function or looking at historical and not real time data?

                  If I change SMA(sma)[1]>SMA(sma)[2]) to SMA(sma)[0]>SMA(sma)[1])

                  I get the entries on the bar I want, but would this not exeute on each tick on live data?
                  Last edited by stalt; 06-28-2019, 11:25 AM.

                  Comment


                    #10
                    Hello stalt,

                    The orders are placed on every tick after smaRising is set to true. This can only occur on the first tick of a new bar (when in real-time data) with CalculateOnBarClose set to false.

                    The order submissions will continue being placed until smaRising is set back to false. (The new order submissions may be ignored just depending on the EntryHandling and EntriesPerDirection setting)
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Thanks for your help and patience. I now have 2 scripts. One to run on historical with the bars ago set @ SMA(sma)[0]>SMA(sma)[1] with no FirstTickofBar section and then another that I ran on Market Replayand plan to run on real time at some point with SMA(sma)[1]>SMA(sma)[2] and the results replicate what I was hoping they would.

                      Running back tests on historical with FirstTickofBar was causing the issue.

                      Again, appreciate your help and patience.

                      Comment


                        #12
                        Hello stalt,

                        In historical data Calculate is always OnBarClose unless TickReplay is enabled.

                        See the help guide:
                        https://ninjatrader.com/support/help.../calculate.htm

                        And this post about intra-bar granularity.
                        https://ninjatrader.com/support/foru...377#post773377

                        Is your inquiry specifically about historical data?
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          This is the first time I have used FirstTickOfBar, and I didn't realize it would change when the orders would be placed when using historical...Using my 2 scripts above, I am seeing (within a tick or 2) the same results using my historical script with historical data and my RealTime script with Market Replay. I have been running the real time script with the first tick of bar in sim for a couple of days and orders are executing as I expect.

                          My issue is solved. Thanks,

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Haiasi, 04-25-2024, 06:53 PM
                          2 responses
                          16 views
                          0 likes
                          Last Post Massinisa  
                          Started by Creamers, Today, 05:32 AM
                          0 responses
                          3 views
                          0 likes
                          Last Post Creamers  
                          Started by Segwin, 05-07-2018, 02:15 PM
                          12 responses
                          1,785 views
                          0 likes
                          Last Post Leafcutter  
                          Started by poplagelu, Today, 05:00 AM
                          0 responses
                          3 views
                          0 likes
                          Last Post poplagelu  
                          Started by fx.practic, 10-15-2013, 12:53 AM
                          5 responses
                          5,407 views
                          0 likes
                          Last Post Bidder
                          by Bidder
                           
                          Working...
                          X