Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Storing a Bool Flag

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

    #16
    Originally posted by NinjaTrader_ChelseaB View Post
    Hello Hammerhorn,

    Attached is an export of a sample script. Please open the output window and see what this script prints.

    In this script, the longTrades bool will become true if the SMA(14) is above the Close[0] price.

    At this point the EnterLong command is sent and the bool is set to false.

    On the next bar the longTrades bool can be set back to true if the SMA(14) is still above the close even if the strategy is in a position.

    The order does not enter again as there is no exit.

    This is to demonstrate that the bool is being changed as expected and then changed back on the next bar.

    Let me know if this makes sense and we can take the next step.
    Yes, your code makes perfect sense.
    Last edited by Hammerhorn; 02-18-2014, 02:06 PM.

    Comment


      #17
      Hammerhorn,

      For the next step I am going to add a Stop Loss.

      What logic would you like to discuss next?
      Chelsea B.NinjaTrader Customer Service

      Comment


        #18
        Originally posted by NinjaTrader_ChelseaB View Post
        Hammerhorn,

        For the next step I am going to add a Stop Loss.

        What logic would you like to discuss next?
        Seriously?

        Comment


          #19
          Hammerhorn,

          Please let me rephrase.

          This demonstrates how to flip a bool and prevent orders.

          Are you able to finish your strategy with this information?
          Chelsea B.NinjaTrader Customer Service

          Comment


            #20
            Originally posted by NinjaTrader_ChelseaB View Post
            Hammerhorn,

            Please let me rephrase.

            This demonstrates how to flip a bool and prevent orders.

            Are you able to finish your strategy with this information?
            No, your test strategy works exactly as mine does. It uses the simple on off switch, but what I want to do is to store the bool = false (your does not in my modified tests). I attempted this in OnExecution, but must have failed, if I was even on the right track. I am confused on why it works now using the bool = false when the strategy exits, verses using the the bool = false on entry. I really need to have it on entry.

            Comment


              #21
              Hello Hammerhorn,

              To clarify, the example script is not working as expected.

              Can you clarify how you want the false to be stored?

              Currently, it is stored until the next bar at which time the bool is flipped back to true.

              Was this not what you were asking for?

              From post #15:
              "On the next bar the longTrades bool can be set back to true if the SMA(14) is still above the close even if the strategy is in a position."
              Chelsea B.NinjaTrader Customer Service

              Comment


                #22
                Originally posted by NinjaTrader_ChelseaB View Post
                Hello Hammerhorn,

                To clarify, the example script is not working as expected.

                Can you clarify how you want the false to be stored?

                Currently, it is stored until the next bar at which time the bool is flipped back to true.

                Was this not what you were asking for?

                From post #15:
                "On the next bar the longTrades bool can be set back to true if the SMA(14) is still above the close even if the strategy is in a position."
                I need to have the bool stay false after a trade until Close[0] is less than the indicator.

                So, for example, once Close[0] is greater than the SMA enter a long position, exit on a profit target, and do not enter again until it crosses below and then back above the SMA. I know I can use the crossabove or other techniques to get one trade, but this will not work as my original script is much more complex and requires other conditions before an entry.

                Comment


                  #23
                  Originally posted by Hammerhorn View Post
                  I need to have the bool stay false after a trade until Close[0] is less than the indicator.

                  So, for example, once Close[0] is greater than the SMA enter a long position, exit on a profit target, and do not enter again until it crosses below and then back above the SMA. I know I can use the crossabove or other techniques to get one trade, but this will not work as my original script is much more complex and requires other conditions before an entry.
                  You cannot have it both ways. You say what I have emphasized, then in the next breath say that you do not want to use the cross. If you want to use a crossing condition, then you either use the supplied method or roll your own.

                  Comment


                    #24
                    Originally posted by koganam View Post
                    You cannot have it both ways. You say what I have emphasized, then in the next breath say that you do not want to use the cross. If you want to use a crossing condition, then you either use the supplied method or roll your own.
                    Are you referring to this,
                    "I know I can use the crossabove or other techniques to get one trade, but this will not work as my original script is much more complex and requires other conditions before an entry. "

                    If so I apologize as I was talking about CrossAbove(), which is designed to have just one trade within so many bars of the cross above. Unfortunately, I can't use this so I need to use the bool flag due to not knowing how many bars. Am I wrong??

                    A quick recap to what I am attempting,

                    1. pirvate int longTrades == true
                    2. Close[0] > SMA(14)
                    3. Long position entered, then longTrades == false
                    4. Profit target or whatever reached, no change, longTrades == false
                    4. Close[0] < SMA(14), then longTrades == true
                    5. Close[0] > SMA(14), no change, longTrades == true
                    6. Long Position entered, then longTrades == false


                    A quick recap on what is actually working

                    1. pirvate int longTrades == true
                    2. Close[0] > SMA(14)
                    3. Long position entered, then longTrades == false (not working)
                    4. Exit conditions met, exit long, longTrades == false
                    4. Close[0] < SMA(14), then longTrades == true
                    5. Close[0] > SMA(14), no change, longTrades == true
                    6. Long Position entered, then longTrades == false
                    Last edited by Hammerhorn; 02-18-2014, 07:34 PM.

                    Comment


                      #25
                      Originally posted by Hammerhorn View Post
                      See below.
                      Code:
                      #region Variables
                              // Wizard generated variables
                              private bool  longTrades = true;
                              private bool shortTrades = true;
                              #endregion
                      
                              /// <summary>
                              /// This method is used to configure the strategy and is called once before any strategy method is called.
                              /// </summary>
                              protected override void Initialize()
                              {
                                  
                                  CalculateOnBarClose = false;
                                              
                                  SetStopLoss(CalculationMode.Ticks, stopLossTicks);
                                                  
                                  //Debugging
                                  TraceOrders        = true;
                              }
                      
                              /// <summary>
                              /// Called on each bar update event (incoming tick)
                              /// </summary>
                              protected override void OnBarUpdate()
                              {
                                  
                                  #region EXIT
                                  
                                  // Long: EXIT - a fail safe in case a stop does not get submitted properly. 
                                  if(Position.MarketPosition == MarketPosition.Long
                                      && Close[0] < SMA(100))
                                    {
                                      ExitLong(DefaultQuantity, "", "");
                                      longTrades = false;
                                  }
                                              
                                  // Short: EXIT - a fail safe in case a stop does not get submitted properly. 
                                  if(Position.MarketPosition == MarketPosition.Short
                                      && Close[0] > SMA(100))
                                  {
                                      ExitShort(DefaultQuantity,"", "");
                                      shortTrades = false;
                                  }        
                                              
                                  #endregion
                                  
                                  #region ENTRY
                      //            if ((ToTime(Time[0]) >= 00000 && ToTime(Time[0]) < 103000)|| (ToTime(Time[0]) >= 164500 && ToTime(Time[0]) <= 235959))
                                      {
                                  
                                  // Long: Enter
                                  if(Position.MarketPosition == MarketPosition.Flat
                                      && longTrades == true
                                      && Close[0] > SMA(100)
                                      && BarsSinceExit() > 1 || BarsSinceExit() == -1)
                                  {
                                      EnterLong(DefaultQuantity,"");
                                      longTrades = false;
                                  }
                                                                                          
                                  // Short: Enter
                                  if(Position.MarketPosition == MarketPosition.Flat
                                      && shortTrades == true
                                      && Close[0] < SMA(100)
                                      && BarsSinceExit() > 1 || BarsSinceExit() == -1)
                                  {
                                       EnterShort(DefaultQuantity,"");
                                      shortTrades = false;
                                    }
                                      }
                                  //Bool Flag Reset
                                  if (Close[0] < SMA(100))
                                      {
                                          longTrades = true;
                                          PrintWithTimeStamp(longTrades.ToString()); 
                                      }
                                      
                                  if (Close[0] > SMA(100))
                                      {
                                          shortTrades = true;
                                          
                                      }
                                  #endregion
                              }    
                              
                              #region OnExecution
                              protected override void OnExecution(IExecution execution)
                              {
                                  // Long Bool Flag
                                  if (execution.Order != null && Position.MarketPosition == MarketPosition.Long)
                                  {
                                      longTrades = false;
                                      PrintWithTimeStamp(longTrades.ToString()); 
                                  }
                                  
                                  // Long Bool Flag
                                  if (execution.Order != null && Position.MarketPosition == MarketPosition.Short)
                                  {
                                      shortTrades = false;
                                      
                                  }
                              }    
                              #endregion
                      You are using Market Orders, so the OnExecution() event handler is not needed in this particular instance.

                      Change the flag reset to use CrossBelow() and CrossAbove() with a 1 bar lookback period.

                      Comment


                        #26
                        Originally posted by koganam View Post
                        You are using Market Orders, so the OnExecution() event handler is not needed in this particular instance.

                        Change the flag reset to use CrossBelow() and CrossAbove() with a 1 bar lookback period.
                        Sorry, to push, but why? Did not think of using the CrossBelow and CrossAbove as the flag reset, I was straight thinking entry. Thank you for your patience.

                        Comment


                          #27
                          Hi Hammerhorn,

                          To clarify, when an order is entered the longTrades bool is changed to false. At that point the you would not like the bool reset to true until the indicator value drops below Close[0] price and the rises above it once again. Is this correct?

                          For this you will need two variables. I have modified the sample I have created to show this.

                          Let me know if this is still not what you are looking for.
                          Attached Files
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #28
                            I've done following and it works

                            I want to execute custom stuff whenever strategy closes out of a position and I've done following to make it work reliably. Most crucial word here is volatile.
                            By declaring a variable as volatile, I effectively tell C# to never cache or
                            skip execution step that involves that variable.

                            Code:
                            public volatile bool myBooleanFlag = false;
                            protected override void OnExecution(IExecution execution)
                            {
                                if (execution.MarketPosition == MarketPosition.Flat)
                                {
                                    if (myBooleanFlag == true)
                                        DoCustomStuff();
                                    myBooleanFlag = false;
                                } else
                                {
                                    myBooleanFlag = true;
                                }
                            }

                            Comment


                              #29
                              Originally posted by Hammerhorn View Post
                              Sorry, to push, but why? Did not think of using the CrossBelow and CrossAbove as the flag reset, I was straight thinking entry. Thank you for your patience.
                              Because that is the meaning of the term, "CrossBelow": As long as we are above, be this; change only when we go below, means "do something when we cross below."

                              Comment


                                #30
                                Originally posted by koganam View Post
                                Because that is the meaning of the term, "CrossBelow": As long as we are above, be this; change only when we go below, means "do something when we cross below."
                                Sorry for my ignorance, but how is that different that Close[0] greater or less than an indicator in this instance?

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                                0 responses
                                596 views
                                0 likes
                                Last Post Geovanny Suaza  
                                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                                0 responses
                                343 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by Mindset, 02-09-2026, 11:44 AM
                                0 responses
                                103 views
                                0 likes
                                Last Post Mindset
                                by Mindset
                                 
                                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                                0 responses
                                556 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by RFrosty, 01-28-2026, 06:49 PM
                                0 responses
                                554 views
                                1 like
                                Last Post RFrosty
                                by RFrosty
                                 
                                Working...
                                X