Prior Cote - AJUSTE

Author: Daniel Jose
0 Views
0 Downloads
0 Favorites
Prior Cote - AJUSTE
ÿþ//+------------------------------------------------------------------+

//|                                                   Prior Cote.mq5 |

//|                                                      Daniel Jose |

//|                                                                  |

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

#property copyright "Daniel Jose"

#property description "Indicador de AJUSTE ( Prior Cote ) para Dólar e Indice"

#property description "Este calcula o ajuste do DIA atual e do anterior"

#property description "Voltado para ser utilizado na B3 ( Bolsa do Brasil )"

#property description "Não é preciso informar o contrato, apenas jogue ele no"

#property description "gráfico que ele irá fazer o resto ..."

#property version   "1.01"

#property indicator_chart_window

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

enum eTypePrior

  {

   PRIOR_COTE,  //Valor do ajuste Diario

   COTE_TODAY  //Valor do ajuste de hoje

  };

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

#property indicator_buffers  1

#property indicator_plots   1

#property indicator_width1  2

#property indicator_color1  clrOrange;

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

#property indicator_type1  DRAW_LINE

#property indicator_style1 STYLE_DASH

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

input color   user01 = clrOrange;  //Cor

input eTypePrior  user02 = PRIOR_COTE;  //Linha indicadora.

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

#define macroGetDate(A) (A - (A % 86400))

#define macroSetTime(A, B, C) ((A * 3600) + (B * 60) + (C - (C % 86400)))

#define macroAddHours(A, B) (B + (A * 3600))

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

double Buff[];

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

int OnInit()

  {

   string sz0;



   PlotIndexSetInteger(0, PLOT_LINE_COLOR, user01);

   SetIndexBuffer(0, Buff, INDICATOR_DATA);

   switch(user02)

     {

      case PRIOR_COTE:

         sz0 = "PRIOR COTE";

         break;

      default   :

         sz0 = "COTE TODAY";

     }

   IndicatorSetString(INDICATOR_SHORTNAME, sz0);



   return INIT_SUCCEEDED;

  }

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

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[])

  {

   static int   siPos = 0;

   static double  sdValue = 0;

   datetime   dt = TimeLocal();

   int     shift = 0;



   if(rates_total == 0)

      return rates_total;

   if(macroGetDate(dt) != macroGetDate(time[rates_total - 1]))

     {

      shift = 0;

      sdValue = 0;

      siPos = rates_total - 1;

     }

   else

      if(macroGetDate(time[rates_total - 1]) != macroGetDate(time[siPos]))

        {

         for(int c0 = rates_total - 1; macroGetDate(time[siPos]) != macroGetDate(time[c0]); siPos++);

         sdValue = 0;

         shift = 1;

        }

   if(sdValue <= 0)

     {

      ArrayInitialize(Buff, EMPTY_VALUE);

      switch(user02)

        {

         case PRIOR_COTE:

            sdValue = CalculePriorCote(time[siPos - 1], false);

            break;

         case COTE_TODAY:

            sdValue = CalculePriorCote(time[siPos], true);

            siPos = rates_total - 1;

            break;

        }

     }

   for(int c0 = siPos - 1; c0 < rates_total; c0++)

      Buff[c0] = sdValue;



   return rates_total;

  }

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

double CalculePriorCote(const datetime dt, bool today)

  {

   MqlRates Rates[], info;

   datetime d1, d2;

   double Price;

   long Vol;

   string sz0 = StringSubstr(_Symbol, 0, 3);



   Price = 0;

   Vol = 0;



   if((sz0 == "WIN") || (sz0 == "IND"))

     {

      d1 = macroSetTime(17, 00, dt);

      d2 = macroSetTime(17, 15, dt);

     }

   else

      if((sz0 == "WDO") || (sz0 == "DOL"))

        {

         d1 = macroSetTime(15, 50, dt);

         d2 = macroSetTime(16, 00, dt);

        }

      else

         return 0;

   if(today)

      if(d2 > dt)

         return -1;

   if(CopyRates(ValidSymbol(today), PERIOD_M1, d1, d2, Rates) <= 0)

      return -1;

   for(int c2 = 0; c2 < ArraySize(Rates); c2++)

     {

      info = Rates[c2];

      Price += (((info.high + info.low + info.close) / 3) * info.real_volume);

      Vol += info.real_volume;

     }

   return (Price / Vol);

  };

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

string ValidSymbol(const bool bNext = false)

  {

   MqlDateTime mdt1;

   string sz0, sz1, sz2, szSymbol;

   datetime dt = TimeLocal();



   szSymbol = _Symbol;

   sz0 = StringSubstr(szSymbol, 0, 3);

   if((sz0 != "WDO") && (sz0 != "DOL") && (sz0 != "WIN") && (sz0 != "IND"))

      return szSymbol;

   sz1 = (sz0 == "WDO" ? "DOL" : (sz0 == "WIN" ? "IND" : sz0));

   sz0 = (sz1 == "DOL" ? "WDO" : "WIN");

   sz2 = (sz0 == "WDO" ? "FGHJKMNQUVXZ" : "GJMQVZ");

   TimeToStruct(dt, mdt1);

   for(int i0 = 0, i1 = mdt1.year - 2000;;)

     {

      szSymbol = StringFormat("%s%s%d", sz0, StringSubstr(sz2, i0, 1), i1);

      if(i0 < StringLen(sz2))

         i0++;

      else

        {

         i0 = 0;

         i1++;

        }

      if(macroGetDate((bNext ? macroAddHours(24, dt) : dt)) < macroGetDate(SymbolInfoInteger(szSymbol, SYMBOL_EXPIRATION_TIME)))

         break;

     }

   return szSymbol;

  }

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

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