_MasterSlaveFull

Download script
_MasterSlaveFull

This script is a sophisticated technical analysis tool designed to transform raw market data into a normalized visual format. Instead of simply plotting price or volume directly, it processes these inputs through several layers of mathematical refinement—including smoothing, deviation analysis, and dynamic scaling—to help traders identify trends and momentum more clearly.

Core Operational Logic

The indicator functions by following a multi-step pipeline to process data:

1. Data Input Selection The tool allows you to choose different types of "raw" data as your starting point. You can feed it standard price types (Open, High, Low, Close, or various averages), the Average True Range (ATR) for volatility, or even market volume.

2. Primary Processing and Smoothing Once an input is chosen, the script applies a Moving Average (MA). This acts as the first "filter" to remove minor market noise. Depending on your settings, it can calculate this using either Simple Moving Averages (SMA) or Exponential Moving Averages (EMA), which place more importance on recent price action.

3. Deviation and Difference Analysis Depending on the specific mode selected, the logic performs one of two primary calculations:

  • Standard Deviation Mode: It calculates how much the current price is deviating from its average. This helps in identifying "overextended" movements.
  • Difference Mode (for Price/Volume): It calculates the distance or "gap" between the current data and its smoothed average, effectively highlighting the strength of a trend.

4. Dynamic Normalization (The Scaling Engine) This is the most unique part of the logic. The script takes the result from the previous step and "normalizes" it into a fixed range (typically between 0 and 1).

  • It looks at a specific "Window" of recent data to find the highest and lowest values.
  • It then maps the current value relative to those highs and lows.
  • Dynamic Windowing: The script can automatically expand this look-back window based on market sensitivity, ensuring that the indicator remains relevant even as market volatility changes.

5. Signal Smoothing and Visualization Finally, the script applies one last layer of smoothing (the "Signal" line) to the normalized data. This helps prevent the indicator from jumping erratically.

Visual Outputs

  • The Main Line: Represents the final, smoothed, and normalized trend.
  • The Histogram: In certain modes (like Volume or ATR), the script can display a histogram that shows the direction of the movement relative to an auxiliary moving average. This allows you to see not just the magnitude of the move, but also whether it is currently "Bullish" or "Bearish."

Summary for the User

Essentially, this tool takes messy market data, strips away the "noise" using moving averages, measures how far the price is stretching away from its norm, and then shrinks that information down into a clean, readable 0-to-1 scale. This makes it easier to see when a move is becoming exhausted or when a new trend is gaining significant momentum.

Price Data Components
Series array that contains tick volumes of each bar
Miscellaneous
Implements a curve of type %1
9 Views
0 Downloads
0 Favorites
_MasterSlaveFull
#property indicator_separate_window // â ïîäîêíå
#property indicator_buffers 7
#property indicator_color1 Red // ñèãíàëüíàÿ
#property indicator_color6 Blue // ãèñòîãðàììà äèíàìèêè äëèíû âûáîðêè
#property indicator_maximum 1
#property indicator_minimum 0

// âõîäíûå ïàðàìåòðû
//-- ðåäêîþçàåìûå ïàðàìåòðû ñäåëàíû íå extern - ìîæíî ðàñøèðèòü ââîä ïðè æåëàíèè
extern int Source=1; // 0 - îáúåì; 1 - ñò.äåâèàöèÿ öåíû; 2 - ATR; 3 - öåíà; 
extern int ApplPrice=0; // 0 - Close; 4 - (high+low)/2; 5 - (high+low+close)/3; 6 - (high+low+close+close)/4;
extern int SourcePeriod=5; // ïåðèîä âõîäíîãî èíäèêàòîðà
extern int Shift=0; // ñäâèã
 bool MAtypeI=1; // 0 - SMA, 1 - EMA
extern int PrePeriod=1; // äëÿ äåâèàöèè äîïîëíèòåëüíîå ñãëàæèâàíèå, îñòàëüíûå - ìåäëåííàÿ MA äëÿ MACD
 bool MAtypeP=1; // 0 - SMA, 1 - EMA
extern int Window=15; // äëèíà âûáîðêè íîðìèðîâàíèÿ
extern int WindowLimit=0; // ìíîæèòåëü îãðàíè÷åíèå äëèíû âûáîðêè íîðìèðîâàíèÿ
extern int WindowShift=0; // ñäâèã âûáîðêè íîðìèðîâàíèÿ
extern bool WindowMethod=0; // 0 - ôèêñèðîâàííàÿ âûáîðêà äëÿ íîðìèðîâàíèÿ, 1 - ïëàâàþùàÿ
extern double Sensitivity=0; // ÷óâñòâèòåëüíîñòü
extern bool abs=1; // âûâîä ÷åðåç áóôåð NP[] ñî çíàêîì äâèæåíèÿ ñò.äåâèàöèè
extern bool ToRange=1; // ïðèíóäèòåëüíîå îãðàíè÷åíèå äèàï. 0...1; èìååò ñìûñë òîëüêî ïðè WindowShift>0
extern double Power=1; // ñòåïåíü ïî íîðìèðîâàíèþ
extern int Signal=1; // ñãëàæèâàíèå íîðìèðîâàííîãî èíäèêàòîðà (ñèãíàëüíàÿ)
 bool MAtypeS=1; // 0 - SMA, 1 - EMA
extern int SignalShift=0; // ñäâèã ñèãíàëüíîé
extern int History=0;
 
// èíäèêàòîðíûå áóôåðû
double Ind[], // íîðìèðóåìûé èíäèêàòîð
      PR[], // ïðåôèëüòðàöèÿ
      NORM[], // ðåçóëüòàòû íîðìèðîâàíèÿ
      SM[], // ñèãíàëüíàÿ
      Input[], // áóôåð àðãóìåíòîâ íîðìèðóåìîãî èíäèêàòîðà (öåíà èëè îáúåì)
      MA[], // ÌÀ íîðìèðóåìîãî èíäèêàòîðà
      NP[], // ïëàâàþùèé ïåðèîä íîðìèðîâàíèÿ - â âû÷èñëèíèÿõ íå ó÷àñòâóåò - äëÿ íàãëÿäíîñòè
      SIGN[]; // åìóëèðîâàííûé áóôåð - âñïîìîãàòåëüíàÿ MA

double sens, // ÷óâñòâèòåëüíîñòü â ìàñøòàáå öåíû/îáúåìà
      sensLimit, // ìèíèìàëüíàÿ ÷óâñâèòåëüíîñòü
      sn,
      IndPeriodK, // êîýôô. EMA äëÿ äåâèàöèè
      PrePeriodK, // êîýôô. EMA äëÿ ïðåôèëüòðà
      SignalK; // êîýôô. EMA äëÿ ñãëàæèâàíèÿ
bool Em=0; // ôëàã äëÿ ýìóëÿöèè âñïîìîãàòåëüíîé MA
//int WindowLimit; // îãðàíè÷åíèå äëèíû âûáîðêè íîðìèðîâàíèÿ

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   switch(Source) {
      case 0: // Îáúåì
         SetIndexStyle(2,DRAW_LINE,0,1,LimeGreen);
         string ShortName="Volume(";
         double punkt=1;
         WindowShift++;
         break;
      case 3: // Ñòîõàñòèê
         SetIndexStyle(2,DRAW_LINE,0,1,LightSeaGreen);
         ShortName="MA(";
         WindowMethod=1; // èñïîëüçîâàòü ïëàâàþùóþ äëèíó âûáîðêè íîðìèðîâàíèÿ
         punkt=Point;
         break;
      case 2: // ñò.äåâèàöèÿ
         SetIndexStyle(2,DRAW_LINE,0,1,MediumSeaGreen);
         ShortName="StDev(";
         punkt=Point;
         break;
      case 1: //ATR
         SetIndexStyle(2,DRAW_LINE,0,1,DodgerBlue);
         ShortName="ATR(";
         punkt=Point;
     }
   if(WindowMethod) {
      if(WindowLimit==0) WindowLimit=Window*10; // îãðàíè÷åíèå äëèíû âûáîðêè íîðìèðîâàíèÿ
      else WindowLimit=MathMax(WindowLimit,Window);
     }
   ShortName=ShortName+_Price()+SourcePeriod;
   if(Shift>0) ShortName=ShortName+"+"+Shift;
   ShortName=ShortName+") ";
   if(PrePeriod>1) ShortName=ShortName+PrePeriod+" ";
   ShortName=ShortName+"["+Window;
   if(WindowMethod && Sensitivity>0) ShortName=ShortName+"_"+WindowLimit;
   if(WindowShift>0) ShortName=ShortName+"+"+WindowShift;
   if(Sensitivity>0) ShortName=ShortName+"@"+DoubleToStr(Sensitivity,1);
   ShortName=ShortName+"]";
   if(Power!=1) ShortName=ShortName+"^"+DoubleToStr(Power,1);
   if(Signal>1 || SignalShift>0) {
      ShortName=ShortName+" ("+Signal;
      if(SignalShift>0) ShortName=ShortName+"+"+SignalShift;
      ShortName=ShortName+")";
     }
   IndicatorShortName(ShortName); // çàäàòü èìÿ èíäèêàòîðà â ïîäîêíå

//---- indicators
   SetIndexBuffer(0,SM); // ñèãíàëüíàÿ
   SetIndexStyle(0,DRAW_LINE);
   SetIndexLabel(0,"Signal");

   SetIndexBuffer(1,Ind); // èíäèêàòîð
   SetIndexStyle(1,DRAW_NONE);
   SetIndexLabel(1,NULL);

   SetIndexBuffer(2,NORM); // ðåçóëüòàòû íîðìèðîâàíèÿ
   SetIndexLabel(2,"Normed");

   SetIndexBuffer(3,MA); // MA èíäèêàòîðà
   SetIndexStyle(3,DRAW_NONE);
   SetIndexLabel(3,NULL);

   SetIndexBuffer(4,Input); // áóôåð âõîäíûõ äàííûõ (îáúåì èëè öåíà)
   SetIndexStyle(4,DRAW_NONE);
   SetIndexLabel(4,NULL);

   SetIndexBuffer(5,NP); // ãèñòîãðàììà è âûâîä ñî çíàêîì ñò.äåâ.
   if(Source==-3 && Sensitivity>0) // ïîêàçàòü äëèíó âûáîðêè íîðìèðîâàíèÿ
     {
      SetIndexStyle(5,DRAW_HISTOGRAM); 
      Source=-Source;
     }
   else SetIndexStyle(5,DRAW_NONE);
   
   if(Source==3 && Sensitivity>0) SetIndexLabel(5,"Window");
   else SetIndexLabel(5,NULL);
   if(abs) SetIndexLabel(5,"NormSign");
   else SetIndexLabel(5,NULL);

   SetIndexBuffer(6,PR); // ðåçóëüòàòû ïðåôèëüòðàöèè èíäèêàòîðà (ïîäàþòñÿ â íîðìèðîâàíèå)
   SetIndexStyle(6,DRAW_NONE);
   SetIndexLabel(6,"Source");

//----
   sensLimit=punkt/1000; // ìèíèìàëüíàÿ ÷óâñâèòåëüíîñòü
   sens=MathMax(Sensitivity*punkt,sensLimit); // ÷óâñòâèòåëüíîñòü â ìàñøòàáå èíäèêàòîðà
   
   if(WindowMethod) sn=sensLimit; // åñëè äèíàìè÷åñêîå íîðìèðîâàíèå
   else sn=sens;
   
   if(SourcePeriod<0) {MAtypeI=0; SourcePeriod=-SourcePeriod;} // SMA
   else IndPeriodK=2.0/(SourcePeriod+1); // EMA
   
   if(PrePeriod<0) {MAtypeP=0; PrePeriod=-PrePeriod;} // SMA
   else PrePeriodK=2.0/(PrePeriod+1); // EMA
   
   if(Signal<0) {MAtypeS=0; Signal=-Signal;} // SMA
   else SignalK=2.0/(Signal+1); // EMA

   if(Source==0 || Source==1) Em=1; // ôëàã äëÿ ýìóëÿöèè âñïîìîãàòåëüíîé MA

   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   if(Em) EmBuff();
   int ic=IndicatorCounted();
   if(Bars-ic-1>1) {ic=reinit();}
   int limit=Bars-ic-1; //BarsPrev=Bars; // êîë-âî ïåðåñ÷åòîâ
   if(History!=0 && limit>History) limit=History-1; // êîë-âî ïåðåñ÷åòîâ ïî èñòîðèè

   for(int i=limit; i>=0; i--) // öèêë ïî íåïîñ÷èòàííûì è ïðåäïîñëåäíåìó áàðàì
     {
      double ma;
      int n;
      int iShift=i+Shift;
      if(Em) { // Ýìóëèðóåìàÿ ÌÀ
         if(MAtypeI) // EMA
            SIGN[i]=IndPeriodK*Price(4,iShift)+(1-IndPeriodK)*SIGN[i+1];
         else { // SMA
            int sh=iShift+SourcePeriod;
            ma=SIGN[i+1]*SourcePeriod-Price(4,sh);
            SIGN[i]=(ma+Close[iShift])/SourcePeriod;
           }
        }

      // âûáîð èñòî÷íèêà
      switch(Source) {
         case 0: // îáúåì
            Input[i]=Volume[iShift]; break;
         case 3: // öåíà
            Input[i]=Price(ApplPrice,iShift); break;
         case 2:
            Input[i]=Price(ApplPrice,iShift); break;
         case 1: // ATR
            Input[i]=MathMax(High[iShift],Close[iShift+1])-MathMin(Low[iShift],Close[iShift+1]);
        }

      // MA
      if(MAtypeI) { // EMA
         MA[i]=IndPeriodK*Input[i]+(1-IndPeriodK)*MA[i+1];
         ma=MA[i];
        }
      else { // SMA
         sh=i+SourcePeriod;
         ma=MA[i+1]*SourcePeriod-Input[sh];
         MA[i]=(ma+Input[i])/SourcePeriod;
         ma=MA[i];
        }

      // ñò.äåâèàöèÿ
      if(Source==2) // íîðìèðóåòñÿ äåâèàöèÿ
        {
         double std=0;
         for(n=0; n<SourcePeriod; n++) {
            double delta=Input[i+n]-ma;
            std+=delta*delta;  // ÷èñëèòåëü SourcePeriod(Price)
           }
         Ind[i]=MathSqrt(std/SourcePeriod);
         //
         if(PrePeriod==1) PR[i]=Ind[i];
         else
            if(MAtypeP)
               PR[i]=PrePeriodK*Ind[i]+(1-PrePeriodK)*PR[i+1];
            else  {
               sh=i+PrePeriod;
               ma=PR[i+1]*PrePeriod-Ind[sh];
               PR[i]=(ma+Ind[i])/PrePeriod;
              }
        }
      else // MA (öåíà èëè îáúåì)
         if(PrePeriod==1) PR[i]=MA[i];
         else {
            if(MAtypeP)
               Ind[i]=PrePeriodK*Input[i]+(1-PrePeriodK)*Ind[i+1];
            else {
               sh=i+PrePeriod;
               ma=PR[i+1]*PrePeriod-Input[sh];
               Ind[i]=(ma+Input[i])/PrePeriod;
              }
            PR[i]=MA[i]-Ind[i];
            if(abs) PR[i]=MathAbs(PR[i]);
           }
           
      // Íîðìèðîâàíèå
      double max=PR[ArrayMaximum(PR,Window,i+WindowShift)];
      double min=PR[ArrayMinimum(PR,Window,i+WindowShift)];
      if(WindowMethod)
         for(n=Window; max-min<sens && n<=WindowLimit; n++) {
            int in=i+n+WindowShift;
            max=MathMax(max,PR[in]);
            min=MathMin(min,PR[in]);
           }
      double x=(PR[i]-min)/MathMax((max-min),sn);
      if(Power!=1 && Power>0) x=MathPow(x,Power);
      NORM[i]=x;

      if(ToRange) // ïðè ñäâèãå âûáîðêè óáðàòü çíà÷åíèÿ âíå äèàï.0...1
         {NORM[i]=MathMax(NORM[i],0); NORM[i]=MathMin(NORM[i],1);}

      if(abs) // âûâîä íîðìèðîâàíèÿ ñî çíàêîì 
         if(Em) // åñëè îáúåì èëè ATR
            {if(Close[i]>SIGN[i+3]) NP[i]=NORM[i]; else NP[i]=-NORM[i];}
         else // åñëè ñò.äåâ.
            {if(Close[i]>MA[i+3]) NP[i]=NORM[i]; else NP[i]=-NORM[i];}

      // Ñãëàæèâàíèå .
      if(Signal==1) SM[i]=NORM[i+SignalShift];
      else
         if(MAtypeS) // EMA
            SM[i]=SignalK*NORM[i+SignalShift]+(1-SignalK)*SM[i+1];
         else { // SMA
            sh=i+SignalShift+Signal;
            ma=SM[i+1]*Signal-NORM[sh];
            SM[i]=(ma+NORM[i+SignalShift])/Signal;
           }
     }
//----
   return(0);
  }

//----------------------------------------------------  
// ô-ÿ äîïîëíèòåëüíîé èíèöèàëèçàöèè
int reinit()
  {
   ArrayInitialize(Input,0.0);
   ArrayInitialize(Ind,0.0);
   ArrayInitialize(PR,0.0);
   ArrayInitialize(NORM,0.0);
   ArrayInitialize(SM,0.0);
   ArrayInitialize(MA,0.0);
   ArrayInitialize(NP,0.0);
   return(0);
  }
  
double Price(int ApplPrice,int i) {
   switch(ApplPrice) {
      case 0: return(Close[i]);
      case 1: return(Open[i]);
      case 2: return(High[i]);
      case 3: return(Low[i]);
      case 4: return((High[i]+Low[i])/2);
      case 5: return((High[i]+Low[i]+Close[i])/3);
      case 6: return((High[i]+Low[i]+2*Close[i])/4);
      default: return(Close[i]);
     }
  }

string _Price() {
   switch(ApplPrice) {
      case 0: return("");
      case 1: return("O");
      case 2: return("H");
      case 3: return("L");
      case 4: return("M");
      case 5: return("T");
      case 6: return("W");
      default: return("");
     }
  }

void EmBuff()
  {  
   static int NewSize;
   if(NewSize==Bars) return;
   NewSize=Bars;
   ArraySetAsSeries(SIGN,0); ArrayResize(SIGN,NewSize); ArraySetAsSeries(SIGN,1);
  }

//+------------------------------------------------------------------+

Comments