找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
首页论坛EAHub 外汇论坛
查看: 54|回复: 0

识别十字星K线的源码

[复制链接]

4万

主题

54

回帖

4万

积分

管理员

积分
45954
发表于 2026-8-27 23:58:00 | 显示全部楼层 |阅读模式
十字星K线的开盘价和收盘价大致相等,它有一个极易识别的形状,被认为是趋势结束和潜在的逆转信号。 以下是识别十字星K线的源码:



//----------------------------------------------------------------------------------------

#property indicator_separate_window

#property indicator_buffers 1

#property indicator_plots   1



#property indicator_type1  DRAW_HISTOGRAM

#property indicator_color1 Orange

#property indicator_style1 STYLE_SOLID

#property indicator_width1 2



//---- input parameters



input int     Candle_WidthMin          = 1;      // Minimum candlestick width in bars

input int     Candle_WidthMax          = 2;      // Maximum candlestick width in bars

input double  Candle_HeightPowerMin    = 0.5;    // Candlestick strength = Height/Width, points per minute

input double  Candle_BodyPowerMax      = 0.2;    // Maximum body height relative to the candlestick height

input double  Candle_ShadowPowerMax    = 0.2;    // Maximum shadow height relative to the candlestick height



//---- indicator buffers

double ExtBuf_Signal[];



int SkipBars; // how many of the oldest bars of the indicator to skip



//+------------------------------------------------------------------+

void OnInit()

{

   ArraySetAsSeries(ExtBuf_Signal , true);

   SetIndexBuffer(0, ExtBuf_Signal, INDICATOR_DATA);

   PlotIndexSetDouble (0, PLOT_EMPTY_VALUE, 0.0);

   

   SkipBars = Candle_WidthMax;

   PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, SkipBars);

}

//+------------------------------------------------------------------+

int OnCalculate(

                const int        rates_total,       // size of the input timeseries

                const int        prev_calculated,   // number of bars processed om previous call

                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[]

               )

{

   ArraySetAsSeries(open , true);

   ArraySetAsSeries(high , true);

   ArraySetAsSeries(low  , true);

   ArraySetAsSeries(close, true);



   int MaxBarNum = rates_total - SkipBars - 1;

   int BarCount  = rates_total - prev_calculated;

   // Пройдем по всем барам, которые надо посчитать

   for (int BarNum=0; BarNum < BarCount; BarNum++)

   {

      double IndValue = 0; // default value (empty) for the calculated indicator bar



      if (BarNum <= MaxBarNum)

      {

         bool DojiExists = false;



         double CandleO=0, CandleH=0, CandleL=0, CandleC=0;

         double CandleWidth=0, CandleHeight, CandleHeightH, CandleHeightL, CandleBody, CandleShadow;

         int    CandleDir=0;



         // Construct metabars with different widths

         for (int CandleBarNum=1; CandleBarNum<=Candle_WidthMax; CandleBarNum++)

         {

            int CurBar=BarNum+CandleBarNum;

            

            // Calculate OHLC prices of the metabar

            CandleO=open[CurBar];

            if (CandleBarNum==1) CandleC=close[CurBar];

            if (CandleBarNum==1 || high[CurBar] > CandleH) CandleH=high[CurBar];      

            if (CandleBarNum==1 || low [CurBar] < CandleL) CandleL=low [CurBar];

      

            CandleWidth=CandleBarNum;

            // If the minimum candlestick width is reached, check other criteria

            if (CandleWidth >= Candle_WidthMin)

            {

               DojiExists=true; // until proven otherwise

        

               //--- Calculate heights of all parts of the candlestick

        

               if (CandleO > CandleC)

               {

                  CandleHeightH=CandleH-CandleO;

                  CandleHeightL=CandleC-CandleL;

                  CandleBody=CandleO-CandleC;

               }

               else

               {

                  CandleHeightH=CandleH-CandleC;

                  CandleHeightL=CandleO-CandleL;

                  CandleBody=CandleC-CandleO;

               }

        

               CandleHeight=CandleH-CandleL;

               

               if (CandleHeightH > CandleHeightL)

               {

                  CandleDir=1;

                  CandleShadow=CandleHeightL;

               }

               else

               {

                  CandleDir=-1;

                  CandleShadow=CandleHeightH;

               }

        

               if (CandleHeight==0) DojiExists=false;

               else

               {

                  // Candlestick power

                  if ((CandleHeight/CandleWidth/_Point) < (Candle_HeightPowerMin*PeriodSeconds()/60)) DojiExists = false;

                  // Body power

                  if ((CandleBody/CandleHeight)   > Candle_BodyPowerMax)   DojiExists = false;

                  // Counter-shadow power

                  if ((CandleShadow/CandleHeight) > Candle_ShadowPowerMax) DojiExists = false;

               }

      

               if (DojiExists)

               {

                  break; // got a candlestick, do not expand the metabar further

               }

        

            }

         }



         // The indicator values are the direction and width of the found Doji candlestick

         if(DojiExists) IndValue = CandleDir * CandleWidth;

      }   

      

      // Show results on a chart

      ExtBuf_Signal[BarNum] = IndValue;

   }



   return(rates_total);

}

//+------------------------------------------------------------------+复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|EAHub外汇论坛

GMT+8, 2026-10-9 14:47 , Processed in 0.107690 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表