#property indicator_separate_window #property indicator_minimum 0 #property indicator_maximum 100 //--- 在单独指标窗口展示三条水平线 #property indicator_level1 20 #property indicator_level2 50 #property indicator_level3 80 //--- 设置水平线的粗细 #property indicator_levelwidth 5 //--- 设置水平线的颜色 #property indicator_levelcolor clrAliceBlue //--- 设置水平线的样式 #property indicator_levelstyle STYLE_DOT //+------------------------------------------------------------------+ //| 自定义指标初始化函数 | //+------------------------------------------------------------------+ int OnInit() { //--- 设置水平线的描述 IndicatorSetString(INDICATOR_LEVELTEXT,0,"First Level (index 0)"); IndicatorSetString(INDICATOR_LEVELTEXT,1,"Second Level (index 1)"); IndicatorSetString(INDICATOR_LEVELTEXT,2,"Third Level (index 2)"); //--- 设置指标缩略名 IndicatorSetString(INDICATOR_SHORTNAME,"IndicatorSetInteger() Demo"); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| 自定义指标迭代函数 | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { static int tick_counter=0; //--- 计算订单号 tick_counter++; //--- 根据订单号计数器计算水平线的颜色 ChangeLevelColor(0,tick_counter,3,6,10); // 三个最后的参数切换颜色 ChangeLevelColor(1,tick_counter,3,6,8); ChangeLevelColor(2,tick_counter,4,7,9); //--- 更改水平线的样式 ChangeLevelStyle(0,tick_counter); ChangeLevelStyle(1,tick_counter+5); ChangeLevelStyle(2,tick_counter+15); //--- 获得宽度为订单号整数除法除以5的余数 int width=tick_counter%5; //--- 迭代所有水平线并设置粗细 for(int l=0;l<3;l++) IndicatorSetInteger(INDICATOR_LEVELWIDTH,l,width+1); //--- 返回prev_calculated值以便下次调用 return(rates_total); } //+------------------------------------------------------------------+ //| 在单独指标窗口设置水平线颜色 | //+------------------------------------------------------------------+ void ChangeLevelColor(int level, // 水平线数 int tick_number,// 被除数,获得除法余数的数 int f_trigger, // 第一个颜色切换除数 int s_trigger, // 第二个颜色切换除数 int t_trigger) // 第三个颜色切换除数 { static color colors[3]={clrRed,clrBlue,clrGreen}; //--- colors[]数组的颜色标引 int index=-1; //--- 计算来自colors[]数组的颜色数为水平线涂色 if(tick_number%f_trigger==0) index=0; // 如果tick_number被f_trigger除,没有余数 if(tick_number%s_trigger==0) index=1; // 如果tick_number被s_trigger除,没有余数 if(tick_number%t_trigger==0) index=2; // 如果tick_number被t_trigger除,没有余数 //--- 如果定义颜色,设置它 if(index!=-1) IndicatorSetInteger(INDICATOR_LEVELCOLOR,level,colors[index]); //--- } //+------------------------------------------------------------------+ //| 在单独指标窗口设置水平线的样式 | //+------------------------------------------------------------------+ void ChangeLevelStyle(int level, // 水平线数 int tick_number// 获得除法余数的数 ) { //--- 存储样式的数组 static ENUM_LINE_STYLE styles[5]= {STYLE_SOLID,STYLE_DASH,STYLE_DOT,STYLE_DASHDOT,STYLE_DASHDOTDOT}; //--- styles[]数组的样式标引 int index=-1; //--- 计算styles[]数组的数字来设置水平线的样式 if(tick_number%50==0) index=5; // 如果tick_number 被50除没有余数,那么样式是STYLE_DASHDOTDOT if(tick_number%40==0) index=4; // ... 样式是 STYLE_DASHDOT if(tick_number%30==0) index=3; // ... STYLE_DOT if(tick_number%20==0) index=2; // ... STYLE_DASH if(tick_number%10==0) index=1; // ... STYLE_SOLID //--- 如果定义样式,设置它 if(index!=-1) IndicatorSetInteger(INDICATOR_LEVELSTYLE,level,styles[index]); } |