//--- 描述
#property description "Script draws \"Linear Regression Channel\" 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="Regression"; // 通道名称
input int InpDate1=10; // 第1个点的日期, %
input int InpDate2=40; // 第2个点的日期, %
input color InpColor=clrRed; // 通道的颜色
input ENUM_LINE_STYLE InpStyle=STYLE_DASH; // 通道线的风格
input int InpWidth=2; // 通道线的宽度
input bool InpFill=false; // 填充通道颜色
input bool InpBack=false; // 背景通道
input bool InpSelection=true; // 突出移动
input bool InpRayLeft=false; // 通道延续向左
input bool InpRayRight=false; // 通道延续向右
input bool InpHidden=true; // 隐藏在对象列表
input long InpZOrder=0; // 鼠标单击优先
//+------------------------------------------------------------------+
//| 通过已给的坐标创建线性回归通道 |
//+------------------------------------------------------------------+
bool RegressionCreate(const long chart_ID=0, // 图表 ID
const string name="Regression", // 通道名称
const int sub_window=0, // 子窗口指数
datetime time1=0, // 第一个点的时间
datetime time2=0, // 第二个点的时间
const color clr=clrRed, // 通道的颜色
const ENUM_LINE_STYLE style=STYLE_SOLID, // 通道线的风格
const int width=1, // 通道线的宽度
const bool fill=false, // 填充通道颜色
const bool back=false, // 在背景中
const bool selection=true, // 突出移动
const bool ray_left=false, // 通道延续到向左
const bool ray_right=false, // 通道延续到向右
const bool hidden=true, // 隐藏在对象列表
const long z_order=0) // 鼠标单击优先
{
//--- 若未设置则设置定位点的坐标
ChangeRegressionEmptyPoints(time1,time2);
//--- 重置错误的值
ResetLastError();
//--- 通过已给的坐标创建通道
if(!ObjectCreate(chart_ID,name,OBJ_REGRESSION,sub_window,time1,0,time2,0))
{
Print(__FUNCTION__,
": failed to create linear regression channel! 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);
//--- 启用 (true) 或禁用 (false) 填充通道的模式
ObjectSetInteger(chart_ID,name,OBJPROP_FILL,fill);
//--- 显示前景 (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 RegressionPointChange(const long chart_ID=0, //图表 ID
const string name="Channel", // 通道名称
const int point_index=0, // 定位点指数
datetime time=0) // 定位点时间坐标
{
//--- 如果没有设置点的时间,将点移动到当前柱
if(!time)
time=TimeCurrent();
//--- 重置错误的值
ResetLastError();
//--- 移动定位点
if(!ObjectMove(chart_ID,name,point_index,time,0))
{
Print(__FUNCTION__,
": failed to move the anchor point! Error code = ",GetLastError());
return(false);
}
//--- 成功执行
return(true);
}
//+------------------------------------------------------------------+
//| 删除通道 |
//+------------------------------------------------------------------+
bool RegressionDelete(const long chart_ID=0, // 图表 ID
const string name="Channel") // 通道名称
{
//--- 重置错误的值
ResetLastError();
//--- 删除通道
if(!ObjectDelete(chart_ID,name))
{
Print(__FUNCTION__,
": failed to delete the channel! Error code = ",GetLastError());
return(false);
}
//--- 成功执行
return(true);
}
//+-------------------------------------------------------------------------+
//| 检查通道定位点的值和为空点设置 |
//| 默认的值 |
//+-------------------------------------------------------------------------+
void ChangeRegressionEmptyPoints(datetime &time1,datetime &time2)
{
//--- 如果第二点的时间没有设置,它将位于当前柱
if(!time2)
time2=TimeCurrent();
//--- 如果第一点的时间没有设置,它则位于第二点左侧的9个柱
if(!time1)
{
//--- 接收最近10柱开盘时间的数组
datetime temp[10];
CopyTime(Symbol(),Period(),time2,10,temp);
//--- 在第二点左侧9柱设置第一点
time1=temp[0];
}
}
//+------------------------------------------------------------------+
//| 脚本程序起始函数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 检查输入参数的正确性
if(InpDate1<0 || InpDate1>100 ||
InpDate2<0 || InpDate2>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 d2=InpDate2*(bars-1)/100;
//--- 创建线性回归通道
if(!RegressionCreate(0,InpName,0,date[d1],date[d2],InpColor,InpStyle,InpWidth,
InpFill,InpBack,InpSelection,InpRayLeft,InpRayRight,InpHidden,InpZOrder))
{
return;
}
//--- 重画图表并等待1秒
ChartRedraw();
Sleep(1000);
//--- 现在,水平向右移动通道
//--- 循环计数器
int h_steps=bars/2;
//--- 移动通道
for(int i=0;i<h_steps;i++)
{
//--- 使用下面的值
if(d1<bars-1)
d1+=1;
if(d2<bars-1)
d2+=1;
//--- 移动定位点
if(!RegressionPointChange(0,InpName,0,date[d1]))
return;
if(!RegressionPointChange(0,InpName,1,date[d2]))
return;
//--- 检查脚本操作是否已经强制禁用
if(IsStopped())
return;
//--- 重画图表
ChartRedraw();
// 0.05 秒延迟
Sleep(50);
}
//--- 1 秒延迟
Sleep(1000);
//--- 删除图表通道
RegressionDelete(0,InpName);
ChartRedraw();
//--- 1 秒延迟
Sleep(1000);
//---
}
|