• 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

Multiple bar conditions:

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

    Multiple bar conditions:

    Hi Guys, I'm trying to do an intrabar test and would appreciate some help. For example the first condition says the bar must do the following:


    PHP Code:

      High
    [0]-Open[0] >9*TickSize
      
    &&Close[0]<Open[0]
      &&
    Close[1]<Open[1]
      &&
    Open[1]-Close[1] <11*TickSize 


    So it says the current bar must have a high of 9 ticks and then pass below the open and also the bar must be below the last bar and the last bar must be 10 ticks or less.

    Now what I'd like to do is then repeat the test so after it has completed the first test and is true THEN complete the following second test:


    PHP Code:
      &&High[0]-Open[0] >9*TickSize
      
    &&Close[0]<Open[0
    So basically it repeats the test of checking price has hit 9 ticks above the open again and also passed below the open again.


    Any ideas how to add another test once the first test is true?


    Thanks
    DJ

    #2
    Hello DJ,

    If I am understanding you correctly, you can set a variable like a bool when the condition is true inside of the first and then when you are checking the second condition you can check to see if it is true. For Example:

    Code:
    //Variables 
    private bool myCondition = false;
    
    if(condition1)
    {
      myCondition = true;
    }
    
    if(condition2 && myCondition)
    {
       //Do something
       myCondition = false;
    }
    JCNinjaTrader Customer Service

    Comment


      #3
      Thanks JC. I'm having a few problems with this. It will draw an arrow whether it reaches the first or second condition first. What I'm trying to do is make it test the first condition first THEN the second condition. So basically it shouldn't draw the arrow on the second condition if the first one hasn't already been reached. Here is a test script.

      Thanks
      DJ
      Attached Files

      Comment


        #4
        Hello djkiwi,

        Thanks for the script.

        On the last line of the second condition you reset the myCondition variable. This reset will only happen if the second condition is true. This means the next time OnBarUpdate is run it will begin with myCondition = true.

        I recommend you remove the reset of myCondition to false from the second conditional and add it as the first line of OnBarUpdate.


        (Just a tip, after making this one change and running over 20 days of 1 minute ES data, the second condition is never hit. You may want to review the conditionals.)


        Please let me know if I can be of any other assistance.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thanks Chelsea. Unfortunately this doesn't work either. It doesn't trigger the second condition before the first,so that problem is solved.

          The problem now is it doesn't trigger the second condition after the first one is met in any circumstance. If price then drops below the open and less than 5 ticks it should trigger the second condition.

          All I'm trying to do is get the logic right so don't worry about the actual conditions. Also test on a 5 minute or 10 range chart etc. I've put in the new version with your ideas.

          Any other ideas?

          Thanks
          DJ
          Attached Files

          Comment


            #6
            Hello djkiwi,

            Once myCondition is true it is immediately made false upon the next bar.

            If you are trying to place a down arrow only after and if the first conditional has been met then, I recommend you move the "myCondition = false;" line to the last line of your second if statement action. Also add "&& myCondition == false" to the first conditional This will reset the myCondition only after the first and second conditions actions have taken place.

            Code:
            if(Close[0]>Open[0]+5*TickSize && myCondition == false)
            {
            Alert("myAlert1", NinjaTrader.Cbi.Priority.High, "Reached 1st condition", "Alert1.wav", 30, Color.Black, Color.Yellow);
            DrawArrowDown("shortswing1234" , 0, High[0] + 5*TickSize, Color.Red);	
            myCondition = true;
            }
            
            if(Close[0]<Open[0]-5*TickSize && myCondition)
            {
            Alert("myAlert2", NinjaTrader.Cbi.Priority.High, "Reached 2nd condition", "Alert4.wav", 30, Color.Black, Color.Yellow);
            DrawArrowUp("shortswing12345" , 0, Low[0] - 5*TickSize, Color.Lime);
            myCondition = false;
            }
            Let me know if you have any questions with this.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi Chelsea, that one doesn't work either. I tried different things but still not working. Sorry for not being clearer. This is what should happen in more detail.

              Condition 1. So the bar goes up 5 ticks above the bar open it should draw the arrow down at the top of the bar.

              Condition 2. If it THEN goes down 5 ticks below the open AFTER condition 1 has been achieved then it should draw the arrow up at the bottom of the bar.

              If it goes down 5 ticks from the open it should not do anything because condition 1 hasn't been satisfied.

              Bear in mind this is an intrabar test so I'm doing tests as the bar evolves. In the code line 44 I'm not sure what to do with that. It doesn't work either way with it included or excluded.

              Cheers
              DJ
              Attached Files

              Comment


                #8
                Hello djkiwi,

                Thanks for the updated code.

                The logic and code for what you are requesting is correct.

                For my testing I am using the ES 03-13 instrument. The conditions for this are rarely met but I am getting both arrows to print without editing your code.

                If you are expecting more arrows you may not mean to overwrite them. Currently, only one up arrow and one down arrow will appear on your chart as they are always overwritten by name. To not overwrite the arrows you can give them unique names.

                For example:
                • Instead of - DrawArrowDown("shortswing1234", 0, High[0] + 5*TickSize, Color.Red);
                • Use - DrawArrowDown("shortswing1234"+CurrentBar, 0, High[0] + 5*TickSize, Color.Red);


                As a test, try using 2 * TickSize instead of 5 * TickSize in your conditions. You will find this is met much more often.

                Also, the "//myCondition = false;" on line 44 can be left out. This would reset the false upon every call to OnBarUpdate.


                Please let me know if I can still be of assistance.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Chelsea. Thanks for your patience.

                  Here is an example from a chart attached o further illustrate. What should happen is once price goes above the open by 5 ticks then it should print a red down arrow at the top of the bar

                  Only once this condition is satisfied and the bar travels down 5 ticks below the open it should then draw the green up arrow at the low of the bar. Note all of this is happening as the bar progresses.

                  What is in fact happening is the bar goes down 5 ticks and the red arrow is plotting without the first condition even being satisfied.

                  If you put this on say a 10 range or renko bar and you will see the issue clearly. You can see the green arrow has plotted on the bottom of the bar even though the bar never traveled 5 ticks above the open.

                  I've attached the exact code I've been using. I tried a few different variations of this but with no luck.

                  Thanks in advance
                  DJ
                  Attached Files
                  Last edited by djkiwi; 03-04-2013, 12:11 PM.

                  Comment


                    #10
                    Hello djkiwi,

                    In the new file you have supplied "djintrabarninja.cs" there are new lines that are causing the behavior.

                    Code:
                    if (!boolAlertSounded) 				
                    DrawArrowDown("shortswing1234"+CurrentBar , 0, High[0] + 5*TickSize, Color.Red);
                    This added line can prevent the red down arrow from being drawn. This is nested inside of the first if statement so the outer if statement could still be true and not draw the arrow. This would allow the green up arrow to draw without the red down arrow appearing first.

                    I see in the second if statement "PlaySound("Alert4.wav");" its possible this line is simply missing from the first if statement. Adding that line directly below the "if (!boolAlertSounded)" in the first if statement's conditional would correct the script to print the arrows at the proper places.

                    Alternatively, you could add "&& !boolAlertSounded to the conditional of the first if statement and remove the "if (!boolAlertSounded)" from the first conditional execution block.


                    Please do not hesitate to contact us for any other NinjaTrader inquiries you may have.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi Chelsea, even with that removed it is still completing the second condition first and drawing the up arrow under the bar without testing whether the first condition has been met. This is the issue. Under no circumstances should it draw the green up arrow first.

                      If you test this on a bar you will clearly see the issue. Here it is without that included.

                      Thanks
                      DJ
                      Attached Files

                      Comment


                        #12
                        Hello djkiwi,

                        I have downloaded and imported your updated file. I was able to use this successfully. Green up arrows only appear after a red down arrow. I also had another technician here test with positive results as well.


                        Please check the history of the chart to see if perhaps the red arrow is displaying further back in time than expected.

                        Also, be sure you have compiled your script and reloaded the script on the chart.

                        To compile the script, right-click the code editor window > select Compile.
                        To reload the script on the chart, right-click the chart > select 'Reload NinjaScript'.


                        If this does not display what you expect, please, send me another screenshot.

                        Let me know if I can still be of assistance.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks Chelsea. That is so very odd how it is working for you guys. Well let me test it out tomorrow and try it out. I am totally stumped!

                          Again, thanks for your patience.

                          Cheers
                          DJ

                          Comment


                            #14
                            Thanks Chelsea, I tried it out and by removing the down arrow from the first condition and it worked ok. The first arrow was being used like a "print" statement anyway so no problem getting rid of it. So now I'm looking at adding a third and subsequent conditions and for some reason it appears to be ignoring the first condition completely. If you look at the screenshot it's putting the arrow below the bar even though I told it in the first condition that it should only plot the arrow if the Close of the previous bar is above the open of the previous bar Close[1]> Open[1] so not sure why its plotting the arrow when the previous bar has closed down. So in the case the bar should go up 5 ticks from the open then close 5 ticks below the open then once the bar then passes above the open it should plot the arrow.

                            I think I've followed your logic correctly and tried multiple positions of the longCondition1 = true;

                            Any ideas on this one? I think once I understand how to do the third condition the rest should be ok.

                            Thanks for your help.
                            DJ
                            Attached Files
                            Last edited by djkiwi; 03-05-2013, 02:53 PM.

                            Comment


                              #15
                              Hello djkiwi,

                              Thanks for your note.

                              I have had a chance to review your new code.

                              The order of operations is correct in that the first condition must trigger before the second and the second before the third. These, however, can happen over a great distance.

                              Currently, there is a check that the close of the previous bar is higher than its open on the first condition. By the time the 3rd condition triggers this most likely no longer the previous bar.

                              To check that the close of the previous bar is greater than the open of the previous bar during condition 3 you will need to add (Close[1] > Open[1]) to the if conditions of condtion 3.

                              Please let me know if this does not resolve your inquiry.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by dtl-saw, Today, 03:36 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post dtl-saw
                              by dtl-saw
                               
                              Started by williamzz, Today, 02:04 PM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_ChrisL  
                              Started by jtrading82, Today, 01:55 PM
                              3 responses
                              21 views
                              0 likes
                              Last Post NinjaTrader_Tyler  
                              Started by StevenNelson, Today, 01:00 PM
                              4 responses
                              19 views
                              0 likes
                              Last Post StevenNelson  
                              Started by allinthechart, Today, 12:21 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post allinthechart  
                              Working...
                              X