OBJ_CHART

图表对象。

ObjChart

注意

在可视化测试期间不支持(不显示)" OBJ_CHART"类型对象。

定位点坐标用像素设置。您可以从 ENUM_BASE_CORNER 枚举选择定位角。

可以为图表对象选择交易品种,周期和比例尺。也可以启用/禁用价格比例和日期展示模式。

示例

下面的脚本创建和移动图表上的图表对象。已开发的特别函数用于创建和改变图形对象的属性。您可以在您的个人应用中使用这些函数"as is" 。

 

//--- 描述
#property description "Script creates \"Chart\" object."
//--- 启动脚本期间显示输入参数的窗口
#property script_show_inputs
//--- 脚本的输入参数
input string           InpName="Chart";             // 对象名称
input string           InpSymbol="EURUSD";          // 交易品种
input ENUM_TIMEFRAMES  InpPeriod=PERIOD_H1;         // 周期
input ENUM_BASE_CORNER InpCorner=CORNER_LEFT_UPPER// 定位角
input int              InpScale=2;                  // 比例
input bool             InpDateScale=true;           // 时间比例显示
input bool             InpPriceScale=true;          // 价格比例显示
input color            InpColor=clrRed;             // 高亮显示时的边界颜色
input ENUM_LINE_STYLE  InpStyle=STYLE_DASHDOTDOT;   // 高亮显示时的线条风格
input int              InpPointWidth=1;             // 移动点大小
input bool             InpBack=false;               // 背景对象
input bool             InpSelection=true;           // 突出移动
input bool             InpHidden=true;              // 隐藏在对象列表
input long             InpZOrder=0;                 // 鼠标单击优先
//+------------------------------------------------------------------+
//| 创建图表对象                                                       |
//+------------------------------------------------------------------+
bool ObjectChartCreate(const long              chart_ID=0,               // 图表 ID
                       const string            name="Chart",             // 对象名称
                       const int               sub_window=0,             // 子窗口指数
                       const string            symbol="EURUSD",          // 交易品种
                       const ENUM_TIMEFRAMES   period=PERIOD_H1,         // 周期
                       const int               x=0,                      // X 坐标
                       const int               y=0,                      // Y 坐标
                       const int               width=300,                // 宽度
                       const int               height=200,               // 高度
                       const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER// 定位角
                       const int               scale=2,                  // 比例
                       const bool              date_scale=true,          // 时间比例显示
                       const bool              price_scale=true,         // 价格比例显示
                       const color             clr=clrRed,               // 高亮显示时的边界颜色
                       const ENUM_LINE_STYLE   style=STYLE_SOLID,        // 高亮显示时的线条风格
                       const int               point_width=1,            // 移动点大小
                       const bool              back=false,               // 在背景中
                       const bool              selection=false,          // 突出移动
                       const bool              hidden=true,              // 隐藏在对象列表
                       const long              z_order=0)                // 鼠标单击优先
  {
//--- 重置错误的值
   ResetLastError();
//--- 创建图表对象
   if(!ObjectCreate(chart_ID,name,OBJ_CHART,sub_window,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create \"Chart\" object! Error code = ",GetLastError());
      return(false);
     }
//--- 设置对象坐标
   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
//--- 设置对象大小
   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);
   ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
//--- 设置相对于定义点坐标的图表的角
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
//--- 设置交易品种
   ObjectSetString(chart_ID,name,OBJPROP_SYMBOL,symbol);
//--- 设置周期
   ObjectSetInteger(chart_ID,name,OBJPROP_PERIOD,period);
//--- 设置比例
   ObjectSetInteger(chart_ID,name,OBJPROP_CHART_SCALE,scale);
//--- 显示 (true) 或隐藏 (false) 时间比例
   ObjectSetInteger(chart_ID,name,OBJPROP_DATE_SCALE,date_scale);
//--- 显示 (true) 或隐藏 (false) 价格比例
   ObjectSetInteger(chart_ID,name,OBJPROP_PRICE_SCALE,price_scale);
//--- 设置对象高亮模式启动时的边界颜色
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- 设置对象高亮模式启动时的边界线条风格
   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
//--- 设置移动对象的定位点大小
   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,point_width);
//--- 显示前景 (false) 或背景 (true)
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- 启用 (true) 或禁用 (false) 通过鼠标移动标签的模式
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- 在对象列表隐藏(true) 或显示 (false) 图形对象名称
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- 设置在图表中优先接收鼠标点击事件
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- 成功执行
   return(true);
  }
//+------------------------------------------------------------------+
//| 设置交易品种和图表对象时间帧                                         |
//+------------------------------------------------------------------+
bool ObjectChartSetSymbolAndPeriod(const long            chart_ID=0,       // 图表 ID (非图表对象)
                                   const string          name="Chart",     // 对象名称
                                   const string          symbol="EURUSD",  // 交易品种
                                   const ENUM_TIMEFRAMES period=PERIOD_H1// 时间帧
  {
//--- 重置错误的值
   ResetLastError();
//--- 设置图表对象交易品种和时间帧
   if(!ObjectSetString(chart_ID,name,OBJPROP_SYMBOL,symbol))
     {
      Print(__FUNCTION__,
            ": failed to set a symbol for \"Chart\" object! Error code = ",GetLastError());
      return(false);
     }
   if(!ObjectSetInteger(chart_ID,name,OBJPROP_PERIOD,period))
     {
      Print(__FUNCTION__,
            ": failed to set a period for \"Chart\" object! Error code = ",GetLastError());
      return(false);
     }
//--- 成功执行
   return(true);
  }
//+------------------------------------------------------------------+
//| 移动图表对象                                                       |
//+------------------------------------------------------------------+
bool ObjectChartMove(const long   chart_ID=0,   // 图表 ID (非图表对象)
                     const string name="Chart"// 对象名称
                     const int    x=0,          // X 坐标
                     const int    y=0)          // Y 坐标
  {
//--- 重置错误的值
   ResetLastError();
//--- 移动对象
   if(!ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x))
     {
      Print(__FUNCTION__,
            ": failed to move X coordinate of \"Chart\" object! Error code = ",GetLastError());
      return(false);
     }
   if(!ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y))
     {
      Print(__FUNCTION__,
            ": failed to move Y coordinate of \"Chart\" object! Error code = ",GetLastError());
      return(false);
     }
//--- 成功执行
   return(true);
  }
//+------------------------------------------------------------------+
//| 改变图表对象大小                                                   |
//+------------------------------------------------------------------+
bool ObjectChartChangeSize(const long   chart_ID=0,   // 图表 ID (非图表对象)
                           const string name="Chart"// 对象名称
                           const int    width=300,    // 宽度
                           const int    height=200)   // 高度
  {
//--- 重置错误的值
   ResetLastError();
//--- 改变对象大小
   if(!ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width))
     {
      Print(__FUNCTION__,
            ": failed to change the width of \"Chart\" object! Error code = ",GetLastError());
      return(false);
     }
   if(!ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height))
     {
      Print(__FUNCTION__,
            ": failed to change the height of \"Chart\" object! Error code = ",GetLastError());
      return(false);
     }
//--- 成功执行
   return(true);
  }
//+------------------------------------------------------------------+
//| 返回图表对象 ID                                                    |
//+------------------------------------------------------------------+
long ObjectChartGetID(const long   chart_ID=0,   // 图表 ID (非图表对象)
                      const string name="Chart"// 对象名称
  {
//--- 准备变量获得图表对象 ID
   long id=-1;
//--- 重置错误的值
   ResetLastError();
//--- 获得ID
   if(!ObjectGetInteger(chart_ID,name,OBJPROP_CHART_ID,0,id))
     {
      Print(__FUNCTION__,
            ": failed to get \"Chart\" object's ID! Error code = ",GetLastError());
     }
//--- 返回结果
   return(id);
  }
//+------------------------------------------------------------------+
//| 删除图表对象                                                       |
//+------------------------------------------------------------------+
bool ObjectChartDelete(const long   chart_ID=0,   // 图表 ID (非图表对象)
                       const string name="Chart"// 对象名称
  {
//--- 重置错误的值
   ResetLastError();
//---删除按钮
   if(!ObjectDelete(chart_ID,name))
     {
      Print(__FUNCTION__,
            ": failed to delete \"Chart\" object! Error code = ",GetLastError());
      return(false);
     }
//--- 成功执行
   return(true);
  }
//+------------------------------------------------------------------+
//| 脚本程序起始函数                                                   |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- 获得市场报价交易品种的数量
   int  symbols=SymbolsTotal(true);
//--- 检查交易品种列表中是否存在指定名称的交易品种
   bool exist=false;
   for(int i=0;i<symbols;i++)
      if(InpSymbol==SymbolName(i,true))
        {
         exist=true;
         break;
        }
   if(!exist)
     {
      Print("Error! ",InpSymbol," symbol is not present in \"Market Watch\"!");
      return;
     }
//--- 检查导入参数的正确性
   if(InpScale<0 || InpScale>5)
     {
      Print("Error! Incorrect values of input parameters!");
      return;
     }
 
//--- 图表窗口大小
   long x_distance;
   long y_distance;
//--- 设置窗口大小
   if(!ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0,x_distance))
     {
      Print("Failed to get the chart width! Error code = ",GetLastError());
      return;
     }
   if(!ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0,y_distance))
     {
      Print("Failed to get the chart height! Error code = ",GetLastError());
      return;
     }
//--- 设置图表对象坐标及其大小
   int x=(int)x_distance/16;
   int y=(int)y_distance/16;
   int x_size=(int)x_distance*7/16;
   int y_size=(int)y_distance*7/16;
//--- 创建图表对象
   if(!ObjectChartCreate(0,InpName,0,InpSymbol,InpPeriod,x,y,x_size,y_size,InpCorner,InpScale,InpDateScale,
      InpPriceScale,InpColor,InpStyle,InpPointWidth,InpBack,InpSelection,InpHidden,InpZOrder))
     {
      return;
     }
//--- 重画图表并等待1秒
   ChartRedraw();
   Sleep(1000);
//--- 拉伸图表对象
   int steps=(int)MathMin(x_distance*7/16,y_distance*7/16);
   for(int i=0;i<steps;i++)
     {
      //--- resize
      x_size+=1;
      y_size+=1;
      if(!ObjectChartChangeSize(0,InpName,x_size,y_size))
         return;
      //--- 检查脚本操作是否已经强制禁用
      if(IsStopped())
         return;
      //--- 重绘图表并等待0.01秒
      ChartRedraw();
      Sleep(10);
     }
//--- 半秒延迟
   Sleep(500);
//--- 改变图表时间帧
   if(!ObjectChartSetSymbolAndPeriod(0,InpName,InpSymbol,PERIOD_M1))
      return;
   ChartRedraw();
//--- 3秒延迟
   Sleep(3000);
//--- 删除对象
   ObjectChartDelete(0,InpName);
   ChartRedraw();
//--- 等待1秒
   Sleep(1000);
//---
  }