SAR oscillator

Download script
SAR oscillator

The SAR Oscillator is a technical analysis tool designed to visualize market momentum and trend direction by calculating the distance between current price action and a trailing stop logic. Rather than simply showing where a price is moving, it focuses on the "gap" or "tension" between the actual market price and the Parabolic Stop and Reverse (SAR) indicator.

Core Logic and Functionality

1. The Baseline Calculation The script begins by identifying the difference between the current closing price of an asset and its corresponding Parabolic SAR value. This calculation provides a raw measurement of how far the price is currently deviating from its historical trend boundary.

2. Trend Identification and Sorting The heart of the script lies in its ability to distinguish between "upward" and "downward" momentum. The logic continuously monitors whether this calculated difference is growing or shrinking compared to previous data points. It categorizes these movements into two distinct states:

  • Bullish Momentum: When the price shows strength relative to the trend boundary, the script isolates these values to represent upward pressure.
  • Bearish Momentum: When the price shows weakness relative to the trend boundary, the script isolates these values to represent downward pressure.

3. Visual Representation To make this data easy for a trader to read, the script separates these two types of momentum into distinct visual layers:

  • Directional Filtering: The logic ensures that bullish and bearish movements are not mixed together. It "cleans" the data so that only the relevant trend information is shown at any given time.
  • Histogram Output: Instead of a single moving line, the script translates these differences into a histogram format. This allows you to see the magnitude of the price movement relative to the trend at a glance.
  • Color Coding: By using specific colors (typically Green for one direction and Red for the other), the tool provides an immediate visual cue regarding which way the market is leaning, while the black line serves as a reference point for the current status.

Summary for the User

In simple terms, this script acts as a "momentum thermometer." It measures how far the price has pulled away from its established trend line. If the histogram moves in one direction and color, it signals that the market is pushing strongly in that direction; if it switches, it signals that the momentum has shifted. This helps traders identify not just where the price is going, but how much "force" is behind the current move.

Indicators Used
Parabolic Stop and Reverse system
Miscellaneous
Implements a curve of type %1
13 Views
0 Downloads
0 Favorites
SAR oscillator
//+------------------------------------------------------------------+
//|                                               SAR Oscillator.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property  copyright "Copyright © 2004, MetaQuotes Software Corp."
#property  link      "http://www.metaquotes.net/"
//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 5
#property  indicator_color1  Green
#property  indicator_color2 Green
#property  indicator_color3  Red
#property  indicator_color4  Red
#property  indicator_color5  Black




//#property  indicator_level1  0.0003
//#property  indicator_level2  -0.0003

//---- indicator buffers
extern double pas=0.02;
extern double max=0.2;

double     ind_buffer1[], ind_buffer1s[];
double     ind_buffer2[], ind_buffer2s[];
double     ind_buffer3[];
double     ind_buffer4[];
double fastcci[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 1 additional buffer used for counting.
   IndicatorBuffers(6);
   //---- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,2);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,1);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,2);
   SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID,1);
   SetIndexStyle(4,DRAW_LINE,0,2);
  
   
//---- 3 indicator buffers mapping
   if(!SetIndexBuffer(0,ind_buffer1) &&
      !SetIndexBuffer(1,ind_buffer1s) &&
      !SetIndexBuffer(2,ind_buffer2) &&
      !SetIndexBuffer(3,ind_buffer2s) &&
      !SetIndexBuffer(4,ind_buffer3))
      Print("cannot set indicator buffers!");
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName(" SAR Oscillator");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| SAR Oscillator                                                   |
//+------------------------------------------------------------------+
int start()
  {
   int    limit;
   int    counted_bars=IndicatorCounted();
   double prev,current;
//---- check for possible errors
   if(counted_bars<0) return(-1);
   //---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- macd counted in the 1-st additional buffer
   for(int i=0; i<limit; i++)
      ind_buffer3[i]=Close[i]-iSAR(NULL,0,pas,max,i);
      ind_buffer4[i]=Close[i]-iSAR(NULL,0,pas,max,i);
      
 
      
      
  
//---- dispatch values between 2 buffers
   bool up=true;
   for(i=limit-1; i>=0; i--)
     {
      current=ind_buffer3[i];
      prev=ind_buffer3[i+1];
      if (((current<0)&&(prev>0))||(current<0))   up= false;    
      if (((current>0)&&(prev<0))||(current>0))   up= true;      
      
      if(!up)
        {
         if(current > prev)
           {
            ind_buffer2s[i]=current;
            ind_buffer2[i]=0.0;
            ind_buffer1[i]=0.0;
            ind_buffer1s[i]=0.0;             
           }        
         else
           {
            ind_buffer2[i]=current;
            ind_buffer2s[i]=0.0;
            ind_buffer1[i]=0.0;
            ind_buffer1s[i]=0.0;            
           }             
        }
        else
        {
         if(current < prev)
           {
            ind_buffer1s[i]=current;
            ind_buffer1[i]=0.0;
            ind_buffer2[i]=0.0;
            ind_buffer2s[i]=0.0;
           }
        else
           {
            ind_buffer1[i]=current;
            ind_buffer1s[i]=0.0;
            ind_buffer2[i]=0.0;
            ind_buffer2s[i]=0.0;
            }           
         }       
     }
//---- done
   return(0);
  }

Comments