How does AB handle a decimal when passed to a function expecting an integer like MA()? Truncate, round, round up, round down, something else?
Thanks in advance,
John
How does AB handle a decimal when passed to a function expecting an integer like MA()? Truncate, round, round up, round down, something else?
Thanks in advance,
John
I guess that you mean "period" parameter (i.e. second parameter), correct? Because first parameter (array) accepts just any number.
If you mean second parameter (i.e. period) then it truncates fractional part unless you use your own rounding
y = MA( Close, period ); // will truncate period
y = MA( Close, ceil( period ) ); // round up
y = MA( Close, floor( period ) ); // down
y = MA( Close, round( period ) ); // round to nearest integer
That is what I meant, thanks Tomasz!
Relatedly, if I wanted to calculate an indictor with variable periods like RSI(x); such that x varied over time is there a way to force recalculation each period or should I do that in a loop? What I am trying to get at is indicators with variable lookback periods that adjust over time.
Best regards,
John
Hello John,
There is a related topic on that, if it works for you.
Thanks for the trail head!
Best,
John
I just told AFL Assistant to do that variable period version of RSI:
function RSIVariablePeriod( array, period_array )
{
Change = array - Ref( array, -1 );
Gain = IIf( Change > 0, Change, 0 );
Loss = IIf( Change < 0, -Change, 0 );
// Calculate smoothing factor for AMA based on the period array.
// wilders smoothing is 1/N exponential
alpha = SafeDivide( 1, Nz( period_array, 1 ), 0 );
// Ensure alpha is within the valid range [0, 1] for AMA.
alpha = Max( 0, Min( 1, alpha ) );
AvgGain = AMA( Gain, alpha );
AvgLoss = AMA( Loss, alpha );
// Calculate Relative Strength (RS). Use SafeDivide to handle division by zero.
RS = SafeDivide( AvgGain, AvgLoss, Null );
// Calculate RSI. Use SafeDivide to handle division by zero (1 + RS).
calculatedRSI = 100 - SafeDivide( 100, 1 + RS, Null );
return calculatedRSI;
}
_SECTION_BEGIN("RSI Comparison");
Period = Param("RSI Period", 14, 2, 200, 1 );
// Calculate RSI using the custom function
CustomRSI = RSIVariablePeriod( Close, Period );
// Calculate RSI using the built-in function
BuiltInRSI = RSI( Period );
// Plot the custom RSI
Plot( CustomRSI, "Custom RSI " + Period, colorBlue, styleLine );
// Plot the built-in RSI
Plot( BuiltInRSI, "Built-in RSI " + Period, colorRed, styleLine );
// Plot horizontal lines for common RSI levels
PlotGrid( 30, colorRed, styleDashed, 1 );
PlotGrid( 70, colorGreen, styleDashed, 1 );
_SECTION_END();
So, this is the new assistant in the 7.x series of AB? Interesting!
Thanks,
John
This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.