Download MetaTrader 5

I can't see text (by ObjectCreate) on the chart while back testing...

To add comments, please log in or register
Kan Hasegawa
10
Kan Hasegawa  

I was making the candlestick price pattern indicator, but the text (supposed to be created by ObjectCreate) didn't show while back testing.

(it does show when I simply apply on the chart.)

I also noticed my indicator doesn't really update by just compiling.

(The other indicators, I can see the changes on the chart immediately when I compiles)

So I have to delete this indicator and put it again to check the changes I made.

I usually like to use for(i=limit-1; i>=0; i--) for calculation, rather than (i=0; i<limit; i++).

but if I use for(i=limit-1; i>=0; i--) on this cahrt, the text (created by ObjectCreate) doesn't show on the chart at all.


I think all of those problem are some what related to the fact I couldn't code with (i=limit-1; i>=0; i--) style (or not).

Could you help me with my code?

1. I want to change to i-- style or I want to know why its not working with i--.

2. I want to see the text on the chart while back testing.

3. I want to see the changes on the chart when I changed and compiles.

Thank you.

#property indicator_chart_window
#property indicator_buffers 2
//A  
#property indicator_color1 Blue
#property indicator_width1  6
//B
#property indicator_color2 DeepPink 
#property indicator_width2  6

/*------------------------------------------------------------------*/
//---- buffers
 double A[];
 double B[];

    bool Bullish_Engulfing,Bullish_Piercing,Hammer,Morning_Star;
    double UpperShadow,Lower_Shadow;
    double Bar1_Whole,Bar1_body;
    double Bar2_Whole,Bar2_body;
    double Bar3_Whole, Bar3_body;
    double BodyHigh_1 = 0;
    double BodyLow_1 = 0;
  
     double Open_1;
     double Open_2;
     double Open_3;
     double Open_4;
     
     double Close_1;
     double Close_2;
     double Close_3;
     double Close_4;
     
     double High_1;
     double High_2;
     double High_3;
     double High_4;
     
     double Low_1;
     double Low_2;
     double Low_3;
     double Low_4;

/*------------------------------------------------------------------*/
int init()
  {
   IndicatorBuffers(2);//
   IndicatorShortName("Candle_Price_Pattern");
     
   //SetIndexStyle(0, DRAW_LINE);
   //SetIndexStyle(0, DRAW_ARROW);    

   SetIndexBuffer(0, A);
   SetIndexStyle(0, DRAW_ARROW);
  SetIndexArrow(0, 71);//
   SetIndexLabel(0, "UP_snapback");  

   SetIndexBuffer(1, B);
   SetIndexStyle(1, DRAW_ARROW);
   SetIndexArrow(1, 72);//
   SetIndexLabel(1, "Down_snapback");  

 return(0);
  }

//+------------------------------------------------------------------+
int deinit() 
  {
  ObjectsDeleteAll(0, OBJ_TEXT);
   return(0);
  }
//+------------------------------------------------------------------+
int start() 
 {

 int i;
    int counted_bars=IndicatorCounted();
    if(counted_bars<0) return(-1);
    if(counted_bars > 0) counted_bars--;
    int limit=Bars-counted_bars;
    for(i=0; i<limit; i++)   
    //for(i=limit-1; i>=0; i--)

   {
   
    A[i] =EMPTY_VALUE;
    B[i] =EMPTY_VALUE;
      
      Open_1=Open[i+1];
      Open_2=Open[i+2];
      Open_3=Open[i+3];
      Open_4=Open[i+4];
     
      Close_1=Close[i+1];
      Close_2=Close[i+2];
      Close_3=Close[i+3];
      Close_4=Close[i+4];
     
      High_1=High[i+1];
      High_2=High[i+2];
      High_3=High[i+3];
      High_4=High[i+4];
     
      Low_1=Low[i+1];
      Low_2=Low[i+2];
      Low_3=Low[i+3];
      Low_4=Low[i+4];
           
      if (Open_1>Close_1) 
      {
         BodyHigh_1 = Open_1;
         BodyLow_1 = Close_1;  
      }
      
      else 
      {
         BodyHigh_1 = Close_1;
         BodyLow_1 = Open_1; 
      }

       UpperShadow = High_1-BodyHigh_1;
       Lower_Shadow = BodyLow_1-Low_1;
       Bar1_Whole=MathAbs((High_1-Low_1));
       Bar1_body=MathAbs((Open_1-Close_1));
       Bar2_Whole=MathAbs((High_2-Low_2));
       Bar2_body=MathAbs((Open_2-Close_2));
       Bar3_Whole=MathAbs((High_3-Low_3));
       Bar3_body=MathAbs((Open_3-Close_3));
/*------------------------------------------------------------------*/


    Bullish_Engulfing=Bullish_Engulfing();
    Bullish_Piercing=Bullish_Piercing();      
    Hammer=Hammer();
    Morning_Star=Morning_Star();

 
/*------------------------------------------------------------------*/

if(Bullish_Engulfing)
    {
    A[i] =Close[i+1];
    ObjectCreate("Bullish_Engulfing"+i, OBJ_TEXT, 0, Time[i+2], Low[i+1]);
    ObjectSetText("Bullish_Engulfing"+i, "Bullish_Engulfing", 12, "Arial", Black);
    ObjectSet("Bullish_Engulfing"+i, OBJPROP_ANGLE, 90);   //vertical
}
   
if(Bullish_Piercing)
    {
    A[i] = Close[i+1];
    ObjectCreate("Bullish_Piercing"+i, OBJ_TEXT, 0, Time[i+2], Low[i+1]);
    ObjectSetText("Bullish_Piercing"+i, "Bullish_Piercing", 12, "Arial", Blue);
    ObjectSet("Bullish_Piercing"+i, OBJPROP_ANGLE, 90);   //vertical
    }

if(Hammer)
    {
    A[i] = Close[i+1];
    ObjectCreate("Hammer"+i, OBJ_TEXT, 0, Time[i+2], Low[i+1]);
    ObjectSetText("Hammer"+i, "Hammer", 12, "Arial", Red);
    ObjectSet("Hammer"+i, OBJPROP_ANGLE, 90);   //vertical
    }
   
if(Morning_Star)
    {
    A[i] = Close[i+1];
    ObjectCreate("Morning_Star"+i, OBJ_TEXT, 0, Time[i+2], Low[i+1]);
    ObjectSetText("Morning_Star"+i, "Morning_Star", 12, "Arial", Purple);
    ObjectSet("Morning_Star"+i, OBJPROP_ANGLE, 90);   //vertical
    }

    }
   return(0);
 }

//+------------------------------------------------------------------+    
//BULLISH_REVERSAL
//+------------------------------------------------------------------+ 
bool Bullish_Engulfing(){if(//
   

    (Open_2 > Close_2) //Red candle bar2
    && (Close_1 > Open_1) //green candle bar1
    //&& (Close_2 >= Open_1) //bar2 close higher than bar1 open//FX関係なさそう
    && (Close_1 >= Open_2) //bar1 close higher than bar2 open(engulf)
    && ((Close_1 - Open_1) > (Open_2 - Close_2))//bar1 body bigger than bar2 body(engulf)
    //&& High_4>High_3 
    && Low_4>Low_3 
    && High_3>High_2 //bar2 high lower than bar3 low
    && Low_3>Low_2 //bar2 low lower than bar3 low
    // && (Low_2>Low_1 //bar 1 low lower than bar 2 low       
    && High_2<High_1 //bar 1 high higher than bar 2 high
    && ((Close_1-Open_1)>(High_1-Low_1)/3) // bar1 body bigger than 1/3 of  bar1
    && ((Open_2-Close_2)>(High_2-Low_2)/3)// bar2 body bigger than 1/3 of  bar2
         
      
)return(true);else return(false);}//
//+------------------------------------------------------------------+
//BEARISH_REVERSAL
//+------------------------------------------------------------------+ 
bool Bullish_Piercing(){if(//

    (Open_2 > Close_2) //Red candle bar2
    && (Close_1 > Open_1) //green candle bar1
    //&& (Close_2 >= Open_1) //bar2 close higher than bar1 open//FX関係なさそう
    && (Close_1 < Open_2) //bar 1 close lower than bar2 open (Piercing)
    &&(((Close_2 + Open_2) / 2) < Close_1)// bar1 close more than half of bar2 body  (Piercing)      
    //&& High_4>High_3 
    && Low_4>Low_3 
    && High_3>High_2 //bar2 high lower than bar3 low
    && Low_3>Low_2 //bar2 low lower than bar3 low
    && High_2>High_1 //bar2 high higher than bar1 high (Piercing)
    // && Low_2>Low_1 //bar2 low higher than bar1 low  (Piercing)//FX関係無さそう。
    //&& (Open [i+1]<Low[ i+2]) //bar1 open Lower than bar2 low //FX関係無さそう。
    && ((Close_1-Open_1)>(High_1-Low_1)/3) // bar1 body bigger than 1/3 of  bar1
    && ((Open_2-Close_2)>(High_2-Low_2)/3)// bar2 body bigger than 1/3 of  bar2

 
)return(true);else return(false);}//(true)
//+------------------------------------------------------------------+    
//BULLISH_REVERSAL
//+------------------------------------------------------------------+ 
bool Hammer(){if(//
   

    //High_3>High_2 //bar3 high Higher than bar2 high    
    Low_3>Low_2 //bar3 low Higher than bar2 low
    &&Low_2>Low_1 // bar2 low Higher than bar1 low
    &&(High_1<High_2)//bar1 high Lower than bar2 high
    &&(High_1<High_3)//bar1 high Lower than bar3 high
    &&(High_1<High_4)//bar1 high Lower than bar4 high
    // && Close_1<Open_1 //Nison said  real body can be black or white
    && UpperShadow<=Bar1_Whole/6 //lower shadow less than 1/6 of whole bar1
    &&((Lower_Shadow)>(4*UpperShadow))//UpperShadow>4*Lower_Shadow
    &&(Lower_Shadow>(2*Bar1_body))//UpperShadow>2*real body
   // &&(Open_1!=Close_1) //not Doji
         
      
)return(true);else return(false);}//(true)
//+------------------------------------------------------------------+
//BEARISH_REVERSAL
//+------------------------------------------------------------------+ 
bool Morning_Star(){if(//

    (Close_3 < Open_3) //bar3 Red candle
    && ((Open_3 - Close_3) / (High_3 - Low_3) > 0.6) //bar3 body 60%以上はBody
    && ((Close_1 - Open_1) / (High_1 - Low_1) > 0.4) //bar3 body 60%以上はBody
    // && (Close_3 > Open_2) //
    // && (Close_2 < Open_2) //bar2 Green candle
    && ((High_2 - Low_2) > (3*(Bar2_body))) //  bar2 real bodyはWholeBody2の1/3以下
    && (Open_1 < Close_1) //bar1 Green candle
    //&& (Open_1 > Open_2) //bar1 Open below bar2 open
    && (High_1 > (Bar3_Whole /2)+Low_3) //bar1 High  above the half of bar3
    //&& (Open_2 < Close_2)// bar2 Green candle
    //&& (Low_2 < Open_2) 
    //&& (High_2 > Open_2) // Second, small candle
    && High_2<Close_1//bar2 Higher lower than Close1
    && (High_2<(Low_3+(Bar3_Whole*0.6)))//bar2 low lower than bar3 40%
    && (Close_2<(Low_3+(Bar3_Whole*0.4)))//bar2 close Lower than bar3 60%
    &&(Close_1> ((High_1 + Low_1)/2))//bar1 close above the halfe of the bar1 whole body
    &&Low_4>Low_3 //bar3 low Higher than bar2 low

 
)return(true);else return(false);}//(true)
Keith Watford
Moderator
10001
Keith Watford  
That code is so messy, I can't be bothered to try to work out what you are trying to do.
whroeder1
15060
whroeder1  
kancrase: I usually like to use for(i=limit-1; i>=0; i--) for calculation, rather than (i=0; i<limit; i++).
Babel
Babel
:

1. I want to change to i-- style or I want to know why its not working with i--.

2. I want to see the text on the chart while back testing.

3. I want to see the changes on the chart when I changed and compiles.

  1. Always count down. Contradictory information on IndicatorCounted() - MQL4 forum. Don't look past bars
        int counted_bars=IndicatorCounted();
        if(counted_bars<0) return(-1);
        if(counted_bars < 4) counted_bars = 4; // Lookback High_4=High[i+4];
        for(i=Bars -1 -counted_bars; i>=0; i--)
    You probably do NOT want to process bar zero: for(...; i>0; ...)
  2. ObjectCreate("Bullish_Engulfing"+i, OBJ_TEXT, 0, Time[i+2], Low[i+1]);
    First time you create say "Bullish_Engulfing1." A few bars later you find a new one and try to AGAIN create "Bullish_Engulfing1." That fails: Objects must be uniquely named. Use
    "Bullish_Engulfing"+Time[i]
    Check your return codes What are Function return values ? How do I use them ? - MQL4 forum Since the text objects is placed t Time[i+2] shouldn't it be named with that? Don't keep repeating the create code - make it a function - simply your code.

  3. Learn to use bools properly
    bool Morning_Star(){if(//
       condition
    )return(true);else return(false);}//(true)
    bool Morning_Star(){ return(
       condition
    );}

  4. I want to see the text on the chart while back testing.
    The tester is for testing EA's not indicators. You have to put a dummy EA (at least) on the chart and then drag the indicator on to the chart.
  5. I want to see the changes on the chart when I changed and compiles.
    No problem there, except in the tester, you have stop the test.
Kan Hasegawa
10
Kan Hasegawa  

WHRoeder:

Yes! it fixed all problems I was dealing with. Thank you for your answers and advice. It is very helpful!

To add comments, please log in or register