Current Bar to CSV

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
Current Bar to CSV
ÿþ//+------------------------------------------------------------------+

//|                                           Current Bar to CSV.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"

#include <Controls\Dialog.mqh>

#include <Controls\Button.mqh>

#include <Controls\Label.mqh>

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

//| Enum VLine Or Rectangle                                          |

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

enum ENUM_VLINE_OR_RECTANGLE

  {

   vline=0,       // VLine

   rectangle=1,   // Rectangle

  };

//--- input parameters

input string                  InpFileName          = "001.csv";               // file name

input bool                    InpSymbolPeriod      = true;                    // Symbol+Period+file name

input ENUM_VLINE_OR_RECTANGLE InpVlineOrRectangle  = rectangle;               // object type

input string                  InpObjName           = "Current Bar to CSV";    // object name

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

//| defines                                                          |

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

//--- indents and gaps

#define INDENT_LEFT                         (11)      // indent from left (with allowance for border width)

#define INDENT_TOP                          (11)      // indent from top (with allowance for border width)

#define INDENT_RIGHT                        (11)      // indent from right (with allowance for border width)

#define INDENT_BOTTOM                       (11)      // indent from bottom (with allowance for border width)

#define CONTROLS_GAP_X                      (5)       // gap by X coordinate

#define CONTROLS_GAP_Y                      (5)       // gap by Y coordinate

//--- for buttons

#define BUTTON_WIDTH                        (60)      // size by X coordinate

#define BUTTON_HEIGHT                       (20)      // size by Y coordinate

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

//| Class CControlsDialog                                            |

//| Usage: main dialog of the Controls application                   |

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

class CControlsDialog : public CAppDialog

  {

private:

   CLabel            m_label;                         // CLabel object

   CLabel            m_label_datetime;                // CLabel object

   CButton           m_button;                        // CButton object



public:

                     CControlsDialog(void);

                    ~CControlsDialog(void);

   //--- create

   virtual bool      Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2);

   //--- chart event handler

   virtual bool      OnEvent(const int id,const long &lparam,const double &dparam,const string &sparam);

   //--- text label

   void              TextLabel(const datetime time_left, const datetime time_right=0);



protected:

   //--- create dependent controls

   bool              CreateLabels(void);

   bool              CreateButton(void);

   //--- handlers of the dependent controls events

   void              OnClickButton(void);

   //---

   datetime          m_date_left;

   datetime          m_date_right;



  };

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

//| Event Handling                                                   |

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

EVENT_MAP_BEGIN(CControlsDialog)

ON_EVENT(ON_CLICK,m_button,OnClickButton)

EVENT_MAP_END(CAppDialog)

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

//| Constructor                                                      |

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

CControlsDialog::CControlsDialog(void) :

   m_date_left(0),

   m_date_right(0)

  {

  }

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

//| Destructor                                                       |

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

CControlsDialog::~CControlsDialog(void)

  {

  }

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

//| Create                                                           |

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

bool CControlsDialog::Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2)

  {

   if(!CAppDialog::Create(chart,name,subwin,x1,y1,x2,y2))

      return(false);

//--- create dependent controls

   if(!CreateLabels())

      return(false);

   if(!CreateButton())

      return(false);

//--- succeed

   return(true);

  }

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

//| Text Label                                                       |

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

void CControlsDialog::TextLabel(const datetime time_left,const datetime time_right=0)

  {

   m_date_left=time_left;

   m_date_right=time_right;

   string text=TimeToString(m_date_left,TIME_DATE|TIME_MINUTES);

   if(m_date_right>D'1970.01.01 00:00')

      text+=" - "+TimeToString(m_date_right,TIME_DATE|TIME_MINUTES);

   m_label_datetime.Text(text);

   ChartRedraw();

  }

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

//| Create the "Labele" elements                                     |

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

bool CControlsDialog::CreateLabels(void)

  {

//--- coordinates

   int x1=INDENT_LEFT;

   int y1=INDENT_TOP;

   int x2=x1+2*BUTTON_WIDTH;

   int y2=y1+BUTTON_HEIGHT;

//--- create

   if(!m_label.Create(m_chart_id,m_name+"Label",m_subwin,x1,y1,x2,y2))

      return(false);

   if(!m_label.Text("You clicked on: "))

      return(false);

   if(!Add(m_label))

      return(false);

//--- coordinates

   x1=x2+CONTROLS_GAP_X;

   x2=x1+BUTTON_WIDTH;

//--- create

   if(!m_label_datetime.Create(m_chart_id,m_name+"Label DateTime",m_subwin,x1,y1,x2,y2))

      return(false);

   if(!m_label_datetime.Text("DateTime"))

      return(false);

   if(!Add(m_label_datetime))

      return(false);

//--- succeed

   return(true);

  }

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

//| Create the "Start" buttom                                        |

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

bool CControlsDialog::CreateButton(void)

  {

//--- coordinates

   int x1=INDENT_LEFT;

   int y1=INDENT_TOP+BUTTON_HEIGHT+CONTROLS_GAP_Y;

   int x2=x1+BUTTON_WIDTH+2*BUTTON_WIDTH;

   int y2=y1+BUTTON_HEIGHT;

//--- create

   if(!m_button.Create(m_chart_id,m_name+"Start",m_subwin,x1,y1,x2,y2))

      return(false);

   if(!m_button.Text("Start"))

      return(false);

   if(!Add(m_button))

      return(false);

//--- succeed

   return(true);

  }

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

//| Event handler                                                    |

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

void CControlsDialog::OnClickButton(void)

  {

   MqlRates rates[];

   if(m_date_left==D'1970.01.01 00:00' && m_date_right==D'1970.01.01 00:00')

      return;

   if(m_date_left==D'1970.01.01 00:00')

      return;

   int copy_rates=-1;

   if(m_date_right==D'1970.01.01 00:00')

      copy_rates=CopyRates(Symbol(),Period(),m_date_left,1,rates);

   else

      copy_rates=CopyRates(Symbol(),Period(),m_date_left,m_date_right,rates);

   if(copy_rates<1)

     {

      Comment(__FUNCTION__+" \"CopyRates: "+IntegerToString(copy_rates));

      return;

     }

//--- correct way of working in the "file sandbox"

   ResetLastError();

   string file_name=InpFileName;

   if(InpSymbolPeriod)

      file_name=Symbol()+"_"+StringSubstr(EnumToString(Period()),7,-1)+"_"+InpFileName;

   int filehandle=FileOpen(file_name,FILE_WRITE|FILE_CSV);

   if(filehandle!=INVALID_HANDLE)

     {

      FileWrite(filehandle,"Date","Open","High","Low","Close");

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

        {

         FileWrite(filehandle,rates[i].time,

                   DoubleToString(rates[i].open,Digits()),

                   DoubleToString(rates[i].high,Digits()),

                   DoubleToString(rates[i].low,Digits()),

                   DoubleToString(rates[i].close,Digits())

                  );

        }

      FileClose(filehandle);

     }

   else

      Comment(__FUNCTION__+" \"Operation FileOpen failed, error ",GetLastError());

  }

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

//| Global Variables                                                 |

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

CControlsDialog ExtDialog;

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//--- create timer

   EventSetTimer(10);

//--- create application dialog

   if(!ExtDialog.Create(0,"Current Bar to CSV",0,40,40,415,135))

      return(INIT_FAILED);

//--- run application

   ExtDialog.Run();

//--- succeed

   return(INIT_SUCCEEDED);

  }

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

//| Timer function                                                   |

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

void OnTimer()

  {

//---

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

   Comment("");

//--- destroy timer

   EventKillTimer();

//--- destroy dialog

   ExtDialog.Destroy(reason);

  }

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

//| Expert chart event function                                      |

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

void OnChartEvent(const int id,         // event ID

                  const long& lparam,   // event parameter of the long type

                  const double& dparam, // event parameter of the double type

                  const string& sparam) // event parameter of the string type

  {

   if(id==CHARTEVENT_OBJECT_DRAG || id==CHARTEVENT_OBJECT_CHANGE)

     {

      if(sparam==InpObjName)

        {

         if(InpVlineOrRectangle==vline)

           {

            if(ObjectGetInteger(ChartID(),sparam,OBJPROP_TYPE)==OBJ_VLINE)

              {

               ExtDialog.TextLabel(ObjectGetInteger(ChartID(),sparam,OBJPROP_TIME));

              }

           }

         else

           {

            if(ObjectGetInteger(ChartID(),sparam,OBJPROP_TYPE)==OBJ_RECTANGLE)

              {

               ExtDialog.TextLabel(ObjectGetInteger(ChartID(),sparam,OBJPROP_TIME,0),ObjectGetInteger(ChartID(),sparam,OBJPROP_TIME,1));

              }

           }

        }

     }

   ExtDialog.ChartEvent(id,lparam,dparam,sparam);

  }

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

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