Login Page - Create Account

Support Board


Date/Time: Wed, 12 Feb 2025 13:19:12 +0000



Post From: High / Low for the time period = extended: wrong start on the tick chart

[2020-10-08 18:09:06]
Ackin - Posts: 1865
User212764)
Could you paste the code for that previous version from v2168 into a post so that we can compare and contrast the code?



//xx68

SCSFExport scsf_HighLowForTimePeriodExtendedLines(SCStudyInterfaceRef sc)
{
  SCSubgraphRef Subgraph_HighOfDay = sc.Subgraph[0];
  SCSubgraphRef Subgraph_LowOfDay = sc.Subgraph[1];

  SCInputRef Input_StartTime = sc.Input[0];
  SCInputRef Input_EndTime = sc.Input[1];
  SCInputRef Input_NumberDaysToCalculate = sc.Input[2];
  SCInputRef Input_Version = sc.Input[4];
  SCInputRef Input_LineStopTimeInput = sc.Input[5];
  SCInputRef Input_DisplayHighLowIncrementally = sc.Input[6];

  SCInputRef Input_InputDataHigh = sc.Input[7];
  SCInputRef Input_InputDataLow = sc.Input[8];
  SCInputRef Input_FridayEveningExtendsIntoSunday = sc.Input[9];

  if (sc.SetDefaults)
  {
    sc.GraphName      = "High/Low for Time Period - Extended";
    sc.GraphRegion      = 0;
    sc.AutoLoop        = 0;
    
    Subgraph_HighOfDay.Name = "High";
    Subgraph_HighOfDay.DrawStyle = DRAWSTYLE_STAIR_STEP;
    Subgraph_HighOfDay.PrimaryColor = RGB(0,255,0);
    Subgraph_HighOfDay. DrawZeros = false;
    
    Subgraph_LowOfDay.Name = "Low";
    Subgraph_LowOfDay.DrawStyle = DRAWSTYLE_STAIR_STEP;
    Subgraph_LowOfDay.PrimaryColor = RGB(255,0,255);
    Subgraph_LowOfDay.DrawZeros = false;

    Input_StartTime.Name = "Start Time";
    Input_StartTime.SetTime(0);

    Input_EndTime.Name = "End Time";
    Input_EndTime.SetTime(SECONDS_PER_DAY - 1);
  
    Input_NumberDaysToCalculate.Name= "Number Of Days To Calculate";
    Input_NumberDaysToCalculate.SetInt(120);

    Input_LineStopTimeInput.Name = "Line Stop Time";
    Input_LineStopTimeInput.SetTime(SECONDS_PER_DAY - 1);

    Input_DisplayHighLowIncrementally.Name = "Display High Low Incrementally";
    Input_DisplayHighLowIncrementally.SetYesNo( true);

    Input_InputDataHigh.Name = "Input Data High";
    Input_InputDataHigh.SetInputDataIndex(SC_HIGH);

    Input_InputDataLow.Name = "Input Data Low";
    Input_InputDataLow.SetInputDataIndex(SC_LOW);

    Input_FridayEveningExtendsIntoSunday.Name = "Friday Evening Extends Into Sunday";
    Input_FridayEveningExtendsIntoSunday.SetYesNo(false);

    Input_Version.SetInt(3);

    return;
  }

  if (Input_Version.GetInt() == 1)
  {
    Input_InputDataHigh.SetInputDataIndex(SC_HIGH);
    Input_InputDataLow.SetInputDataIndex(SC_LOW);

    Input_Version.SetInt(2);
  }

  if (Input_Version.GetInt() <= 2)
  {
    Input_LineStopTimeInput.SetTime(sc.EndTime1);
    Input_Version.SetInt(3);
  }

  if (Input_NumberDaysToCalculate.GetInt() <= 0)
    Input_NumberDaysToCalculate.SetInt(10000);
  
  float & HighOfPeriod = sc.GetPersistentFloatFast(1);
  float & LowOfPeriod = sc.GetPersistentFloatFast(2);

  SCDateTime DaysToCalculateStartDateTime = sc.GetTradingDayStartDateTimeOfBar(sc.BaseDateTimeIn[sc.ArraySize - 1]) - (Input_NumberDaysToCalculate.GetInt() - 1);

  // Loop through chart bars starting at the Update Start Index
  for (int Index = sc.UpdateStartIndex; Index < sc.ArraySize; Index++ )
  {

    const SCDateTime CurrentBarDateTime = sc.BaseDateTimeIn[Index];

    const SCDateTime PreviousBarDateTime = sc.BaseDateTimeIn[max(0, Index - 1)];

    SCDateTime ResetTime;
    ResetTime.SetTime(Input_LineStopTimeInput.GetTime());

    bool NeedReset = (PreviousBarDateTime.GetTimeInSeconds() < ResetTime.GetTimeInSeconds() && CurrentBarDateTime.GetTimeInSeconds() >= ResetTime.GetTimeInSeconds())
      || (PreviousBarDateTime.GetTimeInSeconds() < ResetTime.GetTimeInSeconds() && CurrentBarDateTime.GetTimeInSeconds() < ResetTime.GetTimeInSeconds()
        && PreviousBarDateTime.GetDate() != CurrentBarDateTime.GetDate());


    NeedReset |= Index == 0;

    if (sc.BaseDateTimeIn[Index] < DaysToCalculateStartDateTime)
      continue;

    SCDateTime StartDateTime;
    SCDateTime EndDateTime;

    StartDateTime = CurrentBarDateTime.GetDate();
    EndDateTime = CurrentBarDateTime.GetDate();

    StartDateTime.SetTime(Input_StartTime.GetTime());
    EndDateTime.SetTime(Input_EndTime.GetTime());

    bool TimesAreReversed = false;
    if (Input_StartTime.GetTime() > Input_EndTime.GetTime())
    {
      TimesAreReversed = true;
      if (CurrentBarDateTime.GetTimeInSeconds() < Input_StartTime.GetTime())
      {
        StartDateTime.AddDays(-1);
      }
      else
      {
        EndDateTime.AddDays(1);
      }
    }

    if(TimesAreReversed && Input_FridayEveningExtendsIntoSunday.GetYesNo())
    {
      SCDateTime TradingDayDate(sc.GetTradingDayDate(CurrentBarDateTime));
      int DayOfWeek = TradingDayDate.GetDayOfWeek();
      if (DayOfWeek == MONDAY)
      {
        StartDateTime.AddDays(-2);
        NeedReset = false;
      }
    }

    //reset
    if (NeedReset)
    {
      HighOfPeriod = -FLT_MAX;
      LowOfPeriod = FLT_MAX;    
    }


    bool OutsideTimeRange = true;
    if (CurrentBarDateTime >= StartDateTime && CurrentBarDateTime <= EndDateTime)
    {
      OutsideTimeRange = false;

      if (HighOfPeriod < sc.BaseData[Input_InputDataHigh.GetInputDataIndex()][Index])
        HighOfPeriod = sc.BaseData[Input_InputDataHigh.GetInputDataIndex()][Index];

      if (LowOfPeriod > sc.BaseData[Input_InputDataLow.GetInputDataIndex()][Index])
        LowOfPeriod = sc.BaseData[Input_InputDataLow.GetInputDataIndex()][Index];
    }

    if (HighOfPeriod == -FLT_MAX)
      continue;

    // Set/update all values for current day
    int BackIndex = Index;

    while (true)
    {
      if(BackIndex < 0)
        break;

      const SCDateTime BackIndexDateTime = sc.BaseDateTimeIn[BackIndex];

      if (!OutsideTimeRange && BackIndexDateTime < StartDateTime)
        break;

      Subgraph_HighOfDay[BackIndex] = HighOfPeriod;
      Subgraph_LowOfDay[BackIndex] = LowOfPeriod;


      if (OutsideTimeRange || Input_DisplayHighLowIncrementally.GetYesNo())
        break;

      BackIndex--;

      if(sc.UpdateStartIndex != 0 && BackIndex >= 0)
        sc.EarliestUpdateSubgraphDataArrayIndex = BackIndex;
    }
  }
}






//xx77

SCSFExport scsf_HighLowForTimePeriodExtendedLines(SCStudyInterfaceRef sc)
{
  SCSubgraphRef Subgraph_HighOfDay = sc.Subgraph[0];
  SCSubgraphRef Subgraph_LowOfDay = sc.Subgraph[1];

  SCInputRef Input_StartTime = sc.Input[0];
  SCInputRef Input_EndTime = sc.Input[1];
  SCInputRef Input_NumberDaysToCalculate = sc.Input[2];
  SCInputRef Input_Version = sc.Input[4];
  SCInputRef Input_LineStopTimeInput = sc.Input[5];
  SCInputRef Input_DisplayHighLowIncrementally = sc.Input[6];

  SCInputRef Input_InputDataHigh = sc.Input[7];
  SCInputRef Input_InputDataLow = sc.Input[8];
  SCInputRef Input_FridayEveningExtendsIntoSunday = sc.Input[9];

  if (sc.SetDefaults)
  {
    sc.GraphName = "High/Low for Time Period - Extended";
    sc.GraphRegion = 0;
    sc.AutoLoop = 0;
    
    Subgraph_HighOfDay.Name = "High";
    Subgraph_HighOfDay.DrawStyle = DRAWSTYLE_STAIR_STEP;
    Subgraph_HighOfDay.PrimaryColor = RGB(0,255,0);
    Subgraph_HighOfDay. DrawZeros = false;
    
    Subgraph_LowOfDay.Name = "Low";
    Subgraph_LowOfDay.DrawStyle = DRAWSTYLE_STAIR_STEP;
    Subgraph_LowOfDay.PrimaryColor = RGB(255,0,255);
    Subgraph_LowOfDay.DrawZeros = false;

    Input_StartTime.Name = "Start Time";
    Input_StartTime.SetTime(0);

    Input_EndTime.Name = "End Time";
    Input_EndTime.SetTime(SECONDS_PER_DAY - 1);
  
    Input_NumberDaysToCalculate.Name= "Number Of Days To Calculate";
    Input_NumberDaysToCalculate.SetInt(120);

    Input_LineStopTimeInput.Name = "Line Stop Time";
    Input_LineStopTimeInput.SetTime(SECONDS_PER_DAY - 1);

    Input_DisplayHighLowIncrementally.Name = "Display High Low Incrementally";
    Input_DisplayHighLowIncrementally.SetYesNo( true);

    Input_InputDataHigh.Name = "Input Data High";
    Input_InputDataHigh.SetInputDataIndex(SC_HIGH);

    Input_InputDataLow.Name = "Input Data Low";
    Input_InputDataLow.SetInputDataIndex(SC_LOW);

    Input_FridayEveningExtendsIntoSunday.Name = "Friday Evening Extends Into Sunday";
    Input_FridayEveningExtendsIntoSunday.SetYesNo(false);

    Input_Version.SetInt(3);

    return;
  }

  if (Input_Version.GetInt() == 1)
  {
    Input_InputDataHigh.SetInputDataIndex(SC_HIGH);
    Input_InputDataLow.SetInputDataIndex(SC_LOW);

    Input_Version.SetInt(2);
  }

  if (Input_Version.GetInt() <= 2)
  {
    Input_LineStopTimeInput.SetTime(sc.EndTime1);
    Input_Version.SetInt(3);
  }

  if (Input_NumberDaysToCalculate.GetInt() <= 0)
    Input_NumberDaysToCalculate.SetInt(10000);
  
  float & HighOfPeriod = sc.GetPersistentFloatFast(1);
  float & LowOfPeriod = sc.GetPersistentFloatFast(2);

  const bool IsInputTimesReversed = Input_StartTime.GetTime() > Input_EndTime.GetTime();

  //Make sure the Line Stop Time is outside of the time range.
  if (IsInputTimesReversed)
  {
    if (Input_LineStopTimeInput.GetTime() >= Input_StartTime.GetTime()
      || Input_LineStopTimeInput.GetTime() < Input_EndTime.GetTime())
    {
      Input_LineStopTimeInput.SetTime(Input_EndTime.GetTime());
    }
  }
  else
  {
    if (Input_LineStopTimeInput.GetTime() >= Input_StartTime.GetTime()
      && Input_LineStopTimeInput.GetTime() < Input_EndTime.GetTime())
    {
      Input_LineStopTimeInput.SetTime(Input_EndTime.GetTime());
    }
  }

  SCDateTimeMS DaysToCalculateStartDateTime;
  DaysToCalculateStartDateTime.SetDate(sc.BaseDateTimeIn[sc.ArraySize - 1].GetDate());
  DaysToCalculateStartDateTime.SubtractDays(Input_NumberDaysToCalculate.GetInt() - 1);
  DaysToCalculateStartDateTime.SetTime(Input_StartTime.GetTime());

  if (IsInputTimesReversed)
  {
    if(sc.BaseDateTimeIn[sc.ArraySize - 1].GetTime() < Input_StartTime.GetTime())
      DaysToCalculateStartDateTime.SubtractDays(1);
  }
  

  int InitialCalculationIndex = 0;

  // Loop through chart bars starting at the Update Start Index
  for (int Index = sc.UpdateStartIndex; Index < sc.ArraySize; Index++ )
  {
    const SCDateTimeMS CurrentBarDateTime = sc.BaseDateTimeIn[Index];

    const SCDateTimeMS PreviousBarDateTime = sc.BaseDateTimeIn[max(0, Index - 1)];
    const SCDateTimeMS NextBarDateTime = sc.BaseDateTimeIn[Index + 1];

    SCDateTimeMS ResetTime;
    ResetTime.SetDate(CurrentBarDateTime.GetDate());
    ResetTime.SetTime(Input_LineStopTimeInput.GetTime());

    bool NeedReset =
      (PreviousBarDateTime < ResetTime && CurrentBarDateTime >= ResetTime);


    NeedReset |= Index == InitialCalculationIndex;

    if (sc.BaseDateTimeIn[Index] < DaysToCalculateStartDateTime)
    {
      InitialCalculationIndex = Index + 1;
      continue;
    }

    SCDateTimeMS StartDateTime;
    SCDateTimeMS EndDateTime;

    StartDateTime = CurrentBarDateTime.GetDate();
    EndDateTime = CurrentBarDateTime.GetDate();

    StartDateTime.SetTime(Input_StartTime.GetTime());
    EndDateTime.SetTime(Input_EndTime.GetTime());

    if (IsInputTimesReversed)
    {
      if (CurrentBarDateTime.GetTimeInSeconds() < Input_StartTime.GetTime())
      {
        StartDateTime.SubtractDays(1);
      }
      else
      {
        EndDateTime.AddDays(1);
      }
    }

    if(IsInputTimesReversed && Input_FridayEveningExtendsIntoSunday.GetYesNo())
    {
      SCDateTime TradingDayDate(sc.GetTradingDayDate(CurrentBarDateTime));
      int DayOfWeek = TradingDayDate.GetDayOfWeek();
      if (DayOfWeek == MONDAY)
      {
        StartDateTime.SubtractDays(2);
        NeedReset = false;
      }
    }

    //reset
    if (NeedReset)
    {
      HighOfPeriod = -FLT_MAX;
      LowOfPeriod = FLT_MAX;    
    }


    bool OutsideTimeRange = true;

    bool IsCurrentBarContainingOrGreaterThanStartDateTime =
      (CurrentBarDateTime < StartDateTime
        && NextBarDateTime > StartDateTime)
      || CurrentBarDateTime >= StartDateTime;

    if (IsCurrentBarContainingOrGreaterThanStartDateTime && CurrentBarDateTime <= EndDateTime)
    {
      OutsideTimeRange = false;

      if (HighOfPeriod < sc.BaseData[Input_InputDataHigh.GetInputDataIndex()][Index])
        HighOfPeriod = sc.BaseData[Input_InputDataHigh.GetInputDataIndex()][Index];

      if (LowOfPeriod > sc.BaseData[Input_InputDataLow.GetInputDataIndex()][Index])
        LowOfPeriod = sc.BaseData[Input_InputDataLow.GetInputDataIndex()][Index];
    }

    if (HighOfPeriod == -FLT_MAX)
      continue;

    // Set/update all values for current day
    int BackIndex = Index;

    while (true)
    {
      if(BackIndex < 0)
        break;

      const SCDateTimeMS BackIndexDateTime = sc.BaseDateTimeIn[BackIndex];
      const SCDateTimeMS NextBackIndexDateTime = sc.BaseDateTimeIn[BackIndex+1];

      bool IsCurrentBarContainingOrGreaterThanStartDateTime =
        (BackIndexDateTime < StartDateTime
          && NextBackIndexDateTime > StartDateTime)
        || BackIndexDateTime >= StartDateTime;

      if (!OutsideTimeRange && !IsCurrentBarContainingOrGreaterThanStartDateTime)
        break;

      Subgraph_HighOfDay[BackIndex] = HighOfPeriod;
      Subgraph_LowOfDay[BackIndex] = LowOfPeriod;


      if (OutsideTimeRange || Input_DisplayHighLowIncrementally.GetYesNo())
        break;

      BackIndex--;

      if(sc.UpdateStartIndex != 0 && BackIndex >= 0)
        sc.EarliestUpdateSubgraphDataArrayIndex = BackIndex;
    }
  }
}