Points between color changes

Author: Copyright © 2021, Vladimir Karputov
Price Data Components
Miscellaneous
Uses files from the file systemIt writes information to file
0 Views
0 Downloads
0 Favorites
Points between color changes
ÿþ//+------------------------------------------------------------------+

//|                                 Points between color changes.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

//|                      https://www.mql5.com/en/users/barabashkakvn |

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

#property copyright "Copyright © 2021, Vladimir Karputov"

#property link      "https://www.mql5.com/en/users/barabashkakvn"

#property version   "1.000"

#property description "Interval - in Points (1.00055-1.00045=10 points)"

#property script_show_inputs

//--- input parameters

input datetime             InpStartTime         = D'2021.01.01 00:00:00';

input group             "MA Color N Bars"

input int                  Inp_MA_ma_period     = 15;             // MA: averaging period

input int                  Inp_MA_ma_shift      = 0;              // MA: horizontal shift

input ENUM_MA_METHOD       Inp_MA_ma_method     = MODE_EMA;       // MA: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_applied_price = PRICE_CLOSE;    // MA: type of price

input uchar                Inp_MA_trend_n_bars  = 3;              // MA: trend N Bars

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

//| Script program start function                                    |

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

void OnStart()

  {

//---

   string short_name=StringFormat("(%d, %d, %s, %s, %d)",

                                  Inp_MA_ma_period, Inp_MA_ma_shift,EnumToString(Inp_MA_ma_method),

                                  EnumToString(Inp_MA_applied_price),Inp_MA_trend_n_bars);

   string file_name="Points between color changes MA Color N Bars"+short_name;

//---

   int      handle_iCustom;                        // variable for storing the handle of the iCustom indicator

//--- create handle of the indicator iMA

   handle_iCustom=iCustom(Symbol(),Period(),"MA Color N Bars",Inp_MA_ma_period,Inp_MA_ma_shift,

                          Inp_MA_ma_method,Inp_MA_applied_price,Inp_MA_trend_n_bars);

//--- if the handle is not created

   if(handle_iCustom==INVALID_HANDLE)

     {

      //--- tell about the failure and output the error code

      PrintFormat("Failed to create handle of the iCustom indicator for the symbol %s/%s, error code %d",

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early

      return;

     }

   ChartIndicatorAdd(ChartID(),1,handle_iCustom);

//---

   int counter=0;

   bool result=false;

   double ma_buffer[],ma_color[];

   MqlRates rates[];

   ArraySetAsSeries(ma_buffer,true);

   ArraySetAsSeries(ma_color,true);

   ArraySetAsSeries(rates,true);

   datetime stop_time=TimeCurrent();

   int res_copy_rates=-1;

   while(!result && counter<9)

     {

      //--- determine the number of values calculated in the indicator

      ResetLastError();

      int calculated=BarsCalculated(handle_iCustom);

      if(calculated<=0)

         PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError());

      //---

      ResetLastError();

      res_copy_rates=CopyRates(Symbol(),Period(),InpStartTime,stop_time,rates);

      if(res_copy_rates<=0)

         PrintFormat("CopyRates() returned %d, error code %d",res_copy_rates,GetLastError());

      //---

      ResetLastError();

      if(!iGetArray(handle_iCustom,0,InpStartTime,stop_time,ma_buffer))

         PrintFormat("iGetArray(MAColorBuffer) returned 'false', error code %d",GetLastError());

      int ma_buffer_size=ArraySize(ma_buffer);

      //---

      ResetLastError();

      if(!iGetArray(handle_iCustom,1,InpStartTime,stop_time,ma_color))

         PrintFormat("iGetArray(MAColorColors) returned 'false', error code %d",GetLastError());

      int ma_color_size=ArraySize(ma_color);

      //---

      if(calculated<=0 || res_copy_rates<=0 || res_copy_rates!=ma_buffer_size || res_copy_rates!=ma_color_size)

        {

         counter++;

         Sleep(1000);

        }

      else

        {

         if(res_copy_rates!=ma_buffer_size || res_copy_rates!=ma_color_size)

           {

            counter++;

            Sleep(1000);

           }

         else

            result=true;

        }

     }

//---

   if(!result)

      return;

   ResetLastError();

   int filehandle=FileOpen(file_name+".csv",FILE_WRITE|FILE_CSV);

   if(filehandle==INVALID_HANDLE)

     {

      Print("Operation FileOpen (",file_name,") failed, error ",GetLastError());

      return;

     }

   FileWrite(filehandle,"Color","Points","DateTime");// heading

//---

   double color_curr=-1.0; // '0.0' ->  YellowGreen, '1.0' -> Blue, '2.0' -> Red

   double price_curr=0.0;

   double points=Point();

   for(int i=res_copy_rates-1; i>=0; i--)

     {

      if(color_curr==-1.0)

        {

         if(ma_color[i]==1.0)

           {

            color_curr=1.0;

            price_curr=ma_buffer[i-1];

           }

         else

           {

            if(ma_color[i]==2.0)

              {

               color_curr=2.0;

               price_curr=ma_buffer[i-1];

              }

           }

        }

      else

        {

         if(ma_color[i]!=color_curr) // indicator changed color

           {

            //---

            if(color_curr==1.0 || color_curr==2.0)

              {

               int curr_points=(int)(MathAbs(price_curr-ma_buffer[i-1])/points);

               FileWrite(filehandle,(int)color_curr,curr_points,TimeToString(rates[i].time,TIME_DATE|TIME_MINUTES));

              }

            //---

            if(ma_color[i]==0.0)

              {

               color_curr=-1.0;

               price_curr=0.0;

              }

            else

              {

               if(ma_color[i]==1.0)

                  color_curr=1.0;

               if(ma_color[i]==2.0)

                  color_curr=2.0;

               price_curr=ma_buffer[i];

              }

           }

        }

     }

   FileClose(filehandle);

  }

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

//| Get value of buffers                                             |

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

bool iGetArray(const int handle,const int buffer,const datetime start_time,

               const datetime stop_time,double &arr_buffer[])

  {

   bool result=true;

   if(!ArrayIsDynamic(arr_buffer))

     {

      Print("This a no dynamic array!");

      return(false);

     }

   ArrayFree(arr_buffer);

//--- reset error code

   ResetLastError();

//--- fill a part of the iBands array with values from the indicator buffer

   int copied=CopyBuffer(handle,buffer,start_time,stop_time,arr_buffer);

   if(copied==-1)

     {

      //--- if the copying fails, tell the error code

      PrintFormat("Failed to copy data from the indicator, error code %d",GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated

      return(false);

     }

   return(result);

  }

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

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