Info Spread Panel

Author: Copyright © 2018, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Info Spread Panel
ÿþ//+------------------------------------------------------------------+

//|                                            Info Spread Panel.mq5 |

//|                              Copyright © 2018, Vladimir Karputov |

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

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

#property copyright "Copyright © 2018, Vladimir Karputov"

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

#property version   "1.000"

#property description "Control Panels and Dialogs. Info Spread Panel"

#property indicator_chart_window

#property indicator_plots               0

#property indicator_buffers             0

#property indicator_minimum             0.0

#property indicator_maximum             0.0

#include <Controls\Dialog.mqh>

#include <Controls\Label.mqh>

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

//| 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 CONTROLS_GAP_X                      (5)       // gap by X coordinate

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

//--- for labels

#define LABEL_WIDTH                         (100)     // size by X coordinate

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

//--- input parameters 

input int            InpBars=100;               // Number of bars for search maximum spread

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

//| Class CInfoSpreadPanel                                           |

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

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

class CInfoSpreadPanel : public CAppDialog

  {

private:

   CLabel            m_label_balance;                 // CLabel object

   CLabel            m_label_balance_value;           // CLabel object

   CLabel            m_label_equity;                  // CLabel object

   CLabel            m_label_equity_value;            // CLabel object

   CLabel            m_label_free_margin;             // CLabel object

   CLabel            m_label_free_margin_value;       // CLabel object

   CLabel            m_label_spread;                  // CLabel object

   CLabel            m_label_spread_value;            // CLabel object

   CLabel            m_label_spread_indicator;        // CLabel object



public:

                     CInfoSpreadPanel(void);

                    ~CInfoSpreadPanel(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);

   //---

   void              Balance(const double value)      { m_label_balance_value.Text(DoubleToString(value,2));      }

   void              Equity(const double value)       { m_label_equity_value.Text(DoubleToString(value,2));       }

   void              FreeMargin(const double value)   { m_label_free_margin_value.Text(DoubleToString(value,2));  }

   void              SpreadMax(const long current_value,const long max_value);



protected:

   //--- create dependent controls

   bool              CreateLabelBalance(void);

   bool              CreateLabelBalanceValue(void);

   bool              CreateLabelEquity(void);

   bool              CreateLabelEquityValue(void);

   bool              CreateLabelFreeMargin(void);

   bool              CreateLabelFreeMarginValue(void);

   bool              CreateLabelSpread(void);

   bool              CreateLabelSpreadIndicator(void);



  };

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

//| Event Handling                                                   |

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

EVENT_MAP_BEGIN(CInfoSpreadPanel)



EVENT_MAP_END(CAppDialog)

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

//| Constructor                                                      |

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

CInfoSpreadPanel::CInfoSpreadPanel(void)

  {

  }

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

//| Destructor                                                       |

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

CInfoSpreadPanel::~CInfoSpreadPanel(void)

  {

  }

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

//| Create                                                           |

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

bool CInfoSpreadPanel::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(!CreateLabelBalance())

      return(false);

   if(!CreateLabelBalanceValue())

      return(false);

   if(!CreateLabelEquity())

      return(false);

   if(!CreateLabelEquityValue())

      return(false);

   if(!CreateLabelFreeMargin())

      return(false);

   if(!CreateLabelFreeMarginValue())

      return(false);

   if(!CreateLabelSpread())

      return(false);

   if(!CreateLabelSpreadIndicator())

      return(false);

//--- succeed

   return(true);

  }

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

//| Spread Max                                                       |

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

void CInfoSpreadPanel::SpreadMax(const long current_value,const long max_value)

  {

/*

   max_value      -> 100% ->  30 "|"

   current_value  -> x%    -> x  "|"

*/

   double result=(double)current_value/(double)max_value;

   result*=30;

   if(result>30)

      result=30;

   string text="";

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

     {

      text=text+"|";

     }

   m_label_spread_indicator.Text(text);

//---

  }

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

//| Create the "Label Balance"                                       |

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

bool CInfoSpreadPanel::CreateLabelBalance(void)

  {

//--- coordinates

   int x1=INDENT_LEFT;

   int y1=INDENT_TOP;

   int x2=x1+LABEL_WIDTH;

   int y2=y1+LABEL_HEIGHT;

//--- create

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

      return(false);

   if(!m_label_balance.Text("Balance:"))

      return(false);

   if(!Add(m_label_balance))

      return(false);

//--- succeed

   return(true);

  }

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

//| Create the "Label Balance Value"                                 |

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

bool CInfoSpreadPanel::CreateLabelBalanceValue(void)

  {

//--- coordinates

   int x1=INDENT_LEFT+LABEL_WIDTH+CONTROLS_GAP_X;

   int y1=INDENT_TOP;

   int x2=x1+LABEL_WIDTH;

   int y2=y1+LABEL_HEIGHT;

//--- create

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

      return(false);

   if(!m_label_balance_value.Text("Balance Value"))

      return(false);

   if(!Add(m_label_balance_value))

      return(false);

//--- succeed

   return(true);

  }

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

//| Create the "Label Equity"                                        |

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

bool CInfoSpreadPanel::CreateLabelEquity(void)

  {

//--- coordinates

   int x1=INDENT_LEFT;

   int y1=INDENT_TOP+CONTROLS_GAP_Y+LABEL_HEIGHT;

   int x2=x1+LABEL_WIDTH;

   int y2=y1+LABEL_HEIGHT;

//--- create

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

      return(false);

   if(!m_label_equity.Text("Equity:"))

      return(false);

   if(!Add(m_label_equity))

      return(false);

//--- succeed

   return(true);

  }

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

//| Create the "Label Equity Value"                                  |

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

bool CInfoSpreadPanel::CreateLabelEquityValue(void)

  {

//--- coordinates

   int x1=INDENT_LEFT+LABEL_WIDTH+CONTROLS_GAP_X;

   int y1=INDENT_TOP+CONTROLS_GAP_Y+LABEL_HEIGHT;

   int x2=x1+LABEL_WIDTH;

   int y2=y1+LABEL_HEIGHT;

//--- create

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

      return(false);

   if(!m_label_equity_value.Text("Equity Value"))

      return(false);

   if(!Add(m_label_equity_value))

      return(false);

//--- succeed

   return(true);

  }

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

//| Create the "Label Free Margin"                                   |

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

bool CInfoSpreadPanel::CreateLabelFreeMargin(void)

  {

//--- coordinates

   int x1=INDENT_LEFT;

   int y1=INDENT_TOP+2*(CONTROLS_GAP_Y+LABEL_HEIGHT);

   int x2=x1+LABEL_WIDTH;

   int y2=y1+LABEL_HEIGHT;

//--- create

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

      return(false);

   if(!m_label_free_margin.Text("Free Margin:"))

      return(false);

   if(!Add(m_label_free_margin))

      return(false);

//--- succeed

   return(true);

  }

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

//| Create the "Label Free Margin Value"                             |

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

bool CInfoSpreadPanel::CreateLabelFreeMarginValue(void)

  {

//--- coordinates

   int x1=INDENT_LEFT+LABEL_WIDTH+CONTROLS_GAP_X;

   int y1=INDENT_TOP+2*(CONTROLS_GAP_Y+LABEL_HEIGHT);

   int x2=x1+LABEL_WIDTH;

   int y2=y1+LABEL_HEIGHT;

//--- create

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

      return(false);

   if(!m_label_free_margin_value.Text("Free Margin Value"))

      return(false);

   if(!Add(m_label_free_margin_value))

      return(false);

//--- succeed

   return(true);

  }

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

//| Create the "Label Spread"                                        |

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

bool CInfoSpreadPanel::CreateLabelSpread(void)

  {

//--- coordinates

   int x1=INDENT_LEFT;

   int y1=INDENT_TOP+3*(CONTROLS_GAP_Y+LABEL_HEIGHT);

   int x2=x1+LABEL_WIDTH;

   int y2=y1+LABEL_HEIGHT;

//--- create

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

      return(false);

   if(!m_label_spread.Text("Spread:"))

      return(false);

   if(!Add(m_label_spread))

      return(false);

//--- succeed

   return(true);

  }

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

//| Create the "Label Spread Indicator"                              |

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

bool CInfoSpreadPanel::CreateLabelSpreadIndicator(void)

  {

//--- coordinates

   int x1=INDENT_LEFT;

   int y1=INDENT_TOP+4*(CONTROLS_GAP_Y+LABEL_HEIGHT);

   int x2=x1+LABEL_WIDTH+CONTROLS_GAP_X+LABEL_WIDTH;

   int y2=y1+LABEL_HEIGHT;

//--- create

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

      return(false);

   if(!m_label_spread_indicator.Text("||||||||||||||||||||||||||||||"))

      return(false);

   if(!Add(m_label_spread_indicator))

      return(false);

//--- succeed

   return(true);

  }

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

//| Global Variables                                                 |

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

CInfoSpreadPanel ExtDialog;

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//--- create application dialog

   if(!ExtDialog.Create(0,"Info Spread Panel",0,250,40,496,210))

      return(INIT_FAILED);

//--- run application

   ExtDialog.Run();

//--- succeed

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//--- 

   Comment("");

//--- destroy dialog

   ExtDialog.Destroy(reason);

  }

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

//| Custom indicator iteration function                              |

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

int OnCalculate(const int        rates_total,// size of input time series 

                const int        prev_calculated,   // number of handled bars at the previous call 

                const datetime&  time[],            // Time array 

                const double&    open[],            // Open array 

                const double&    high[],            // High array 

                const double&    low[],             // Low array 

                const double&    close[],           // Close array 

                const long&      tick_volume[],     // Tick Volume array 

                const long&      volume[],          // Real Volume array 

                const int&       spread[]           // Spread array 

                )

  {

//---

   ArraySetAsSeries(spread,true);



   ExtDialog.Balance(AccountInfoDouble(ACCOUNT_BALANCE));

   ExtDialog.Equity(AccountInfoDouble(ACCOUNT_EQUITY));

   ExtDialog.FreeMargin(AccountInfoDouble(ACCOUNT_MARGIN_FREE));



   int i_max=ArrayMaximum(spread,0,InpBars);

   long spread_max=spread[i_max];

   ExtDialog.SpreadMax(spread[0],spread[i_max]);

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

   return(rates_total);

  }

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

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

  {

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

  }

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

Comments