• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Draw only once

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

    Draw only once

    Hi all,

    I have this simple kind of "if":

    protected override void OnBarUpdate()
    {
    // Use this method for calculating your indicator values. Assign a value to each
    // plot below by replacing 'Close[0]' with your own formula.
    Plot0.Set(Close[0]);

    if (CurrentBar < 26)return;
    if (CrossAbove(CCI(14), 5,5)&&XYZ[0]>XYZ[1] )
    {
    ///Do something
    DrawArrowUp("Dot"+ CurrentBar , false, 1,High[0]- 8*TickSize, Color.Gold);
    }

    The problem is that it will draw the arrow below EVERY bar that match the above condition when all what I want ids to draw it only one (first time conditions met)

    Help to fix the code will be greatly appreciated.

    #2
    Hello Tzachi,

    Thanks for your inquiry.

    You can accomplish such a task by creating a doOnce bool and including that with your if statement.

    I've attached a hypothetical example below:

    Code:
    private bool doOnce = true;
    
    protected override void OnBarUpdate()
    {
    	if(VariableA > VariableB && doOnce == true)
    	{
    		// Do something
    		doOnce = false; // Set to false so we don't do it again.
    	}
    	
    	if(SomeConditionToReset)
    	{
    		doOnce = true; // Reset the doOnce bool when we want to allow it to trigger again
    	}
    }
    Our publicly available Basic Programming Syntax documentation should provide enough reference for this task: https://ninjatrader.com/support/help...sic_syntax.htm

    Please let me know if you have any questions.
    Last edited by NinjaTrader_Jim; 11-27-2017, 01:47 PM.
    JimNinjaTrader Customer Service

    Comment


      #3
      Many thanks for your quick reply.

      When I added "private doOnce = true;"

      and the condition into the If... I get an error below:

      Invalid token '=' in class, struct, or interface member declaration

      Comment


        #4
        Oops!

        I forgot to include the type with the declaration.

        Should be: private bool doOnce = true;
        JimNinjaTrader Customer Service

        Comment


          #5
          Thank you.

          Now no error but when have:

          if (XYZ [0] > XYZ[1] && doOnce == true)
          {

          DrawArrowUp("Dot"+ CurrentBar , false, 1,High[0]- 8*TickSize, Color.Gold);
          doOnce = false;
          }
          I get no arrows at all.
          Last edited by Tzachi; 11-27-2017, 02:05 PM.

          Comment


            #6
            Hello Tzachi,

            Please use Print() statements to monitor your logic as it executes. Likely, the arrow drew on the first time the condition became true, and since the bool is no longer set to true, the arrow never draws again.

            You will want to create Print statements that tell you the values of the variables you use in your if statement before the if statement is reached so you can see how it is evaluated.

            I've included a forum post for more information on debugging your code:

            Debugging - https://ninjatrader.com/support/foru...ead.php?t=3418

            Please let us know if we can be of further assistance.
            JimNinjaTrader Customer Service

            Comment


              #7
              Via Print method I found out that it did print the arrow up one time, (like 20 days ago), the first time when conditions agreed upon as it should do - but never again.

              Any Idea why it was not resetting to look for the conditions later on?

              Comment


                #8
                You need to create a condition that resets the doOnce bool variable back to true.

                Code:
                if(SomeConditionToReset)
                {
                	doOnce = true; // Reset the doOnce bool when we want to allow it to trigger again
                }
                JimNinjaTrader Customer Service

                Comment


                  #9
                  BEAUTIFUL

                  Works as I wished for.

                  Many thanks.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by CriticalMind, 11-29-2015, 12:39 PM
                  3 responses
                  1,321 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by novel168, 06-14-2019, 09:20 PM
                  1 response
                  7 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by johnnybegoode, Today, 08:00 PM
                  3 responses
                  7 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by digibob, Today, 03:00 PM
                  1 response
                  6 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by yeshujbp, Today, 02:50 PM
                  1 response
                  10 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Working...
                  X