如何正确循环;画开线并在被击中后停止画图?

时间:2018-07-08 09:38:29

标签: mql4

我在查找/理解正确的逻辑时遇到困难。

该指标应该向后看X条,画出一条有效蜡烛的开放线(即,上一个尺寸的两倍),并且当价格超过该线时,它应该更改颜色(并在其停止绘制被感动了

第一部分/第一循环(找到蜡烛并画线)到目前为止已正常工作。但是我迷失了如何/在哪里循环第二部分(检查价格是否越过线,改变颜色和/或停止画线)

  • 我是否在一个循环中完成所有操作,还是需要使用两个循环?
  • 如果有两个循环,它们必须分开还是嵌套?
  • 在第二个循环中:我应该通过iOpen来获取开放行的价格,还是使用GetObject从行中获取价格?

任何帮助或正确方向的指针都将不胜感激。 另外,请原谅我平庸的英语,因为它不是我的母语。

谢谢

编辑:我需要几行,每条有效蜡烛中的一行。这是我到目前为止的代码:

  #property indicator_chart_window
  #property indicator_buffers         2
  #property indicator_color1          Green
  #property indicator_color2          Red

  extern bool    DrawArrows           = true;
  extern int     TimeFrame            = 0;
  extern double  CandleFactor         = 1.8 ; // Min Candle Factor
  extern double  CandleMinPip         = 20 ; // Min Pips to show Candle (in pts)
  extern int     CountBars            = 24;
  extern color   LinecolorL           = Green;
  extern color   LinecolorS           = Red;
  extern color   LinecolorHit         = Blue;
  extern int     Width                = 2;
  extern int     ShiftLineRightBars   = 6 ;

  double Up[], Down[], EntryPriceArray[], OpenPriceArray[];
  double close2, open2, close1, open1, pips1, pips2, EntryPrice, OpenPrice;
  string can2, can1, label, period;
  int i;
  color Linecolor;
  static bool CrossedLine = false;
  //+------------------------------------------------------------------+

  int init()
    {
     SetIndexBuffer(0,Up);
     SetIndexBuffer(1,Down);
     if(DrawArrows){SetIndexStyle(0,DRAW_ARROW);}else{SetIndexStyle(0,DRAW_NONE);}
     if(DrawArrows){SetIndexStyle(1,DRAW_ARROW);}else{SetIndexStyle(1,DRAW_NONE);}
     SetIndexArrow(0,233);
     SetIndexArrow(1,234);

     CandleMinPip = CandleMinPip*Point;

     if (Period() == 1){period = "M1"; Width = 1; ShiftLineRightBars = ShiftLineRightBars + 5;}
     if (Period() == 5){period = "M5"; Width = 2; ShiftLineRightBars = ShiftLineRightBars + 3;}
     if (Period() == 15){period = "M15"; Width = 2; ShiftLineRightBars = ShiftLineRightBars + 2;}
     if (Period() == 30){period = "M30"; Width = 3;}
     if (Period() == 60){period = "H1"; Width = 3;}
     if (Period() == 240){period = "H4"; Width = 4;}
     if (Period() == 1440){period = "D1"; Width = 5;}

     return(0);
    }
  //+------------------------------------------------------------------+
  int deinit()
    { if (CountBars == 0) {CountBars=Bars;}
      for( i = 0; i < CountBars; i++) {
      ObjectDelete("Line_"+period+"_"+Time[i+1]);
    }
     return(0);
    }
  //+------------------------------------------------------------------+
  int start() {
     if (CountBars == 0) {CountBars=Bars;}
     for( i = 0; i < CountBars; i++){

        close1=iClose(NULL,TimeFrame,i+1);
        close2=iClose(NULL,TimeFrame,i+2);
        open1=iOpen(NULL,TimeFrame,i+1);
        open2=iOpen(NULL,TimeFrame,i+2);
        pips1 = (MathAbs(open1-close1));
        pips2 = (MathAbs(open2-close2));

              if ( pips1 > CandleMinPip){
               if ( pips1 > pips2*CandleFactor){
                int CandleTime = Time[i+1];                  // Get the Candle time of Candle i+1
                // int CandleTimeClose = CandleTime+(Period()*60*ShiftLineRightBars);
                int CandleTimeClose = Time[0];
                label = "Line_"+period+"_"+Time[i+1];

                  if (open1-close1 < 0){                   // Candle 1 is Bullish
                  Up[i+1]=iLow(NULL,TimeFrame,i+1);
                  EntryPrice = iHigh(NULL,TimeFrame,i+1);
                  Linecolor = LinecolorL;
                  }

                  if (open1-close1 > 0){                   // Candle 1 is Bearish
                  Down[i+1]=iHigh(NULL,TimeFrame,i+1);
                  EntryPrice = iLow(NULL,TimeFrame,i+1);
                  Linecolor = LinecolorS;
                  }

                  OpenPrice = iOpen(NULL,TimeFrame,i+1);  // Open Price of BiggerCandle

                  ObjectCreate(label, OBJ_TREND, 0, CandleTime, OpenPrice, CandleTimeClose, OpenPrice, 0, 0);
                  ObjectSet(label, OBJPROP_RAY, 0);
                  ObjectSet(label, OBJPROP_COLOR, Linecolor);
                  ObjectSet(label, OBJPROP_WIDTH, Width);
                  ObjectSet(label, OBJPROP_STYLE, 0);

                } // End CandleFactor  
               } // End CandleMinPip

                    if (open1-close1 > 0 &&  Bid > OpenPrice && CrossedLine == false) {   // If Bid crosses/is above the OpenPrice, Change Line Color and Change CandleTimeClose
                    ObjectSet(label, OBJPROP_COLOR, LinecolorHit);
                    CrossedLine = true;
                    //CandleTimeClose = TimeCurrent();
                    //ObjectSet(label, OBJPROP_TIME2, CandleTimeClose);
                    }

                    if (open1-close1 < 0 &&  Bid < OpenPrice && CrossedLine == false) {   // If Bid crosses/is below the OpenPrice, Change Line Color and Change CandleTimeClose
                    ObjectSet(label, OBJPROP_COLOR, LinecolorHit);
                    CrossedLine = true;
                    //CandleTimeClose = TimeCurrent();
                    //ObjectSet(label, OBJPROP_TIME2, CandleTimeClose);
                    }
     } // End "i"                
     return(0);
  }

0 个答案:

没有答案