// Downloaded From https://www.WiseStockTrader.com
_SECTION_BEGIN("Measurer");
if (ParamToggle("Enabled", "No|Yes", 1))
{
  EnableTextOutput(False);
  
  what = ParamList("Object", "Bar count|Price change (close)|Price change (high/low)|Average volume|Total volume|Average BW MFI", 0);
  mColor = ParamColor("Color", colorBlue);
  mStyle = ParamStyle("Style", styleLine | styleDashed, styleDashed | styleThick);
  mbgColor = ParamColor("Label Bg Color", colorWhite);
  showInTitle = ParamToggle("Show in title", "No|Yes", 1);
  
  SelectedBar = SelectedValue(BarIndex());
  StartRangeBar = BeginValue(BarIndex());
  FinishRangeBar = EndValue(BarIndex());
  
  // determine start & end of range
  if (StartRangeBar > 0 AND FinishRangeBar < BarCount - 1) // range defined by markers
  {
    start = StartRangeBar;
    end = FinishRangeBar;
  }
  else
  {
    if (StartRangeBar > 0) // range defined by start marker & selection
    {
      start = StartRangeBar;
      end = SelectedBar;
    }
    else // range defined by selected bar and end
    {
      start = SelectedBar;
      end = FinishRangeBar;
    }
  }
  if (start > end) { tmp = start; start = end; end = tmp; }
  
  // measuring
  switch (what)
  {
    // bar count
    case "Bar count":
      Value = end - start;
      Label = WriteVal(Value, 1.0);
      if (showInTitle) Title = "\n" + EncodeColor(mColor) + "Measuring bars: " + WriteVal(Value, 1.0);
      break;
    // price change (close)
    case "Price change (close)":
      Value = Close[end] - Close[start];
      Label = WriteVal(Value, 1.3);
      if (showInTitle) Title = "\n" + EncodeColor(mColor) + "Measuring price change (close): " + WriteVal(Value, 1.3);
      if (start != end) Plot(LineArray(start, Close[start], end, Close[end]), "", mColor, mStyle | styleNoLabel);
      break;
    // price change (high/low)
    case "Price change (high/low)":
      if (High[end] >= High[start])
        Value = High[end] - Low[start];
      else
        Value = -(High[start] - Low[end]);
      Label = WriteVal(Value, 1.3);
      if (showInTitle) Title = "\n" + EncodeColor(mColor) + "Measuring price change (high/low): " + WriteVal(Value, 1.3);
      if (start != end)
      {
        if (High[end] >= High[start])
          Plot(LineArray(start, Low[start], end, High[end]), "", mColor, mStyle | styleNoLabel);
        else
          Plot(LineArray(start, High[start], end, Low[end]), "", mColor, mStyle | styleNoLabel);
      }
      break;
    // Average volume
    case "Average volume":
      Bars = end - start + 1; // (inclusive)
      Value = 0;
      for (i = start; i <= end AND i < BarCount; i++) Value += Volume[i];
      Value = Value / Bars;
      Label = WriteVal(Value, 1.0);
      if (showInTitle) Title = "\n" + EncodeColor(mColor) + "Measuring average volume: " + WriteVal(Value, 1.0);
      break;
    // Total volume
    case "Total volume":
      Value = 0;
      for (i = start; i <= end AND i < BarCount; i++) Value += Volume[i];
      Value = Value;
      Label = WriteVal(Value, 1.0);
      if (showInTitle) Title = "\n" + EncodeColor(mColor) + "Measuring total volume: " + WriteVal(Value, 1.0);
      break;
    // Average BW MFI
    case "Average BW MFI":
      Bars = end - start + 1; // (inclusive)
      Value = 0;
      for (i = start; i <= end AND i < BarCount; i++) Value += 100000 * (High[i] - Low[i]) / Volume[i];
      Value = Value / Bars;
      fmt = 1;
      if (log10(Value) < 0) fmt += (-floor(log10(Value)) + 2) / 10;
      Label = WriteVal(Value, fmt);
      if (showInTitle) Title = "\n" + EncodeColor(mColor) + "Measuring average BW MFI: " + WriteVal(Value, fmt);
      break;
  }
  
  if (start != end)
  {
    // get highest visible value
    Hh = -1e8;
    for (i = Status("firstvisiblebarindex"); i < Status("lastvisiblebarindex") AND i < BarCount; i++)
    {
      if (High[i] > Hh) Hh = High[i];
    }
    VGrid = (Status("axismaxy") - Status("axisminy")) * 0.01;
    VH = Hh;
    Plot(LineArray(start, VH, end, VH), "", mColor, mStyle | styleNoLabel);
    PlotText(Label, start + (end - start) / 2 - 2, VH + VGrid, mColor, mbgColor);
  }
  EnableTextOutput(True);
}
_SECTION_END();