OBJ_VLINE

垂直线。

注意

绘制垂直线时,可以设置所有图表窗口的线显示模式(属性 OBJPROP_RAY)。

示例

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

//--- 描述

#property description "Script draws \"Vertical Line\" graphical object."

#property description "Anchor point date is set in percentage of"

#property description "the chart window width in bars."

//--- 启动脚本期间显示输入参数的窗口

#property script_show_inputs

//--- 脚本的输入参数

input string          InpName="VLine";     // 线的名称

input int             InpDate=25;          // 事件日期,%

input color           InpColor=clrRed;     // 线的颜色

input ENUM_LINE_STYLE InpStyle=STYLE_DASH; // 线的风格

input int             InpWidth=3;          // 线的宽度

input bool            InpBack=false;       // 背景线

input bool            InpSelection=true;   // 突出移动

input bool            InpRay=true;         // 线延续下降

input bool            InpHidden=true;      // 隐藏在对象列表

input long            InpZOrder=0;         // 鼠标单击优先

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

//| 创建垂直线                                                        |

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

bool VLineCreate(const long            chart_ID=0,        // 图表 ID

                 const string          name="VLine",      // 线的名称

                 const int             sub_window=0,      // 子窗口指数

                 datetime              time=0,            // 线的时间

                 const color           clr=clrRed,        // 线的颜色

                 const ENUM_LINE_STYLE style=STYLE_SOLID, // 线的风格

                 const int             width=1,           // 线的宽度

                 const bool            back=false,        // 在背景中

                 const bool            selection=true,    // 突出移动

                 const bool            ray=true,          // 线延续下降

                 const bool            hidden=true,       // 隐藏在对象列表

                 const long            z_order=0)         // 鼠标单击优先

  {

//--- 如果没有线的时间,通过收盘柱来绘制它

   if(!time)

      time=TimeCurrent();

//--- 重置错误的值

   ResetLastError();

//--- 创建垂直线

   if(!ObjectCreate(chart_ID,name,OBJ_VLINE,sub_window,time,0))

     {

      Print(__FUNCTION__,

            ": failed to create a vertical line! Error code = ",GetLastError());

      return(false);

     }

//--- 设置线的颜色

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//--- 设置线的显示风格

   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);

//--- 设置线的宽度

   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);

//--- 显示前景 (false) 或背景 (true)

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

//--- 启用 (true) 或禁用 (false) 通过鼠标移动线的模式

//--- 当使用ObjectCreate函数创建图形对象时,对象不能

//--- 默认下突出并移动。在这个方法中,默认选择参数

//--- true 可以突出移动对象

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

//--- 启用 (true) 或禁用 (false) the mode of displaying the line in the chart subwindows

   ObjectSetInteger(chart_ID,name,OBJPROP_RAY,ray);

//--- 在对象列表隐藏(true) 或显示 (false) 图形对象名称

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

//--- 设置在图表中优先接收鼠标点击事件

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

//--- 成功执行

   return(true);

  }

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

//| 移动垂直线                                                        |

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

bool VLineMove(const long   chart_ID=0,   // 图表 ID

               const string name="VLine", // 线的名称

               datetime     time=0)       // 线的时间

  {

//--- 如果没有设置线的时间,将线移动到收盘柱

   if(!time)

      time=TimeCurrent();

//--- 重置错误的值

   ResetLastError();

//--- 移动垂直线

   if(!ObjectMove(chart_ID,name,0,time,0))

     {

      Print(__FUNCTION__,

            ": failed to move the vertical line! Error code = ",GetLastError());

      return(false);

     }

//--- 成功执行

   return(true);

  }

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

//| 删除垂直线                                                        |

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

bool VLineDelete(const long   chart_ID=0,   // 图表 ID

                 const string name="VLine") // 线的名称

  {

//--- 重置错误的值

   ResetLastError();

//--- 删除垂直线 

   if(!ObjectDelete(chart_ID,name))

     {

      Print(__FUNCTION__,

            ": failed to delete the vertical line! Error code = ",GetLastError());

      return(false);

     }

//--- 成功执行

   return(true);

  }

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

//| 脚本程序起始函数                                                   |

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

void OnStart()

  {

//--- 检查输入参数的正确性

   if(InpDate<0 || InpDate>100)

     {

      Print("Error! Incorrect values of input parameters!");

      return;

     }

//--- 图表窗口的可见柱的数量

   int bars=(int)ChartGetInteger(0,CHART_VISIBLE_BARS);

//--- 存储要使用的日期值的数组

//--- 设置和改变线定位点的坐标

   datetime date[];

//--- 内存分配

   ArrayResize(date,bars);

//--- 填写日期数组

   ResetLastError();

   if(CopyTime(Symbol(),Period(),0,bars,date)==-1)

     {

      Print("Failed to copy time values! Error code = ",GetLastError());

      return;

     }

//--- 定义画线的点

   int d=InpDate*(bars-1)/100;

//--- 创建垂直线

   if(!VLineCreate(0,InpName,0,date[d],InpColor,InpStyle,InpWidth,InpBack,

      InpSelection,InpRay,InpHidden,InpZOrder))

      return;

//--- 重画图表并等待1秒

   ChartRedraw();

   Sleep(1000);

//--- 现在,移动线

//--- 循环计数器

   int h_steps=bars/2;

//--- 移动线

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

     {

      //--- 使用下面的值

      if(d<bars-1)

         d+=1;

      //--- 移动点

      if(!VLineMove(0,InpName,date[d]))

         return;

      //--- 检查脚本操作是否已经强制禁用

      if(IsStopped())

         return;

      //--- 重画图表

      ChartRedraw();

      // 0.03 秒延迟

      Sleep(30);

     }

//--- 1 秒延迟

   Sleep(1000);

//--- 删除图表通道

   VLineDelete(0,InpName);

   ChartRedraw();

//--- 1 秒延迟

   Sleep(1000);

//---

  }