can'get the right buffer value with iCustom

 

Hello,

I'm trying to write a very simple rule into my EA where when Supertrend Indicator turns bearish on H1 Timeframe it closes the opened buy order

and when it turns bullish on H1 Timeframe it closes the opened sell order, but for some (probably obvious reasons for you coders) it doesn't work.

So i'm trying to figure out why is it working with iMA rules (see commented condition in the code) but when trying with iCustom it simply doesn't.

I also tried to include it into a custom indicator i coded myself and i get the same problem so i don't think it is EA related.


i also tried with buffer 3

iCustom(NULL,0,"SuperTrend nrp new mtf 2 histo",10,PRICE_CLOSE,4.0,60,3,1) == 1
iCustom(NULL,0,"SuperTrend nrp new mtf 2 histo",10,PRICE_CLOSE,4.0,60,3,1) == -1

but same result.

in the MT4 data window there are only 2 buffers showing value [trend up first buffer =1.0000 second buffer empty]  and [trend down second buffer = 1.0000 first buffer empty]

I tried many combinason lite >1 <1 ==EMPTY_VALUE  !=1 !=0 !=EMPTY_VALUE etc... but the problem remain the same.


here is the code section in the EA:

(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   
         OrderSymbol()==Symbol() &&
         OrderMagicNumber()==MagicNumber 
         )  
        {
         if(OrderType()==OP_BUY)  
           {
              //if((MarketInfo(Symbol(), MODE_SPREAD)<=MaxSpread && iMA(NULL,0,1,0,MODE_LWMA,PRICE_CLOSE,1)<iMA(NULL,0,300,0,MODE_SMA,PRICE_CLOSE,1))) //here is your close buy rule
             
              //if((MarketInfo(Symbol(), MODE_SPREAD)<=MaxSpread && iMA(NULL,60,6,0,MODE_LWMA,PRICE_CLOSE,1)<iMA(NULL,60,100,0,MODE_SMA,PRICE_CLOSE,1)
              //&& iMA(NULL,60,6,0,MODE_LWMA,PRICE_CLOSE,2)>iMA(NULL,60,100,0,MODE_SMA,PRICE_CLOSE,2)))
             
              if ((MarketInfo(Symbol(), MODE_SPREAD)<=MaxSpread && iCustom(NULL,0,"SuperTrend nrp new mtf 2 histo",10,PRICE_CLOSE,4.0,60,0,1) == 1 && iCustom(NULL,0,"SuperTrend nrp new mtf 2 histo",10,PRICE_CLOSE,4.0,60,1,0) == 1 ))
              {
                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
              }
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else 
           {
                //if((MarketInfo(Symbol(), MODE_SPREAD)<=MaxSpread && iMA(NULL,0,1,0,MODE_LWMA,PRICE_CLOSE,1)>iMA(NULL,0,300,0,MODE_SMA,PRICE_CLOSE,1))) // here is your close sell rule
                
                //if((MarketInfo(Symbol(), MODE_SPREAD)<=MaxSpread && iMA(NULL,60,6,0,MODE_LWMA,PRICE_CLOSE,1)>iMA(NULL,60,100,0,MODE_SMA,PRICE_CLOSE,1) 
                //&& iMA(NULL,60,6,0,MODE_LWMA,PRICE_CLOSE,2)<iMA(NULL,60,100,0,MODE_SMA,PRICE_CLOSE,1)))
                if ((MarketInfo(Symbol(), MODE_SPREAD)<=MaxSpread && iCustom(NULL,0,"SuperTrend nrp new mtf 2 histo",10,PRICE_CLOSE,4.0,60,1,1) == 1 && iCustom(NULL,0,"SuperTrend nrp new mtf 2 histo",10,PRICE_CLOSE,4.0,60,0,0) == 1 ))
                {
                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
                }
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
   return(0);
}


here is the code of the Supertrend Indicator:

//------------------------------------------------------------------
#property copyright "mladen"
#property link      "mladenfx@gmail.com"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  clrLimeGreen
#property indicator_color2  clrOrangeRed
#property indicator_width1  2
#property indicator_width2  2
#property indicator_minimum 0
#property indicator_maximum 1

extern int                period       = 10;
extern ENUM_APPLIED_PRICE appliedPrice = PRICE_CLOSE;
//extern double             multiplier   = 3.0;             ###MODIFIED
extern double             multiplier   = 4.0;
extern ENUM_TIMEFRAMES    TimeFrame    = 0; //default chart TF :: Use 5, 15, 30, 60, etc...

//
//
//
//
//

double Trend[];
double TrendDoA[];
double TrendDoB[];
double Direction[];
double Up[];
double Dn[];

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

int init()
{
   IndicatorBuffers(6);
      SetIndexBuffer(0, TrendDoA); SetIndexStyle(0,DRAW_HISTOGRAM);SetIndexLabel(0,"SuperTrend");
      SetIndexBuffer(1, TrendDoB); SetIndexStyle(1,DRAW_HISTOGRAM);SetIndexLabel(1,"SuperTrend");
      SetIndexBuffer(2, Trend);  
      SetIndexBuffer(3, Direction);
      SetIndexBuffer(4, Up);
      SetIndexBuffer(5, Dn);
         period    = MathMax(1,period);
         TimeFrame = MathMax(TimeFrame,+_Period);
   IndicatorShortName("SuperTrend");
   return(0);
}
int deinit() { return(0); }

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

int start()
{
   int counted_bars = IndicatorCounted();
      if(counted_bars < 0) return(-1);
      if(counted_bars > 0) counted_bars--;
           int limit = MathMin(MathMax(Bars-counted_bars,3*TimeFrame/_Period),Bars-1);

   //
   //
   //
   //
   //
      
   for(int i = limit; i >= 0; i--)
   {
      int y = iBarShift(NULL,TimeFrame,Time[i]);
         double atr    = iATR(NULL,TimeFrame,period,y);
         double cprice = iMA(NULL,TimeFrame,1,0,MODE_SMA,appliedPrice,y);
         double mprice = (iHigh(NULL,TimeFrame,y)+iLow(NULL,TimeFrame,y))/2;
                Up[i]  = mprice+multiplier*atr;
                Dn[i]  = mprice-multiplier*atr;
         
         //
         //
         //
         //
         //
         
         Direction[i] = Direction[i+1];
            if (cprice > Up[i+1]) Direction[i] =  1;
            if (cprice < Dn[i+1]) Direction[i] = -1;
         TrendDoA[i] = EMPTY_VALUE;
         TrendDoB[i] = EMPTY_VALUE;
            if (Direction[i] > 0) { Dn[i] = MathMax(Dn[i],Dn[i+1]); Trend[i] = Dn[i]; }
            else                  { Up[i] = MathMin(Up[i],Up[i+1]); Trend[i] = Up[i]; }
            if (Direction[i] == 1)  TrendDoA[i] = 1; 
            if (Direction[i] ==-1)  TrendDoB[i] = 1; 
   }
   return(0);
}


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

void CleanPoint(int i,double& first[],double& second[])
{
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (first[i+1] == EMPTY_VALUE)
      if (first[i+2] == EMPTY_VALUE) 
            { first[i]  = from[i]; first[i+1]  = from[i+1]; second[i] = EMPTY_VALUE; }
      else  { second[i] = from[i]; second[i+1] = from[i+1]; first[i]  = EMPTY_VALUE; }
   else     { first[i]  = from[i];                          second[i] = EMPTY_VALUE; }
}

any explanation welcome..

thanx

 

Use buffer 3 only

PS: that indicator is not my indicator (ie: someone made some changes to it). Use the original

 

thanx for your reply

I finally replaced these lines:

iCustom(NULL,0,"SuperTrend nrp new mtf 2 histo",10,PRICE_CLOSE,4.0,60,0,1) == 1 && iCustom(NULL,0,"SuperTrend nrp new mtf 2 histo",10,PRICE_CLOSE,4.0,60,1,0) == 1

iCustom(NULL,0,"SuperTrend nrp new mtf 2 histo",10,PRICE_CLOSE,4.0,60,1,1) == 1 && iCustom(NULL,0,"SuperTrend nrp new mtf 2 histo",10,PRICE_CLOSE,4.0,60,0,0) == 1

by

iCustom(NULL,0,"SuperTrend nrp new mtf 2 histo",10,PRICE_CLOSE,4.0,60,3,cnt) == -1

iCustom(NULL,0,"SuperTrend nrp new mtf 2 histo",10,PRICE_CLOSE,4.0,60,3,cnt) ==  1

and it's now behaving the way i wanted to.


for some unknown reasons  this syntax doesn't work with this indicator:

iCustom(NULL,0,"SuperTrend nrp new mtf 2 histo",10,PRICE_CLOSE,4.0,60,3,cnt) ==  1 && iCustom(NULL,0,"SuperTrend nrp new mtf 2 histo",10,PRICE_CLOSE,4.0,60,3,cnt+1) == -1

It's kind of problematic since i have no way to find out the previous bar state.

I would be interested to try your indicator's original version Mladen but i can't find it.
Reason: