专家顾问 - 杂项问题 - 页 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
  }
//+------------------------------------------------------------------+

或者你可以把它放在一个单独的函数 中。

我把它放在OnTick()函数中,但你可能想最终在定时器函数中运行它,因为现在它必须收到一个tick才能改变状态,如果你在一个新的tick到来之前下单,它将仍然是旧的状态,这可能是不可取的,但同样,定时器在测试器中不工作,所以这但最终在定时器函数中切换状态会快得多,而且在市场关闭、没有新的跳动点时也能工作,请记住这一点,因为你不希望最后问自己为什么不工作,而发现只是因为没有新的跳动点。

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

或者你可以把它放在一个单独的函数 中。

我把它放在OnTick()函数中,但你可能想最终在定时器函数中运行它,因为现在它必须收到一个tick才能改变状态,如果你在一个新的tick到来之前下单,它将仍然是旧的状态,这可能是不可取的,但同样,定时器在测试器中不工作,所以这但最终在定时器函数中切换状态会快得多,而且在市场关闭、没有新的跳动点时也能工作,请记住这一点,因为你不希望最后问自己为什么不工作,而发现只是因为没有新的跳动点。

伙计,我还没有尝试,我觉得你的2条评论会完成我的EA代码的那一部分。
非常感谢,你正确地理解了我。

 

再一次感谢你,在你的大力帮助下,我解决了我的问题。


我可以用OnChartEvent() 代替OnTick() 吗,可以吗?
我已经试过了,我还没有看到任何问题,但你使用了OnTick() 函数,我只是想确定一下。

@ Marco 我想问的是,图像的位置是否有问题,因为当关闭(/False/1)时,止损是有效的,而当打开(/True/0)时,止损是无效的。(只是我不想改变任何东西,这个位图对我来说非常危险,不是开玩笑--我看到它仍然需要我花很长的时间)
在我开始尝试之前,我还需要问一下,对于 位图标签 对象的全局变量,有什么具体的 东西可以使用吗(我问的原因是我看到位图标签对象需要一些与其他对象完全不同的东西...等等)

谢谢你的帮助。

 

不,你可以按照文档来做。

是的,你可以在OnChartEvent()中使用它,但请记住,你不能包括例如尾随停止的代码,因为当你点击它时,它只会运行一次。

对于位图的问题,到文件夹中把on重命名为onOLD,然后把off重命名为on,再把onOLD重命名为off,这应该可以解决你的问题,只需重命名这两个文件。

或者你可以使用下面这些按钮。

如果你需要更小的东西,我也可以调整它们的大小。

附加的文件:
onoff.zip  1 kb
 
Marco vd Heijden:

重命名对我来说是有效的,但我觉得我应该再检查 一次,因为在你的代码中,它工作得很正常。

全局变量。我 尝试了下面的代码,但没有结果,即使我在全局变量窗口中看到它。

//---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 );
}

你为我做了很多,非常感谢。

 
//+------------------------------------------------------------------+
//| 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);
  }
//+------------------------------------------------------------------+


似乎是在工作。

 

Marco vd Heijden: 

似乎是在工作。

但它不保存最后的变化。
例如:我把它添加到图表中,然后我把它切换为 "打开",然后切换到另一个时间段,它返回 "关闭",这不正常。

我正在为另一个功能 "Lot, Stop Loss, Take Profit "使用全局变量,它工作得很好,但这个位图标签 却不工作。
请帮助我,或者给我建议,如何才能解决这个位图标签的问题。

非常感谢,伙计。

 

在你帮助我之后,下面的代码工作得非常好,谢谢你,伙计。

我按照你说的重命名了,效果不错,但我想知道,我是不是做错了什么?
(只是我很担心)

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

谢谢你的帮助。

 
这很简单,如果它按设计工作,你就没有做错什么,否则它就根本无法工作。
原因: