Customer indicator works correct, but in EA has the indicator buffer incorrect content - page 2

 

Well, let's go over them step by step if you really want to get them right... 

//+------------------------------------------------------------------+
//|                                            ADI_EA_10112010_1.mq4 |
//| komplexer SuperTrend auf Heiken Ashi                          AK |
//|                                                  10.11.10        |
//+------------------------------------------------------------------+
#property copyright "AK"
#property link      ""

//---- input parameters
extern double    TakeProfit=350.0;    // Default 350.0
extern double    Lots=0.1;             
extern double    TrailingStop=0.0;  // Default 35.0
extern bool      Test=false;

// SuperTrend Parameter
extern int     Nbr_Periods=10;
extern double  Multiplier=3.0;

int c_altBars=0;                    // Balkenzähler
bool b_trade=false;                 // Tradekennzeichen
bool b_print=false;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
int cnt, ticket, total, isCrossed;

if (Bars<100) {
   Print("bars less than 100");
   return (0);
}
/*if (TakeProfit<10) {
   Print("TakeProfit less then 10");
   return(0);                          //check TakeProfit
}*/


// new Bar?
if (c_altBars < Bars) {
   c_altBars = Bars;
    
// only for test purposes
double sT_5   = iCustom(NULL,0,"Heiken Ashi and TK1",Nbr_Periods,Multiplier,7,5);
double sT_4   = iCustom(NULL,0,"Heiken Ashi and TK1",Nbr_Periods,Multiplier,7,4);
double sT_3   = iCustom(NULL,0,"Heiken Ashi and TK1",Nbr_Periods,Multiplier,7,4);
double sT_2   = iCustom(NULL,0,"Heiken Ashi and TK1",Nbr_Periods,Multiplier,7,2);
double sT_0   = iCustom(NULL,0,"Heiken Ashi and TK1",Nbr_Periods,Multiplier,7,0);

// changeTrend[1] in indicator; buffer7[1] is trade trigger in EA
double sT_1   = iCustom(NULL,0,"Heiken Ashi and TK1",Nbr_Periods,Multiplier,7,1);

Alert(Bars+" "+sT_5+" "+sT_4+" "+sT_3+" "+sT_2+" "+sT_1+" "+sT_0+" "+TimeToStr(Time[0]));

if (sT_1 == 1.38){
   isCrossed = 1;
   Print ("Crossed = 1 "+" "+TimeToStr(Time[0]));
}   
else if (sT_1 == 1.42){  
   isCrossed = 2; 
   Print ("Crossed = 2"+" "+TimeToStr(Time[0]));
   }
   else {
      isCrossed = 0;
   }
   
// nur traden, wenn ein Tradewechsel stattgefunden hat und kein Testmodus   
   if (Test == false)
      b_trade = true;
   else
      b_trade = false;            
/* 
// nur eimal pro Periodedurchführen
if (c_altBars < Bars) {
   c_altBars = Bars;
//weiteren Code - siehe unten       
*/

total=OrdersTotal();
if(total<1) {
   if(isCrossed ==1 && b_trade == true) {
      isCrossed = 0;
      b_trade = false;
//      ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"ADI EA",12345,0,Green);
      ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,0,0,"ADI EA",12345,0,Green);
      if(ticket>0) {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
         Print("BUY order opened: ",OrderOpenPrice());
      }
      else 
         Print("Error openning BUY order: ",GetLastError());
      return (0);
   }
   if(isCrossed==2 && b_trade == true) {
       isCrossed = 0;
       b_trade = false;
//      ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"ADI EA",12345,0,Red);
      ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,0,0,"ADI EA",12345,0,Red);
      if(ticket>0) {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
         Print("SELL order opened: ",OrderOpenPrice());
      }
      else 
         Print("Error openning SELL order: ",GetLastError());
      return (0);
   }
   return (0);
}   
   
for(cnt=0;cnt<total;cnt++) {
   OrderSelect(cnt, SELECT_BY_POS,MODE_TRADES);
   if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()) {
      if(OrderType()==OP_BUY) {                                //long position is opened
         // should be closed?
         if(isCrossed==2 && b_trade == true) {
            isCrossed = 0;
            b_trade = false;
         // close long position     
            OrderClose(OrderTicket(),OrderLots(),Bid,1,Violet);//close position
         // and open short position                                                       
            ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,0,0,"ADI EA",12345,0,Red);
            if(ticket>0) {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                  Print("SELL order opened: ",OrderOpenPrice());
            }
            else Print("Error openning SELL order: ",GetLastError());
            return(0);           // exit
         }
         // check for trailing stop, by buy trend
         if(TrailingStop>0) {
            if(Bid-OrderOpenPrice()>Point*TrailingStop) {
               if(OrderStopLoss()<Bid-Point*TrailingStop) {
                  OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
                  return(0);
               }
            }
         }
      }
      else {                                                   // go to short position
         // should be closed?
         if(isCrossed==1 && b_trade == true) {
            isCrossed = 0;
            b_trade = false;
         // close short position    
            OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);   //close position
         // and open long position
            ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,0,0,"ADI EA",12345,0,Green);
            if(ticket>0) {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               Print("BUY order opened: ",OrderOpenPrice());
            }
            else 
               Print("Error openning BUY order: ",GetLastError());
            return(0);                                         // exit
         }
         // chack for trailing stop, by sell trend
         if(TrailingStop>0) {
            if(OrderOpenPrice()-Ask>Point*TrailingStop) {
               if((OrderStopLoss()>(Ask-Point*TrailingStop)) || (OrderStopLoss()==0)) {
                  OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
                  return(0); 
               }
            }
         }
      }   
   }   
}
} //  eimal pro Periode
return(0);
}
//+------------------------------------------------------------------+
This is what you've attached....
 

... This is after I've removed function init, deinit and comments to make it more clear...

 

extern double    TakeProfit=350.0; 
extern double    Lots=0.1;             
extern double    TrailingStop=0.0;  
extern bool      Test=false;

extern int     Nbr_Periods=10;
extern double  Multiplier=3.0;

int c_altBars=0;                    
bool b_trade=false;                 
bool b_print=false;

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
   int cnt, ticket, total, isCrossed;

   if (Bars<100) {
      Print("bars less than 100");
      return (0);
   }

   if (c_altBars < Bars) 
   {
      c_altBars = Bars;
    
      // only for test purposes
      double sT_5   = iCustom(NULL,0,"Heiken Ashi and TK1",Nbr_Periods,Multiplier,7,5);
      double sT_4   = iCustom(NULL,0,"Heiken Ashi and TK1",Nbr_Periods,Multiplier,7,4);
      double sT_3   = iCustom(NULL,0,"Heiken Ashi and TK1",Nbr_Periods,Multiplier,7,4);
      double sT_2   = iCustom(NULL,0,"Heiken Ashi and TK1",Nbr_Periods,Multiplier,7,2);
      double sT_0   = iCustom(NULL,0,"Heiken Ashi and TK1",Nbr_Periods,Multiplier,7,0);

      // changeTrend[1] in indicator; buffer7[1] is trade trigger in EA
      double sT_1   = iCustom(NULL,0,"Heiken Ashi and TK1",Nbr_Periods,Multiplier,7,1);

      Alert(Bars+" "+sT_5+" "+sT_4+" "+sT_3+" "+sT_2+" "+sT_1+" "+sT_0+" "+TimeToStr(Time[0]));

      if (sT_1 == 1.38)
      { 
         isCrossed = 1; Print ("Crossed = 1 "+" "+TimeToStr(Time[0]));
      }   
      else if (sT_1 == 1.42)
      {  
         isCrossed = 2; Print ("Crossed = 2"+" "+TimeToStr(Time[0]));
      }
      else isCrossed = 0; 
   
      if (Test == false) b_trade = true;
      else b_trade = false;            

      total=OrdersTotal();
      if(total<1) 
      {
         if(isCrossed ==1 && b_trade == true) {
            isCrossed = 0;
            b_trade = false;

            ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,0,0,"ADI EA",12345,0,Green);
            if(ticket>0) {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               Print("BUY order opened: ",OrderOpenPrice());
            }
            else 
               Print("Error openning BUY order: ",GetLastError());
            return (0);
         }
         if(isCrossed==2 && b_trade == true) {
             isCrossed = 0;
             b_trade = false;

            ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,0,0,"ADI EA",12345,0,Red);
            if(ticket>0) {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               Print("SELL order opened: ",OrderOpenPrice());
            }
            else 
               Print("Error openning SELL order: ",GetLastError());
            return (0);
         }
         return (0);
      }   
   
      for(cnt=0;cnt<total;cnt++) {
         OrderSelect(cnt, SELECT_BY_POS,MODE_TRADES);
         if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()) {
            if(OrderType()==OP_BUY) {  //long position is opened
               
               if(isCrossed==2 && b_trade == true) { // should be closed?
                  isCrossed = 0; b_trade = false;
               // close long position     
                  OrderClose(OrderTicket(),OrderLots(),Bid,1,Violet); // close position
               // and open short position                                                       
                  ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,0,0,"ADI EA",12345,0,Red);
                  if(ticket>0) {
                     if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                        Print("SELL order opened: ",OrderOpenPrice());
                  }
                  else Print("Error openning SELL order: ",GetLastError());
                  return(0);           // exit
               }
               // check for trailing stop, by buy trend
               if(TrailingStop>0) {
                  if(Bid-OrderOpenPrice()>Point*TrailingStop) {
                     if(OrderStopLoss()<Bid-Point*TrailingStop) {
                        OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
                        return(0);
                     }
                  }
               }
            }
            else { // go to short position
               
               if(isCrossed==1 && b_trade == true) { // should be closed?
                  isCrossed = 0; b_trade = false;
               // close short position    
                  OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);   //close position
               // and open long position
                  ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,0,0,"ADI EA",12345,0,Green);
                  if(ticket>0) {
                     if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                     Print("BUY order opened: ",OrderOpenPrice());
                  }
                  else 
                     Print("Error openning BUY order: ",GetLastError());
                  return(0); // exit
               }
               // chack for trailing stop, by sell trend
               if(TrailingStop>0) {
                  if(OrderOpenPrice()-Ask>Point*TrailingStop) {
                     if((OrderStopLoss()>(Ask-Point*TrailingStop)) || (OrderStopLoss()==0)) {
                        OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
                        return(0); 
                     }
                  }
               }
            }   
         }   
      }
   } //  eimal pro Periode
   return(0);
}

Now, can you please describe what exactly this EA is trying to achieve? I have the idea from your code but I want to make sure what you're intending to achieve... 

 

I repeat (see quite above),

trade trigger (buffer7[1] of the indicator) in the EA delivers wrong values. The buffer7 in indicator (see white line in chart and output listing) delivers correct values.

Conclusion/main question:

Why function "double sT_1 = iCustom(NULL,0,"Heiken Ashi and TK1",Nbr_Periods,Multiplier,7,1" delivers wrong values (see output listing)?

 

Oh, my bad. Ok I tested them. Notice this : 

the debugging Print uses these block. changeTrend is your focus right?

      if (trend[i]==1) {
         TrendUp[i]=dn[i];
         if (changeOfTrend == 1) {
            TrendUp[i+1] = TrendDown[i+1];
            changeOfTrend = 0;
            changeTrend[i] = 1.38;            // changeTrend-Buffer for EA application(LONG) 
            Print(TimeToStr(Time[i])+": "+changeTrend[i]+" "+i+" long "+Bars);
         }
      }
      else if (trend[i]==-1) {
         TrendDown[i]=up[i];
         if (changeOfTrend == 1) {
            TrendDown[i+1] = TrendUp[i+1];
            changeOfTrend = 0; 
            changeTrend[i] = 1.42;            // changeTrend-Buffer for EA application (SHORT)
            Print(TimeToStr(Time[i])+" "+changeTrend[i]+" "+i+" short "+Bars);
         }
      }

 Notice that both print debugging of your indicator happens inside this loop header, so it always print them starting from i=Bars and finishes at i=0

   for (i = Bars; i >= 0; i--) {
while on your EA, your alert debug prints not within any loop and always printing bar index 0 to 5
      double sT_1   = iCustom(NULL,0,"Heiken Ashi and TK1",Nbr_Periods,Multiplier,7,1);

      Print(Bars+" "+sT_5+" "+sT_4+" "+sT_3+" "+sT_2+" "+sT_1+" "+sT_0+" "+TimeToStr(Time[0]));

This print values u see from your indicator only able to display few bars values from the oldest bar when new ticks arrive and rewrites them with new values all over again. Never reaching or displaying latest bar values you wanted to see, i.e. 0 to 5. 

So you're comparing head's values to tail's values. Try comparing the EA alert values to values in DataWindow instead (hover your mouse over the latest bars). It matches. changeTrend values is 1.40 up to bar index 32 (I use H1). So iCustom calls them fine. 

 

I use D1,

values in EA are unambiguously wrong!!!. See please the time information 9. - 11.11.2010 by print debugging in. There entry 1.42000000 is absent.

Position i=0 10.11.2010 and i=1 11.11.2010 must be 1.42000000

Indicator:
Heiken Ashi and TK1 EURUSD,Daily: 2010.11.10 00:00 1.42000000 8 short 2115
---------------- Info ---          -- Time ---

EA:
ADI_EA_14112010_1 EURUSD,Daily: Alert: 1089 1.40000000 1.40000000 1.40000000 1.40000000 1.40000000 1.40000000 2010.11.11 00:00
ADI_EA_14112010_1 EURUSD,Daily: Alert: 1088 1.40000000 1.40000000 1.40000000 1.40000000 1.40000000 1.40000000 2010.11.10 00:00
ADI_EA_14112010_1 EURUSD,Daily: Alert: 1087 1.40000000 1.40000000 1.40000000 1.40000000 1.40000000 1.40000000 2010.11.09 00:00
------------------------------------Info---------------   i= 4        i=3        i=2        i=1         i=0    --Time-- 

 

This is why I recommend you to re-read the documentation to have solid grasp of program flow.

You don't have clear program flow. Every buffer must be filled before you make decision on it. You can't make decision on iteration!

I can explain where you made your mistake exactly but that would mean re-writing it for you. Try to understand my last hint in bold. There's nothing wrong with iCustom. There's definitely something wrong with your program's logical flow.   

 

I don't state, that function iCustom works wrong!

Clarification:

- Buffer7 is filled (mostly with 1.4, occasionally with trigger 1.38 or 1.42) in indicator correct in time. In EA per new bar I would like to find trade trigger from the buffer7. Where is decision on iteration?

- EA with tick easier indicator but with same program flow (see source code below) works full correct.

double sT_1   = iCustom(NULL,0,"SuperTrend 101109",Nbr_Periods,Multiplier,2,1);


I don't expect that you re-writing my program. If you exactly know, where I made mistake. Then you should indicate the source of error. This is the forum aim.

All other is a bold statement without proof.


Thank you for help!!!

Files:
 


Oh, boy.... Ok, so you only wrote the EA, the SuperTrend was not written by you, correct? The problem lies in the iteration of SuperTrend. 

Let me rephrase this : I can explain where you made your mistake exactly but that would mean re-writing it for you  -- > means : To sort it out exactly would be to re-write the indicator.  

It is recounting every bars on every tick. All other is a bold statement without proof.  You're making me doin' it, aren't you :) 

 

I'm not boy but girl.

I'm not understand your argumentation. The easier indicator (one step- exact the same SuperTrend, without Heiken Ashi-see source code above) works with the same EA correct.

Consequently: Why should I change interation of SuperTrend, if it works in the same EA correct?


Your sentence is wrong, not oldets but youngest (see above):

This print values u see from your indicator only able to display few bars values from the oldest bar when new ticks arrive and rewrites them with new values all over again.

 

mql7, It's like when someone says 'Oh, lord...' they don't call someone a lord. it's just an interjection. I'm not calling you a boy or anything. 

I made the wrong assumption of you being the author of SuperTrend. So, obviously that was wrongly addressed, my bad.

I'm convinced you really needed this, so I will help you as best as I can. Just give me some time to finish off some work. I don't promise a quick response, but I'll help you sort this out.

No, the sentence is correct.  When you have decrementing loop :

for (i = Bars; i >= 0; i--) {

and inside that loop you print or do something that uses that decrementing index, you will start with index Bars, which is the oldest bars. and work your way down to the youngest bar indexed 0

So the youngest bar never had a chance to get displayed at least on my observation.