Login Page - Create Account

Support Board


Date/Time: Tue, 22 Apr 2025 21:18:46 +0000



Post From: Is there a Moving Average - Time Period ASCIL function or similar?

[2025-01-05 02:53:42]
cmet - Posts: 690
No, but you can create one,

// Function to calculate Moving Average Time Period

void CalculateMATPFunction(
SCFloatArrayRef InputArray,
SCFloatArrayRef OutputArray,
SCDateTimeArrayRef DateTimeArray,
SCDateTime TimePeriodLength,
int StartIndex,
int EndIndex,
SCStudyInterfaceRef sc)
{
int LookbackStartIndex = sc.GetContainingIndexForSCDateTime(sc.ChartNumber, (DateTimeArray[StartIndex] - TimePeriodLength).GetAsDouble());

for (int BarIndex = StartIndex; BarIndex < EndIndex; BarIndex++)
{
float Sum = 0;
int Count = 0;

for (int LookbackIndex = LookbackStartIndex; LookbackIndex <= BarIndex; LookbackIndex++)
{
if (InputArray[LookbackIndex] == 0.0f)
continue;

Sum += InputArray[LookbackIndex];
Count++;
}

if (Count > 0)
OutputArray[BarIndex] = Sum / Count;
else
OutputArray[BarIndex] = 0;

if (BarIndex < EndIndex - 1)
{
SCDateTime NewLookbackStartDateTime = DateTimeArray[BarIndex + 1] - TimePeriodLength;

while (true)
{
SCDateTime CurrentAbsoluteTimeDifference = SCDateTime(DateTimeArray[LookbackStartIndex] - NewLookbackStartDateTime).GetAbsoluteValue();
SCDateTime NextAbsoluteTimeDifference = SCDateTime(DateTimeArray[LookbackStartIndex + 1] - NewLookbackStartDateTime).GetAbsoluteValue();

if (NextAbsoluteTimeDifference > CurrentAbsoluteTimeDifference)
break;

LookbackStartIndex++;
}
}
}
}