SYMBOL_RATE

Author: Copyright 2019, MetaQuotes Software Corp.
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
SYMBOL_RATE
ÿþ//+------------------------------------------------------------------+

//|                                                       SymbolRate |

//|                                      Copyright 2018, CompanyName |

//|                                       http://www.companyname.net |

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

#property copyright "Copyright 2019, MetaQuotes Software Corp."

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

#property version   "1.00"

#property description "SymbolRate"

#property indicator_separate_window

#property indicator_buffers 5

#property indicator_plots   1

//--- plot symbol

#property indicator_label1  "SymbolRate_Candles"

#property indicator_type1   DRAW_COLOR_CANDLES

#property indicator_color1  clrRed,clrAqua//C'0,0,0',C'0,0,0',C'0,0,0',C'0,0,0',C'0,0,0',C'0,0,0'

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- indicator buffers

double         KBuffer1[];

double         KBuffer2[];

double         KBuffer3[];

double         KBuffer4[];

double         KColors[];

enum method

  {

   add = 1,   // "+"

   subtract,  // "-"

   multiply,  // "*"

   divide,    // "/"

   logdivide,  // "log(/)"

  };

input string InpName = "GBPUSD";//
Tðy

input method InpMethod = divide;//—{Õl

MqlRates rates[];

int limit = 10000;

int showbars = 0;

string iname = InpName;

string myname = "",symbolname = "";

//int symbolhandle = INVALID_HANDLE;

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,KBuffer1,INDICATOR_DATA);

   SetIndexBuffer(1,KBuffer2,INDICATOR_DATA);

   SetIndexBuffer(2,KBuffer3,INDICATOR_DATA);

   SetIndexBuffer(3,KBuffer4,INDICATOR_DATA);

   SetIndexBuffer(4,KColors,INDICATOR_COLOR_INDEX);

   PlotIndexSetString(0,PLOT_LABEL,"Open;" + "High;" + "Low;" + "Close");

//--- 0(zz)<P\
NÂSNØ~6R

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);

   ArraySetAsSeries(KBuffer1,true);

   ArraySetAsSeries(KBuffer2,true);

   ArraySetAsSeries(KBuffer3,true);

   ArraySetAsSeries(KBuffer4,true);

   ArraySetAsSeries(KColors,true);

   ArraySetAsSeries(rates,true);

   ZeroMemory(rates);



   StringTrimRight(iname);

   StringTrimLeft(iname);

   StringToUpper(iname);

   bool is_custom;

   if(!SymbolExist(iname,is_custom))

     {

      Alert(iname + ":¡l	g~b0R勤NfÁTÍy!");

      return(INIT_FAILED);

     }

   else

      if(is_custom)

         Alert(iname + ":ꁚ[IN¤NfÁTÍy!");



   CopyRates(iname,PERIOD_CURRENT,0,(int)SeriesInfoInteger(iname,PERIOD_CURRENT,SERIES_BARS_COUNT),rates);

   ChartSetInteger(0,CHART_FOREGROUND,ChartWindowFind(),false);

   ChartSetInteger(0,CHART_SHOW_PRICE_SCALE,ChartWindowFind(),true);



   string pstr = EnumToString(_Period);

   string mstr = EnumToString(InpMethod);

   if(!StringToUpper(mstr))

      mstr = method_tostr(InpMethod);



   myname = iname + method_tostr(InpMethod) + _Symbol;

   if(InpMethod > 2)

     {

      string str[4];

      str[0] = StringSubstr(iname,0,3);

      str[1] = StringSubstr(iname,3,-1);

      str[2] = StringSubstr(_Symbol,0,3);

      str[3] = StringSubstr(_Symbol,3,-1);

      //ArrayPrint(str);

      if(InpMethod == multiply)

        {

         if(str[1] == str[2])

            myname = str[0] + str[3];

         if(str[0] == str[3])

            myname = str[2] + str[1];

        }

      if(InpMethod == divide)

        {

         if(str[1] == str[3])

            myname = str[0] + str[2];

         if(str[0] == str[2])

            myname = str[3] + str[1];

        }

     }

   string comment = myname + "," + StringSubstr(pstr,StringFind(pstr,"_") + 1);

//--- indicator name

   IndicatorSetString(INDICATOR_SHORTNAME,comment + ": SymbolRate(" + iname + "," + mstr + "," + pstr + ")");

   ObjectCreate(0,"Bid_" + myname,OBJ_HLINE,ChartWindowFind(),0,0);

   ObjectSetInteger(0,"Bid_" + myname,OBJPROP_STYLE,STYLE_DASHDOT);

   symbolname = SymbolInfoInteger(myname,SYMBOL_EXIST) ? myname : "";

   showbars = (int)ChartGetInteger(0,CHART_FIRST_VISIBLE_BAR);

   limit = TerminalInfoInteger(TERMINAL_MAXBARS);

   return(INIT_SUCCEEDED);

  }

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

//|                                                                  |

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

void OnDeinit(const int reason)

  {

   ObjectDelete(0,"Bid_" + myname);

   ObjectsDeleteAll(0,"Bid_",ChartWindowFind(),OBJ_HLINE);

  }

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

//|                                                                  |

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

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

  {

   ArraySetAsSeries(time,true);

   ArraySetAsSeries(open,true);

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

   ArraySetAsSeries(close,true);

   int to_copy;

   if(prev_calculated > rates_total || prev_calculated <= 0)

      to_copy = rates_total;

   else

     {

      to_copy = rates_total - prev_calculated;

      to_copy++;

     }

   int bars = iBars(iname,PERIOD_CURRENT);

   if(limit < to_copy)

      to_copy = limit;

   if(bars < to_copy) //(int)MathMin(bars,MathMax(showbars,to_copy)

      to_copy = bars;

   if(1 > to_copy)

      to_copy = 1;



   int copied = CopyRates(iname,PERIOD_CURRENT,0,to_copy,rates);

   if(copied != to_copy)

     {

      copied = CopyRates(iname,PERIOD_CURRENT,0,showbars,rates);

      if(prev_calculated < 1)

         printf("For the symbol %s, managed to receive only %d bars of %d requested ones",iname,copied,to_copy);

      if(copied < 1)

         return 0;

     }

   int pos = 0;

   for(int i = 0; i < copied; i++)

     {

      while(pos < copied && pos <= rates_total - prev_calculated &&  !_StopFlag)

        {

         if(rates[i].time > time[pos])

            break;

         KBuffer1[pos] = apply(rates[i].open,open[pos],InpMethod);

         KBuffer2[pos] = apply(rates[i].high,high[pos],InpMethod);

         KBuffer3[pos] = apply(rates[i].low,low[pos],InpMethod);

         KBuffer4[pos] = apply(rates[i].close,close[pos],InpMethod);

         KColors[pos] = (KBuffer1[pos] > KBuffer4[pos]) ? 1 : 0;

         pos ++;

        }

     }

   ObjectMove(0,"Bid_" + myname,0,0,StringLen(symbolname) ? SymbolInfoDouble(symbolname,SYMBOL_BID) : apply(SymbolInfoDouble(iname,SYMBOL_BID),SymbolInfoDouble(NULL,SYMBOL_BID),InpMethod));

   return(rates_total);

  }

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

//| ChartEvent function                                              |

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

void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

  {

//---

   if(id == CHARTEVENT_CHART_CHANGE)

     {

      long v_bars;

      showbars = (ChartGetInteger(0,CHART_FIRST_VISIBLE_BAR,0,v_bars) || ChartGetInteger(0,CHART_VISIBLE_BARS,0,v_bars)) ? (int)v_bars : 0;

      //--- 9hncÔk‹Oôf9ech½[¦^

      if(ChartGetInteger(0,CHART_SCALE) > 2)

        {

         PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_COLOR_CANDLES);

         PlotIndexSetInteger(0,PLOT_LINE_WIDTH,2);

        }

      else

        {

         PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_COLOR_BARS);

         PlotIndexSetInteger(0,PLOT_LINE_WIDTH,1);

        }

      //PlotIndexSetInteger(0,PLOT_LINE_COLOR,colors[color_index]);

     }

  }

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

//|                                                                  |

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

string method_tostr(method f)

  {

   switch(f)

     {

      case 1:

         return "+";

      case 2:

         return "-";

      case 3:

         return "*";

      case 4:

         return "/";

      case 5:

         return "log(/)";

      default:

         return EnumToString(f);

     }

  }

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

//|                                                                  |

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

double apply(double x,double y,method f)

  {

   double ret = EMPTY_VALUE;

   switch(f)

     {

      case 1:

         ret = x + y;

         break;

      case 2:

         ret = x - y;

         break;

      case 3:

         ret = x * y;

         break;

      case 4:

         if(y != 0)

            ret = x / y;

         break;

      case 5:

         if(x > 0 && y > 0)

            ret = log((double)x / (double)y);

         break;

     }

   return ret;

  }

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

Comments