Mtf_SuperTrend nrp new

Author: mladen
Indicators Used
Indicator of the average true rangeMoving average indicator
0 Views
0 Downloads
0 Favorites
Mtf_SuperTrend nrp new
//+------------------------------------------------------------------+
//|                                               SuperTrend nrp.mq4 |
//|                                                           Mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Magenta
#property indicator_color2 DarkOrange
#property indicator_color3 Gold


extern string note_TimeFrames_ = "M1;5,15,30,60H1;240H4;1440D1;10080W1;43200MN|0-currentTF";
extern int    TimeFrame        = 0;
extern int    period           = 10;
extern int    appliedPrice     = PRICE_CLOSE;
extern double multiplier       = 3.0;

//
//
//
//
//

double Trend[];
double TrendDoA[];
double TrendDoB[];
double Direction[];
double Up[];
double Dn[];


int    timeFrame;
bool   calculating = false;
bool   returnBars  = false;
string IndicatorFileName;


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
      IndicatorBuffers(6);
      SetIndexBuffer(0, Trend);
      SetIndexBuffer(1, TrendDoA);
      SetIndexBuffer(2, TrendDoB);
      SetIndexBuffer(3, Direction);
      SetIndexBuffer(4, Up);
      SetIndexBuffer(5, Dn);
      
      
      
      SetIndexLabel(0,"SuperTrend");
      SetIndexLabel(1,"SuperTrend");
      SetIndexLabel(2,"SuperTrend");
   
      period = MathMax(1,period);  

      timeFrame = MathMax(stringToTimeFrame(TimeFrame),Period());

   //
   //
   //
   //
   //
         
   IndicatorFileName = WindowExpertName();
   return(0);
}




//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//


int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=MathMin(Bars-counted_bars,Bars-1);
           if (returnBars) { Trend[0] = limit; return(0); }

   //
   //
   //
   //
   //

   if (timeFrame > Period()) limit = MathMax(limit,MathMin(Bars,iCustom(NULL,timeFrame,IndicatorFileName,"getBarsCount",0,0)*timeFrame/Period()));
   if (!calculating && timeFrame != Period())
   {
      for(i=limit;i>=0;i--)
      {
         int      bar  = iBarShift(NULL,timeFrame,Time[i]);
         datetime	time = iTime    (NULL,timeFrame,bar);
         
   Trend[i]    = iCustom(NULL,timeFrame,IndicatorFileName,"calculate",period,appliedPrice,multiplier,0,bar);
   TrendDoA[i] = iCustom(NULL,timeFrame,IndicatorFileName,"calculate",period,appliedPrice,multiplier,1,bar);
   TrendDoB[i] = iCustom(NULL,timeFrame,IndicatorFileName,"calculate",period,appliedPrice,multiplier,2,bar);
   Direction[i]= iCustom(NULL,timeFrame,IndicatorFileName,"calculate",period,appliedPrice,multiplier,3,bar);
   Up[i]       = iCustom(NULL,timeFrame,IndicatorFileName,"calculate",period,appliedPrice,multiplier,4,bar);
   Dn[i]       = iCustom(NULL,timeFrame,IndicatorFileName,"calculate",period,appliedPrice,multiplier,5,bar);
   }               
   return(0);         
 }

   //
   //
   //
   //
   //
   
   
      
   if (Direction[limit] <= 0) CleanPoint(limit,TrendDoA,TrendDoB);
   for(i = limit; i >= 0; i--)
   {
         double atr    = iATR(NULL,0,period,i);
         double cprice = iMA(NULL,0,1,0,MODE_SMA,appliedPrice,i);
         double mprice = (High[i]+Low[i])/2;
                Up[i]  = mprice+multiplier*atr;
                Dn[i]  = mprice-multiplier*atr;
         
         //
         //
         //
         //
         //
         
         Direction[i] = Direction[i+1];
            if (cprice > Up[i+1]) Direction[i] =  1;
            if (cprice < Dn[i+1]) Direction[i] = -1;
         TrendDoA[i] = EMPTY_VALUE;
         TrendDoB[i] = EMPTY_VALUE;
            if (Direction[i] > 0) { Dn[i] = MathMax(Dn[i],Dn[i+1]); Trend[i] = Dn[i]; }
            else                  { Up[i] = MathMin(Up[i],Up[i+1]); Trend[i] = Up[i]; PlotPoint(i,TrendDoA,TrendDoB,Trend); }
   }
   return(0);
}


int stringToTimeFrame(string tfs)
{
   for(int l = StringLen(tfs)-1; l >= 0; l--)
   {
      int char = StringGetChar(tfs,l);
          if((char > 96 && char < 123) || (char > 223 && char < 256))
               tfs = StringSetChar(tfs, l, char - 32);
          else 
              if(char > -33 && char < 0)
                  tfs = StringSetChar(tfs, l, char + 224);
   }

   //
   //
   //
   //
   //
   
   int tf=Period();
         if (tfs=="M1" || tfs=="1")     tf=PERIOD_M1;
         if (tfs=="M5" || tfs=="5")     tf=PERIOD_M5;
         if (tfs=="M15"|| tfs=="15")    tf=PERIOD_M15;
         if (tfs=="M30"|| tfs=="30")    tf=PERIOD_M30;
         if (tfs=="H1" || tfs=="60")    tf=PERIOD_H1;
         if (tfs=="H4" || tfs=="240")   tf=PERIOD_H4;
         if (tfs=="D1" || tfs=="1440")  tf=PERIOD_D1;
         if (tfs=="W1" || tfs=="10080") tf=PERIOD_W1;
         if (tfs=="MN" || tfs=="43200") tf=PERIOD_MN1;
   return(tf);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void CleanPoint(int i,double& first[],double& second[])
{
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

//
//
//
//
//

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (first[i+1] == EMPTY_VALUE)
      {
      if (first[i+2] == EMPTY_VALUE) {
          first[i]    = from[i];
          first[i+1]  = from[i+1];
          second[i]   = EMPTY_VALUE;
         }
      else {
          second[i]   = from[i];
          second[i+1] = from[i+1];
          first[i]    = EMPTY_VALUE;
         }
      }
   else
      {
         first[i]   = from[i];
         second[i]  = EMPTY_VALUE;
      }
}


Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---