Supertrend_v4

Author: Raul Canessa
Indicators Used
Moving average indicatorIndicator of the average true range
Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
Supertrend_v4
ÿþ//+------------------------------------------------------------------+

//|                                                   Supertrend.mq4 |

//|                                                     Raul Canessa |

//|                               https://www.tecnicasdetrading.com/ |

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

#property copyright "Raul Canessa"

#property link      "https://www.tecnicasdetrading.com/"

#property version   "1.00"

#property strict

#property indicator_chart_window

#property description "Indicador SuperTrend creado con fines ilustrativos "

                      "Si tienen interés en automatizar sus estrategias "

                      "o crear un indicador técnico personalizado pueden contactarnos al correo rcanessa@gmail.com. "

                      

                      "Más información en www.tecnicasdetrading.com"

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

//| Función de inicialiación del indicador SuperTrend                |

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

#property indicator_buffers 2       //Número de buffers del indicador 1

#property indicator_color1 Red  //Color de banda inferior del indicador 

#property indicator_color2 Lime   //Color de banda inferior del indicador 



extern int Periodo_ATR=10;           //Periodo del ATR

extern int Multiplicador=3;          //Valor del multiplicador



double BandaUP_Final[];               //Banda superior

double BandaDown_Final[];             //Banda inferior

double Supertrend[];                  //Valor del Supertrend

double SupertrendUp[];                  //Valor del Supértrend superior

double SupertrendDown[];                  //Valor del Supértrend inferior



double BandaUP;                   //Banda superior básica

double BandaDown;                //Banda inferior básica



int OnInit()

  {

   IndicatorBuffers(5);        //Buffers del indicador

   SetIndexBuffer(0,SupertrendUp);  //Asignación de Array a buffer 1

   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);     //Estilo línea de indicador

   SetIndexBuffer(1,SupertrendDown);  //Asignación de Array a buffer 1

   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);     //Estilo línea de indicador

   SetIndexBuffer(2,Supertrend);  //Asignación de Array a buffer 1

   SetIndexStyle(2,DRAW_NONE);     //Estilo línea de indicador

   SetIndexBuffer(3,BandaUP_Final);  //Asignación de Array a buffer 1

   SetIndexBuffer(4,BandaDown_Final);  //Asignación de Array a buffer 1



   

   //Etiqueta de Técnicas de Trading

   ObjectCreate("Label_Tecnicas", OBJ_LABEL, 0, 0, 0);  //Creación de etiqueta de TecnicadeTrading

   ObjectSet("Label_Tecnicas", OBJPROP_CORNER, 0);    // Esquina de referencia

   ObjectSet("Label_Tecnicas", OBJPROP_XDISTANCE, 20);// Coordenada X

   ObjectSet("Label_Tecnicas", OBJPROP_YDISTANCE, 25);// Coordenada Y

   ObjectSetText("Label_Tecnicas","SuperTrend - TecnicasDeTrading.com",15,"Arial",Blue);

   return(INIT_SUCCEEDED);

  }

int deinit()

  {

   ObjectDelete("Label_Tecnicas");

   return(0);

  } 

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

//| Custom indicator iteration function                              |

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

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

   //Variables de cálculo

   int i;               //Cantidad de barras usadas en el cálculo

   int Cant_Bars;       //Número de barras contadas en cada tick por el indicador

   int j=0;               //Variable de cálculo         

   //Conteo de barras

   Cant_Bars=IndicatorCounted();

   i=Bars-Cant_Bars-1;

   if(i>0)

    j=i-Periodo_ATR+1;

   if(i==0)

    j=0;

   BandaUP_Final[Bars-Periodo_ATR]=High[Bars-Periodo_ATR];

   BandaDown_Final[Bars-Periodo_ATR]=Low[Bars-Periodo_ATR];

   Supertrend[Bars-Periodo_ATR]=iMA(NULL,0,Periodo_ATR,0,MODE_SMA,PRICE_CLOSE,Bars-Periodo_ATR);

   while(j>=0)

    {

     //Cálculo de la banda superior y banda inferior básicas

     BandaUP=(High[j]+Low[j])/2+Multiplicador*iATR(NULL,0,Periodo_ATR,j);  

     BandaDown=(High[j]+Low[j])/2-Multiplicador*iATR(NULL,0,Periodo_ATR,j); 

     //Cálculo de la banda superior y banda inferior finales

     if(BandaUP<BandaUP_Final[j+1]||Close[j+1]>BandaUP_Final[j+1])

      {

       BandaUP_Final[j]=BandaUP;

      }

     else

      {

       BandaUP_Final[j]=BandaUP_Final[j+1];

      } 

     if(BandaDown>BandaDown_Final[j+1]||Close[j+1]<BandaDown_Final[j+1])

      {

       BandaDown_Final[j]=BandaDown;

      }

     else

      {

       BandaDown_Final[j]=BandaDown_Final[j+1];

      }   

     //Cálculo de SuperTrend 

     if(Supertrend[j+1]==BandaUP_Final[j+1]&&Close[j]<BandaUP_Final[j])

       {

        Supertrend[j]=BandaUP_Final[j];

       } 

     if(Supertrend[j+1]==BandaUP_Final[j+1]&&Close[j]>BandaUP_Final[j])

       {

        Supertrend[j]=BandaDown_Final[j];

       }

     if(Supertrend[j+1]==BandaDown_Final[j+1]&&Close[j]>BandaDown_Final[j])

      {

       Supertrend[j]=BandaDown_Final[j];

      } 

     if(Supertrend[j+1]==BandaDown_Final[j+1]&&Close[j]<BandaDown_Final[j])

      {

       Supertrend[j]=BandaUP_Final[j];

      }

   //Cálculo final del indicador Supertrend   

     if(Supertrend[j]==BandaUP_Final[j])

       {

        SupertrendUp[j]=Supertrend[j];

        SupertrendUp[j+1]=Supertrend[j+1];

       } 

     if(Supertrend[j]==BandaDown_Final[j]) 

       {

        SupertrendDown[j]=Supertrend[j];

        SupertrendDown[j+1]=Supertrend[j+1];

       }

     j--;    

   

    }

   return(rates_total);

  }

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

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 ---