Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Can Someone explain this piece of code Part 1

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

    Can Someone explain this piece of code Part 1

    I am an amateur coder. I am trying to learn new concepts as I go by looking at other indicators. Can someone please explain these two pieces of code. The person who wrote this didn't put in notes explaining what it does. Thank you.

    1

    double high0 = !(Input is PriceSeries || Input is Bars) ? Input[0] : High[0];
    double low0 = !(Input is PriceSeries || Input is Bars) ? Input[0] : Low[0];
    double close0 = !(Input is PriceSeries || Input is Bars) ? Input[0] : Close[0];


    2

    if (BarsArray[0].BarsType.IsRemoveLastBarSupported && CurrentBar < saveCurrentBar)
    {
    currentSwingHigh = SwingHighPlot.IsValidDataPoint(0) ? SwingHighPlot[0] : 0;
    currentSwingLow = SwingLowPlot.IsValidDataPoint(0) ? SwingLowPlot[0] : 0;
    lastSwingHighValue = swingHighSeries[0];
    lastSwingLowValue = swingLowSeries[0];
    swingHighSeries[Strength] = 0;
    swingLowSeries[Strength] = 0;

    lastHighCache.Clear();
    lastLowCache.Clear();
    for (int barsBack = Math.Min(CurrentBar, constant) - 1; barsBack >= 0; barsBack--)
    {
    lastHighCache.Add(!(Input is PriceSeries || Input is Bars) ? Input[barsBack] : High[barsBack]);
    lastLowCache.Add(!(Input is PriceSeries || Input is Bars) ? Input[barsBack] : Low[barsBack]);
    }
    saveCurrentBar = CurrentBar;
    return;
    }

    #2
    Hello jamestrader21x,

    Thanks for your post.

    This statement: double high0 = !(Input is PriceSeries || Input is Bars) ? Input[0] : High[0]; Is assigning either the Input value or the High data series value to the double variable high0
    The "?" is asking is the preceding condition true, if so then use the Input[0] if the preceding condition was false use the High[0] instead. The condition being asked is if the Input is NOT a price series or if the Input is NOT a bar. The ultimate goal is to check to see if the input is an Indicator instead of a price series or bar type and assign the input value from the indicator or the High of the data series.

    Here is an external reference to the ternary conditional operator "?": https://docs.microsoft.com/en-us/dot...ional-operator

    I suspect that the above answers your question for both sections of code as the second section contains a number of uses of the same C# ternary conditional operator.

    Comment


      #3
      Oh! It's the short hand version of writing an if statement! I haven't seen that in quite awhile. I completely forgot about it! I'm so used to using if(parameter) {do something;} I appreciate the review.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      650 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      370 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      109 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      574 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      577 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X