Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

False 'If' Statement still Executing

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

    False 'If' Statement still Executing

    I have identified the culprit but am at a loss as to where the issue is... Below is the simplified code, and attached is the indicator. The problem I believe is the system indicator being utilized is missing something needed to properly reference it?

    Below you will see, and assumedly duplicate on your end, an If () condition that when false is still executing the Then {} sequence.

    Note the line still prints & the text box prints also but not quite correctly. // at least on my end here

    Set the bool to false as per code and it works correctly... // In my actual construct there are two bools and if one is false it won't print at all, which is correct procedure.

    Bottom line looking to identify why the executable condition is still printing when the PriorDayOHLC has rendered a false condition.


    Code:


    region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.DrawingTools;

    #endregion

    //This namespace holds Indicators in this folder and is required. Do not change it.

    namespace NinjaTrader.NinjaScript.Indicators.j2

    {
    region Defaults

    public class Testing : Indicator

    {
    private PriorDayOHLC PriorDayOHLC1;
    private CurrentDayOHL CurrentDayOHL1;



    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {



    Description = "xxx"
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    DisplayInDataBox = true;
    IsOverlay = true;
    PaintPriceMarkers = true;
    DrawOnPricePanel = true;
    IsAutoScale = true;
    DrawHorizontalGridLines = true;
    DrawVerticalGridLines = true;

    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
    //See Help Guide for additional information.
    IsSuspendedWhileInactive = true;


    TextBrush = Brushes.White;
    AutoBrush = Brushes.Yellow; /// automated line brush
    Opacity = 100;
    Size = 12;
    Test1 = true;

    }


    else if (State == State.DataLoaded)
    {

    PriorDayOHLC1 = PriorDayOHLC(Close);
    CurrentDayOHL1 = CurrentDayOHL(Close);


    }
    }

    #endregion

    region Code Sets

    protected override void OnBarUpdate()
    {
    NinjaTrader.Gui.Tools.SimpleFont myFont = new NinjaTrader.Gui.Tools.SimpleFont("Arial", Size) { Size = Size, Bold = true };


    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1)
    return;


    ///************************************************** ****************************************

    // set 1

    if

    (
    (Close[0] == PriorDayOHLC1.PriorClose[0])
    && (Test1 == true)

    // Ninja Forum , above code is the problem... Prior Day close appears to be the culprit and I don't know why... Note when price here is not exact to PD Close, the statement is false
    // but the line will still print and the text box is incorrectly positioned.

    // If the above prior day condition is false, nothing should print.

    ||

    (Test1 == false)

    /// Prints correctly

    )


    {
    Draw.HorizontalLine(this, "pdcl1", true, PriorDayOHLC1.PriorClose[0], AutoBrush, DashStyleHelper.Solid, 2);

    Draw.Text(this, "Test3", true, "See me at far right of screen", 0, PriorDayOHLC1.PriorClose[0] , 10, TextBrush, myFont, TextAlignment.Right, Brushes.Transparent, Brushes.Transparent, Opacity);

    }

    }

    #endregion

    region Properties

    /// Display

    [XmlIgnore]
    [Display(ResourceType = typeof(Custom.Resource), Name = "TextBrush", GroupName = "Display", Order = 1)]
    public Brush TextBrush { get; set; }

    [Browsable(false)]
    public string TextBrushSerialize
    {
    get { return Serialize.BrushToString(TextBrush); }
    set { TextBrush = Serialize.StringToBrush(value); }
    }


    [XmlIgnore]
    [Display(ResourceType = typeof(Custom.Resource), Name = "Automated Line Brush", GroupName = "Display", Order = 2)]
    public Brush AutoBrush { get; set; }

    [Browsable(false)]
    public string AutoBrushSerialize
    {
    get { return Serialize.BrushToString(AutoBrush); }
    set { AutoBrush = Serialize.StringToBrush(value); }
    }

    [NinjaScriptProperty]
    [Display(Name="Opacity", Description="Opacity", GroupName="Display", Order = 4)]
    public int Opacity
    { get; set; }

    [NinjaScriptProperty]
    [Display(Name = "Font Size", Description = "Font Size in pixels", GroupName = "Display", Order = 5)]
    public int Size
    { get; set; }

    [NinjaScriptProperty]
    [Display(Name="Test1", Description = "xxxx", GroupName="Parameters", Order = 1)]
    public bool Test1
    { get; set; }


    #endregion

    }
    }​
    Attached Files
    Last edited by johnMoss; 01-15-2025, 07:41 AM.

    #2
    Hello johnMoss,

    To clarify, the condition in question is:

    if ( (Close[0] == PriorDayOHLC1.PriorClose[0]) && (Test1 == true) || (Test1 == false) )​

    This condition will evaluate as true if 'the current bar close is equal to the prior day close AND Test1 is true' OR 'Test1 is false (ignoring the close conditions)'.


    Below is a link to a support article on how to add debugging prints to understand behavior.


    Please add the following print one line above the condition.

    Print( string.Format("{0} | Close[0]: {1} == PriorDayOHLC1.PriorClose[0]: {2} && Test1: {3} == true || Test1: {3} == false", Time[0], Close[0], PriorDayOHLC1.PriorClose[0], Test1));

    Save the output to a text file and attach this to your next post.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      It's like Christmas when i see your name pop up ... Thanx C (!) // Note the Bye Bye line at top I have in another post to platform support. Somebody's joke somewhere in a .dll... Assumedly harmless.
      Attached Files
      Last edited by johnMoss; 01-15-2025, 08:53 AM. Reason: the second file attached print statement was placed after if() & before Then{}

      Comment


        #4
        Hello johnMoss,

        At what date and time is the condition evaluating as true when unexpected?

        The first output in the file doesn't appear to show the condition was true.

        1/9/2025 5:02:00 PM | Close[0]: 5944 == PriorDayOHLC1.PriorClose[0]: 0 && Test1: True == true || Test1: True == false

        Close[0] is not equal to PriorClose[0].
        Test1 is true so the OR isn't evaluating as true.

        I would say this condition should be false on Jan 9th at 5:02 PM.

        May I also have you add a print within the logic block of the condition? (in the same place the Draw.HorizontalLine() and Draw.Text() is being called)

        Print( string.Format("{0} | Condition true", Time[0]));

        This would help to show that the condition is actually evaluating as true when the output conditions are showing it should be evaluating as false.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          At what date and time is the condition evaluating as true when unexpected? // I'm running it on a one minute chart and on every bar where it is false it will print the line & text. The line shows up as where it should be if it was true & the text box winds up toward the center of the screen...

          The first output in the file doesn't appear to show the condition was true.

          1/9/2025 5:02:00 PM | Close[0]: 5944 == PriorDayOHLC1.PriorClose[0]: 0 && Test1: True == true || Test1: True == false

          Close[0] is not equal to PriorClose[0].
          Test1 is true so the OR isn't evaluating as true.


          I would say this condition should be false on Jan 9th at 5:02 PM.
          // I deliberately set it to have to be equal so to easily produce what should be false conditions.

          May I also have you add a print within the logic block of the condition? (in the same place the Draw.HorizontalLine() and Draw.Text() is being called)

          Print( string.Format("{0} | Condition true", Time[0]));

          This would help to show that the condition is actually evaluating as true when the output conditions are showing it should be evaluating as false.
          // Done & attached...


          So I looked at the output too and the condition is evaluating correctly. It's reporting False yet the line & text are still printing on the chart...
          Attached Files

          Comment


            #6
            Hello johnMoss,

            Did you remove the first print I requested you add?

            The print for the branching command?

            We want to see what values are being compared in the branching command when the condition is evaluating as true.

            From this new output file we can't see the values compared in the condition.

            We can see that Jan 9th doesn't appear to show the condition is true at all. The output appears to only show prints for Jan 12th, 14th, and 15th.

            You are saying on Jan 9th at 5:02 there is text appearing on that bar?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I examined the the last output you requested & compared to price action from today's overnight. The condition evaluated to true exactly when it should have & no others. What appears to be happening is that the condition is correctly evaluating to false yet the line & text box are still printing. I have attached the zipped indicator construct again here. Are ya'll allowed to load on your end or is that a no no?

              If so, can you simply copy & paste what I sent in the first transmission here & see for yourself? In the first thread that code-set is a complete rendition.

              Elsewise, just to make sure I've correctly placed your print requests, note code below... Attached also is the output result...

              Code with Print Statements:

              Print( string.Format("{0} | Close[0]: {1} == PriorDayOHLC1.PriorClose[0]: {2} && Test1: {3} == true || Test1: {3} == false", Time[0], Close[0], PriorDayOHLC1.PriorClose[0], Test1));

              if
              (

              (Close[0] == PriorDayOHLC1.PriorClose[0])
              && (Test1 == true)
              ||
              (Test1 == false)
              )

              {
              Print( string.Format("{0} | Condition true", Time[0]));

              Draw.HorizontalLine(this, "pdcl1", true, PriorDayOHLC1.PriorClose[0], AutoBrush, DashStyleHelper.Solid, 2);
              Draw.Text(this, "Test3", true, "See me at far right of screen", 0, PriorDayOHLC1.PriorClose[0] , 10, TextBrush, myFont, TextAlignment.Right, Brushes.Transparent, Brushes.Transparent, Opacity);
              }
              Attached Files

              Comment


                #8
                Hello johnMoss,

                1/9/2025 5:02:00 PM | Close[0]: 5944 == PriorDayOHLC1.PriorClose[0]: 0 && Test1: True == true || Test1: True == false
                1/9/2025 5:03:00 PM | Close[0]: 5944.5 == PriorDayOHLC1.PriorClose[0]: 0 && Test1: True == true || Test1: True == false​

                The Close at 5944 is not equal to the PriorClose[0] which is 0 so that would not be true, and Test1 is True, so the OR condition within the branching command would also not evaluate as true.

                The 'Condition true' message is not appearing so this would confirm that the condition did not evaluate as true on Jan 9th at 5:02 pm.

                I would not expect the text to be appearing over the Jan 9th 5:02 PM bar on the chart.

                May I have a screenshot of the chart showing the Jan 9th 5:02 PM bar?


                Where the condition did evaluate as true is on Jan 12th at 6:45 PM.

                1/12/2025 6:45:00 PM | Close[0]: 5861.5 == PriorDayOHLC1.PriorClose[0]: 5861.5 && Test1: True == true || Test1: True == false
                1/12/2025 6:45:00 PM | Condition true​

                On the Jan 12th bar at 6:45 PM I would expect text to be drawn
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Attached are two screen shots. Note you can see yesterday's close on the timeline. On the first shot [Test1] I show Test bool condition set to false and it works correctly. On the second [Test2] it is set to true and has the requirement. Note the line still prints and the text box di d too & is now in the center when it should be on the right normally. Regardless, neither object should display on the chart at all.

                  In this simplified example of what I'm up to, if the condition is not true then I do not want to see the line & text box at all...
                  Attached Files

                  Comment


                    #10
                    Hello johnMoss,

                    Lets keep things simple. Please don't make changes to the logic as we are investigating that logic.

                    There should be only one screenshot, and it should be of the chart that produced the output in the text file.

                    You have not provided a date and time where you are seeing text drawn that is unexpected as requested in post # 4.
                    I've been assuming you are seeing text being drawn on every bar when the condition is false so I've been focusing on the first lines in the output file.

                    This doesn't seem to be the case..

                    First, the screenshot is not showing the Jan 9th 5:02 bar.

                    If you don't want to focus on the first bar printed in the output file, please let me know the information I have requested in post # 4 on the date and time the behavior is occurring.


                    You have stated the following:
                    "Regardless, neither object should display on the chart at all."

                    This would not be the case with the logic in this script.

                    The condition evaluated as true several times in this output. I do expect that there would be text on the chart when the condition has evaluated as true.

                    There should be text on the chart that made this output, based on the output text file you have provided.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Attached is the single screenshot you want. In the picture attached I am running a live chart as in today right now and what you see is that price is nowhere near yesterday's close so therefore absolutely nothing commanded should display at the moment this shot was taken yet it still does. This is a one minute chart and what is happening is on every bar update the line & text are being displayed, whereas they should be absent as price is a mile away...

                      May I respectfully submit that if you simply load the code onto a chart on your end this behavioral discrepancy will become quite obvious... That said and in Navy parlance, I grant you this one is a rather peculiar gripe

                      My suspicion remains that the presence of the Prior Day OHLC indicator is somehow invoking this behavior...
                      Attached Files

                      Comment


                        #12
                        Hello johnMoss,

                        I've made a video of testing the prints.


                        I do not see any issue. The test script is drawing text and lines correctly as it has been coded to do.

                        There should be text on the chart. The condition did evaluate as true.


                        Where you have mentioned:
                        "Navy parlance, I grant you this one is a rather peculiar gripe"

                        I'm not understanding what this means.

                        The script appears to be functioning correctly.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          As I'm writing my response, I suddenly realized what my error is. I have not created the object removal code to remove the line if the condition is no longer met. For heaven's sake...
                          Excuse me while I go step in front of the firing squad

                          In my defense I moved my elderly mom in; a bit distractive...

                          Navy Parlance: The word 'gripe' is a systems failure report from a user, such as an aircraft pilot.

                          Cheers & thank you for your indulgence re mentally challenged

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Nightmaregpu, Yesterday, 10:55 AM
                          4 responses
                          26 views
                          0 likes
                          Last Post NinjaTrader_Gaby  
                          Started by PH_GMT, 02-12-2025, 12:40 PM
                          10 responses
                          62 views
                          0 likes
                          Last Post NinjaTrader_Gaby  
                          Started by Ringer13, Yesterday, 01:59 AM
                          2 responses
                          18 views
                          0 likes
                          Last Post Ringer13  
                          Started by pjsmith, Yesterday, 09:58 AM
                          2 responses
                          37 views
                          0 likes
                          Last Post pjsmith
                          by pjsmith
                           
                          Started by MiCe1999, 01-29-2025, 07:04 PM
                          1 response
                          34 views
                          1 like
                          Last Post icebergdelphi  
                          Working...
                          X