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

Only looking at the most current three candles

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

    Only looking at the most current three candles

    Hi,

    I want to build logic into a strategy to only look at the current and the next 2 previous candles (ie. candles 2, 1, and 0). I don't care about any of the previous candles, and I don't need it to be backtested. I only want the strategy to look at those three candles.

    Does anyone know if that's possible?

    Cheers,
    Jack

    #2
    Hi Jack, thanks for writing in.

    Price data can be looked back upon with indexing e.g.

    if(CurrentBar < 2) return;

    Print(Close[2]);//third from last bar
    Print(Close[1]);//second from last bar
    Print(Close[0]);//latest bar

    to ignore the historical data, add if(State == State.Historical) return; to the top of OnBarUpdate. Here is a list of all available price series:
    https://ninjatrader.com/support/help...riceseries.htm

    Best regards,
    -ChrisL
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      I don't know if that's what I'm looking for. Essentially, I have a button that I can push to start looking for a setup, and when I push it, I want it to only look at the most current candles and then take a trade off of some specific inputs. Currently, it keeps looking back constantly, and takes trades I don't want it to because certain conditions are being met by candles that are already meet those conditions.

      I guess what I'd like is for me to start my strategy, and it waits for 0 to close and then starts looking for my setup. Ie. The flow that would be most ideal is, strategy initiated, it waits for the current candle 0 to close (turning into [1]), then the strategy looks for the setup to occur on the current candle [0].

      Is there a way to make it wait for the most recent candle to finish?

      Comment


        #4
        Hi Seastragg,
        In addition to adding if(State == State.Historical) return; to the top of OnBarUpdate as suggested by ChrisL, perhaps an idea to look at percentcomplete on start up when trading realtime and log the number of the first closing bar to a variable?
        https://ninjatrader.com/ru/support/h...ntcomplete.htm
        NT-Roland
        Last edited by NT-Roland; 06-01-2021, 06:41 PM.

        Comment


          #5
          Hi Jack, thanks for your reply.

          If you ignore the historical data with if(State == State.Historical) return; the next bar to close will be the latest bar on the chart. If you do not ignore the historical data all bars on the chart will be processed in OnBarUpdate.

          Kind regards,
          -ChrisL
          Chris L.NinjaTrader Customer Service

          Comment


            #6
            I guess I'm unsure where it goes. I tried this configuration and it didn't work. Does it go somewhere else?

            protected override void OnBarUpdate()
            {
            if(State == State.Historical) return;
            if (longButtonClicked
            && setup inputs etc

            Comment


              #7
              Hi Jack, thanks for your reply.

              A button click event should not be captured within OnBarUpdate. I linked an example you can follow if you would like to submit orders by a button in an indicator:

              https://ninjatraderecosystem.com/use...bar-buttons-2/

              Best regards,
              -ChrisL

              Disclaimer:
              The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.
              Last edited by NinjaTrader_ChrisL; 06-02-2021, 01:35 PM.
              Chris L.NinjaTrader Customer Service

              Comment


                #8
                Hi Chris,

                That is exactly the button file I am using. It has the button click in the OnBarUpdate() as seen from this copy pasta. I have my own setup that I have put into the long and short(and it's taking trades as I intended, except sometimes it takes trades as soon as the button is clicked). What I would like is for the strategy to not start looking for my setup inputs until the bar that I click the long/short button to close. Ie. if I click the button within a 5 minute candle at 10:12, I want the setups to start being looked at once that candle closes at ~10:15;01.

                Can you please help me understand where the "if(State == State.Historical) return;" should go in that button file?

                protected override void OnBarUpdate()
                {
                if (longButtonClicked

                && Close[1] >= MAX(High, 2)[2] + 0 * TickSize
                && High[1] < CurrentDayOHL().CurrentHigh[1]
                && High[0] > High[1])
                EnterLong();

                if (shortButtonClicked
                && Low[1] > CurrentDayOHL().CurrentLow[1]
                && Low[0] < Low[1]
                && Close[1] < MIN(Low, 2)[2] - 0 * TickSize)
                EnterShort();

                if (!longButtonClicked
                && Position.MarketPosition == MarketPosition.Long)
                ExitLong();

                if (!shortButtonClicked
                && Position.MarketPosition == MarketPosition.Short)
                ExitShort();
                }

                Comment


                  #9
                  Hi Jack, thanks for your reply and sorry for the oversight.

                  Instead of calling EnterLong, you would need to keep track of the CurrentBar property and set a flag similar to this:

                  Code:
                  private int cb;
                  private bool flag = false;
                  protected override void OnBarUpdate()
                  {
                  if(State == State.Historical) return;
                  
                  
                  
                  if (longButtonClicked
                  
                  && (Position.MarketPosition == MarketPosition.Flat)
                  //&& (IsRising(EMA1) == true)
                  //&& (IsRising(EMA2) == true)
                  )// && (IsRising(EMA3) == true))
                  
                  {
                  cb = CurrentBar;
                  flag = true;
                  }
                  
                  if(CurrentBar == cb+1 && flag)
                  {
                  EnterLongLimit(GetCurrentBid(0),"Long");
                  }
                  To have a button that immediately responds to the click, you would need to use an Addon style Account object and submit orders with the Submit() method when the WPF button is clicked.
                  https://ninjatrader.com/support/help...ount_class.htm - Account class


                  Best regards,
                  -ChrisL
                  Chris L.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by mattbsea, Today, 05:44 PM
                  0 responses
                  3 views
                  0 likes
                  Last Post mattbsea  
                  Started by RideMe, 04-07-2024, 04:54 PM
                  6 responses
                  31 views
                  0 likes
                  Last Post RideMe
                  by RideMe
                   
                  Started by tkaboris, Today, 05:13 PM
                  0 responses
                  2 views
                  0 likes
                  Last Post tkaboris  
                  Started by GussJ, 03-04-2020, 03:11 PM
                  16 responses
                  3,282 views
                  0 likes
                  Last Post Leafcutter  
                  Started by WHICKED, Today, 12:45 PM
                  2 responses
                  20 views
                  0 likes
                  Last Post WHICKED
                  by WHICKED
                   
                  Working...
                  X