• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • User/Email: Password:
  • 7:03am
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 7:03am
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

Supertrend and MTF supertrend IDEA 11 replies

Is there a SuperTrend indicator which doesn't repaint? 1 reply

Help needed - program Supertrend EA indicator to specific parameters 0 replies

Help with Supertrend Indicator error 2 replies

Modified Supertrend indicator 0 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
  • 1
Attachments: How to reference Custom Indicator (SuperTrend)
Exit Attachments

How to reference Custom Indicator (SuperTrend)

  • Last Post
  •  
  • Page 1 2
  • Page 1 2
  •  
  • Post #1
  • Quote
  • First Post: Aug 16, 2010 4:15pm Aug 16, 2010 4:15pm
  •  bluemele
  • | Joined Apr 2010 | Status: Member | 904 Posts
Hi all,

I am building another EA and curious on how to reference an indicator, more specifically the SuperTrend Indicator.

I want to put in a long when it crosses the ATR threshold, and vice versa for short. How would I reference this? Any help is much appreciated!



Indicator is here:

Inserted Code
//+------------------------------------------------------------------+
//|                                              SuperTrend.mq4 v1.2 |
//|                   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[5000];
   double up[5000], dn[5000], medianPrice, atr;
   int counted_bars = IndicatorCounted();
//---- check for possible errors
   if(counted_bars < 0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars > 0) counted_bars--;
   limit=Bars-counted_bars;
   //Print(limit);
   
//----
   for (i = Bars; i >= 0; 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;
         }
      }
   }
   WindowRedraw();
      
//----
   return(0);
  }
//+------------------------------------------------------------------+
We are our own best indicator.
  • Post #2
  • Quote
  • Aug 16, 2010 9:57pm Aug 16, 2010 9:57pm
  •  luxinterior
  • Joined Nov 2006 | Status: MT4 EA Coder Since 2006 | 300 Posts
Read about the iCustom function in the mql help file.

Good luck

Lux
MT4 EA, Indicator and Alert Coder Since 2006
 
 
  • Post #3
  • Quote
  • Aug 16, 2010 11:25pm Aug 16, 2010 11:25pm
  •  bluemele
  • | Joined Apr 2010 | Status: Member | 904 Posts
Quoting luxinterior
Disliked
Read about the iCustom function in the mql help file.

Good luck

Lux
Ignored
THank you, will take a look!
We are our own best indicator.
 
 
  • Post #4
  • Quote
  • Aug 17, 2010 12:20am Aug 17, 2010 12:20am
  •  bluemele
  • | Joined Apr 2010 | Status: Member | 904 Posts
Quoting luxinterior
Disliked
Read about the iCustom function in the mql help file.

Good luck

Lux
Ignored
Lux et al,

I am confused. I pass this code to the indicator:

Inserted Code
double val=iCustom(NULL, TimeFrame, "SuperTrend",0,0,0);

and I get the current bid price.

I pass:

Inserted Code
double val = iCustom(NULL,TimeFrame,"SuperTrend","Nbr_Periods=3,Multiplier=1.25",0,0);

And I get the number of bars in history...

I am trying to pass the two extern parameters and set them to 3, and 1.25 respectively.

I am confused as I have been searching on the net and I don't see where it shows you how to pass parameters??

Thanks in advance...
We are our own best indicator.
 
 
  • Post #5
  • Quote
  • Aug 17, 2010 12:23am Aug 17, 2010 12:23am
  •  bluemele
  • | Joined Apr 2010 | Status: Member | 904 Posts
It seems that you set up an icustom call for each parameter that you want to pass? Am I understanding this properly?

If so, I don't understand how you would get a return value from an indicator like the one I posted above?

Very confused...
We are our own best indicator.
 
 
  • Post #6
  • Quote
  • Aug 17, 2010 12:31am Aug 17, 2010 12:31am
  •  bluemele
  • | Joined Apr 2010 | Status: Member | 904 Posts
Ok, I reviewed this thread, but still confused..

http://forum.mql4.com/8150

It mentions you have to declare a buffer for each indicator output. For the supertrend posted above, what is the output?? I don't see it returning any variables/values?

I see it drawing something on the screen, and that is it?

Please advise....
We are our own best indicator.
 
 
  • Post #7
  • Quote
  • Aug 17, 2010 12:44pm Aug 17, 2010 12:44pm
  •  bluemele
  • | Joined Apr 2010 | Status: Member | 904 Posts
Ok, so, it seems to me that each icustom call extracts a value from the indicator which gets put into the buffer. Correct?

If so, what happens when the custom indicator requires two parameters to be passed, how do you combine them to get the defined output?

Please help.
We are our own best indicator.
 
 
  • Post #8
  • Quote
  • Aug 17, 2010 1:50pm Aug 17, 2010 1:50pm
  •  hayseed
  • Joined Nov 2006 | Status: Member | 3,547 Posts
it might seem like a small matter, but when you post the code rather than uploading the indicator it requires us to take a couple extra steps.....

10 and 3 are your parameters......

0 and 1 are the target buffers, 0 is up trend buffer, 1 is downtred buffer.....

the last 1 is shift.....

.......h



Inserted Code
 
iCustom(NULL,0,"SuperTrend",10,3,0,1) //buffer 0 ie uptrend
 
iCustom(NULL,0,"SuperTrend",10,3,1,1) //buffer 1 ie downtrend
to trade and code, keep both simple... no call to impress....h
 
 
  • Post #9
  • Quote
  • Aug 17, 2010 4:58pm Aug 17, 2010 4:58pm
  •  kennyhubbard
  • Joined Sep 2008 | Status: Member | 346 Posts
Hi Gil,

Quote
Disliked
If so, what happens when the custom indicator requires two parameters to be passed, how do you combine them to get the defined output?

You pass the parameter to the indicator as you did in an earlier post(but without naming them). They must be in the order of extern variables as defined at the start of the indicator. The next digit you send is the buffer that you wish to extract. There are up to 8 buffers( in your particular example only 2), and since the iCustom function can only return 1 result, you must tell it which buffer you want the results of. As hayseed mentioned, the last parameter is the shift.

I personally think the iCustom is an highly innefficient way of getting data into your EA. It will slow it down no end, since (IMHO) the indicator will be re-initialised with every call to iCustom. It also then writes to the display which even more inefficient. You have the code and the formulas, so I would rather extract them and use them directly in your EA.

To give you an example, you could paste the bulk of the indicator code into a function and to return the equivalent to :-

Inserted Code
iCustom(NULL,0,"SuperTrend",10,3,1,1) //buffer 1 ie downtrend

would be the same as getting

Inserted Code
TrendDown[1]

from the code that you posted.
 
 
  • Post #10
  • Quote
  • Aug 17, 2010 6:17pm Aug 17, 2010 6:17pm
  •  bluemele
  • | Joined Apr 2010 | Status: Member | 904 Posts
Quoting kennyhubbard
Disliked
Hi Gil,



You pass the parameter to the indicator as you did in an earlier post(but without naming them). They must be in the order of extern variables as defined at the start of the indicator. The next digit you send is the buffer that you wish to extract. There are up to 8 buffers( in your particular example only 2), and since the iCustom function can only return 1 result, you must tell it which buffer you want the results of. As hayseed mentioned, the last parameter is the shift.

I personally think the iCustom is an highly innefficient...
Ignored
Kenny,

Thanks again! You are great!

I tried just putting it into a function, but unfortunately, I couldn't prevail after 10 minutes so I thought I would just call the indicator. I agree, it doesn't seem efficient at all.

I will do my best to get the code out and working straight into a function, but the code is a bit confusing to my limited programming mind.

I will BUG you again if I can't figure it out. Thank you again!

We are our own best indicator.
 
 
  • Post #11
  • Quote
  • Aug 17, 2010 6:18pm Aug 17, 2010 6:18pm
  •  bluemele
  • | Joined Apr 2010 | Status: Member | 904 Posts
Quoting hayseed
Disliked
it might seem like a small matter, but when you post the code rather than uploading the indicator it requires us to take a couple extra steps.....

10 and 3 are your parameters......

0 and 1 are the target buffers, 0 is up trend buffer, 1 is downtred buffer.....

the last 1 is shift.....

.......h



Inserted Code
 
iCustom(NULL,0,"SuperTrend",10,3,0,1) //buffer 0 ie uptrend
 
iCustom(NULL,0,"SuperTrend",10,3,1,1) //buffer 1 ie downtrend
Ignored
Hayseed!

Thank you for the explanation! This helps a lot. I believe I can do this method now. I will try porting over the code to an internal function also which will be a good lesson in learning.

Thank you! Sorry for the basic questions, still new at MQL4!

We are our own best indicator.
 
 
  • Post #12
  • Quote
  • Aug 17, 2010 6:41pm Aug 17, 2010 6:41pm
  •  kennyhubbard
  • Joined Sep 2008 | Status: Member | 346 Posts
Hi Gil,

You are welcome mate.

On second thoughts, it may be better to stick with the iCustom. Transferring the code of an indicator to an EA is way more efficient, but it is not nearly as simple as I made it out to be.

It would be an excellent learning exercise but it may be worth getting to grips with creating simple indicators before you try it.
 
 
  • Post #13
  • Quote
  • Aug 17, 2010 7:02pm Aug 17, 2010 7:02pm
  •  bluemele
  • | Joined Apr 2010 | Status: Member | 904 Posts
Quoting kennyhubbard
Disliked
Hi Gil,

You are welcome mate.

On second thoughts, it may be better to stick with the iCustom. Transferring the code of an indicator to an EA is way more efficient, but it is not nearly as simple as I made it out to be.

It would be an excellent learning exercise but it may be worth getting to grips with creating simple indicators before you try it.
Ignored
Thanks Kenny, well, already on my way.... ;0 Will give it a shot... I think I have removed 90% of the unneeded code, so we will see...

I have to understand the SuperTrend code...
We are our own best indicator.
 
 
  • Post #14
  • Quote
  • Aug 17, 2010 7:51pm Aug 17, 2010 7:51pm
  •  bluemele
  • | Joined Apr 2010 | Status: Member | 904 Posts
Kenny/Hayseed,

I have put the following for a call to the indicator, would this be correct to get the current price? It only works on the output of the bar I am asking (which is fine) I believe and does this seem right?

Inserted Code
//+------------------------------------------------------------------+
//|                                                SuperTrend V1.mq4 |
//|                      Copyright © 2010, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
double ExtMapBuffer1[];
double ExtMapBuffer2[];

int      Periods = 3;
double   Multiplier = 1.25;
int      TimeFrame = 240;
string   TrendDirection = "Long";

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
  
      
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   
      ExtMapBuffer1[1] = iCustom(NULL, 0, "SuperTrend",Periods,Multiplier,TimeFrame,0,1);  //downtrend value
 
      ExtMapBuffer1[2] = iCustom(NULL, 0, "SuperTrend",Periods,Multiplier,TimeFrame,1,1);  //uptrend value
      
      if(ExtMapBuffer1[1] < ExtMapBuffer1[2])
      {
         TrendDirection = "Down";
      }
   
      Comment("UpTrend: ", ExtMapBuffer1[1], "n", "DownTrend: ", ExtMapBuffer2[2], "n", "Trade Direction: ", TrendDirection);
   
//----
   return(0);
  }

I added the Timeframe option in the indicator so I can switch through the timeframes...
We are our own best indicator.
 
 
  • Post #15
  • Quote
  • Aug 17, 2010 8:04pm Aug 17, 2010 8:04pm
  •  bluemele
  • | Joined Apr 2010 | Status: Member | 904 Posts
The most confusing part of this for me, is ...

How do you know which 'buffer' is what? Confusing to me...

Buffer[0] is uptrend, why is that?? Where is that clear?

We are our own best indicator.
 
 
  • Post #16
  • Quote
  • Aug 17, 2010 8:43pm Aug 17, 2010 8:43pm
  •  hayseed
  • Joined Nov 2006 | Status: Member | 3,547 Posts
Quoting bluemele
Disliked
The most confusing part of this for me, is ...

How do you know which 'buffer' is what? Confusing to me...

Buffer[0] is uptrend, why is that?? Where is that clear?

Ignored
//---
it might look confusing now, but in time you will be able to read the code..... it's just like your learning to read, speak and write another language.... in this case called mq4 .......


each time you get ready to throw in the towel, remind yourself, chinese looks pretty darn confusing also, but there's a whole pack of little 6 year olds learn it every day.......

//---

at first mq4 is daunting..... you will progress...... one day you'll find yourself writing code on napkins at mcdonalds..... before long there will not be a piece of paper in your house without code on it...... eventually you will be able to write the entire thing in your mind......

it's just a matter of time......h





SetIndexBuffer(0, TrendUp);
SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2);
SetIndexLabel(0, "Trend Up");
to trade and code, keep both simple... no call to impress....h
 
 
  • Post #17
  • Quote
  • Aug 17, 2010 10:10pm Aug 17, 2010 10:10pm
  •  bluemele
  • | Joined Apr 2010 | Status: Member | 904 Posts
I C!

That make sense...

The missing link!



Thank you! And, yes, I used to do that a long time ago as well.. Funny how the mind works....
We are our own best indicator.
 
 
  • Post #18
  • Quote
  • Aug 17, 2010 10:12pm Aug 17, 2010 10:12pm
  •  bluemele
  • | Joined Apr 2010 | Status: Member | 904 Posts
Ok, any idea why this doesn't work? Makes sense to me...

Inserted Code
int start()
  {
//----
   
      ExtMapBuffer1[1] = iCustom(NULL, 0, "SuperTrend",Periods,Multiplier,TimeFrame,0,1);  //uptrend value
 
      ExtMapBuffer1[2] = iCustom(NULL, 0, "SuperTrend",Periods,Multiplier,TimeFrame,1,1);  //downtrend value
      
      if(Bid <= ExtMapBuffer1[1]) 
      {
         TrendDirection = "Up!";
      }
      if(Bid >= ExtMapBuffer1[2]) 
      {
         TrendDirection = "Down!";
      }
   
      Comment("UpTrend: ", ExtMapBuffer1[1], "n", "DownTrend: ", ExtMapBuffer2[2], "n", "Trade Direction: ", TrendDirection);
   
//----
   return(0);
  }
We are our own best indicator.
 
 
  • Post #19
  • Quote
  • Aug 17, 2010 11:02pm Aug 17, 2010 11:02pm
  •  bluemele
  • | Joined Apr 2010 | Status: Member | 904 Posts
I think I got it!

I changed to the following and it is outputting fine!

Inserted Code
//+------------------------------------------------------------------+
//|                                                SuperTrend V1.mq4 |
//|                      Copyright © 2010, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
double ExtMapBuffer1[];
double ExtMapBuffer2[];

int      Periods = 3;
double   Multiplier = 1.25;
int      TimeFrame = 240;
string   TrendDirection = "Long";


double   UpTrend,DownTrend;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
  
      
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   
      //ExtMapBuffer1[1] = iCustom(NULL, 0, "SuperTrend",Periods,Multiplier,TimeFrame,0,1);  //uptrend value
 
      //ExtMapBuffer1[2] = iCustom(NULL, 0, "SuperTrend",Periods,Multiplier,TimeFrame,1,1);  //downtrend value
      UpTrend = iCustom(NULL, 0, "SuperTrend",Periods,Multiplier,TimeFrame,0,1);
      DownTrend = iCustom(NULL, 0, "SuperTrend",Periods,Multiplier,TimeFrame,1,1);
      
      /*if(Bid <= ExtMapBuffer1[1]) 
      {
         TrendDirection = "Up!";
      }
      if(Bid >= ExtMapBuffer1[2]) 
      {
         TrendDirection = "Down!";
      }
   
      //Comment("UpTrend: ", ExtMapBuffer1[1], "n", "DownTrend: ", ExtMapBuffer2[2], "n", "Trade Direction: ", TrendDirection);
   */
      Comment("UpTrend: ", UpTrend, "n", "DownTrend: ", DownTrend);
//----
   return(0);
  }
We are our own best indicator.
 
 
  • Post #20
  • Quote
  • Aug 17, 2010 11:06pm Aug 17, 2010 11:06pm
  •  zznbrm
  • Joined Jul 2008 | Status: Member | 878 Posts
I agree with kennyhubbard in regard to using iCustom().

Here is a function to calculate the TrendMagic value in an EA (or script or whatever). I think Supertrend is very similar to TrendMagic. Hopefully, this will at least point you in the right direction. I did limited testing of this, but I think everything is in order.

BTW, this function can be used as a "shell" for all other indicators you want to turn into functions. Some indicators are easier to "functionize" than others. Anyway, hope this helps.

Inserted Code
 
double getTrendMagic( string strSymbol, int intTF, int intCciPeriod, int intAtrPeriod, int intShift )
{
   double dblTM, dblCci, dblPrevTM, dblAtr;
   int intCurrShift;
 
   for ( int inx = 50; inx >= 0; inx-- )
   {
      intCurrShift = intShift + inx;
      dblCci = iCCI( strSymbol, intTF, intCciPeriod, PRICE_TYPICAL, intCurrShift );
      dblAtr = iATR( strSymbol, intTF, intAtrPeriod, intCurrShift );
 
      if ( dblCci >= 0 )   
      {
         dblTM = iLow( strSymbol, intTF, intCurrShift ) - dblAtr;
 
         if ( dblTM < dblPrevTM )    dblTM = dblPrevTM;
      }     
      else                  
      {
         dblTM = iHigh( strSymbol, intTF, intCurrShift ) + dblAtr; 
 
         if ( dblTM > dblPrevTM )    dblTM = dblPrevTM;   
      }  
 
      dblPrevTM = dblTM;
   }
 
   return( dblTM );
}
 
 
  • Platform Tech
  • /
  • How to reference Custom Indicator (SuperTrend)
  • Reply to Thread
    • Page 1 2
    • Page 1 2
0 traders viewing now
  • More
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2022