MV_AVG_R module routines - WIN32

Company Information Software Products TDF Market Reports Download Area take me Home

MV_AVG_R.C - MoVing AVeraGe Routines

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 -

???     *new_???_struct() ; // this call may have arguments for some inds.
void    free_???_struct( ??? *ind_ptr ) ;
void    reset_???_struct( ??? *ind_ptr ) ; 
or for example -

typedef struct {                // Simple moving averages
    float   sma ;
    int     ma_length ;
    int     data_idx ;
    int     enough_data ;
    float   *data ;             // internal data cache
}   SMA ;

SMA *new_sma_struct( int ma_length ) ; void free_sma_struct( SMA *sma_ptr ) ; void reset_sma_struct( SMA *sma_ptr ) ;

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 ) ;

Function new_sma_struct
Include file MV_AVG_R.H
Prototype SMA * new_sma_struct( int ma_length )
Remarks Allocate, initialize and return a virgin simple moving average structure.

Function reset_sma_struct
Include file MV_AVG_R.H
Prototype void reset_sma_struct( SMA *sma_ptr )
Remarks Reset a SMA data structure for a new data series.

Function update_sma_struct
Include file MV_AVG_R.H
Prototype void update_sma_struct( SMA *sma_ptr , float new_data )
Remarks Update the caller's SMA with the new data. Once we have collected enough data to calc the SMA, sma_ptr->enough_data will be set to TRUE and sma_ptr->sma can be used.

Function free_sma_struct
Include file MV_AVG_R.H
Prototype void free_sma_struct( SMA *sma_ptr )
Remarks Return the SMA and its sub-structure memory to the system.

Function calc_sma_ind
Include file MV_AVG_R.H
Prototype void calc_sma_ind( DATA_REC *data_ptr , int data_cnt , int which_field , float *inds , int ma_length )
Remarks Calculate a simple moving average indicator of ma_length for the requested field for the DATA_REC data series. The indicator will be built in the caller's data area inds.

Function new_ema_struct
Include file MV_AVG_R.H
Prototype EMA * new_ema_struct( int ma_length )
Remarks Allocate, initialize and return a virgin exponential moving average structure.

Function reset_ema_struct
Include file MV_AVG_R.H
Prototype void reset_ema_struct( EMA *ema_ptr )
Remarks Reset a EMA data structure for a new data series.

Function update_ema_struct
Include file MV_AVG_R.H
Prototype void update_ema_struct( EMA *ema_ptr , float new_data )
Remarks Update the caller's EMA with the new data. Once we have collected enough data for the EMA to be up to speed, ema_ptr->enough_data will be set to TRUE. Since this is a EMA the var ema_ptr->ema will always have a value.

Function calc_ema_ind
Include file MV_AVG_R.H
Prototype void calc_ema_ind( DATA_REC *data_ptr , int data_cnt , int which_field , float *inds , int ma_length )
Remarks Calculate a exponential moving average indicator of ma_length for the requested field for the DATA_REC data series. The indicator will be built in the caller's data area inds.

Function calc_ema_coeff
Include file MV_AVG_R.H
Prototype void calc_ema_coeff( int ma_length , float *coeff_a , float *coeff_b )
Remarks Calculate the coefficients for a exponential moving average. They would be used as -> new_ema = ( new_data * coeff_a ) + ( old_ema * coeff_b ).

Function shift_moving_average
Include file MV_AVG_R.H
Prototype void shift_moving_average( int shift_offset , int data_cnt , float *inds )
Remarks This routine will take a array of floats (which can be either moving averages or other indicators) and will shift data_cnt items by the shift_offset amount. If the shift_offset is positive the indicator series will be shifted forward in time yielding a leading indicator else if negative the series will be shift backwards in time yeilding a lagging indicator. The cells that are freed up by the shift will be set to the indicator magic cookie display value IND_VAL_INVALID so they will not be plotted. It is the caller's responsibility to insure the float array has enough room for the positive shifted values and that his count of indicators in the array are updated for the shift, for negative shifts the first shift_offset number of data items will be dropped into the bit bucket.

Table of Contents Function Index

generated on 22 September 1998 - 12:55:16
© 1998 Tierra del Fuego Ltd.