OBJ_TRENDBYANGLE

角度趋势线。

注意

对于角度趋势线,它可以指定延续向右和/或向左展示的模式(OBJPROP_RAY_RIGHTOBJPROP_RAY_LEFT 根据属性)。

角度和第二个定位点的坐标都可以用于设置线的倾斜度。

示例

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

//--- 描述

#property description "Script draws \"Trend Line By Angle\" graphical object."

#property description "Anchor point coordinates are set in percentage of the size of"

#property description "the chart window."

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

#property script_show_inputs

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

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

input int             InpDate1=50;         // 第1个点的日期, %

input int             InpPrice1=75;        // 第1个点的价格, %

input int             InpAngle=0;          // 线型倾斜角

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

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

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

input bool            InpBack=false;       // 背景线

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

input bool            InpRayLeft=false;    // 线延续向左

input bool            InpRayRight=true;    // 线延续向右

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

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

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

//| 创建角度趋势线                                                     |

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

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

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

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

                        datetime              time=0,            // 点的时间

                        double                price=0,           // 点的价格

                        const double          angle=45.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_left=false,    // 线延续向左

                        const bool            ray_right=true,    // 线延续向右

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

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

  {

//--- 创建第二个点以便于鼠标拖拽趋势线

   datetime time2=0;

   double   price2=0;

//--- 若未设置则设置定位点的坐标

   ChangeTrendEmptyPoints(time,price,time2,price2);

//--- 重置错误的值

   ResetLastError();

//--- 使用2个点创建趋势线

   if(!ObjectCreate(chart_ID,name,OBJ_TRENDBYANGLE,sub_window,time,price,time2,price2))

     {

      Print(__FUNCTION__,

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

      return(false);

     }

//--- 改变趋势线的斜角;改变斜角时,第二点的坐标

//--- 根据新的角度值,自动重新定义线的点

   ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);

//--- 设置线的颜色

   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) 延续向左显示线的模式

   ObjectSetInteger(chart_ID,name,OBJPROP_RAY_LEFT,ray_left);

//--- 启用 (true) 或禁用 (false) 延续向右显示线的模式

   ObjectSetInteger(chart_ID,name,OBJPROP_RAY_RIGHT,ray_right);

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

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

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

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

//--- 成功执行

   return(true);

  }

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

//| 改变趋势线定位点的坐标                                              |

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

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

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

                      datetime     time=0,           // 定位点时间坐标

                      double       price=0)          // 定位点价格坐标

  {

//--- 如果没有设置点的位置,则将其移动到当前的卖价柱

   if(!time)

      time=TimeCurrent();

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- 重置错误的值

   ResetLastError();

//--- 移动趋势线定位点  

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

     {

      Print(__FUNCTION__,

            ": failed to move the anchor point! Error code = ",GetLastError());

      return(false);

     }

//--- 成功执行

   return(true);

  }

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

//| 改变趋势线的斜角                                                   |

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

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

                      const string name="TrendLine", // 趋势线名称

                      const double angle=45)         // 趋势线斜角

  {

//--- 重置错误的值

   ResetLastError();

//--- 改变趋势线的斜角 

   if(!ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle))

     {

      Print(__FUNCTION__,

            ": failed to change the line's slope angle! Error code = ",GetLastError());

      return(false);

     }

//--- 成功执行

   return(true);

  }

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

//| 删除趋势线                                                        |

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

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

                 const string name="TrendLine") // 线的颜色

  {

//--- 重置错误的值

   ResetLastError();

//--- 删除趋势线

   if(!ObjectDelete(chart_ID,name))

     {

      Print(__FUNCTION__,

            ": failed to delete a trend line! Error code = ",GetLastError());

      return(false);

     }

//--- 成功执行

   return(true);

  }

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

//| 检查趋势线定位点的值和为空点设置                                     |

//| 默认的值                                                          |

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

void ChangeTrendEmptyPoints(datetime &time1,double &price1,

                            datetime &time2,double &price2)

  {

//--- 如果第一点的时间没有设置,它将位于当前柱

   if(!time1)

      time1=TimeCurrent();

//--- 如果第一点的价格没有设置,则它将用卖价值

   if(!price1)

      price1=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- 设置第二坐标,辅助点

//--- 第二个点是左侧第9个柱并且价格相同

   datetime second_point_time[10];

   CopyTime(Symbol(),Period(),time1,10,second_point_time);

   time2=second_point_time[0];

   price2=price1;

  }

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

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

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

void OnStart()

  {

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

   if(InpDate1<0 || InpDate1>100 || InpPrice1<0 || InpPrice1>100)

     {

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

      return;

     }

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

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

//--- 价格数组大小

   int accuracy=1000;

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

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

   datetime date[];

   double   price[];

//--- 内存分配

   ArrayResize(date,bars);

   ArrayResize(price,accuracy);

//--- 填写日期数组

   ResetLastError();

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

     {

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

      return;

     }

//--- 填写价格数组

//--- 找出图表的最高值和最低值

   double max_price=ChartGetDouble(0,CHART_PRICE_MAX);

   double min_price=ChartGetDouble(0,CHART_PRICE_MIN);

//--- 定义变化的价格并填写该数组

   double step=(max_price-min_price)/accuracy;

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

      price[i]=min_price+i*step;

//--- 定义画线的点

   int d1=InpDate1*(bars-1)/100;

   int p1=InpPrice1*(accuracy-1)/100;

//--- 创建趋势线

   if(!TrendByAngleCreate(0,InpName,0,date[d1],price[p1],InpAngle,InpColor,InpStyle,

      InpWidth,InpBack,InpSelection,InpRayLeft,InpRayRight,InpHidden,InpZOrder))

     {

      return;

     }

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

   ChartRedraw();

   Sleep(1000);

//--- 现在,移动和旋转线

//--- 循环计数器

   int v_steps=accuracy/2;

//--- 移动定位点并改变线的斜角

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

     {

      //--- 使用下面的值

      if(p1>1)

         p1-=1;

      //--- 移动点

      if(!TrendPointChange(0,InpName,date[d1],price[p1]))

         return;

      if(!TrendAngleChange(0,InpName,18*(i+1)))

         return;

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

      if(IsStopped())

         return;

      //--- 重画图表

      ChartRedraw();

     }

//--- 1 秒延迟

   Sleep(1000);

//--- 从图表删除

   TrendDelete(0,InpName);

   ChartRedraw();

//--- 1 秒延迟

   Sleep(1000);

//---

  }