指标: 自动趋势线 - 页 2

 
表演
 
Autotrendlines 是最好的免费指标之一。非常感谢该指标的创造者。但它有一个大问题!它会删除用户自己绘制的任何趋势线。我一直在等待这个问题的解决。因此,如果这个问题能在新版本中得到解决,我会非常感激。
 
Autotrendlines 是最好的免费指标之一。非常感谢该指标的创造者。但它有一个大问题!它会删除用户自己绘制的任何趋势线。我一直在等待这个问题的解决。因此,如果这个问题能在新版本中得到解决,我会非常感激。
 
这里的评论只删除指标创建的线,而将其他趋势留在图表上 //+------------------------------------------------------------------+ //| AutoTrendLines.mq5 | //| Copyright 2012, Rone.| //| rone.sergey@gmail.com | //+------------------------------------------------------------------+// https://www.mql5.com/zh/code/1220 //+------------------------------------------------------------------+//| 自动趋势线。| //| 类型 1.有两个极值。| //| 1) 从当前条形图向左 "走",寻找第一个 | //| (右)极值点,两边都是 InpRightExmSide 条形图。| //| 2) 再次从第一个极值点向左走,寻找第二个(左)极值点,两边都是 InpLeftExmSide 柱形图。| //| 3) 绘制趋势线。| //| | //| 类型 2。使用极值和 delta。| //| 1) 从当前条形图向左 "走",在 InpLeftExmSide 条形图两侧寻找第二个(左侧)极值点。| //| 3) 绘制趋势线。| //| | //| 注意: | //| 1)只有当出现新的条形图时,才会重新计算趋势线 | //| 2)当前未形成的条形图不包括在计算中 | //| 3)极值指的是左侧和右侧 | //| N 个条形图的最小值在上,最大值在下的条形图。| //+------------------------------------------------------------------+ #property copyright "Copyright 2012, Rone." #property link "rone.sergey@gmail.com" #property version "1.00" #property description "Automatic trend lines" //--- #property indicator_chart_window //--- string prefisso="AUTO_TRND"; enum ENUM_LINE_TYPE { EXM_EXM, // 1: By 2 extremums EXM_DELTA // 2: Extremum and delta }; //+------------------------------------------------------------------+ //| Class CPoint | //+------------------------------------------------------------------+ class CPoint { private: double price; datetime time; public:CPoint(); CPoint(const double p, const datetime t); ~CPoint() {}; void setPoint(const double p, const datetime t); bool operator==(const CPoint &other) const; bool operator!=(const CPoint &other) const; void operator=(const CPoint &other); double getPrice() const; datetime getTime() const; }; //--- CPoint::CPoint(void) { price = 0; time = 0; } //--- CPoint::CPoint(const double p, const datetime t) { price = p; time = t; } //--- void CPoint::setPoint(const double p, const datetime t) { price = p; time = t; } //--- bool CPoint::operator==(const CPoint &other) const { return price == other.price && time == other.time; } //--- bool CPoint::operator!=(const CPoint &other) const { return !operator==(other); } //--- void CPoint::operator=(const CPoint &other) { price = other.price; time = other.time; } //--- double CPoint::getPrice(void) const { return(price); } //--- datetime CPoint::getTime(void) const { return(time); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ CPoint curLeftSup, curRightSup, curLeftRes, curRightRes, nullPoint; //+------------------------------------------------------------------+ //| input parameters | //+------------------------------------------------------------------+ input ENUM_LINE_TYPE InpLineType = EXM_DELTA;// Line type input int InpLeftExmSide = 10; // Left extremum side (Type 1, 2) input int InpRightExmSide = 3; // Right extremum side (Type 1) input int InpFromCurrent = 3;// 输入 bool InpPrevExmBar = false; // 计算极值之前的条形图(类型 2) //--- 输入 int InpLinesWidth = 2; // 线宽 输入 color InpSupColor = clrRed; // 支持线颜色 输入 color InpResColor = clrBlue; // 阻力线颜色 //--- 全局变量 int minRequiredBars; //+------------------------------------------------------------------+ //| 自定义指标初始化函数 | //+------------------------------------------------------------------+ int OnInit() { //--- minRequiredBars = InpLeftExmSide * 2 + MathMax(InpRightExmSide, InpFromCurrent) * 2; //--- 指标缓冲区映射 //--- return(0); } //+------------------------------------------------------------------+ //| 自定义指标去初始化函数 | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- int obj_total = ObjectsTotal(0,0);for(int i=obj_total -1; i>=0; i--) { string name= ObjectName(0,i); if(StringFind(name,prefisso,0) == 0) ObjectDelete(0, name);} //--- } //+------------------------------------------------------------------+ //| 自定义指标迭代函数 | //+------------------------------------------------------------------+ 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[]) { //--- int leftIndex, rightIndex;double delta, tmpDelta; //--- if ( rates_total < minRequiredBars ) { Print("Not enough data to calculate"); return(0); } //--- if ( prev_calculated != rates_total ) { switch ( InpLineType ) { case EXM_DELTA: //--- 支持左侧点 leftIndex = rates_total - InpLeftExmSide - 2; for ( ; ;isLowestLow(leftIndex, InpLeftExmSide, low) && leftIndex > minRequiredBars; leftIndex-- ); curLeftSup.setPoint(low[leftIndex], time[leftIndex]); //--- 支持右侧点 rightIndex = rates_total - InpFromCurrent - 2; delta = (low[rightIndex] - low[leftIndex]) / (rightIndex - leftIndex); if ( !InpPrevExmBar ) { leftIndex += 1; } for ( int tmpIndex = rightIndex - 1; tmpIndex > leftIndex; tmpIndex-- ) { tmpDelta = (low[tmpIndex] - curLeftSup.getPrice()) / (tmpIndex - leftIndex); if ( tmpDelta < delta ) { delta = tmpDelta; rightIndex = tmpIndex; } } curRightSup.setRightSup.} curRightSup.setPoint(low[rightIndex], time[rightIndex]); //--- Resistance Left Point leftIndex = rates_total - InpLeftExmSide - 2; for ( ; !isHighestHigh(leftIndex, InpLeftExmSide, high) && leftIndex > minRequiredBars; leftIndex-- ); curLeftRes.setPoint(high[leftIndex], time[leftIndex]); //--- 阻力右侧点 rightIndex = rates_total - InpFromCurrent - 2; delta = (high[leftIndex] - high[rightIndex]) / (rightIndex - leftIndex); if ( !InpPrevExmBar ) { leftIndex += 1; } for ( int tmpIndex = rightIndex - 1; tmpIndex > leftIndex; tmpIndex-- ) { tmpDelta = (curLeftRes.getPrice() - high[tmpIndex]) / (tmpIndex - leftIndex); if ( tmpDelta < delta ) { delta = tmpDelta; rightIndex = tmpIndex; }} curRightRes.setPoint(high[rightIndex], time[rightIndex]); //--- break; case EXM_EXM: default://--- 支持右侧点 rightIndex = rates_total - InpRightExmSide - 2; for ( ; ; isLowestLow(rightIndex, InpRightExmSide, low) && rightIndex > minRequiredBars; rightIndex-- ); curRightSup.setPoint(low[rightIndex], time[rightIndex]); //--- 支持左侧点 leftIndex = rightIndex - InpRightExmSide; for ( ; !isLowestLow(leftIndex, InpLeftExmSide, low) && leftIndex > minRequiredBars; leftIndex-- ); curLeftSup.setPoint(low[leftIndex], time[leftIndex]); //--- 阻力右点 rightIndex = rates_total - InpRightExmSide - 2; for ( ; ;isHighestHigh(rightIndex, InpRightExmSide, high) && rightIndex > minRequiredBars; rightIndex-- ); curRightRes.setPoint(high[rightIndex], time[rightIndex]); //--- 阻力左侧点 leftIndex = rightIndex - InpRightExmSide; for ( ; ;isHighestHigh(leftIndex, InpLeftExmSide, high) && leftIndex > minRequiredBars; leftIndex-- ); curLeftRes.setPoint(high[leftIndex], time[leftIndex]); //--- break; } //--- 绘制支撑位和阻力位。//--- 绘制支撑位和阻力位 if ( curLeftSup != nullPoint && curRightSup != nullPoint ) { drawLine(prefisso+"Current_Support", curRightSup, curLeftSup, InpSupColor); } if ( curLeftRes != nullPoint && curRightRes != nullPoint ) { drawLine(prefisso+"Current_Resistance", curRightRes, curLeftRes, InpResColor); } } //--- 返回前值。} //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ //| The Local Low search function | //+------------------------------------------------------------------+ bool isLowestLow(int bar, int side, const double &Low[]) { //--- for ( int i = 1; i <= side; i++ ) { if ( Low[bar] > Low[bar-i] || Low[bar] > Low[bar+i] ) { return(false); } //--- return(true).} //--- return(true); } //+------------------------------------------------------------------+ //| 本地高点搜索函数 | //+------------------------------------------------------------------+ bool isHighestHigh(int bar, int side, const double &High[]) { //--- for ( int i = 1; i <= side; i++ ) { if ( High[bar] < High[bar-i] || High[bar] < High[bar+i] ) { return(false); }} //--- return(true); } //+------------------------------------------------------------------+ //| 绘制趋势线函数 | //+------------------------------------------------------------------+ void drawLine(string name, CPoint &right, CPoint &left, color clr) { //--- ObjectDelete(0, name); //--- ObjectCreate(0, name, OBJ_TREND, 0, right.getTime()、right.getPrice()、left.getTime()、left.getPrice());ObjectSetInteger(0, name, OBJPROP_WIDTH, InpLinesWidth);ObjectSetInteger(0, name, OBJPROP_COLOR, clr);ObjectSetInteger(0, name, OBJPROP_RAY_LEFT, true);ObjectSetInteger(0, name, OBJPROP_SELECTABLE, true);//--- } //+------------------------------------------------------------------+
 
代码不错,但趋势线移动 频繁,而且下降趋势应为负斜率,上升趋势应为正斜率。