conseiller expert - questions diverses - page 10

 
//+------------------------------------------------------------------+
//|                                                     Stoploss.mq4 |
//|      Copyright 2016, Marco vd Heijden, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Marco vd Heijden, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create object
   BitmapLabelCreate(
                     0,                     // chart's ID
                     "BmpLabel",            // label name
                     0,                     // subwindow index
                     20,                    // X coordinate
                     20,                    // Y coordinate
                     "\\Images\\on.bmp",    // image in On mode
                     "\\Images\\off.bmp",   // image in Off mode
                     0,                     // visibility scope X coordinate
                     0,                     // visibility scope Y coordinate
                     0,                     // visibility scope shift by X axis
                     0,                     // visibility scope shift by Y axis
                     0,                     // pressed/released
                     CORNER_LEFT_UPPER,     // chart corner for anchoring
                     ANCHOR_LEFT_UPPER,     // anchor type
                     clrRed,                // border color when highlighted
                     STYLE_SOLID,           // line style when highlighted
                     1,                     // move point size
                     false,                 // in the background
                     false,                 // highlight to move
                     true,                  // hidden in the object list
                     0);                    // priority for mouse click

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(ObjectGetInteger(0,"BmpLabel",OBJPROP_STATE)==true)
     {
      Print("Stoploss active");
      // Do Something...
     }

   if(ObjectGetInteger(0,"BmpLabel",OBJPROP_STATE)==false)
     {
      Print("Stoploss inactive");
      // Do Something...
     }
  }
//+------------------------------------------------------------------+
//| Create Bitmap Label object                                       |
//+------------------------------------------------------------------+
bool BitmapLabelCreate(const long              chart_ID=0,               // chart's ID
                       const string            name="BmpLabel",          // label name
                       const int               sub_window=0,             // subwindow index
                       const int               x=0,                      // X coordinate
                       const int               y=0,                      // Y coordinate
                       const string            file_on="",               // image in On mode
                       const string            file_off="",              // image in Off mode
                       const int               width=0,                  // visibility scope X coordinate
                       const int               height=0,                 // visibility scope Y coordinate
                       const int               x_offset=10,              // visibility scope shift by X axis
                       const int               y_offset=10,              // visibility scope shift by Y axis
                       const bool              state=false,              // pressed/released
                       const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // chart corner for anchoring
                       const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER, // anchor type
                       const color             clr=clrRed,               // border color when highlighted
                       const ENUM_LINE_STYLE   style=STYLE_SOLID,        // line style when highlighted
                       const int               point_width=1,            // move point size
                       const bool              back=false,               // in the background
                       const bool              selection=false,          // highlight to move
                       const bool              hidden=true,              // hidden in the object list
                       const long              z_order=0)                // priority for mouse click
  {
//--- reset the error value
   ResetLastError();
//--- create a bitmap label
   if(!ObjectCreate(chart_ID,name,OBJ_BITMAP_LABEL,sub_window,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create \"Bitmap Label\" object! Error code = ",GetLastError());
      return(false);
     }
//--- set the images for On and Off modes
   if(!ObjectSetString(chart_ID,name,OBJPROP_BMPFILE,0,file_on))
     {
      Print(__FUNCTION__,
            ": failed to load the image for On mode! Error code = ",GetLastError());
      return(false);
     }
   if(!ObjectSetString(chart_ID,name,OBJPROP_BMPFILE,1,file_off))
     {
      Print(__FUNCTION__,
            ": failed to load the image for Off mode! Error code = ",GetLastError());
      return(false);
     }

   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);         //--- set label coordinates
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);         //--- set visibility scope for the image; if width or height values
   ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
   ObjectSetInteger(chart_ID,name,OBJPROP_XOFFSET,x_offset);    //--- set the part of an image that is to be displayed in the visibility scope
   ObjectSetInteger(chart_ID,name,OBJPROP_YOFFSET,y_offset);
   ObjectSetInteger(chart_ID,name,OBJPROP_STATE,state);         //--- define the label's status (pressed or released)
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);       //--- set the chart's corner, relative to which point coordinates are defined
   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);       //--- set anchor type
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);           //--- set the border color when object highlighting mode is enabled
   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);         //--- set the border line style when object highlighting mode is enabled
   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,point_width);   //--- set a size of the anchor point for moving an object
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);           //--- display in the foreground (false) or background (true)
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);//--- enable (true) or disable (false) the mode of moving the label by mouse
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);       //--- hide (true) or display (false) graphical object name in the object list
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);      //--- set the priority for receiving the event of a mouse click in the chart
   return(true);                                                //--- successful execution
  }
//+------------------------------------------------------------------+
 
//+------------------------------------------------------------------+
//|                                                     Stoploss.mq4 |
//|      Copyright 2016, Marco vd Heijden, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Marco vd Heijden, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

bool use_stoploss; // stoploss flag
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create object
   BitmapLabelCreate(
                     0,                     // chart's ID
                     "BmpLabel",            // label name
                     0,                     // subwindow index
                     20,                    // X coordinate
                     20,                    // Y coordinate
                     "\\Images\\on.bmp",    // image in On mode
                     "\\Images\\off.bmp",   // image in Off mode
                     0,                     // visibility scope X coordinate
                     0,                     // visibility scope Y coordinate
                     0,                     // visibility scope shift by X axis
                     0,                     // visibility scope shift by Y axis
                     0,                     // pressed/released
                     CORNER_LEFT_UPPER,     // chart corner for anchoring
                     ANCHOR_LEFT_UPPER,     // anchor type
                     clrRed,                // border color when highlighted
                     STYLE_SOLID,           // line style when highlighted
                     1,                     // move point size
                     false,                 // in the background
                     false,                 // highlight to move
                     true,                  // hidden in the object list
                     0);                    // priority for mouse click

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- check stoploss
   CheckStoplossState();
  
   if(use_stoploss==0)
    {
     // Do something...
    }
  
   if(use_stoploss==1)
    {
     // Do Something else...
    }
  }
//+------------------------------------------------------------------+
//| Check Stoploss state                                             |
//+------------------------------------------------------------------+
void CheckStoplossState()
  {
   if(ObjectGetInteger(0,"BmpLabel",OBJPROP_STATE)==true)
     {
      if(use_stoploss==0)
        {
         Alert("Stoploss active");
         use_stoploss=1;
        }
     }
   if(ObjectGetInteger(0,"BmpLabel",OBJPROP_STATE)==false)
     {
      if(use_stoploss==1)
        {
         Alert("Stoploss inactive");
         use_stoploss=0;
        }
     }
  }
//+------------------------------------------------------------------+
//| Create Bitmap Label object                                       |
//+------------------------------------------------------------------+
bool BitmapLabelCreate(const long              chart_ID=0,               // chart's ID
                       const string            name="BmpLabel",          // label name
                       const int               sub_window=0,             // subwindow index
                       const int               x=0,                      // X coordinate
                       const int               y=0,                      // Y coordinate
                       const string            file_on="",               // image in On mode
                       const string            file_off="",              // image in Off mode
                       const int               width=0,                  // visibility scope X coordinate
                       const int               height=0,                 // visibility scope Y coordinate
                       const int               x_offset=10,              // visibility scope shift by X axis
                       const int               y_offset=10,              // visibility scope shift by Y axis
                       const bool              state=false,              // pressed/released
                       const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // chart corner for anchoring
                       const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER, // anchor type
                       const color             clr=clrRed,               // border color when highlighted
                       const ENUM_LINE_STYLE   style=STYLE_SOLID,        // line style when highlighted
                       const int               point_width=1,            // move point size
                       const bool              back=false,               // in the background
                       const bool              selection=false,          // highlight to move
                       const bool              hidden=true,              // hidden in the object list
                       const long              z_order=0)                // priority for mouse click
  {
//--- reset the error value
   ResetLastError();
//--- create a bitmap label
   if(!ObjectCreate(chart_ID,name,OBJ_BITMAP_LABEL,sub_window,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create \"Bitmap Label\" object! Error code = ",GetLastError());
      return(false);
     }
//--- set the images for On and Off modes
   if(!ObjectSetString(chart_ID,name,OBJPROP_BMPFILE,0,file_on))
     {
      Print(__FUNCTION__,
            ": failed to load the image for On mode! Error code = ",GetLastError());
      return(false);
     }
   if(!ObjectSetString(chart_ID,name,OBJPROP_BMPFILE,1,file_off))
     {
      Print(__FUNCTION__,
            ": failed to load the image for Off mode! Error code = ",GetLastError());
      return(false);
     }

   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);         //--- set label coordinates
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);         //--- set visibility scope for the image; if width or height values
   ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
   ObjectSetInteger(chart_ID,name,OBJPROP_XOFFSET,x_offset);    //--- set the part of an image that is to be displayed in the visibility scope
   ObjectSetInteger(chart_ID,name,OBJPROP_YOFFSET,y_offset);
   ObjectSetInteger(chart_ID,name,OBJPROP_STATE,state);         //--- define the label's status (pressed or released)
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);       //--- set the chart's corner, relative to which point coordinates are defined
   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);       //--- set anchor type
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);           //--- set the border color when object highlighting mode is enabled
   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);         //--- set the border line style when object highlighting mode is enabled
   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,point_width);   //--- set a size of the anchor point for moving an object
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);           //--- display in the foreground (false) or background (true)
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);//--- enable (true) or disable (false) the mode of moving the label by mouse
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);       //--- hide (true) or display (false) graphical object name in the object list
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);      //--- set the priority for receiving the event of a mouse click in the chart
   return(true);                                                //--- successful execution
  }
//+------------------------------------------------------------------+

Ou vous pouvez le mettre dans une fonction à part entière.

Je l'ai mis dans la fonction OnTick() mais vous pourriez vouloir l'exécuter dans la fonction timer éventuellement parce que maintenant il doit recevoir un tick afin de changer d'état et si vous placez l'ordre avant qu'un nouveau tick arrive il aura toujours l'ancien état ce qui peut être indésirable mais encore une fois le timer ne fonctionne pas dans le testeur donc ceci est préférable à des fins de test, mais éventuellement, le changement d'état dans la fonction de minuterie sera beaucoup plus rapide et fonctionnera également lorsque le marché est fermé et qu'il n'y a pas de ticks entrants. N'oubliez pas cela, car vous ne voulez pas finir par vous demander pourquoi cela ne fonctionne pas et découvrir que c'est uniquement parce qu'il n'y a pas de ticks entrants.

 
Marco vd Heijden:
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- check stoploss
   CheckStoplossState();
  
   if(use_stoploss==0)
    {
     // Do something...
    }
  
   if(use_stoploss==1)
    {
     // Do Something else...
    }
  }
//+------------------------------------------------------------------+
//| Check Stoploss state                                             |
//+------------------------------------------------------------------+
void CheckStoplossState()
  {
   if(ObjectGetInteger(0,"BmpLabel",OBJPROP_STATE)==true)
     {
      if(use_stoploss==0)
        {
         Alert("Stoploss active");
         use_stoploss=1;
        }
     }
   if(ObjectGetInteger(0,"BmpLabel",OBJPROP_STATE)==false)
     {
      if(use_stoploss==1)
        {
         Alert("Stoploss inactive");
         use_stoploss=0;
        }
     }
  }

Ou vous pouvez le mettre dans une fonction à part entière.

Je l'ai mis dans la fonction OnTick() mais vous pourriez vouloir l'exécuter dans la fonction timer éventuellement parce que maintenant il doit recevoir un tick afin de changer d'état et si vous placez l'ordre avant qu'un nouveau tick arrive il aura toujours l'ancien état ce qui peut être indésirable mais encore une fois le timer ne fonctionne pas dans le testeur donc ceci est préférable à des fins de test, mais éventuellement, le changement d'état dans la fonction de minuterie sera beaucoup plus rapide et fonctionnera également lorsque le marché est fermé et qu'il n'y a pas de ticks entrants. N'oubliez pas cela, car vous ne voulez pas finir par vous demander pourquoi cela ne fonctionne pas et découvrir que c'est uniquement parce qu'il n'y a pas de ticks entrants.

Je n'ai pas encore essayé, je sens que vos 2 commentaires vont terminer cette partie du code de mon EA.
Un grand merci, vous m'avez bien compris.

 

Encore une fois, merci beaucoup, j'ai résolu mon problème grâce à votre aide précieuse.


Puis-je utiliser OnChartEvent() au lieu de OnTick(), est-ce possible ?
J'ai déjà essayé, je ne vois pas encore de problème, mais vous avez utilisé la fonction OnTick() , je veux juste être sûr.

@Marco Je veux juste demander s'il y a un problème avec les emplacements de l'image, parce que lorsque Off ( / False / 1 ) Stop Loss actif, et lorsque On ( / True / 0 ) Stop Loss inactif. (Je ne voudrais pas changer quoi que ce soit, ce bitmap est très dangereux pour moi, sans blague - je vois que cela prend encore beaucoup de temps pour moi).
J'ai aussi besoin de demander avant de commencer à essayer, s'il y a des choses spécifiques à utiliser concernant les variables globales pour l'objet Bitmap Label? (La raison de ma demande est que je vois que l'objet d'étiquette Bitmap a besoin de quelque chose de très différent des autres... et ainsi de suite).

Merci d'avance.

 

Non, vous pouvez simplement suivre la documentation.

Et oui, il est possible de l'utiliser dans la fonction OnChartEvent(), mais n'oubliez pas que vous ne pouvez pas inclure, par exemple, le code trailingstop, car il ne sera exécuté qu'une seule fois lorsque vous cliquerez dessus.

Pour le problème du bitmap, allez dans le dossier et renommez on en onOLD, puis renommez off en on, et enfin renommez onOLD en off, cela devrait résoudre votre problème en renommant simplement les deux fichiers.

Vous pouvez aussi utiliser les boutons ci-dessous.

Je peux aussi les redimensionner si vous avez besoin de quelque chose de plus petit.

Dossiers :
onoff.zip  1 kb
 
Marco vd Heijden:

Le renommage a fonctionné pour moi, mais je pense que je dois le vérifier à nouveau, car dans votre code, il fonctionne correctement.

Variables globales : J'ai essayé comme le code ci-dessous pour cela mais aucun résultat même si je le vois dans la fenêtre des variables globales.

//---Global Variables
use_stoploss_gv = _Symbol + "test BmpLabel GV";

if ( GlobalVariableCheck( use_stoploss_gv ) == true )
{
    use_stoploss = GlobalVariableGet( use_stoploss_gv );
}
else
{
    GlobalVariableSet( use_stoploss_gv, use_stoploss );
}

Vous m'avez beaucoup aidé, merci beaucoup.

 
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

//---Global Variables
   string use_stoploss_gv=_Symbol+"test BmpLabel GV";

   if(GlobalVariableCheck(use_stoploss_gv)==true)
     {
      use_stoploss=GlobalVariableGet(use_stoploss_gv);
      Print(use_stoploss_gv," Exists");
     }
   else
     {
      GlobalVariableSet(use_stoploss_gv,use_stoploss);
      Print(use_stoploss_gv," Created");
     }

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+


Ça a l'air de fonctionner.

 

Marco vd Heijden: 

Il semble que cela fonctionne.

Mais il ne sauvegarde pas les dernières modifications.
Par exemple : je l'ajoute au graphique, puis je le commute pour qu'il devienne 'On' et ensuite je passe à une autre échelle de temps, il retourne 'Off', ce n'est pas normal.

Et j'utilise des variables globales pour d'autres fonctions 'Lot, Stop Loss, Take Profit' qui fonctionnent parfaitement, mais cette étiquette Bitmap ne fonctionne pas.
S'il vous plaît, aidez-moi ou donnez des conseils sur la façon dont je peux résoudre ce problème d'étiquette Bitmap.

Merci beaucoup.

 

Le code ci-dessous fonctionne parfaitement après que vous m'ayez aidé, merci mec.

Et j'ai renommé comme vous l'avez dit, cela fonctionne bien, mais je me demande si je fais quelque chose de mal ?
(je suis juste inquiet)

// function order sell
void ordersell()
{
    CheckStopLossState();

    if ( use_stoploss == 0 )
    { sl = Bid + points_to_change( stoploss * 10 ); }

    if ( use_stoploss == 1 )
    { sl = 0; }

    OrderSend( ... sl, ... );
    return;
}

Merci d'avance.

 
C'est simple, si cela fonctionne comme prévu, vous ne faites rien de mal, sinon cela ne fonctionne tout simplement pas.