Wednesday, 26 December 2012

SuperTrend 2

int trade() 
{
double trend4 = iCustom(NULL,0,"SuperTrend",5,1.5,1,3); //achat -> vente
double trend1 = iCustom(NULL,0,"SuperTrend",5,1.5,0,2); //achat -> vente


double trend3 = iCustom(NULL,0,"SuperTrend",5,1.5,1,2); //vente -> achat
double trend2 = iCustom(NULL,0,"SuperTrend",5,1.5,0,3); //vente -> achat

int total = OrdersTotal();
double ordre1;
double ordre2;


if(trend2 < 5 && trend3 < 5 && total == 0)//vente
{
ordre1 = OrderSend(Symbol(),OP_SELL,0.08,Bid,3,Bid+(5000*Point),Bid-(5000*Point),"EA de Greg3395",1,0,Red);
}

  if(trend2 > 5 && trend3 > 5 && total == 1) //achat
{
   if(OrderSelect(ordre1, SELECT_BY_TICKET))
  {
   if(OrderType()==OP_SELL)
   {
   OrderClose(OrderTicket(), 0.08, Bid, 3, CLR_NONE);
   }  
  } 



if(trend1 < 5 && trend4 < 5 && total == 0) //achat
{
ordre2 = OrderSend(Symbol(),OP_BUY,0.08,Ask,3,Ask-(5000*Point),Ask+(5000*Point),"EA de Greg3395",2,0,Blue);



if(trend1 > 5 && trend4 > 5 && total == 1) //achat
{
   if(OrderSelect(ordre2, SELECT_BY_TICKET))
  {
    if(OrderType()==OP_BUY)
    {
    OrderClose(OrderTicket(), 0.08, Ask, 3, CLR_NONE);
    }
  }
}

return(0);
}

supertrend value


i made something approaching what i want but i cant understand the behaviour of this EA : its following supertrend switches sometimes, but it also reacts when the market reaches supertrend value...
here are the interresting lines of this code :
//green supertrend and previous value
double super0 = iCustom(NULL,0,"SuperTrend",0,1);
double prevsuper0 = iCustom(NULL,0,"SuperTrend",0,2);
//red supertrend and previous value
double super1 = iCustom(NULL,0,"SuperTrend",1,1);
double prevsuper1 = iCustom(NULL,0,"SuperTrend",1,2);
// turns to red
if ((super1!=2147483647) && (prevsuper1==2147483647)) Order=SIGNAL_CLOSEBUY;
if ((super1!=2147483647) && (prevsuper1==2147483647)) Order=SIGNAL_SELL;
//turns to green
if ((super0!=2147483647) && (prevsuper0==2147483647)) Order=SIGNAL_CLOSESELL;
if ((super0!=2147483647) && (prevsuper0==2147483647)) Order=SIGNAL_BUY;
i also wonder why this weird value of 2147483647 when the line isnt of the same color as the buffer, instead of a clean EMPTY_VALUE...
thanks again!!!

iCustom(NULL, 0, "SuperTrend",Periods,Multiplier,TimeFrame,0,1); //uptrend value


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



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.

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?

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...


I think I got it! 

I changed to the following and it is outputting fine!

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.









"built in" indicators

iCustom() is used for indicator that are not "built in" indicators
If the indicator is a "built in" then you do not need iCustom(). Here is a list of "built in" indicators :

So, if you are not going to use one of the above, you are going to have to use iCustom() for what you described

i have created my EA - ASCTrend.mq4



i have created my EA - ASCTrend.mq4 with MQ4 builder, which uses asctrend signal with ICustom function. it can open a BUY trade when UP signal, and SELL when DOWN.
i want to add to my EA some filter - with backtesting in some pairs, its look good fo filter with BBands ind, bud i dont know how to add this filter in my EA - something like that I think, but have no luck.

double Buy1_1 = iCustom(Symbol(),0,"PerkyAsctrend1",0,1);
double Buy2_1 = iCustom(NULL, 0, "BBands_Stop_v1", 4, 0, Current + 1);
double Buy2_2 = 1;

double Sell1_1 = iCustom(Symbol(),0,"PerkyAsctrend1",1,1);
double Sell2_1 = iCustom(NULL, 0, "BBands_Stop_v1", 5, 0, Current + 1);
double Sell2_2 = 1;

if (Buy1_1 && Buy2_1 > Buy2_2) Order = SIGNAL_BUY;

if (Sell1_1 && Sell2_1 > Sell2_2) Order = SIGNAL_SELL;













iCustom Question



I have an indicator which compares several values. I wish to bring those values into my EA. So far, what I've found, of the use of iCustom function is; calling the name of the indicator and comparing two different time periods of the same MA. How would I use iCustom to compare two different values of the same indicator.

For example:

Let's say that I have an MA of the average of the High and Low for a 15 min Period, and a MA of Close Price over 13 Periods, in the same Indicator. Let's say that I want to place a Buy, in my EA, when the MA of the Close Price crosses under the MA of the average of High/ Low, in the Indicator.


How would I utilize the iCustom Function to capture a state which occurs between two different MAs in the same Indicator?







 You just read the different indicators in your EA using icustom and do a compare

val1 = iCustom(NULL, 0,"sar_filter_alert",5,9,3,3,1,1,0.02,0.2,0,0);
val2 = iCustom(NULL, 0,"AsciiTrade",10,20,5,0,0);

Then if(val1 > val2 etc

or if you want to compare 2 values in the same indicator, change the values in the second read

val1 = iCustom(NULL, 0,"sar_filter_alert",5,9,3,3,1,1,0,0);
val2 = iCustom(NULL, 0,"sar_filter_alert",25,19,3,3,1,3,1,0);


Supertrend of a specific bar using iCustom

Hi homi,

Let�s assume we want to return the value of the indicator �Supertrend� of a specific bar using iCustom

If we want to get the returned value of the Supertrend first line:
We can write this price of code inside start() function:

Code:
double val=iCustom(NULL, 0, "SuperTrend",0,0,pos);
Here val will hold the returned value of Supertrend indicator(first line).

If you want the returned value of the second line, we can write:

Code:
double val=iCustom(NULL, 0, "SuperTrend",0,1,pos);
This is a program which will use Supertrend indicator using iCustom:


Code:
#property copyright "Coders Guru"
#property link      "http://www.forex-tsd.com"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,ExtMapBuffer2);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }


int start()
  {
   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--;
   
   int pos=Bars-counted_bars;
   
     
 
   while(pos>=0)
     {
         ExtMapBuffer1[pos] = iCustom(NULL, 0, "SuperTrend",0,0,pos);
         ExtMapBuffer2[pos] = iCustom(NULL, 0, "SuperTrend",0,1,pos);

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

==================================================
Use something like this in your EA:

Buff0=iCustom(NULL,0,"Indicator Name",0,0); //Top color of indicator
Buff0=iCustom(NULL,0,"Indicator Name",0,1); //1-Back

Buff1=iCustom(NULL,0,"Indicator Name",1,0); //Next to top color of indicator
Buff1=iCustom(NULL,0,"Indicator Name",1,1); //1-Back

Modify the variables in the indicator itself, and recompile the indicator which will update the buffers and the indicator on the graph!
===================================================


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

I-Customer Section for Expert Advisors



hello traders and programmers

i hope i�m right here to open this new thread , the idea is to post ONLY expert advisor code of ALL INDICATORS what you know for programming and test your own expert advisor .
SORRY FOR MY BAD ENGLISH.......

the form looks like a "FUNCTION" - my first examples :

Indikator: TURBO JRSX: 

Int CheckTurboJRSX()
{
double rsxcurr = 0,rsxprev1 = 0,rsxprev2 = 0;
rsxcurr = iCustom(Symbol(), Period(), "Turbo_JRSX", 17, 0, 1);
rsxprev1 = iCustom(Symbol(), Period(), "Turbo_JRSX", 17, 0, 2);
rsxprev2 = iCustom(Symbol(), Period(), "Turbo_JRSX", 17, 0, 3);

if (rsxcurr > rsxprev1 && rsxcurr > 30 && rsxprev1 < 30) return ( 1) ; //BUY
if (rsxcurr < rsxprev1 && rsxcurr < 70 && rsxprev1 > 70) return ( -1) ; //SELL
return(0);
}
FUNCTION CALLING:
if(CheckTurboJRSX()== 1) {example: open order buy ....}
if(CheckTurboJRSX()==-1) {example: open order sell ....}
-----------------------------------------------------------------------

Indikator: Parabolic Sar:

int CheckParabolicSar()
{
double sarCurrent = iSAR(NULL, 0, 0.02, 0.2, 0);
double sarPrevious = iSAR(NULL, 0, 0.02, 0.2, 1);
if(sarCurrent <= Ask && sarPrevious>sarCurrent) return ( 1) ; //BUY
if(sarCurrent >= Bid && sarPrevious<sarCurrent) return ( -1) ; //SELL
return(0);
}
FUNCTION CALLING:
if(CheckParabolicSar()== 1 {example: open order buy ....}
if(CheckParabolicSar()==-1 {example: open order sell ....}
note: parabolic sar is standard indicator , it`s not needed to attach for downloading....
-----------------------------------------------------------------------

Indikator: Fisher:

EXTERN VARIABLES:

extern int FisherPeriod = 5;
extern double UP_Value = 0.25;
extern double DN_Value =-0.25;
int FisherCheck()
{
double FisherUP = iCustom(NULL,0,"Fisher_Yur4ik",FisherPeriod,1,0);
double FisherDN = iCustom(NULL,0,"Fisher_Yur4ik",FisherPeriod,2,0);

if(FisherUP>UP_Value) return ( 1) ; //BUY
if(FisherDN<DN_Value) return (-1); //SELL
return (0);
}
FUNCTION CALLING:

if(FisherCheck()== 1 {example: open order buy ....}
if(FisherCheck()==-1 {example: open order sell ....}
-----------------------------------------------------------------------

Indikator: iBullsPower/iBearsPower:

EXTERN VARIABLES:

extern int BullBearPeriod=5;
int CheckiBullsBearsPower()
{
double bull = iBullsPower(NULL,0,BullBearPeriod,PRICE_CLOSE,1);
double bear = iBearsPower(NULL,0,BullBearPeriod,PRICE_CLOSE,1);

if(bull +bear>0) return ( 1) ; //BUY
if(bull+bear<0) return ( -1) ; //SELL
return(0);
}
FUNCTION CALLING:

if(CheckiBullsBearsPower()== 1 {example: open order buy ....}
if(CheckiBullsBearsPower()==-1 {example: open order sell ....}
note: iBullsPower and iBearsPower are standard indicators , it`s not needed to attach for downloading....

-----------------------------------------------------------------------

Indikator: Support and Resistance:

int CheckSupportResident()
{
double iTmpR = iCustom(Symbol(),0,"Support and Resistance",0,0);
double iTmpS = iCustom(Symbol(),0,"Support and Resistance",1,0);
if (Close[0]==iTmpR) return ( 1) ; //BUY
if (Close[0]==iTmpS) return ( -1) ; //SELL
return(0);
}
FUNCTION CALLING:

if(CheckSupportResident()== 1 {example: open order buy ....}
if(CheckSupportResident()==-1 {example: open order sell ....}
-----------------------------------------------------------------------

Indikator: TrendEnvelopes_v2:

EXTERN VARIABLES:

extern int SignalCandle =1;
extern int MA_Period =34;

Int CheckTrendEnvelopes_V2()
{
double TrendEnv_long = iCustom(Symbol(),Period(),"TrendEnvelopes_v2",MA_P eriod,0,SignalCandle); //buy
double TrendEnv_short = iCustom(Symbol(),Period(),"TrendEnvelopes_v2",MA_P eriod,1,SignalCandle); //sell

(TrendEnv_long <9999) return ( 1) ; //BUY
(TrendEnv_short <9999) return ( -1) ; //SELL
return(0);
}
FUNCTION CALLING:

if(CheckTrendEnvelopes_V2()== 1 {example: open order buy ....}
if(CheckTrendEnvelopes_V2()==-1 {example: open order sell ....}
-----------------------------------------------------------------------

hope this samples help to inspirit this thread..... will add more indicators next time .......

have fun and greetings from austria

forex2006
 Attached Files
  • File Type: mq4 Turbo_JRSX.mq4 (2.7 KB, 51 views)
  • http://www.forex-tsd.com/attachments/metatrader-4-mql-4-development-course/80765d1238265180-icustom-function-turbo_jrsx.mq4
  • File Type: mq4 Fisher_Yur4ik.mq4 (2.0 KB, 42 views)
  • http://www.forex-tsd.com/attachments/metatrader-4-mql-4-development-course/80766d1238266186-icustom-function-fisher_yur4ik.mq4
  • File Type: mq4 Support and Resistance.mq4 (5.2 KB, 44 views)
  • http://www.forex-tsd.com/attachments/metatrader-4-mql-4-development-course/80767d1238267548-icustom-function-support-resistance.mq4
  • File Type: mq4 TrendEnvelopes_v2.mq4 (4.2 KB, 52 views)
  • http://www.forex-tsd.com/attachments/metatrader-4-mql-4-development-course/80768d1238267853-icustom-function-trendenvelopes_v2.mq4

Coding Zigzag



zigzag differs from most indicators in that for the large precentage of time it's value is 0, with only an occassional non-zero number..... that renders it almost unusable in the standard iCustom form.....

// ---> zig = iCustom(NULL,0,"ZigZag",15,5,3, 0, i);

to be useful you almost certainly will have to create an array to store all the "non-zero" numbers..... such as;

if(zig>0)
{
zag[n] = zig;
}

then you can do the common

if(zag[1] > zag[2]) // do this

else // or

if(zag[1] < zag[2]) // do that


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

EA write using this Advanced ADX indicator


I am want to write a EA write using this Advanced ADX indicator...

Buy
ADX color green and (ADX this hour> ADX previous hour)


Sell
ADX color red and (ADX this hour< ADX previous hour)

thx
sonic


PHP Code:
for (Bars 205>= 0--)
   {
   
  
double adx1         =iCustom(NULL0"Advanced_ADX",13,0,i); 
  
double adx2         =iCustom(NULL0"Advanced_ADX",13,1,i); 
  
   if ((
adx1[i]>adx2[i])&&(adx1[i+1] > adx1[i]) // BUY 
      
BUY routine}
   
   if ((
adx1[i]<adx2[i])&&(adx1[i+1] < adx1[i]) // SELL  
      
SELL routine}

}  
Attached Thumbnails Attached ThumbnailsClick image for larger version. 

Name: advanced_adx_1.gif 
Views: 417 
Size: 16.1 KB 
ID: 63026 
 Attached Files