ANSI C simple and exponential style moving average routines. This
module also contains a routine to support leading (what Joe DiNapoli
calls Key Of The Day) and lagging a moving average array or a display
indicator array.
Let's take this opportunity to show an overview to the stand-alone
indicator modules. The stand-alone indicator modules are designed to
be called by the user with the user provided data where as the CSTM_RPT
indicator routines are called by CSTM_RPT for the user. The standalone
modules all have a consistant user interface.
Each indicator will have its own typedef-ed data structure which will
contain all the internal vars necessary to calculate the indicator. This
data structure will also handle caching of the raw input data stream if
this is necessary for the calculations. There will be 2 routines for
allocation and deallocation for each indicator structure. There will also
be a reset routine to reset the internal vars for a new data series.
These routines will be of the form -
There will also be 2 routines that you can call to get an indicator
calculated for you. The first way is with an update_???_struct() call.
The form expects your program to handle stepping through each value of
the data series but it allows you to calculate an indicator off of any
data series not just normal DATA_REC data. Of course some indicators
do require normal DATA_REC (like a stochastic which needs high, low &
close to calc). Once the indicator is up to speed the enough_data field
will be set to TRUE and the indicator can be used. For example -
void update_sma_struct( SMA *sma_ptr , float new_data ) ;
// given - float *data_array ; // loaded with some data
sma_ptr = new_sma_struct( 20 ) ;
for( i = 0 ; i
update_sma_struct( sma_ptr , data_array[ i ] ) ;
if( sma_ptr->enough_data )
printf( "SMA = %f \n" , sma_ptr->sma ) ;
}
The other form directly calculates any indicator suitable for charting.
This means that the returned indicator also contains some magic cookie
values for the graphics subsystem - IND_VAL_INVALID which means that the
indicator is not up to speed and so graphics subsystem suppresses the plot
of that data point. The indicator will either be simple float arrays for
simple indicators like SMA's or typedef-ed structures for more complicated
indicator systems like Bollinger Bands. These routines expect to given a
DATA_REC data series and a which field to use flag. The calc routine will
handle the allocation & deallocation of the low-level indicator structure
but now your program will be responsible for ownership of the higher level
indicator display structure.
void calc_sma_ind( DATA_REC *data_ptr ,
int data_cnt , int which_field , float *inds , int ma_length ) ;
// given - DATA_REC *data_array // loaded with 50 bars of some data
// and a - float *sma_20_inds ;
sma_20_inds = my_calloc( sizeof( float ) * 50 ) ;
calc_sma_ind( data_array , 50 , DF_CLOSE , sma_20_inds , 20 ) ;
// that's all you need to do and then you can
grfx = draw_cmd_chart( GRX_FULL__SCR , GRX_FG_FLT_DATA , 50 , sma_20_inds
, "SMA 20" ) ; // to display it
And here is a more complicated example -
typedef struct { // Bollinger Bands indicator display structure
float *upper_band ;
float *mean ;
float *lower_band ;
float *percent_b ;
} BBAND_IND ;
bband_ptr = new_bband_ind_struct( 20 )
calc_bband_ind( data_array , 50 , DF_CLOSE , sma_20_inds , 20 , 2.0 ) ;
// that's all you need to do and then you can
grfx = draw_cmd_chart( GRX_FULL_SCR , GRX_FG_DATA_REC , 50 , data_array ,
"Some Data with BBands" ) ;
overlay_line_plot( grfx , bband_ptr->upper_band , MA1_COLOR ) ;
overlay_line_plot( grfx , bband_ptr->mean , MA1_COLOR ) ;
overlay_line_plot( grfx , bband_ptr->lower_band , MA1_COLOR ) ;
free_bband_ind_struct( bband_ptr ) ;