Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Storing a Bool Flag

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

    Storing a Bool Flag

    My strategy is suppose to make one long trade above a certain indicator and after that only trade long or short upon crossover. The issue is that I can't get the flag to stay stored. Also, how do I print the current state of the bool flag? I am only getting a print on a change, which I get, but trades are still happening.

    Below is the long entry
    Code:
     if(Position.MarketPosition == MarketPosition.Flat
                    && longTrades == true
                    && Close[0] > indicator
                    && BarsSinceExit() > 1 || BarsSinceExit() == -1)
                {
                    EnterLong(DefaultQuantity,"");
                    longTrades = false;
                }
    Below is what I thought would store the bool flag, but it is not.
    Code:
    #region OnExecution
            protected override void OnExecution(IExecution execution)
            {
                // Long Bool Flag
                if (execution.Order != null && Position.MarketPosition == MarketPosition.Long)
                {
                    longTrades = false;
                    PrintWithTimeStamp("Long bool flag store");
                }
                
                // Long Bool Flag
                if (execution.Order != null && Position.MarketPosition == MarketPosition.Short)
                {
                    shortTrades = false;
                    PrintWithTimeStamp("Short bool flag store");
                }
            }    
            #endregion
    I also have a boolean reset, which works.

    Any education in bool flags is much appreciated.

    #2
    Hi Hammerhorn,

    Thanks for your post.

    Is the bool at any point suppose to be true? I am not seeing in your code anywhere where this would be turned to true.

    Is this bool initialized as true?
    What is the behavior you expect? As in, which condition is suppose to change the value of the bool?
    What is the behavior you are getting? As in, does the bool reset after each tick, or after each bar, or after a condition?

    To print the state of the bool use:

    Print(longTrades.ToString());
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I have

      private bool longTrades = true;
      private bool shortTrades = true;

      Comment


        #4
        It is working, but not as intended. I moved the longTrades = false to the ExitLong.

        Comment


          #5
          Originally posted by Hammerhorn View Post
          I have

          private bool longTrades = true;
          private bool shortTrades = true;
          You initialized the flag as true, then set it to false in the code. Where in the code do you ever set the flag to true?

          Comment


            #6
            Originally posted by koganam View Post
            You initialized the flag as true, then set it to false in the code. Where in the code do you ever set the flag to true?
            Yes, I just didn't post as it was always reverting back to true, I guess I should have.

            Code:
            //Bool Flag Reset
                        if (Close[0] < indicator)
                             {
                                longTrades = true;
                                PrintWithTimeStamp(longTrades.ToString()); 
                            }
                            
                        if (Close[0] > indicator)
                            {
                                shortTrades = true;
                                
                            }

            Comment


              #7
              Hammerhorn,

              Originally posted by Hammerhorn View Post
              Yes, I just didn't post as it was always reverting back to true, I guess I should have.

              Code:
              //Bool Flag Reset
                          if (Close[0] < indicator)
                               {
                                  longTrades = true;
                                  PrintWithTimeStamp(longTrades.ToString()); 
                              }
                              
                          if (Close[0] > indicator)
                              {
                                  shortTrades = true;
                                  
                              }
              What is the behavior you expect? As in, when should this bool be changed?
              What is the behavior you are getting? As in, when is the bool actually being changed?

              In the code for longTrades, you check that Close[0] < indicator. What is the value of indicator. If indicator is always larger than Close[0] then the bool is always going to be reset.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_ChelseaB View Post
                Hammerhorn,



                What is the behavior you expect? As in, when should this bool be changed?
                What is the behavior you are getting? As in, when is the bool actually being changed?

                In the code for longTrades, you check that Close[0] < indicator. What is the value of indicator. If indicator is always larger than Close[0] then the bool is always going to be reset.

                Lets just say the indicator is the SMA. If Close[0] is less than SMA, the bool flag is reset to true. Once above and a trade is entered it is to change to false and stay that way until Close[0] is below SMA again. This was not working. So, I moved the setting the bool flag to false into the exit parameters, which is working. The issue with this is when I implement my dynamic stop the exit will not be fired and the bool flag will not change to false. Of course I could also, but the bool flag to false there too. I am just not there yet on this strategy.

                My question is why would it not work on entry, but works on exit??

                Comment


                  #9
                  Originally posted by Hammerhorn View Post
                  Lets just say the indicator is the SMA. If Close[0] is less than SMA, the bool flag is reset to true. Once above and a trade is entered it is to change to false and stay that way until Close[0] is below SMA again. This was not working. So, I moved the setting the bool flag to false into the exit parameters, which is working. The issue with this is when I implement my dynamic stop the exit will not be fired and the bool flag will not change to false. Of course I could also, but the bool flag to false there too. I am just not there yet on this strategy.

                  My question is why would it not work on entry, but works on exit??
                  Stick in some Print() statements and use them to examine the state of the flag. If the conditions of the flag are met, the flag will be modified. If it is not doing what you expect, then it is because it is doing what you wrote, and what you wrote is not what you were trying to do. You need to bring your code into conformity with your intent.

                  If you are willing to post the entire code, (of course, replacing your secret sauce with something innocuous), I can take a quick gander to see if and where you have a logic flaw. I trust that your code is well-commented as to what you are doing?

                  Comment


                    #10
                    Hammerhorn,

                    Once above and a trade is entered it is to change to false and stay that way until Close[0] is below SMA again.
                    You mention that this is not working. Would you like to focus on this?

                    To clarify, if the SMA is above the close price the longTrades bool is false. If the SMA falls below the close price then change the bool to true.

                    After the bool has been changed to true, allow for a long entry if the strategy position is flat and it has been at least one bar since the last exit. After placing the buy order set the bool to false.

                    At this point, the bool can be changed back to true on the next bar if the SMA is still below the Close[0] price.

                    Is this correct?
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_ChelseaB View Post
                      Hammerhorn,



                      You mention that this is not working. Would you like to focus on this?

                      To clarify, if the SMA is above the close price the longTrades bool is false. If the SMA falls below the close price then change the bool to true.

                      After the bool has been changed to true, allow for a long entry if the strategy position is flat and it has been at least one bar since the last exit. After placing the buy order set the bool to false.

                      At this point, the bool can be changed back to true on the next bar if the SMA is still below the Close[0] price.

                      Is this correct?
                      Yes, and I actually want that to happen.

                      Comment


                        #12
                        Originally posted by koganam View Post
                        Stick in some Print() statements and use them to examine the state of the flag. If the conditions of the flag are met, the flag will be modified. If it is not doing what you expect, then it is because it is doing what you wrote, and what you wrote is not what you were trying to do. You need to bring your code into conformity with your intent.

                        If you are willing to post the entire code, (of course, replacing your secret sauce with something innocuous), I can take a quick gander to see if and where you have a logic flaw. I trust that your code is well-commented as to what you are doing?
                        Posting code soon and thanks.
                        Last edited by Hammerhorn; 02-18-2014, 12:56 PM.

                        Comment


                          #13
                          Originally posted by koganam View Post
                          Stick in some Print() statements and use them to examine the state of the flag. If the conditions of the flag are met, the flag will be modified. If it is not doing what you expect, then it is because it is doing what you wrote, and what you wrote is not what you were trying to do. You need to bring your code into conformity with your intent.

                          If you are willing to post the entire code, (of course, replacing your secret sauce with something innocuous), I can take a quick gander to see if and where you have a logic flaw. I trust that your code is well-commented as to what you are doing?
                          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

                          Comment


                            #14
                            Just realized that this is not the best example. Should have changed the period for exit to something smaller. As the bool flag will only be reset when on exit. This is not the case in my strategy has i have a few indicators that I am using.

                            Comment


                              #15
                              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.
                              Attached Files
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              597 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
                              555 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X