cat brain.log | less

Getting it down on `paper`

Parabolic SAR Calculation

Below you will find demonstrative code that calculates the Parabolic SAR indicator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
 * Computes the Parabolic SAR, as originally developed by Welles Wilder.
 *
 * @param array $his Extreme Points: The highest high
 * @param array $los Extreme Points: The lowest low
 * @param float $afInc Acceleration Factor Increment, for each time point
 *   Lower increment = less sensitive (indicitive)
 * @param float $afMax Acceleration Factor Max, rno matter how long the trend
 *   Lower max step = less sensitive (reactive)
 * @param bool $withDir Indicates if trend direction should be returned.
 * @return array 
 *  If $withDir is true, result is tuple of length 2.
 *    Tuple contains ( array of SAR values, array of trend direction )
 *  Else if $withDir is false, result is simply an array of SAR values.
 */
function parabolicSAR($his,$los,$afInc=0.02,$afMax=0.20,$withDir=false) {
 if( !is_array($his) || !is_array($los) ) {
   trigger_error("First 2 parameters must be arrays: his, los",E_USER_WARNING);
   return ($withDir) ? array(false,false) : false;
 }
 if( count($his) != count($los) ) { 
   trigger_error("Arrays must be equal length.", E_USER_ERROR);
   return ($withDir) ? array(false,false) : false;
 }
 if( count($his) < 2 ) { 
   return ($withDir) ? array(array(),array()) : array();
 }
 
 $keys = array_keys($his);
 $his = array_values($his);
 $los = array_values($los);
 
 // Initialize trend to whatever
 $trend = ($his[1]>=$his[0] || $los[0]<=$los[1]) ? +1 : -1;
 
 // Previous SAR: Use first data point&#039;s extreme value, depending on trend
 $pSAR = ($trend>0) ? $los[0] : $his[0];
 
 // Extreme point: Highest during uptrend || lowest during downtrend
 $EP = ($trend>0) ? $his[0] : $los[0];
 
 // Acceleration factor
 $AF = $afInc;
 
 // Initialize results based on trend guess
 $r = array($keys[0] => $pSAR);   // SAR Results
 $d = array($keys[0] => $trend);  // Trend Directions
 
 // Compute "tomorrow" SAR
 for($i=1,$n=count($keys)-1; $i<$n; $i++) {
   // Do for uptrend
   if( $trend > 0 ) { 
 
     // Making higher highs: accelerate
     if( $his[$i] > $EP ) {
       $EP = $his[$i];
       $AF = min($afMax,$AF+$afInc);
     }
 
     // Tomorrow&#039;s SAR based on today&#039;s price action.
     $nSAR = $pSAR + $AF * ($EP - $pSAR);
 
     // Rule: SAR can never be above prior period&#039;s low or the current low.
     $nSAR = ($i>0) ? min($los[$i],$los[$i-1],$nSAR) : min($los[$i],$nSAR);
 
     // Rule: If SAR crosses tomorrow&#039;s price range, the trend switches.
     if( $nSAR > $los[$i+1] ) { 
       $trend = -1;
       $nSAR = $EP;      // set to the last EP recorded on the previous trend
       $EP = $los[$i+1]; // reset accordingly to this period&#039;s maximum
       $AF = $afInc;     // reset to its initial value of 0.02.
     }
 
   } else {
   // Do for downtrend
 
     // Making lower lows: accelerate
     if( $los[$i] < $EP ) { 
       $EP = $los[$i];
       $AF = min($afMax,$AF+$afInc);
     }
 
     // Tomorrow&#039;s SAR based on today&#039;s price action.
     $nSAR = $pSAR + $AF * ($EP - $pSAR);
 
     // Rule: SAR can never be below prior period&#039;s highs or the current high.
     $nSAR = ($i>0) ? max($his[$i],$his[$i-1],$nSAR) : max($his[$i],$nSAR);
 
     // Rule: If SAR crosses tomorrow&#039;s price range, the trend switches.
     if( $nSAR < $his[$i+1] ) {
       $trend = +1;
       $nSAR = $EP;      // set to the last EP recorded on the previous trend
       $EP = $his[$i+1]; // reset accordingly to this period&#039;s maximum
       $AF = $afInc;     // reset to its initial value of 0.02.
     }
 
   } // end: if uptrend else downtrend
 
   $r[$keys[$i+1]] = $nSAR;
   $d[$keys[$i+1]] = $trend;
   $pSAR = $nSAR;
 } // END: for(i..n)
 
 return ($withDir) ? array($r,$d) : $r;
} // END: function parabolicSAR($his,$los,$afInc=0.02,$afMax=0.20)

 

Sources:

 

Comments

No comments so far.

(comments are closed)