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

Current price at the same time that a minute-based indicator is calculated

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

    Current price at the same time that a minute-based indicator is calculated

    I have developed a strategy that is based on comparing the price with a regression but when I program an entry it happens late. The configuration is as follows:
    Code:
    Calculate = Calculate.OnBarClose;
    and the price is based on the last price every minute.
    The indicator is calculated as follows:

    Code:
    LinReg1 = LinReg(89);
    My difficulty is in programming a conditional so that when the current price (not the closing price) reaches a level, make an entry.

    Code:
    if((GetCurrentAsk() - TickSize < KeltnerChannelEMA1.Midline[0])) {                        
                        EnterLong(Convert.ToInt32(DefaultQuantity), "");
    
                }
    How can I manage the current time to compare with a 1 minute bar based indicator?

    #2
    Hi lju45, thanks for your question.

    If you need OnBarUpdate to be called on every incoming tick then Calculate = Calculate.OnEachTick; We have an example here that shows how to separate OnEachTick logic from OnBarClose logic by using the IsFirstTickOfBar property:

    https://ninjatrader.com/support/help...either_cal.htm

    In your case, calculate the LinReg on every minute, then check the Ask price on every tick.

    Please let me know if I can assist any further.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi NinjaTRader_ChrisL, thanks for your reply the code is very usefull.

      If I change Calculate to OnEachTick the price have to be based on Last Thick? I'm trying the next:

      Code:
      if (BarsPeriod.BarsPeriodType == BarsPeriodType.Minute)
      {
      if (LinReg(10)[1] > LinReg(10)[0] )
      EnterLong("long entry");
      }
      Is it correct? LinReg(10)[1] is the value of the last minute? and LinReg(10)[2] is the value of two minutes ago?
      On the other hand, how can I change in the code the bars on the chart to be represented each minute and not each second?



      Last edited by lju45; 05-08-2020, 05:43 AM.

      Comment


        #4
        Hi lju45, thanks for your reply.

        An example of my last post would look like this:

        Code:
        public class TestLinReg : Indicator
            {
        [B]private LinReg MyLR;[/B]
        
                protected override void OnStateChange()
                {
                    if (State == State.SetDefaults)
                    {
        
                        Calculate                                    = Calculate.OnEachTick;
        
                    }
                    else if (State == State.Configure)
                    {
        
                    }
        [B]else if(State == State.DataLoaded)
                    {
                        MyLR = LinReg(20);
                    }[/B]
                }
        
                protected override void OnBarUpdate()
                {
        [B]if(CurrentBar < 20)
                        return;
        
                    if(IsFirstTickOfBar)
                    {
                        Print("LinReg Update " + MyLR[0] + " " + Time[0]);
                    }
        
                    Print("Tick Update " + Time[0]);[/B]
                }
            }

        If you are running this code on a 1 minute chart, the code within if(IsFirstTickOfBar) will run on every minute and everything outside of that block in OnBarUpdate will run on each incoming tick. The code you posted recently will also work if your script runs OnEachTick, except for the if (BarsPeriod.BarsPeriodType == BarsPeriodType.Minute) that only checks if the primary series is a minute chart (could be 1, 2, 60 minute).

        Please let me know if I can assist any further.
        Last edited by NinjaTrader_ChrisL; 05-08-2020, 11:29 AM.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Hi ChrisL,

          Thank you, but I can't compile due to next conditional:
          if(CurrentBar &lt; 20) If I delete this conditional I have the next output:

          LinReg Update 2857,28928571429 05/05/2020 22:58:00
          Tick Update 05/05/2020 22:58:00
          LinReg Update 2857,76785714286 05/05/2020 22:59:00
          Tick Update 05/05/2020 22:59:00
          LinReg Update 2858,43571428571 05/05/2020 23:00:00
          Tick Update 05/05/2020 23:00:00

          So each tick update is in one minute but I would like to have it on each tick.

          Comment


            #6
            Hi lju45,

            It looks like copy/paste from the forum copied over some formatting artifacts. The correct syntax is:

            if(CurrentBar < 20)
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Thank you,
              In any case, the same result continues to appear. In block OnBarUpdate() it does not change every tick but every minute

              LinReg Update 2857,28928571429 05/05/2020 22:58:00
              Tick Update 05/05/2020 22:58:00
              LinReg Update 2857,76785714286 05/05/2020 22:59:00
              Tick Update 05/05/2020 22:59:00
              LinReg Update 2858,43571428571 05/05/2020 23:00:00
              Tick Update 05/05/2020 23:00:00

              What is wrong?
              Last edited by lju45; 05-08-2020, 11:48 AM.

              Comment


                #8
                Hi lju45, thanks for your reply.

                LinReg can still be retrieved on every tick. Just move the call out of IsFirstTickOfBar.
                Chris L.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks NinjaTrader_ChrisL,
                  I think we are not talking about the same because when the code runs on a 1 minute chart, the output I get is every minute. In the example you have put I get the LinReg update every minute (which is inside IsFirstTickofBar) but I keep getting the TickUpdate every minute (being outside of IsFirstTickofBar) instead of getting it on every price update.

                  LinReg Update 2857,76785714286 05/05/2020 21:59:00
                  Tick Update 05/05/2020 21:59:00
                  LinReg Update 2858,43571428571 05/05/2020 22:00:00
                  Tick Update 05/05/2020 22:00:00

                  The output I am looking for is

                  LinReg Update 2858,43571428571 05/05/2020 21:59:00
                  Tick Update 05/05/2020 21:59:01
                  Tick Update 05/05/2020 21:59:03
                  ....
                  Tick Update 05/05/2020 21:59:59
                  LinReg Update 2858,43571428571 05/05/2020 22:00:00
                  Tick Update 05/05/2020 22:00:01
                  .....



                  Comment


                    #10
                    Hello lju45,

                    This is Jim responding on behalf of Chris who is out of the office at this time.

                    When the script processes historical data, it will be processed following Calculate.OnBarClose behaviors. If you need historical OnEachTick/OnPriceChange updates, you can enable Tick Replay.

                    Using Tick Reply - https://ninjatrader.com/support/help...ick_replay.htm

                    Please also note that once you are processing realtime data, the script will update with Calculate.OnEachTick/Calculate.OnPriceChange but the timestamp seen from Time[0] will still reflect the timestamp of the bar we are building. If we apply the script against a minute bar, we will see the timestamp of the minute bar that is building, not the timestamp of the tick that is received.

                    Demo - https://drive.google.com/file/d/1Vvc...w?usp=drivesdk

                    If you would like to see the timestamp of the ticks that come through, a single tick data series can be added and you can check the timestamp of that single tick data series. More information on working with multi time frame and multi instrument strategies can be found below.



                    We look forward to asssiting.
                    JimNinjaTrader Customer Service

                    Comment


                      #11
                      Thank you very much Jim, it is very clear with your explanation.
                      I would like to add a second question since when I program an entry and do the backtest, the price at which the entry is executed is above what I have programmed. I explain, the condition is as follows, when the price reaches a certain level the purchase order must be executed:
                      Code:
                                      if(oktotrade 
                                      && (GetCurrentAsk() <=  KeltnerChannelEMA(K_f, K_p).Midline[0] + offset_long )
                                      && Position.MarketPosition != MarketPosition.Long) {                        
                                          EnterLong(Convert.ToInt32(DefaultQuantity), "");
                                      Print(Time[0]);
                                      Print(GetCurrentAsk());
                                      Print(KeltnerChannelEMA(K_f, K_p).Midline[0]
                                     }
                      I atach the chart produced.
                      The ninjascript output is:
                      13/05/2020 18:58:00
                      2811,25
                      2811,34508122413

                      As you can see, the order has been taken at 2813. However, the ninjascript output shows that the order has been fulfilled when the price is 2811.25.
                      The entry price taken is the opening of the candle for one minute, instead of the tick on which the condition is met.

                      Why does this difference occur?

                      Thank you

                      Comment


                        #12
                        Hi lju45, Thanks for your reply.

                        What is the value of the default input series when the order is placed? Try printing out Close[0], this is the price EnterLong is using.

                        I look forward to hearing from you.
                        Chris L.NinjaTrader Customer Service

                        Comment


                          #13

                          Hello,

                          If I represent Close [0] it matches the price of EnterLong, that is 2813, but I would like to take the entry price to 2811.25 which is when my condition is met. How can I program so that the price is on the tick and not close?

                          Thank you

                          Comment


                            #14
                            Hi lju45, thanks for your reply.

                            You can place a limit order using GetCurrentBid() as the limit price e.g.

                            EnterLongLimit(GetCurrentBid());

                            Note the price can still go up after this order is placed so it has the possibility of not filling.

                            Please let me know if I can assist any further.



                            Chris L.NinjaTrader Customer Service

                            Comment


                              #15
                              Thank you very much Chris,

                              Yes it works, it didn't occur to me to use a limit order.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by bortz, 11-06-2023, 08:04 AM
                              47 responses
                              1,606 views
                              0 likes
                              Last Post aligator  
                              Started by jaybedreamin, Today, 05:56 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post jaybedreamin  
                              Started by DJ888, 04-16-2024, 06:09 PM
                              6 responses
                              19 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by Jon17, Today, 04:33 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post Jon17
                              by Jon17
                               
                              Started by Javierw.ok, Today, 04:12 PM
                              0 responses
                              15 views
                              0 likes
                              Last Post Javierw.ok  
                              Working...
                              X