Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Logical OR ?

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

    Logical OR ?

    I have the following statement but the logical OR is not working - am I using the correct syntax?

    if (Trend_flo == 1)
    {
    if ((Open[0] <= TrStop[0]) | (High[0] <= TrStop[0]) | (Low[0] <= TrStop[0]) | (Close[0] <= TrStop[0]))
    {
    Trend_flo =0;
    BarColor = Color.Magenta;
    //Print("End of Trend_flo Long: "+ Trend_flo);
    Print("TrStop: Barnumber "+ CurrentBar+" is "+TrStop[0]+" Trendflo is "+Trend_flo);
    }
    else
    {
    if (LastBar < CurrentBar)
    {

    if (Close[0] > Close[1]) TrStop.Set(Low[0] - StDev * 1.382);
    if (Low[0] - High[1] > StDev) TrStop.Set(Low[0] - StDev * 0.618);
    if (TrStop[0] < TrStop[1]) TrStop.Set(TrStop[1]);
    if (TrStop[0] == 0) TrStop.Set(Close[0]);
    DrawDot("", true, 0, TrStop[0], Color.Orange);
    BarColor = Color.Blue;
    Print("TrStop: Barnumber "+ CurrentBar+" is "+TrStop[0]+" Trendflo is "+Trend_flo);
    }

    }

    }

    At Bar 22 Trendflo==1 and from BAr 23 -38 the code in orange is working correctly but at Bar Number 39 the current Low is less than Trstop '(Low[0] <= TrStop[0])' yet Trend_flo is not RESET (=0??

    This is weird?

    #2
    mefTrader, the logical OR operator uses two "pipes", not one.
    Code:
    ||
    AustinNinjaTrader Customer Service

    Comment


      #3
      It's 2 bars " || "

      if ((Open[0] <= TrStop[0]) || (High[0] <= TrStop[0]) || (Low[0] <= TrStop[0]) || (Close[0] <= TrStop[0]))

      Comment


        #4
        Just tried that and does not work for me either!

        if ((Open[0] <= TrStop[0]) || (High[0] <= TrStop[0]) || (Low[0] <= TrStop[0]) || (Close[0] <= TrStop[0]))
        {
        Trend_flo =0;
        BarColor = Color.Magenta;
        //Print("End of Trend_flo Long: "+ Trend_flo);
        Print("TrStop: Barnumber "+ CurrentBar+" is "+TrStop[0]+" Trendflo is "+Trend_flo);
        }

        Comment


          #5
          mefTrader, that is how the logical OR operator works. There is probably a bug lurking in your code somewhere. This post by Josh starts to describe the debugging process.
          AustinNinjaTrader Customer Service

          Comment


            #6
            Yes I am using Josh's debugging process already but can not see anything glaringly obvious .

            Trend_flo does not seem to get reset! when Trend_flo=1 and I have checked that all the conditions are true by printing them out to a file!

            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.
            if (CurrentBar < length1) return;

            if(Time[0].Date != Time[1].Date)
            {
            //Trend_flo =0;
            //Print("This is a message");
            //Print("Trend_flo initial condition: "+ Trend_flo);

            }
            double StDev = StdDev(length1)[0];
            //Print("StDev: "+ StDev);
            //double PerR = PercentR(length);
            double HH =0.0;
            double Divisor = 0.0;
            HH = MAX(High, length)[0];
            //Print("HH: "+ HH);
            Divisor = HH - MIN(Low, length)[0];
            //Print("Divisor: "+ Divisor);

            if (Divisor != 0)
            PercentR.Set(100 - ( ( HH - Close[0] ) / Divisor ) * 100);
            else
            PercentR.Set(0.0);

            //Print("PercentR[0]: "+ PercentR[0].ToString());

            if ((PercentR[0] >= 70) && (PercentR[1] < 70) && (Trend_flo == 0) && (Close[0] > TrStop[0]))
            {
            TrStop.Set(Low[0] - StDev*1.382);
            DrawDot("TStop_Long", true, 0, TrStop[0], Color.Orange);
            BarColor = Color.Blue;
            Trend_flo = 1;
            Print("This is a message");
            Print("Trend_flo Long: "+ Trend_flo);
            LastBar = CurrentBar;
            Print("TrStop: Barnumber "+ CurrentBar+" is "+TrStop[0]+" Trendflo is "+Trend_flo);
            }

            if ((PercentR[0] <= 30) && (PercentR[1] > 30) && (Trend_flo == 0) && (Close[0] < TrStop[0]))
            {
            TrStop.Set(High[0] + StDev*1.382);
            DrawDot("TStop_Short", true, 0, TrStop[0], Color.Yellow);
            BarColor = Color.Red;
            Trend_flo = -1;
            Print("Trend_flo Short: "+ Trend_flo);
            LastBar = CurrentBar;
            }

            if (Trend_flo == 1)
            {
            if ((Open[0] <= TrStop[0]) || (High[0] <= TrStop[0]) || (Low[0] <= TrStop[0]) || (Close[0] <= TrStop[0]))
            {
            Trend_flo =0;
            BarColor = Color.Magenta;
            //Print("End of Trend_flo Long: "+ Trend_flo);
            Print("TrStop: Barnumber "+ CurrentBar+" is "+TrStop[0]+" Trendflo is "+Trend_flo);
            }
            else
            {
            if (LastBar < CurrentBar)
            {

            if (Close[0] > Close[1]) TrStop.Set(Low[0] - StDev * 1.382);
            if (Low[0] - High[1] > StDev) TrStop.Set(Low[0] - StDev * 0.618);
            if (TrStop[0] < TrStop[1]) TrStop.Set(TrStop[1]);
            if (TrStop[0] == 0) TrStop.Set(Close[0]);
            DrawDot("TStop_Long"+ CurrentBar, true, 0, TrStop[0], Color.Orange);
            BarColor = Color.Blue;
            Print("TrStop: Barnumber "+ CurrentBar+" is "+TrStop[0]+" Trendflo is "+Trend_flo);
            }

            }
            }

            if (Trend_flo == -1)
            {
            if ((Open[0] >= TrStop[0]) || (High[0] >= TrStop[0]) || (Low[0] >= TrStop[0]) || (Close[0] >= TrStop[0]))
            {
            Trend_flo =0;
            BarColor = Color.Magenta;
            Print("TrStop: Barnumber "+ CurrentBar+" is "+TrStop[0]+" Trendflo is "+Trend_flo);
            }
            else
            {
            if (LastBar < CurrentBar)
            {
            if (Close[0] < Close[1]) TrStop.Set(High[0] + StDev * 1.382);
            if (Low[1] - High[0] > StDev) TrStop.Set(High[0] + StDev * 1.382);
            if (TrStop[0] > TrStop[1]) TrStop.Set(TrStop[1]);
            if (TrStop[0] == 0) TrStop.Set(Close[0]);
            DrawDot("TStop_Short"+ CurrentBar, true, 0, TrStop[0], Color.Yellow);
            BarColor = Color.Red;
            Print("TrStop: Barnumber "+ CurrentBar+" is "+TrStop[0]+" Trendflo is "+Trend_flo);
            }

            }
            }

            // Set the plot value
            Trend.Set(Trend_flo);
            //Trend.Set(1);

            }

            Comment


              #7
              mefTrader, unfortunately we are unable to provide debugging services due to bandwidth limitations.
              AustinNinjaTrader Customer Service

              Comment


                #8
                The following logic works

                if ((Open[0] <= TrStop[1]) || (High[0] <= TrStop[1]) || (Low[0] <= TrStop[1]) || (Close[0] <= TrStop[1])

                trying to understand why Open,High, Low, Close are evaluated first and have to reference the previous TrStop state - is for timing purposes - 'race conditions on logic'? i.e. classic '<=' versus = type assignments?

                BTW took my code directly from TS and seems to interpret it differently.

                Comment


                  #9
                  mefTrader <= means less than or equal to. It is a comparison, not an assignment (like = is).

                  NT and TS operate very different. I suspect that is where some of these problems are coming from.
                  AustinNinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by AttiM, 02-14-2024, 05:20 PM
                  12 responses
                  213 views
                  0 likes
                  Last Post DrakeiJosh  
                  Started by cre8able, 02-11-2023, 05:43 PM
                  3 responses
                  237 views
                  0 likes
                  Last Post rhubear
                  by rhubear
                   
                  Started by frslvr, 04-11-2024, 07:26 AM
                  8 responses
                  116 views
                  1 like
                  Last Post NinjaTrader_BrandonH  
                  Started by stafe, 04-15-2024, 08:34 PM
                  10 responses
                  47 views
                  0 likes
                  Last Post stafe
                  by stafe
                   
                  Started by rocketman7, Today, 09:41 AM
                  3 responses
                  11 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Working...
                  X