趋势线乖离率统计直方图
Params array<Numeric> timepoint([20241121.2244,20241202.2259]); array<Numeric> price_value([1945.30,1862.07]); integer column_number(5); Vars global numeric x1; global Numeric x2; global Numeric y1; global Numeric y2; global Numeric k; global numeric b; global numeric i; numeric x; numeric y; plot trend; TimeStamp my_time; Global array<Numeric> delta_data; Global array<Numeric> deltaT; Numeric size; Numeric width; plot deltaT_plot; Defs //此处添加策略函数 Events //此处实现事件函数 //初始化事件函数,策略运行期间,首先运行且只有一次 OnInit() { PrintClear; deltaT_plot.figure(1); deltaT_plot.setOption("gant","fill",true); deltaT_plot.setOption("target","fill",true); deltaT_plot.setOption("gant", "color", white); deltaT_plot.setOption("target", "color", Yellow); } //Bar更新事件函数,参数indexs表示变化的数据源图层ID数组 OnBaropen(ArrayRef<Integer> indexs) { my_time.date = timepoint[0]; my_time.time = 0; x1 = DateTimeToBarIndex(my_time); my_time.date = timepoint[1]; my_time.time = 0; x2 = DateTimeToBarIndex(my_time); y1 = Price_value[0]; y2 = price_value[1]; k = (y2-y1)/(x2-x1); b = y2-k*x2; If(date+time>=timepoint[0]) { x = DateTimeToBarIndex(date + time); y = k*x+b; trend.line("trend", y);//作图 Commentary("x="+text(x)); Commentary("y="+text(y)); } If(date+time==timepoint[0]) { delta_data[0] = close-y; } If(date+time>timepoint[0]) { numeric i; print("currentbar="+text(CurrentBar)); print("数组大小="+text(GetArraySize(delta_data))); for i = 0 to GetArraySize(delta_data)-1 { If(close-y > delta_data[i]) Continue; Else { ArrayInsert(delta_data,i,close-y); Break; } } print("i="+text(i)); print("数组大小="+text(GetArraySize(delta_data))); if(i == GetArraySize(delta_data)) { delta_data[i] = close-y; print("尾部插入"); } } //bar开盘时把上一根bar的数据插入 If(BarStatus==2) { //print(TextArray(delta_data)); //直方图 size = GetArraySize(delta_data);//取数据容量 width = round((delta_data[size-1]-delta_data[0])/column_number,0); print(TextArray(delta_data)); print("column_number=" + text(column_number)); print("size =" + text(size)); print("width =" + text(width)); numeric i = 0; Numeric j = 0; Numeric numb = 0; for i = 0 to size - 1//构造直方图统计数据 { If(delta_data[i] > delta_data[0] + (j + 1)*width) { deltaT[j] = numb; j = j + 1; numb = 0; } numb = numb + 1; } numeric sum; for i = 0 to column_number - 2//作图 { sum = sum + deltaT[i]; print("delta="+text(close-y)); print(text(delta_data[0] + i*width)); print(text(delta_data[0] + (i + 1)*width)); If(close-y>delta_data[0] + i * width and close-y<=delta_data[0] + (i + 1)*width) { deltaT_plot.barv("target", i, deltaT[i]); print("作图"); }Else { deltaT_plot.barv("deltaT", i, deltaT[i]); } } deltaT[column_number - 1] = size - sum; If(close-y>delta_data[0] + (column_number - 1) * width) { deltaT_plot.barv("target", i, deltaT[i]); print("作图"); } deltaT_plot.barv("deltaT", column_number - 1, deltaT[column_number - 1]); print("deltaT:"+TextArray(deltaT)); } }