Calculated Real Rate

Author: Copyright © 2019, Vladimir Karputov
Miscellaneous
It issuies visual alerts to the screen
2 Views
0 Downloads
0 Favorites
Calculated Real Rate
ÿþ//+------------------------------------------------------------------+

//|                                         Calculated Real Rate.mq5 |

//|                              Copyright © 2019, Vladimir Karputov |

//|                                           http://wmua.ru/slesar/ |

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

#property copyright "Copyright © 2019, Vladimir Karputov"

#property link      "http://wmua.ru/slesar/"

#property version   "1.000"

#property indicator_separate_window

#property indicator_buffers 2

#property indicator_plots   2

//--- plot Label1

#property indicator_label1  "Calculated"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrLawnGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Label2

#property indicator_label2  "Real"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrDeepPink

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- input parameters

input string   InputFirst  = "EURUSD"; // First currency pair

input string   InputSecond = "GBPUSD"; // Second currency pair

//--- indicator buffers

double         CalculatedBuffer[];

double         RealBuffer[];

string         m_first_symbol="";

string         m_second_symbol="";

string         m_calculated_symbol="";

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   m_first_symbol=InputFirst;

   m_second_symbol=InputSecond;

   StringTrimLeft(m_first_symbol);

   StringTrimRight(m_first_symbol);

   StringTrimLeft(m_second_symbol);

   StringTrimRight(m_second_symbol);

//---

   bool is_custom=false;

   m_calculated_symbol=StringSubstr(m_first_symbol,0,3)+StringSubstr(m_second_symbol,0,3);

   bool symbol_exist=SymbolExist(m_calculated_symbol,is_custom);

   if(!symbol_exist)

     {

      string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?

                      " 0AGQB=K9 A8<2>; "+m_calculated_symbol+" =5 ACI5AB2C5B!":

                      "Calculated symbol "+m_calculated_symbol+" does not exist";

      Alert(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);

      return(INIT_PARAMETERS_INCORRECT);

     }

   if(!SymbolSelect(m_calculated_symbol,true))

     {

      string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?

                      " 0AGQB=K9 A8<2>; "+m_calculated_symbol+" =5 C40;>AL ?@8:@5?8BL!":

                      "Calculated symbol "+m_calculated_symbol+" failed to attach!";

      Alert(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);

      return(INIT_PARAMETERS_INCORRECT);

     }

   SymbolSelect(m_first_symbol,true);

   SymbolSelect(m_second_symbol,true);

//--- indicator buffers mapping

   SetIndexBuffer(0,CalculatedBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,RealBuffer,INDICATOR_DATA);

//--- an empty value

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);

//---

   return(INIT_SUCCEEDED);

  }

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

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

  {

//---

   int limit=prev_calculated-1;

   if(prev_calculated==0)

     {

      limit=0;

      ArrayInitialize(CalculatedBuffer,0.0);

      ArrayInitialize(RealBuffer,0.0);

     }

//---

   MqlRates first[],second[],calculated[];

   ArraySetAsSeries(first,true);

   ArraySetAsSeries(second,true);

   ArraySetAsSeries(calculated,true);

   int start_pos=0,count=1;

   if(CopyRates(m_first_symbol,Period(),start_pos,count,first)!=count ||

      CopyRates(m_second_symbol,Period(),start_pos,count,second)!=count ||

      CopyRates(m_calculated_symbol,Period(),start_pos,count,calculated)!=count)

     {

      return(0);

     }

   ArraySetAsSeries(time,true);

//---

   if(first[0].time!=second[0].time)

     {

      CalculatedBuffer[0]=0.0;

      RealBuffer[0]=0.0;

      return(rates_total);

     }

   if(second[0].time!=calculated[0].time)

     {

      CalculatedBuffer[0]=0.0;

      RealBuffer[0]=0.0;

      return(rates_total);

     }

   if(calculated[0].time!=time[0])

     {

      CalculatedBuffer[0]=0.0;

      RealBuffer[0]=0.0;

      return(rates_total);

     }

//---

   CalculatedBuffer[rates_total-1]=first[0].close/second[0].close;

   RealBuffer[rates_total-1]=calculated[0].close;

//--- return value of prev_calculated for next call

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