CChartObject

 

Hallo,


mich würde ja mal interessieren ob ich das so wirklich darf, ohne das es zu Konflikten in der weiteren Verarbeitung kommt.


ich bin gerade dabei, mir meine Buttons und Lines auf OOP umzustellen

dazu wollte ich eigentlich gleich mal die fertige Library nehmen


dazu definiere ich mir im Globalen Bereich

#include <ChartObjects\ChartObject.mqh>                  CChartObject         objects;
#include <ChartObjects\ChartObjectsLines.mqh>            CChartObjectHLine    hline;
#include <ChartObjects\ChartObjectsTxtControls.mqh>      CChartObjectButton   controls;


und in der OnInit baue ich mir die Linien, die könnte ich jetzt noch auf Fehler abfragen, aber das ist Schönheit des Codes und hat nix mit der Funktion zu tun.


Wenn ich das so definiere, ja ist nicht sinnvoll schon klar, dann zeichnet er mir die Linien ein.

int OnInit()
  {
   hline.Create(0,  "ask ", 0, SymbolInfoDouble(_Symbol, SYMBOL_ASK));
   hline.Selected(true);
   hline.Color(clrYellow);
   hline.Style(STYLE_SOLID);
   hline.Width(2);
   hline.Save(true);
   
   hline.Create(0,  "bid ", 0, SymbolInfoDouble(_Symbol, SYMBOL_BID));
   hline.Selected(true);
   hline.Color(clrGreen);
   hline.Style(STYLE_SOLID);
   hline.Width(2);
   hline.Save(true);
   }

ich gehe davon aus, das dies passiert, weil er einfach den Code von oben nach unten abarbeitet.

Jetzt will ich aber die Lines in weitere Folge wieder auswählen. Nachdem ich beim hline.Selected kein Objekt dazu angebe, kann ich das ja dann auch nicht mehr direkt auswählen


Wie kann ich hier vermeiden, das er sich beim erzeugen der Linien, oder auch Buttons, da ist es ja gleich, erhängt und wie kann ich die Linien in weiterer Folge wieder auswählen?

muss ich für jede Line die ich erzeuge eine Deklaration machen?

CChartObjectHLine    hline1;
CChartObjectHLine    hline2;
CChartObjectHLine    hline3;
CChartObjectHLine    hline4;


danke mal im Voraus

amando

 

Eine umfassende Erklärung ist mir hier zu umfangreich, sorry,

Nur so viel: OOP hat mit den ChartObjekten nicht wirklich was zu tun ausser, daß die Chartobjekte selbst eigene In OOP programmierte Objekte sind.

Du kannst aber diese Objekte OOP-mässig verwalten.

Ich schenke dir diesen relativ simplen aber in der Praxis sehr brauchbaren Code.

Einfach #include <HLine.mqh> und fertig.

Im Code brauchst du dann nur mehr HLine.Draw(name,price, ....'optionale parameter') schreiben. Sogar die Instanz wird in der mqh definiert. Das halte ich für besonders trickreich.

Der Code würde noch etwas Überarbeitung benötigen, aber er funktioniert.

//+------------------------------------------------------------------+
//|                                                        HLine.mqh |
//|                                Copyright © 2018 Ing. Otto Pauser |
//|                       https://www.mql5.com/de/users/kronenchakra |
//+------------------------------------------------------------------+

#define HL_PREFIX      "HLINE_"                       // prefix to idenify the objects
#ifndef STYLE #define STYLE ENUM_LINE_STYLE \ #endif  // simple abbreviation

#define HL_COLOR      clrSilver            // default color
#define HL_WIDTH      1                    // default linewidth
#define HL_STYLE      STYLE_SOLID          // default linestyle
#define HL_SWIND      0                    // default subwindow

//+------------------------------------------------------------------+
//| CHLine definition                                                |
//+------------------------------------------------------------------+
class CHLine                                 // the class
{
   private:                                  // private declarations
      string   line_name;
      int      line_wind;
      double   lineprice;
      color    linecolor;
      int      linewidth;
      STYLE    linestyle;
      color    newColor;                     // check the color
      int      newWidth;                     // check the width
      STYLE    newStyle;                     // check the style
      int      subwindow;
      bool     persistent;                   // if true, the line is not deleted in destructor
      bool     disabled;                     // flag for faster updating

   public:                                   // public declarations
               CHLine(void);                 // default constructor
              ~CHLine(void);                 // destructor
                                             // draw or update a line 
      bool     Draw( string _name,           // unique name
                     double _price,          // price
                     color  _color=NULL,     // color
                     STYLE  _style=NULL,     // linestyle
                     int    _width=NULL,     // linewidth
                     string _sname=NULL      // subwindowname
                   );

      bool     Delete(string _name);                              // delete line
      int      DeleteAll(void);                                   // delete all lines
                                                                  // access to properties
      void     Persistent(bool how=true) { persistent=how;   }    // do not delete in destructor
      void     Color(color aColor)       { linecolor=aColor; }    // set the linecolor
      void     Width(int   aWidth)       { linewidth=aWidth; }    // set the linewidth
      void     Style(STYLE aStyle)       { linestyle=aStyle; }    // set the linestyle
      void     Subwindow(int aSubwin=0)  { subwindow=aSubwin;}    // set the subwindow
      void     SetEnabled(bool _how=true){ disabled  = !_how;}    // switch/set diabled flag
};

CHLine HLine;                                         // create the instance here !

//+------------------------------------------------------------------+
//| CHLine implementation                                            |
//+------------------------------------------------------------------+
void CHLine::CHLine(void)                             // default constructor
{
   line_name=NULL;                                    // initialize
   lineprice=NULL;                                    // initialize
   linecolor=HL_COLOR;                                // set the default color
   linewidth=HL_WIDTH;                                // set the default linewidth
   linestyle=HL_STYLE;                                // set the default linestyle
   newColor =NULL;                                    // check the color
   newWidth =NULL;                                    // check the width
   newStyle =NULL;                                    // check the style
   subwindow=HL_SWIND;                                // set the default subwindow
   disabled =false;                                   // default enabled
}

void CHLine::~CHLine(void)                            // destructor
{
   if(persistent) return;                             // exit here if persistent selected
   ObjectsDeleteAll(0,HL_PREFIX);                     // deletes all chartobjects with this prefix
}

bool CHLine::Delete(string _name)
{
   line_name = HL_PREFIX + _name;                     // combine the name with the prefix
   bool result = ObjectDelete(0,line_name);

#ifdef _DEBUG                                         // if in visual debug mode
   ChartRedraw();                                     // show the result immediatelv
#endif

   return(result);
}

int CHLine::DeleteAll(void)
{
   bool result = ObjectsDeleteAll(0,HL_PREFIX);       // deletes all chartobjects with this prefix

#ifdef _DEBUG                                         // if in visual debug mode
   ChartRedraw();                                     // show the result immediatelv
#endif

   return(result);
}

//+------------------------------------------------------------------+
//| CHLine::Draw - the main function to display a line               |
//+------------------------------------------------------------------+
bool CHLine::Draw(string _name, double _price, color _color=NULL, STYLE _style=NULL, int _width=NULL, string _sname=NULL)
{
   if(disabled) return(true);                                                 // flag disable was set, exit here
   line_name = HL_PREFIX + _name;                                             // combine the prefix with the name
   line_wind = (_sname==NULL) ? subwindow : GetSwindNum(_sname);              // check the subwindow

   newColor = (_color==NULL) ? linecolor : _color;                            // check the color
   newWidth = (_width==NULL) ? linewidth : _width;                            // check the width
   newStyle = (_style==NULL) ? linestyle : _style;                            // check the style

   if(ObjectFind(0,line_name)==WRONG_VALUE)                                   // if the chartobject does not exist
     {                                                                        // create and set properties
      if(!ObjectCreate    (0,line_name,OBJ_HLINE,line_wind,0,_price,0,0,0,0)) return(false);
      if(!ObjectSetInteger(0,line_name,OBJPROP_SELECTABLE,false            )) return(false);
      if(!ObjectSetInteger(0,line_name,OBJPROP_SELECTED  ,false            )) return(false);
      if(!ObjectSetInteger(0,line_name,OBJPROP_COLOR,newColor)) return(false);  // refresh color
      if(!ObjectSetInteger(0,line_name,OBJPROP_STYLE,newStyle)) return(false);  // refresh style
      if(!ObjectSetInteger(0,line_name,OBJPROP_WIDTH,newWidth)) return(false);  // refresh width
     }

   if(_price!=lineprice)                                                           // refresh values only if necessary
      {
         if(!ObjectSetDouble (0,line_name,OBJPROP_PRICE,_price  )) return(false);  // refresh position (price)
         lineprice= _price;
      }

   if(newColor!=linecolor)
      {
         if(!ObjectSetInteger(0,line_name,OBJPROP_COLOR,newColor)) return(false);  // refresh color
         linecolor= newColor;
      }

   if(newStyle!=linestyle)
      {
         if(!ObjectSetInteger(0,line_name,OBJPROP_STYLE,newStyle)) return(false);  // refresh style
         linestyle= newStyle;
      }

   if(newWidth!=linewidth)
      {
         if(!ObjectSetInteger(0,line_name,OBJPROP_WIDTH,newWidth)) return(false);  // refresh width
         linewidth= newWidth;
      }

#ifdef _DEBUG                                         // if in visual debug mode
   ChartRedraw();                                     // show the result immediatelv
#endif

   return(true);
}

//+------------------------------------------------------------------+ 
//| Returns number of the chart window                               | 
//+------------------------------------------------------------------+ 
int GetSwindNum(string short_name="") 
{ 
   if((ENUM_PROGRAM_TYPE)MQL5InfoInteger(MQL5_PROGRAM_TYPE)==PROGRAM_INDICATOR) 
      return(ChartWindowFind());                // function is called from the indicator, name is not required 
   else 
      return(ChartWindowFind(0,short_name));    // function is called from an Expert Advisor or script 
}

Viel spass damit! It's easy!

edit: Ich hab da noch einen Blödsinn reingeschrieben, den hab ich wieder gelöscht.

Dateien:
HLine.mqh  17 kb
 

amando :
...

Wie kann ich hier vermeiden, das er sich beim erzeugen der Linien, oder auch Buttons, da ist es ja gleich, erhängt und wie kann ich die Linien in weiterer Folge wieder auswählen?

 //+------------------------------------------------------------------+ 
 //| Expert initialization function                                   | 
 //+------------------------------------------------------------------+ 
 int OnInit ()
  {
 //--- 
  hline.Create( 0 ,   "ask " , 0 , SymbolInfoDouble ( _Symbol , SYMBOL_ASK ));
   hline.Selectable( true ); 
  hline.Selected( true );
  hline.Color( clrYellow );
  hline.Style( STYLE_SOLID );
  hline.Width( 2 );
   hline.Save( true );
 
  hline.Create( 0 ,   "bid " , 0 , SymbolInfoDouble ( _Symbol , SYMBOL_BID ));
   hline.Selectable( true ); 
  hline.Selected( true );
  hline.Color( clrGreen );
  hline.Style( STYLE_SOLID );
  hline.Width( 2 );
   hline.Save( true );
 
   EventSetMillisecondTimer ( 100 );
 //--- 
   return ( INIT_SUCCEEDED );
  }
 //+------------------------------------------------------------------+ 
 //|                                                                  | 
 //+------------------------------------------------------------------+ 
 void OnTimer ()
  {
  hline.Attach( 0 , "ask " , 0 , 1 );
   const color newcolor=hline.Color()+ 1 ;
  hline.Color(newcolor);
   ChartRedraw ();
  }

muss ich für jede Line die ich erzeuge eine Deklaration machen?

Nein, tust du nicht.
Grund der Beschwerde: