分享

底背离

 weewas 2012-05-09

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double OsMA(int ai_0){
  double MacdMain,MacdSignal,OsMA;
  MacdMain= iMACD(NULL,0,fast,slow,signal,PRICE_CLOSE,MODE_MAIN,ai_0);
  MacdSignal= iMACD(NULL,0,fast,slow,signal,PRICE_CLOSE,MODE_SIGNAL,ai_0);
  OsMA=MacdMain-MacdSignal;
  return(OsMA);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int MacdTrend(){
 
    int firstPeakOrTroughShift = GetFirstPeakOrTrough(0);
    double firstPeakOrTroughOsMA = OsMA(firstPeakOrTroughShift);
  
    if(firstPeakOrTroughOsMA > 0)
         {
           int peak_0   = GetIndicatorLastPeak(0);
           int trough_0 = GetIndicatorLastTrough(peak_0);
           int peak_1   = GetIndicatorLastPeak(trough_0);
           int trough_1 = GetIndicatorLastTrough(peak_1);
         }
       else
         {       
           trough_0 = GetIndicatorLastTrough(0);
           peak_0   = GetIndicatorLastPeak(trough_0);
           trough_1 = GetIndicatorLastTrough(peak_0);        
           peak_1   = GetIndicatorLastPeak(trough_1);                      
         }
       //---- 
       if(peak_0 == -1 || peak_1 == -1 || trough_0 == -1 ||
          trough_1 == -1)
           return(0);
       //----         
       double indicatorLastPeak = OsMA(peak_0);
       double indicatorThePeakBefore = OsMA(peak_1);
       double indicatorLastTrough = OsMA(trough_0);
       double indicatorTheTroughBefore = OsMA(trough_1);

       int pricePeak_0 = peak_0;
       int pricePeak_1 = peak_1;
       int priceTrough_0 = trough_0;
       int priceTrough_1 = trough_1;
       //----
       double priceLastPeak = High[pricePeak_0];
       double priceThePeakBefore = High[pricePeak_1];
       double priceLastTrough = Low[priceTrough_0];
       double priceTheTroughBefore = Low[priceTrough_1];
       
       if(peak_0==2 && priceLastPeak > priceThePeakBefore && indicatorLastPeak < indicatorThePeakBefore )return(-1);
       if(trough_0==2 && priceLastTrough < priceTheTroughBefore && indicatorLastTrough > indicatorTheTroughBefore )return(1);

     return(0);
}


int GetFirstPeakOrTrough(int index)
  {
   for(int i = index + 1; i < Bars; i++)
     {
       if((OsMA(i) >= OsMA(i-1) && OsMA(i) >= OsMA(i+1) &&
          OsMA(i)>= OsMA(i+2) && OsMA(i) > positiveSensitivity) ||
          (OsMA(i) <= OsMA(i-1) && OsMA(i) <= OsMA(i+1) &&
          OsMA(i) <= OsMA(i+2) && OsMA(i) < negativeSensitivity))
           return(i);
     }
   return(-1);
  }

 


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int GetIndicatorLastPeak(int index){//查找最高点的位置

   int regionStart = GetPositiveRegionStart(index);
   if(regionStart == -1)
       return(-1);
//----  
   int peakShift = 0;
   double peakValue = 0;
//----
   for(int i = regionStart; i < Bars; i++)
     {
       if(OsMA(i) > peakValue && OsMA(i) >= OsMA(i-1) &&
          OsMA(i) >= OsMA(i+1) && OsMA(i) >= OsMA(i+2) &&
          OsMA(i) > positiveSensitivity)
         {
           peakValue = OsMA(i);
           peakShift = i;
         }
       if(OsMA(i) < 0)
           break; 
     }
   return(peakShift);
}
 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int GetIndicatorLastTrough(int index){//查找之前最低点的位置

   int regionStart = GetNegativeRegionStart(index);
   if(regionStart == -1)
       return(-1);
//----        
   int troughShift = 0;
   double troughValue = 0;
//----  
   for(int i = regionStart; i < Bars; i++)
     {
       if(OsMA(i) < troughValue && OsMA(i) <= OsMA(i-1) &&
          OsMA(i) <= OsMA(i+1) && OsMA(i) <= OsMA(i+2) &&
          OsMA(i) < negativeSensitivity)
         {
           troughValue = OsMA(i);
           troughShift = i;
         }  
       if(OsMA(i) > 0)
           break; 
     } 
   return(troughShift);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int GetPositiveRegionStart(int index)//计算上一次高点的位置。
  {
   int regionStart;
   for(int i = index + 1; i < Bars; i++)
     {
       if(OsMA(i) >= OsMA(i-1) && OsMA(i) >= OsMA(i+1) &&
          OsMA(i) >= OsMA(i+2) && OsMA(i) > positiveSensitivity)
           return(i);   
     }
   return(-1);
}

int GetNegativeRegionStart(int index){//计算上一次地点的位置
   for(int i = index + 1; i < Bars; i++)
     {
       if(OsMA(i) <= OsMA(i-1) && OsMA(i) <= OsMA(i+1) &&
          OsMA(i) <= OsMA(i+2) && OsMA(i) < negativeSensitivity)
           return(i);  
     }
   return(-1);
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约