Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

"Ready" state on higher timeframe

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

    "Ready" state on higher timeframe

    In my strategy, I want to do this:

    Once the higher timeframe achieves <this status>,
    enter a trade at the first instance of the lower timeframe achieving <this other status>,
    even if it takes more than a few lower timeframe bars to do it.

    The problem I'm incurring is that most of the time, by the time the lower timeframe achieves <this other status>, the higher timeframe <this status> is no longer true. So I can't use logic that combines the two with "&&" statements. And once I make it complex and use "||" conditions (if lower timeframe is <this other status> and either higher timeframe bar [0] or [1] or [2] is <this status>), things start to degrade because the logic is so wide open.

    Is there a way to "turn on" a flag (based on higher timeframe conditions) that lasts until the lower timeframe turns it off, either through a trade or when the conditions no longer merit it? Sort of like "if the lower timeframe achieves <this other status>, enter long only if the higher timeframe has indicated that you have the green light". I feel like it should be obvious, but I'm not sure I understand the code looping cycle when it comes to maintaining local values after the next bar opens (because of CalculateOnBarClose).

    Thanks in advance,
    Gary

    #2
    Hello,

    Boolean variables are useful as flags in this type of situation. You can set a bool to True when the higher-timeframe condition is true, then that bool will remain True until you change it, even if the higher-timeframe confition is no longer true. You can then test the bool value along with the lower-timeframe condition, then finally set the bool back to False after performing the actions kicked off by the lower-timeframe condition.

    Please let me know if I can assist further.
    Dave I.NinjaTrader Product Management

    Comment


      #3
      Just so I understand this...

      So if I run the strategy on a 1-min chart, and a condition on the 15-min chart is met, I set a bool to 1 and it will remain a 1 until it's deliberately changed?

      So using CalculateOnBarClose won't reset it at the end of each minute?

      Comment


        #4
        That is correct.

        However, in C#, boolean variables are set to "true" or "false", rather than "1" or "0". Variable values persist throughout each call to OnBarUpdate(). The CalculateOnBarClose setting only affects how often the calculations of the script are run. With CalculateOnBarClose = True, the script will run its calculations at the close of each bar, but your variables will not be reset.

        I should also note that you should declare the bool variable in the Variables namespace, not within OnBarUpdate(), so that it is not local to that method.

        Please let me know if I can assist further.
        Dave I.NinjaTrader Product Management

        Comment


          #5
          Dave, I thought I had it all locked up after your last response, but after some further experimentation, I've found that I may not have described the situation quite right.

          Let's say that I want my strategy to do the equivalent of this:

          Once the higher timeframe achieves <this status>,
          enter a trade at the first instance of the lower timeframe achieving <this other status>,
          even if it takes more than a few lower timeframe bars to do it...
          AND
          Once the lower timeframe achieves <this other status> and executes a trade,
          the higher timeframe "flag" (or bool) is switched OFF and stays OFF.

          Right now I'm getting the flag when the higher timeframe reaches <this status>, and everything kicks off as it should. But the next bar opens and since the higher timeframe is still <this status>, the flag gets switched back on again even if I include a line in the same segment as the trade entry code to switch the flag back to false.

          (FYI, I needed more than just a true/false bool, so I implemented a private int that allows me a -1 value (short), a 1 value (long) and a 0 value (nothing).)

          Code:
          if (DoubleStochastics (BarsArray[1], 11).K[0] > 90)
                          {    
                              EntrySetupStatus = -1;  // Switches the Ready flag ON
                          }
                                           
                          if (EntrySetupStatus == -1)
                          {                     
                              if (Close[0] < HMA(21)[0] && Close[1] > HMA(21)[1])
                              {
                              <ENTER SHORT>
                                  EntrySetupStatus = 0;
                              }
                          }
          Using this code, EntrySetupStatus continues to get reset to -1 each time a new lower timeframe bar closes and the higher timeframe still meets the appropriate criteria. How do I write it so that once the trade is entered, the EntrySetupStatus value gets set to 0 and stays 0 until the next "cycle" where the higher timeframe reaches <this status> anew? In other words, once these conditions are met (higher timeframe and lower timeframe), I want to execute ONE trade, and no more until the conditions are right a totally separate, unique time in the future (not this same instance again).

          Thanks in advance,
          Gary

          Comment


            #6
            Code:
            if (DoubleStochastics (BarsArray[1], 11).K[0] > 90 [B]&& Position.MarketPosition == MarketPosition.Flat[/B])
                            {    
                                EntrySetupStatus = -1;  // Switches the Ready flag ON
                            }
                                             
                            if (EntrySetupStatus == -1)
                            {                     
                                if (Close[0] < HMA(21)[0] && Close[1] > HMA(21)[1])
                                {
                                <ENTER SHORT>
                                    EntrySetupStatus = 0;
                                }
                            }
            One way to accomplish this would be to check to ensure that your account position is flat before setting EntrySetupStatus to -1, as you can see in the code above. This will ensure that the flag does not get reset to -1 while you are currently in a trade.

            Please let me know if I can assist further.
            Dave I.NinjaTrader Product Management

            Comment


              #7
              Thanks, Dave...but it's more than just avoiding a second trade while I'm already in one. It's limiting the times the system enters a trade to once per "setup". It's entirely likely that I'd be in and out with a profit before that higher timeframe bar closes, opening the door to a second one that may or may not work in my favor. If the higher timeframe setup is right, then trade once and then stay quiet until the next time - not this same time - the higher timeframe setup is right. Is there a way to code this?

              Comment


                #8
                Hello,

                I see what you mean now. Rather than testing for the market position, you might instead only set the EntrySetupStatus back to 0 when the larger-timeframe condition goes back into False territory. For example, you could do something like:

                Code:
                if (CrossBelow(DoubleStochastics(BarsArray[1], 11).K[0], 90) && EntrySetupStatus != 0)
                {
                EntrySetupStatus = 0;
                }
                This would essentially say, "if we already have an entry flag, and the larger-timeframe condition is no longer true, then reset the entry flag to 0." You could add any other conditions that would need to be true or false in order for the entry flag to be reset, as well.
                Dave I.NinjaTrader Product Management

                Comment


                  #9
                  I do appreciate your time and your patience....

                  That's one step closer, but there are times when the higher timeframe will reach a "ready" state and it will take quite a few lower timeframe bars to reach its "ready" state, sometimes so many that the higher timeframe is no longer technically "ready". What I'm trying to accomplish here is: once the higher timeframe is "ready", it stays ready until there's a trade generated by the lower timeframe or it's otherwise switched off using other logic I haven't gotten to yet.

                  It's like someone turning on their turn signal as they approach a highway exit. No matter when they turn it on, it won't turn off until the steering wheel turns far enough. Then it'll remain off until the driver physically turns it back on again at some other exit down the road.

                  So it's like this:

                  *Once the higher timeframe achieves <this status>, set a flag to -1.
                  *The flag should stay -1 until the secondary timeframe reaches its ready state (and this may take so long that the higher timeframe builds bars that are no longer "ready", but that's OK).
                  *Once the lower timeframe achieves its ready state (with the higher timeframe flag = -1) and executes a trade, the higher timeframe "flag" (or bool) is switched OFF and stays OFF until the next time around.

                  Comment


                    #10
                    Originally posted by hawks67 View Post
                    Thanks, Dave...but it's more than just avoiding a second trade while I'm already in one. It's limiting the times the system enters a trade to once per "setup". It's entirely likely that I'd be in and out with a profit before that higher timeframe bar closes, opening the door to a second one that may or may not work in my favor. If the higher timeframe setup is right, then trade once and then stay quiet until the next time - not this same time - the higher timeframe setup is right. Is there a way to code this?
                    To put it in other words, you want there to be one and only one trigger on any one "higher time frame" bar?

                    Comment


                      #11
                      Originally posted by koganam View Post
                      To put it in other words, you want there to be one and only one trigger on any one "higher time frame" bar?
                      Yes, that would be a good way to put it, thanks. But that one trigger on that one bar has to remain in effect until something in the code specifically turns it off, like a successful trade or some other separately-specified change in conditions.
                      Last edited by hawks67; 03-17-2015, 02:50 PM.

                      Comment


                        #12
                        Originally posted by hawks67 View Post
                        Yes, that would be a good way to put it, thanks. But that one trigger on that one bar has to remain in effect until something in the code specifically turns it off, like a successful trade or some other separately-specified change in conditions.
                        if (CurrentBars[1] != HigherTimeFrameTriggerBar && /*other conditions here*/)
                        {
                        // handle your business
                        HigherTimeFrameTriggerBar = CurrentBars[1] ; // so now this bar can never be entered again until we get a new HigherTimeFrame bar.
                        }

                        Comment


                          #13
                          Originally posted by koganam View Post
                          if (CurrentBars[1] != HigherTimeFrameTriggerBar && /*other conditions here*/)
                          {
                          // handle your business
                          HigherTimeFrameTriggerBar = CurrentBars[1] ; // so now this bar can never be entered again until we get a new HigherTimeFrame bar.
                          }

                          OK, that's awesome, and I thank you....but I clearly have fooled you into thinking I know more than zip.

                          So apologies in advance if some of these questions are of the Supremely Dumb variety:

                          So that code would prevent subsequent trades from occurring during that one HigherTimeFrameTriggerBar. Would it also ensure that the flag that is set as a result of the HigherTimeFrameTriggerBar reaching a certain status stay set that way into perpetuity unless acted upon by other lines of code?

                          What type would I make the HigherTimeFrameTriggerBar in the Variables?

                          So the first time the right conditions in the "handle your business" portion are attained, the HigherTimeFrameTriggerBar gets set to the CurrentBars[1], preventing any more action on that one HigherTimeFrameTriggerBar...and the next time the higher timeframe conditions are right, the new HigherTimeFrameTriggerBar will have a new value, because it will be a different bar than the one before, ensuring that the code will enter that loop as many times as necessary until the "handle your business" part gets executed and the HigherTimeFrameTriggerBar gets freshly set to the new CurrentBars[1] value. Is that right? Just making sure I understand....

                          Thanks much!!

                          Comment


                            #14
                            Originally posted by hawks67 View Post
                            OK, that's awesome, and I thank you....but I clearly have fooled you into thinking I know more than zip.

                            So apologies in advance if some of these questions are of the Supremely Dumb variety:
                            When it comes to trading and programming, the only dumb questions are the ones that are not asked. We all started somewhere, even if I happen to be an auto-didact.
                            So that code would prevent subsequent trades from occurring during that one HigherTimeFrameTriggerBar. Would it also ensure that the flag that is set as a result of the HigherTimeFrameTriggerBar reaching a certain status stay set that way into perpetuity unless acted upon by other lines of code?
                            The purpose of the construct is to ensure that the code block can be entered exactly once, and only once, on any bar: so any state variables set in the block cannot be altered except by code external to the block.
                            What type would I make the HigherTimeFrameTriggerBar in the Variables?
                            The same type as CurrentBar, an int.
                            So the first time the right conditions in the "handle your business" portion are attained, the HigherTimeFrameTriggerBar gets set to the CurrentBars[1], preventing any more action on that one HigherTimeFrameTriggerBar...and the next time the higher timeframe conditions are right, the new HigherTimeFrameTriggerBar will have a new value, because it will be a different bar than the one before, ensuring that the code will enter that loop as many times as necessary until the "handle your business" part gets executed and the HigherTimeFrameTriggerBar gets freshly set to the new CurrentBars[1] value. Is that right? Just making sure I understand....

                            Thanks much!!
                            Pretty much, but not quite. Your other conditions will also be gating entry, so also get to determine when the block can be entered. Once the block is entered, it will not be entered again on that bar.

                            Comment


                              #15
                              Awesome, thanks! I'll report back with the results...

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by MacDad, 02-25-2024, 11:48 PM
                              7 responses
                              158 views
                              0 likes
                              Last Post loganjarosz123  
                              Started by Belfortbucks, Today, 09:29 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post Belfortbucks  
                              Started by zstheorist, Today, 07:52 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post zstheorist  
                              Started by pmachiraju, 11-01-2023, 04:46 AM
                              8 responses
                              151 views
                              0 likes
                              Last Post rehmans
                              by rehmans
                               
                              Started by mattbsea, Today, 05:44 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post mattbsea  
                              Working...
                              X