Rank: Advanced Member
Groups: Registered, Registered Users, Subscribers Joined: 9/8/2004(UTC) Posts: 2,266
Was thanked: 1 time(s) in 1 post(s)
|
Here is the sar calculation :
[code:1:7a933bb5a3] void PASCAL Calc_ParabolicSAR(struct Data_rec *Data,
struct DataInfo_rec *psDest,
float fStep, float maximum)
{
int lp; // 'Loop' variable to loop thru all data.
float ep, epYesterday; // Column #2, Extreme Price.
float diff; // Column #3, difference between EP and SAR.
float af; // Column #4, Acceleration Factor.
float af_x_diff; // Column #5, AF times DIFF.
float nextSAR; // This is the highest high during a long trade or the
// lowest low during a short trade. It becomes the next
// SAR value after a trade occurs.
BOOL longPos; // TRUE if in a long trade, FALSE if short.
BOOL firstValidTrade; // The first trade is assumed to be invalid. This
// variable keeps track of the second trade (ie, the
// first valid trade) so we can inform the caller.
float initTotal; // Used to total the first MIN_DAYS-1 of data to see
// what general trend is.
# define MIN_DAYS 15+1 // Minimum number of days of data to determine
// first trade.
# ifdef SNINclc2_TRACE
FILE *traceFile;
#define TRACE_FILE_NAME "TRACE"
#endif
if( NoData( Data->Clse.FirstValid, Data->Clse.LastValid, &psDest->FirstValid, &psDest->LastValid ) )
return;
if ( Data->Clse.LastValid > MIN_DAYS )
{
/*---------------------------------------------*/
/* Initialze day #1. This was written based on*/
/* Mr. Wilder's comments on page 16. */
/*---------------------------------------------*/
firstValidTrade = 0;
psDest->FirstValid =
psDest->LastValid = Data->Clse.LastValid;
initTotal = 0.0f;
/*---------------------------------------------*/
/* Total the first MIN_DAYS-1 days to see if we*/
/* start in an up trend or a down trend. (We */
/* start in an up trend if the price on day */
/* MIN_DAYS is greater than the average price */
/* during the first MIN_DAYS-1 days). */
/*---------------------------------------------*/
for ( lp = 1; lp <= MIN_DAYS - 1; lp++ )
initTotal += Data->Clse.value [lp];
/*---------------------------------------------*/
/* We want to capture the first trade. So if */
/* the market is up, we want to start long. */
/*---------------------------------------------*/
longPos = Data->Clse.value [MIN_DAYS] > ( initTotal / ( MIN_DAYS - 1 ) );
/*---------------------------------------------*/
/* We don't know what to use for the first SIP */
/* we'll just guess using the first day's data.*/
/*---------------------------------------------*/
if ( longPos )
nextSAR = Data->Low.value [1];
else
nextSAR = Data->High.value [1];
/*---------------------------------------------*/
/* The SAR values are kept in the indicator */
/* storage array. We will init it to the next */
/* SAR too. */
/*---------------------------------------------*/
psDest->value [1] = nextSAR;
ep = nextSAR;
epYesterday = nextSAR;
af =
diff =
af_x_diff = 0.0f;
# ifdef SNINclc2_TRACE
traceFile = fopen(TRACE_FILE_NAME, "wt");
fprintf(traceFile,"DAY --High- --Low-- --SAR-- ---EP--- --DIFF- --AF--- AF*DIFF\\n");
#endif
/*---------------------------------------------*/
/* Loop through day 1, to the day before the */
/* last day (this is done because each pass */
/* thru this loop sets the following day's val.*/
/*---------------------------------------------*/
for ( lp = 1; lp < Data->Clse.LastValid; lp++ )
{
# ifdef SNINclc2_TRACE
fprintf(traceFile,"%3d %7.3f %7.3f %7.3f ",
lp,
Data->High.value[lp],
Data->Low.value[lp],
psDest->value[lp]);
#endif
/*---------------------------------------------*/
/* Column #2: Calculate the Extreme Price (EP).*/
/*---------------------------------------------*/
if ( longPos )
{
ep = max(Data->High.value[lp], epYesterday);
nextSAR = max(Data->High.value[lp], nextSAR);
} /* if longPos */
else
{
ep = min(Data->Low.value[lp], epYesterday);
nextSAR = min(Data->Low.value[lp], nextSAR);
} /* else */
# ifdef SNINclc2_TRACE
fprintf(traceFile," %7.3f", ep);
#endif
/*---------------------------------------------*/
/* Column #3: Calculate the difference between */
/* the SAR and the EP. */
/*---------------------------------------------*/
diff = (float)fabs( psDest->value [lp] - ep );
# ifdef SNINclc2_TRACE
fprintf(traceFile," %7.3f", diff);
#endif
/*---------------------------------------------*/
/* Column #4: Calculate the acceleration factor*/
/*---------------------------------------------*/
if ( ep != epYesterday )
{
if ( af + fStep < maximum )
{
af += fStep;
} /* if af */
else
{
af = maximum;
} /* else */
} /* if ep */
# ifdef SNINclc2_TRACE
fprintf(traceFile," %7.3f", af);
#endif
/*---------------------------------------------*/
/* Column #5: AF times the difference. (This */
/* doesn't really require an array for storage,*/
/* but it makes it easier to compare to with */
/* Wilder's book. */
/*---------------------------------------------*/
af_x_diff = (float)( af * diff );
# ifdef SNINclc2_TRACE
fprintf(traceFile," %7.3f\\n", af_x_diff);
#endif
/*---------------------------------------------*/
/* Column #1: Calculate the next day's SAR. */
/*---------------------------------------------*/
if ( longPos )
{
psDest->value [lp + 1] = psDest->value [lp] + af_x_diff;
/*---------------------------------------------*/
/* Don't allow tomorrow's SAR to be above */
/* today's Low. */
/*---------------------------------------------*/
[/code:1:7a933bb5a3]
|