Supertrend -> Icustom

 

Hi I have read many threads about this, but i still can find a solution...

I wanted to try this common and very popular indicator, the "supertrend"...but still not finding the right way...

actually I'm in stall with this code...


 double val_super_trend_up = iCustom(NULL, 5, "SuperTrend_plus", 10, 3.0,0,2);
 double val_super_trend_down = iCustom(NULL, 5, "SuperTrend_plus", 10, 3.0,1,2);
 
 Print("val_super_trend_up: ",val_super_trend_up,"  val_super_trend_down: ",val_super_trend_down);


and the result is this:

supertrend

the problem is that one is a price and looks correct for the line, the second is unexpected for me...looks like a time...

I just need something that returns to me: "now uptrend, now downtrend". Like 0/1...not price or something else

 

I have downloaded these two versions of the same indicator from this website...I have called SuperTrend and SuperTrend_plus. (wich one do you think is better?)...the result with Icustom anyways is similar.

//+------------------------------------------------------------------+
//|                                                   SuperTrend.mq4 |
//|                   Copyright © 2008, Jason Robinson (jnrtrading). |
//|                                   http://www.spreadtrade2win.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Jason Robinson."
#property link      "http://www.spreadtrade2win.com"

#property indicator_chart_window
#property indicator_color1 Lime
#property indicator_color2 Red
#property indicator_width1 2
#property indicator_width2 2
#property indicator_buffers 2
double TrendUp[],TrendDown[];
int changeOfTrend;
extern int Nbr_Periods=10;
extern double Multiplier=3.0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexBuffer(0,TrendUp);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
   SetIndexLabel(0,"Trend Up");
   SetIndexBuffer(1,TrendDown);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
   SetIndexLabel(1,"Trend Down");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit,i,flag,flagh,trend[];
   double up[],dn[],medianPrice,atr;

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

   int xsize=ArraySize(TrendUp);
   ArrayResize(trend,xsize);
   ArrayResize(up,xsize);
   ArrayResize(dn,xsize);
//----
   for(i=limit; i>=0; i--) 
     {
      atr=iATR(NULL,0,Nbr_Periods,i);
      //Print("atr: "+atr[i]);
      medianPrice=(High[i]+Low[i])/2;
      //Print("medianPrice: "+medianPrice[i]);
      up[i]=medianPrice+(Multiplier*atr);
      //Print("up: "+up[i]);
      dn[i]=medianPrice-(Multiplier*atr);
      //Print("dn: "+dn[i]);
      trend[i]=1;

      if(Close[i]>up[i+1]) 
        {
         trend[i]=1;
         if(trend[i+1]==-1) changeOfTrend=1;
         //Print("trend: "+trend[i]);

        }
      else if(Close[i]<dn[i+1]) 
        {
         trend[i]=-1;
         if(trend[i+1]==1) changeOfTrend=1;
         //Print("trend: "+trend[i]);
        }
      else if(trend[i+1]==1) 
        {
         trend[i]=1;
         changeOfTrend=0;
        }
      else if(trend[i+1]==-1) 
        {
         trend[i]=-1;
         changeOfTrend=0;
        }

      if(trend[i]<0 && trend[i+1]>0) 
        {
         flag=1;
        // Print("flag: "+flag);
        }
      else 
        {
         flag=0;
        // Print("flagh: "+flag);
        }

      if(trend[i]>0 && trend[i+1]<0) 
        {
         flagh=1;
       //  Print("flagh: "+flagh);
        }
      else 
        {
         flagh=0;
        // Print("flagh: "+flagh);
        }

      if(trend[i]>0 && dn[i]<dn[i+1])
         dn[i]=dn[i+1];

      if(trend[i]<0 && up[i]>up[i+1])
         up[i]=up[i+1];

      if(flag==1)
         up[i]=medianPrice+(Multiplier*atr);

      if(flagh==1)
         dn[i]=medianPrice-(Multiplier*atr);

      //-- Draw the indicator
      if(trend[i]==1) 
        {
         TrendUp[i]=dn[i];
         if(changeOfTrend==1) 
           {
            TrendUp[i+1] = TrendDown[i+1];
            changeOfTrend= 0;
           }
        }
      else if(trend[i]==-1) 
        {
         TrendDown[i]=up[i];
         if(changeOfTrend==1) 
           {
            TrendDown[i+1]= TrendUp[i+1];
            changeOfTrend = 0;
           }
        }
     }
  Print("TrendUp: ",TrendUp[0],"TrendDown: ",TrendDown[0]," ChangeofTrend: ",changeOfTrend," flag: ",flag," flagh: ",flagh);
//----
   return(0);
  }
//+------------------------------------------------------------------+

supertrend_plus

//+------------------------------------------------------------------+
//|                                    Non Repainting SuperTrend.mq4 |
//|                     Copyright © 2012, Marketcalls (Rajandran R). |
//|                                        http://www.marketcalls.in |
//+------------------------------------------------------------------+


#property indicator_chart_window
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_width1 1
#property indicator_width2 1
#property indicator_color3 Blue
#property indicator_color4 Red
#property indicator_width3 1
#property indicator_width4 1
#property indicator_buffers 4
double TrendUp[],TrendDown[];
double UpBuffer[];
double DnBuffer[];

int changeOfTrend;
extern int Nbr_Periods=10;
extern double Multiplier=4.0;

//---- input parameters
extern int sidFontSize=30;
extern string sidFontName="Ariel";
extern string NoteRedGreenBlue="Red/Green/Blue each 0..255";
extern int sidRed=30;
extern int sidGreen= 30;
extern int sidBlue = 30;
extern int sidXPos = 30;
extern int sidYPos = 150;

extern bool tagDisplayText=true;
extern string tagText="www.marketcalls.in";
extern int tagFontSize=15;
extern string tagFontName="Ariel";
extern int tagRed=30;
extern int tagGreen= 30;
extern int tagBlue = 30;
extern int tagXPos = 200;
extern int tagYPos = 300;

//---- data
string SID = "Symbol";
int sidRGB = 0;
string TAG = "Tag";
int tagRGB = 0;
string tf;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexBuffer(0,TrendUp);
   SetIndexStyle(0,DRAW_LINE,1,1);
   SetIndexLabel(0,"Trend Up");
   SetIndexBuffer(1,TrendDown);
   SetIndexStyle(1,DRAW_LINE,1,1);
   SetIndexLabel(1,"Trend Down");

   SetIndexStyle(2,DRAW_ARROW,EMPTY);
   SetIndexStyle(3,DRAW_ARROW,EMPTY);

   SetIndexBuffer(2,UpBuffer);
   SetIndexBuffer(3,DnBuffer);

   SetIndexArrow(2,233);
   SetIndexArrow(3,234);

   SetIndexLabel(3,"Up Signal");
   SetIndexLabel(4,"Down Signal");

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectDelete(SID);
   ObjectDelete(TAG);

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {

   double ClosePrice=Close[0];

//      watermark(TAG, tagText, tagFontSize, tagFontName, tagRGB, tagXPos, tagYPos); 
  // string str=StringConcatenate("www.marketcalls.in      LTP : ",ClosePrice);
  // watermarkclose(TAG,str,22,tagFontName,tagRGB,80,20);

   int i,flag,flagh,trend[];
   double up[],dn[],medianPrice,atr;

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

//Print(limit);

   int size=ArraySize(TrendUp);
   ArrayResize(trend,size);
   ArrayResize(up,size);
   ArrayResize(dn,size);
//----
   for(i=limit; i>=1; i--) 
     {
      TrendUp[i]=EMPTY_VALUE;
      TrendDown[i]=EMPTY_VALUE;
      atr=iATR(NULL,0,Nbr_Periods,i);
      //Print("atr: "+atr[i]);
      medianPrice=(High[i]+Low[i])/2;
      //Print("medianPrice: "+medianPrice[i]);
      up[i]=medianPrice+(Multiplier*atr);
      //Print("up: "+up[i]);
      dn[i]=medianPrice-(Multiplier*atr);
      //Print("dn: "+dn[i]);
      trend[i]=1;

      if(Close[i]>up[i+1]) 
        {
         trend[i]=1;
         if(trend[i+1]==-1) changeOfTrend=1;
         //Print("trend: "+trend[i]);

        }
      else if(Close[i]<dn[i+1]) 
        {
         trend[i]=-1;
         if(trend[i+1]==1) changeOfTrend=1;
         //Print("trend: "+trend[i]);
        }
      else if(trend[i+1]==1) 
        {
         trend[i]=1;
         changeOfTrend=0;
        }
      else if(trend[i+1]==-1) 
        {
         trend[i]=-1;
         changeOfTrend=0;
        }

      if(trend[i]<0 && trend[i+1]>0) 
        {
         flag=1;
         //Print("flag: "+flag);
        }
      else 
        {
         flag=0;
         //Print("flagh: "+flag);
        }

      if(trend[i]>0 && trend[i+1]<0) 
        {
         flagh=1;
         //Print("flagh: "+flagh);
        }
      else 
        {
         flagh=0;
         //Print("flagh: "+flagh);
        }

      if(trend[i]>0 && dn[i]<dn[i+1])
         dn[i]=dn[i+1];

      if(trend[i]<0 && up[i]>up[i+1])
         up[i]=up[i+1];

      if(flag==1)
         up[i]=medianPrice+(Multiplier*atr);

      if(flagh==1)
         dn[i]=medianPrice-(Multiplier*atr);

      //-- Draw the indicator
      if(trend[i]==1) 
        {
         TrendUp[i]=dn[i];
         if(changeOfTrend==1) 
           {
            TrendUp[i+1] = TrendDown[i+1];
            changeOfTrend= 0;
           }
        }
      else if(trend[i]==-1) 
        {
         TrendDown[i]=up[i];
         if(changeOfTrend==1) 
           {
            TrendDown[i+1]= TrendUp[i+1];
            changeOfTrend = 0;
           }

        }
      if(trend[i]==1 && trend[i+1]==-1) 
        {
         UpBuffer[i] = iLow(Symbol(),0,i)-(3*Point);
         DnBuffer[i] = EMPTY_VALUE;

        }
      if(trend[i]==-1 && trend[i+1]==1) 
        {
         UpBuffer[i] = EMPTY_VALUE;
         DnBuffer[i] = iHigh(Symbol(),0,i)+(3*Point);
        }

     }
   WindowRedraw();
   
   Print("TrendUp: ",TrendUp[1],"TrendDown: ",TrendDown[1]," UpBuffer: ",UpBuffer[1]," DownBuffer: ",DnBuffer[1]," flagh: ",flagh);

//----
   return(0);
  }
//+------------------------------------------------------------------+

void watermarkclose(string obj,string text,int fontSize,string fontName,color colour,int xPos,int yPos)
  {
   ObjectCreate(obj,OBJ_LABEL,0,0,0);
   ObjectSetText(obj,text,fontSize,fontName,Red);
   ObjectSet(obj,OBJPROP_CORNER,0);
   ObjectSet(obj,OBJPROP_XDISTANCE,xPos);
   ObjectSet(obj,OBJPROP_YDISTANCE,yPos);
   ObjectSet(obj,OBJPROP_BACK,true);
  }
//+------------------------------------------------------------------+
 
  1. le mariani: the problem is that one is a price and looks correct for the line, the second is unexpected for me...looks like a time...
    PICNIC. There is no problem. That is exactly what you should expect. Only one line is displayed on any one bar.
    Constant Description Value
    EMPTY_VALUE Empty value in an indicator buffer. Default custom indicator empty value 2147483647 (0x7FFFFFFF)
              Other constants - Named Constants - Standard Constants, Enumerations and Structures - MQL4 Reference

  2. You should encapsulate your iCustom calls to make your code self-documenting.
              Detailed explanation of iCustom - MQL4 and MetaTrader 4 - MQL4 programming forum

 

I had read your thread...

so that kind of answer means that actually the indicator is displaying an uptrend, the green line?...and as soon as it will turn in downtrend the first value will be 2147483647 , and the second a price?...

(it didn't seem to me by the tester, that it was switching...just the first displayed the price...)

 

I don't think is correct in this way...because it shows me always a value below the current price.


I have tried also on EUR-JPY now...that is in downtrend...and it returns the same...

SuperTrend_plus USDJPY,M5: TrendUp: 110.7356 TrendDown: 2147483647 UpBuffer: 2147483647 DownBuffer: 2147483647 flagh: 0

and current Bid is 110.8


EurUSD is uptrend now instead and i have the same

19:55:13.577 SuperTrend_plus EURUSD,M5: TrendUp: 1.1672TrendDown: 2147483647 UpBuffer: 2147483647 DownBuffer: 2147483647 flagh: 0

Current Bid is 1.16929

How can I determine if the current line is the blue or the red...with only that value, it is hard to say for me...

 

I have this feed. The only effectively downtrend of the three couples...returns the value on the right field just one time...very weird....


0 21:27:55.731 micro_hedge EURUSD,M5: val_super_trend_su: 1.1692  val_super_trend_giu: 2147483647

0 21:27:55.731 micro_hedge EURUSD,M5: val_super_trend_su: 1.1692  val_super_trend_giu: 2147483647
0 21:27:57.266 micro_hedge GBPUSD,H1: val_super_trend_su: 1.3167  val_super_trend_giu: 2147483647
0 21:27:57.283 micro_hedge GBPUSD,H1: val_super_trend_su: 1.3167  val_super_trend_giu: 2147483647
0 21:27:57.331 micro_hedge GBPUSD,H1: val_super_trend_su: 1.3167  val_super_trend_giu: 2147483647
0 21:27:57.378 micro_hedge GBPUSD,H1: val_super_trend_su: 1.3167  val_super_trend_giu: 2147483647
0 21:27:57.695 micro_hedge EURUSD,M5: val_super_trend_su: 1.1692  val_super_trend_giu: 2147483647
0 21:27:57.695 micro_hedge EURUSD,M5: val_super_trend_su: 1.1692  val_super_trend_giu: 2147483647
0 21:27:57.706 micro_hedge EURUSD,M5: val_super_trend_su: 1.1692  val_super_trend_giu: 2147483647
0 21:27:57.707 micro_hedge EURUSD,M5: val_super_trend_su: 1.1692  val_super_trend_giu: 2147483647
0 21:27:57.707 micro_hedge GBPUSD,H1: val_super_trend_su: 1.3167  val_super_trend_giu: 2147483647
0 21:27:57.737 micro_hedge EURUSD,M5: val_super_trend_su: 1.1692  val_super_trend_giu: 2147483647
0 21:27:57.737 micro_hedge EURUSD,M5: val_super_trend_su: 1.1692  val_super_trend_giu: 2147483647
0 21:27:57.770 micro_hedge USDJPY,M5: val_super_trend_su: 2147483647  val_super_trend_giu: 110.897
0 21:27:57.786 micro_hedge USDJPY,M5: val_super_trend_su: 110.744  val_super_trend_giu: 2147483647
0 21:27:57.802 micro_hedge EURUSD,M5: val_super_trend_su: 1.1692  val_super_trend_giu: 2147483647
0 21:27:57.802 micro_hedge USDJPY,M5: val_super_trend_su: 110.744  val_super_trend_giu: 2147483647
0 21:27:57.802 micro_hedge GBPUSD,H1: val_super_trend_su: 1.3167  val_super_trend_giu: 2147483647
0 21:27:57.803 micro_hedge EURUSD,M5: val_super_trend_su: 1.1692  val_super_trend_giu: 2147483647
0 21:27:57.851 micro_hedge USDJPY,M5: val_super_trend_su: 110.744  val_super_trend_giu: 2147483647
0 21:27:57.943 micro_hedge EURUSD,M5: val_super_trend_su: 1.1692  val_super_trend_giu: 2147483647
0 21:27:57.943 micro_hedge EURUSD,M5: val_super_trend_su: 1.1692  val_super_trend_giu: 2147483647
0 21:27:57.944 micro_hedge GBPUSD,H1: val_super_trend_su: 1.3167  val_super_trend_giu: 2147483647
0 21:27:57.974 micro_hedge USDJPY,M5: val_super_trend_su: 110.744  val_super_trend_giu: 2147483647
0 21:27:58.044 micro_hedge GBPUSD,H1: val_super_trend_su: 1.3167  val_super_trend_giu: 2147483647
0 21:27:58.220 micro_hedge GBPUSD,H1: val_super_trend_su: 1.3167  val_super_trend_giu: 2147483647
0 21:27:58.362 micro_hedge GBPUSD,H1: val_super_trend_su: 1.3167  val_super_trend_giu: 2147483647
0 21:27:58.705 micro_hedge USDJPY,M5: val_super_trend_su: 110.744  val_super_trend_giu: 2147483647
0 21:27:58.778 micro_hedge USDJPY,M5: val_super_trend_su: 110.744  val_super_trend_giu: 2147483647
0 21:27:58.778 micro_hedge GBPUSD,H1: val_super_trend_su: 1.3167  val_super_trend_giu: 2147483647
 
ale mariani: How can I determine if the current line is the blue or the red...with only that value, it is hard to say for me...

there are two values per bar. Figure out which buffer corresponds to which color.

 
yes, I understand...but in second case even if the trend is downtrend...it appears only one time the value, and then it follows with value on the left with price, and the one on the right with  2147483647, even if is still downtrend ...like for USD/JPN above. It doesn't look symmetric at all.
 
ale mariani: yes, I understand...but …

There is no but.

  1. In the supper trend that are only two buffers
  2. in the supper trend plus there are four. Buffers two or three are only set on a change in direction.
 

ok I have understood now...perfectly...and I did a script in this way...


  val_super_trend_su = iCustom(NULL, PERIOD_M5, "SuperTrend_plus",10,3.0,2,1);
  val_super_trend_giu = iCustom(NULL, PERIOD_M5, "SuperTrend_plus", 10, 3.0,3,1);
 if(val_super_trend_su != "2147483647" ){
 
 //buy
 
 }
if(val_super_trend_giu != "2147483647" ){
 
 //sell

 }

WHAT HAPPENS NOW?

SHORT WORKS PERFECTLY, LONG NOTHING...IT DOESN'T ANSWER.

Im really getting crazy with this Indicator.

Or the MT4 tester is not working properly...or this iCustom not working....

If I try with some "print" here...


if(trend[i]==1 && trend[i+1]==-1) 
        {
         UpBuffer[i] = iLow(Symbol(),0,i)-(3*Point);
         DnBuffer[i] = 0;
       Print("uptrend: ",UpBuffer[i]);
        }
      if(trend[i]==-1 && trend[i+1]==1) 
        {
        UpBuffer[i] = 0;
        DnBuffer[i] = iHigh(Symbol(),0,i)+(3*Point);
       Print("downtrend: ",DnBuffer[i]);
       }

It seems to work in the tester...long and short as well...it prints when there is a change of trend.

But icustom doesnt seem to read the "UpBuffer"...I think it's crazy...

 

I have converted it in a function because I was so sick with the indicator and now it works...it buys and sell as well...just repeats selling and buy consecutive times also...but it is not difficult to fix that one i think...


Do you think I have written correctly the function?...is it the same?


it got also some money, in the last two weeks EUR/USD according to the tester...:D


int Super_Trend(int Nbr_Periods,double Multiplier, int timeframe, int shift){
  
   double price_now;
   
   if(shift == 0){
   
      price_now = Bid;
   
    }else if(shift > 0){
   
      price_now = iClose(0,timeframe,0 + shift);
    
    }
   
   double price_prev = iClose(0,timeframe,1 + shift);
   double ma1=iMA(0,timeframe,Nbr_Periods,0,MODE_EMA,PRICE_MEDIAN,0 + shift);
   double ma1_prev=iMA(0,timeframe,Nbr_Periods,0,MODE_EMA,PRICE_MEDIAN,1 + shift);
   double atr  =iATR(NULL,timeframe,Nbr_Periods,0);
   double atr_prev  =iATR(NULL,timeframe,Nbr_Periods,1);
   double up = ma1 + atr*Multiplier;
   double down = ma1 - atr*Multiplier;
   double up_prev = ma1_prev + atr_prev*Multiplier; 
   double down_prev = ma1_prev - atr_prev*Multiplier;
   
   Print("up: ",up," down: ",down," up_prev: ",up_prev," down_prev: ",down_prev);
//
//----

   if(price_now > up && price_prev < up_prev){
   
   //trendup
   
      return(1);
   
   }
   if(price_now < down && price_prev > down_prev){
   
   //trenddown
   
      return(2);
   
   }
    
  


}
Reason: