Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

If - Else Not Working As Expected

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

    If - Else Not Working As Expected

    I have a strategy that paints candle bars using the code below (not calling an indicator). The candles paint properly at the close of each bar. However when I run my if-else statement the program goes into the else statement every bar on the chart even when it shouldn't. Since the if statement is true and paints an "up bar" I can't understand why it goes into the else section.


    double highestHigh = High[HighestBar(High,Length)];
    double lowestLow = Low[LowestBar(Low,Length)];
    double smaATR = SMA(ATR(Period),Period)[0];

    double upperVol = highestHigh - (Factor*smaATR);
    double lowerVol = lowestLow + (Factor*smaATR);

    if(Close[0] > upperVol && Close[0] > lowerVol)
    {
    BarBrush = upBrush;
    upsignal = 1 ;
    }
    else
    {
    BarBrush = dnBrush;
    downsignal = 2;
    Print(CurrentBar);
    }

    #2
    Hi,

    Have you tried adding Print() statements to see the values being tested in your if statement? I understand it could be a lot of output, so I have used and if to check for a specific bar # or time to get a single Print.

    There's not enough information at least for me to see what it could be without knowing the output of the formula of course.

    Good luck!

    Steven
    Last edited by StevenL; 02-10-2022, 12:20 AM.

    Comment


      #3
      Hey StevenL

      I did have a Print "Current Bar" in the Else section but i was misreading the results. After your suggestion I put another print statement in the If section and that showed me it is working fine. I have something wrong elsewhere in my code that led me to believe if was the If-Else. I'll work on that but thanks for the hint, it helped!

      Comment


        #4
        Great to hear. Interesting snippet of code you have going on there. If you need help with anything, let me know. I don't mind helping with coding as long as I'm learning and helping.

        I have code that I add to all of my indicators and strategies that allows me to toggle print statements from the properties panel. It's about the best you can do without compiling everything in debug mode and then attaching Visual Studio to the NinjaTrader.exe to debug. I actually haven't done that to a strategy yet.

        Good luck.

        Comment


          #5
          That code creates the painted candles that Linda Raschke uses. The other parameters are below if you want to make an indicator. I found I need to smooth out the candle color changes so I stay in a trade longer. I prefer them over Heiken Ashe or Range bars.

          int Period = 9;
          double Factor = 2.5;
          int Length = 16;
          Brush upBrush = Brushes.Blue
          Brush dnBrush = Brushes.Red

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Today, 05:17 AM
          0 responses
          44 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          124 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          65 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          42 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          46 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X