直方图
Params numeric ma_length(20); Vars series<Numeric> ma20; Global array<Numeric> gant_data; Global array<Numeric> gant; plot gant_plot; Numeric column_number; Numeric size; Numeric width; Events OnInit() { printclear; gant_plot.figure(1); gant_plot.setOption("gant","fill",true); gant_plot.setOption("target","fill",true); gant_plot.setOption("gant", "color", white); gant_plot.setOption("target", "color", Yellow); } OnBarOpen(ArrayRef<Integer> indexes) { If(CurrentBar==ma_length) { gant_data[0] = ma20[1];//输入点 } If(CurrentBar>ma_length) { numeric i; print("currentbar="+text(CurrentBar)); print("数组大小="+text(GetArraySize(gant_data))); for i = 0 to GetArraySize(gant_data)-1 { If(ma20[1] > gant_data[i]) Continue; Else { ArrayInsert(gant_data,i,ma20[1]); Break; } } print("i="+text(i)); print("数组大小="+text(GetArraySize(gant_data))); if(i == GetArraySize(gant_data)) { gant_data[i] = ma20[1]; print("尾部插入"); } } //bar开盘时把上一根bar的数据插入 } OnBar(ArrayRef<Integer> indexes) { ma20 = Average(close,ma_length); PlotNumeric("ma20",ma20); //决定画几个分段 column_number = 5; If(BarStatus == 2) { size = GetArraySize(gant_data); width = round((gant_data[size-1]-gant_data[0])/column_number,0); print(TextArray(gant_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(gant_data[i] > gant_data[0] + (j + 1)*width) { gant[j] = numb; j = j + 1; numb = 0; } numb = numb + 1; } numeric sum; for i = 0 to column_number - 2//作图 { sum = sum + gant[i]; print("ma20="+text(ma20)); print(text(gant_data[0] + i*width)); print(text(gant_data[0] + (i + 1)*width)); If(ma20>gant_data[0] + i * width and ma20<=gant_data[0] + (i + 1)*width) { gant_plot.barv("target", i, gant[i]); print("作图"); }Else { gant_plot.barv("gant", i, gant[i]); } } gant[column_number - 1] = size - sum; If(ma20>gant_data[0] + (column_number - 1) * width) { gant_plot.barv("target", i, gant[i]); print("作图"); } gant_plot.barv("gant", column_number - 1, gant[column_number - 1]); //print(TextArray(gant)); } }