Download MetaTrader 5

Tutorial {icustom} Indicator to Expert Advisor

To add comments, please log in or register
lex3879
139
lex3879  

Time for the experts to show your skills ..

Lets use 3 indicators and change it into simple buy/sell expert advisors..

First wolfwave . buy or sell every swing

//+------------------------------------------------------------------+
//| A WolfWave finder based on ZIGZAG.MQ4 |
// ---------------------------------------+

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Black
#property indicator_color2 Blue
#define MaxAnalyze 200
#define UpperDistance 15
#define LowerDistance 5
#define Title "WW"

//---- indicator parameters
extern int ExtDepth=50;
extern int ExtDeviation=3;
extern int ExtBackstep=2;

//---- indicator buffers
double ExtMapBuffer[];
double ExtMapBuffer2[];
int timeFirstBar=0;


int init()
{
IndicatorBuffers(2);
//---- drawing settings

SetIndexStyle(0,DRAW_SECTION);
//---- indicator buffers mapping
SetIndexBuffer(0,ExtMapBuffer);
SetIndexBuffer(1,ExtMapBuffer2);

SetIndexEmptyValue(0,0.0);
SetIndexEmptyValue(1,0.0);

ArraySetAsSeries(ExtMapBuffer,true);
ArraySetAsSeries(ExtMapBuffer2,true);
//---- indicator short name
IndicatorShortName("WolfWave");
//---- initialization done

return(0);
}

int deinit()
{
for (int i=1;i<=5;i++)
{
ObjectDelete(Title + ""+i);
}

ObjectDelete(Title + "Line-2-4");
ObjectDelete(Title + "Line-1-3");
ObjectDelete(Title + "Line-1-4");
ObjectDelete(Title + "Line-2-5");
return(0);
}

int start()
{
int counted_bars=IndicatorCounted();
int shift, back,lasthighpos,lastlowpos;
double val,res;
double curlow,curhigh,lasthigh,lastlow;
int num=0;

int Peak[MaxAnalyze],h,i,j;
int Wolf[6];
string WolfWave="None";
double Winkel1, Winkel2;
bool found=false;


if (Bars-1<ExtDepth)return(0);

static int time2,time3,time4;

static double ZigZag2,ZigZag3,ZigZag4;

int MaxBar,limit,supr2_bar,supr3_bar,supr4_bar;

if (counted_bars<0)return(-1);

if (counted_bars>0) counted_bars--;

MaxBar=Bars-ExtDepth;

if (counted_bars==0 || Bars-counted_bars>2)
{
limit=MaxBar;
}
else
{
//----
supr2_bar=iBarShift(NULL,0,time2,TRUE);
supr3_bar=iBarShift(NULL,0,time3,TRUE);
supr4_bar=iBarShift(NULL,0,time4,TRUE);
//----
limit=supr3_bar;
if ((supr2_bar<0)||(supr3_bar<0)||(supr4_bar<0))
{
limit=MaxBar;
}
}

//---- èíèöèàëèçàöèÿ íóëÿ
if (limit>=MaxBar || timeFirstBar!=Time[Bars-1])
{
timeFirstBar=Time[Bars-1];
for (shift=Bars-1; shift>0;shift--) {ExtMapBuffer[shift]=0.0; ExtMapBuffer2[shift]=0.0;}
limit=MaxBar;
}
//----


for(shift=Bars-ExtDepth; shift>=0; shift--)
{
val=Low[Lowest(NULL,0,MODE_LOW,ExtDepth,shift)];
if(val==lastlow) val=0.0;
else
{
lastlow=val;
if((Low[shift]-val)>(ExtDeviation*Point)) val=0.0;
else
{
for(back=1; back<=ExtBackstep; back++)
{
res=ExtMapBuffer[shift+back];
if((res!=0)&&(res>val)) ExtMapBuffer[shift+back]=0.0;
}
}
}
if (Low[shift]==val) ExtMapBuffer[shift]=val;
//--- high
val=High[Highest(NULL,0,MODE_HIGH,ExtDepth,shift)];
if(val==lasthigh) val=0.0;
else
{
lasthigh=val;
if((val-High[shift])>(ExtDeviation*Point)) val=0.0;
else
{
for(back=1; back<=ExtBackstep; back++)
{
res=ExtMapBuffer2[shift+back];
if((res!=0)&&(res<val)) ExtMapBuffer2[shift+back]=0.0;
}
}
}
if (High[shift]==val) ExtMapBuffer2[shift]=val;
}

// final cutting
lasthigh=-1; lasthighpos=-1;
lastlow=-1; lastlowpos=-1;

for(shift=Bars-ExtDepth; shift>=0; shift--)
{
curlow=ExtMapBuffer[shift];
curhigh=ExtMapBuffer2[shift];
if((curlow==0)&&(curhigh==0)) continue;
//---
if(curhigh!=0)
{
if(lasthigh>0)
{
if(lasthigh<curhigh) ExtMapBuffer2[lasthighpos]=0;
else ExtMapBuffer2[shift]=0;
}
//---
if(lasthigh<curhigh || lasthigh<0)
{
lasthigh=curhigh;
lasthighpos=shift;
}
lastlow=-1;
}
//----
if(curlow!=0)
{
if(lastlow>0)
{
if(lastlow>curlow) ExtMapBuffer[lastlowpos]=0;
else ExtMapBuffer[shift]=0;
}
//---
if((curlow<lastlow)||(lastlow<0))
{
lastlow=curlow;
lastlowpos=shift;
}
lasthigh=-1;
}
}

for(shift=Bars-1; shift>=0; shift--)
{
if(shift>=Bars-ExtDepth) ExtMapBuffer[shift]=0.0;
else
{
res=ExtMapBuffer2[shift];
//if(res!=0.0) ExtMapBuffer[shift]=res;
if(res>0.0) ExtMapBuffer[shift]=res;
}
}


i=0;j=0;
res=0;
for (shift=0;i<3;shift++)
{
if (ExtMapBuffer[shift]>0)
{
i++;
if (i==1 && ExtMapBuffer[shift]==High[shift])
{
j=shift;
res=ExtMapBuffer[shift];
}
if (i==2 && res>0 && ExtMapBuffer[shift]==High[shift])
{
if (ExtMapBuffer[shift]>=ExtMapBuffer[j]) ExtMapBuffer[j]=0; else ExtMapBuffer[shift]=0;
res=0;
i=0;
j=0;
shift=0;
}
}
}



if (limit<MaxBar)
{
ExtMapBuffer[supr2_bar]=ZigZag2;
ExtMapBuffer[supr3_bar]=ZigZag3;
ExtMapBuffer[supr4_bar]=ZigZag4;
for(int qqq=supr4_bar-1; qqq>supr3_bar; qqq--)ExtMapBuffer[qqq]=0;
for(int ggg=supr3_bar-1; ggg>supr2_bar; ggg--)ExtMapBuffer[ggg]=0;
}
//+---+============================================+

//+--- èñïðàâëåíèå âîçíèêàþùèõ ãîðáîâ
double vel1, vel2, vel3, vel4;
int bar1, bar2, bar3, bar4;
int count;
if (limit==MaxBar)supr4_bar=MaxBar;
for(int bar=supr4_bar; bar>=0; bar--)
{
if (ExtMapBuffer[bar]!=0)
{
count++;
vel4=vel3;bar4=bar3;
vel3=vel2;bar3=bar2;
vel2=vel1;bar2=bar1;
vel1=ExtMapBuffer[bar];bar1=bar;
if (count<3)continue;
if ((vel3<vel2)&&(vel2<vel1)){ExtMapBuffer[bar2]=0;bar=bar3+1;}
if ((vel3>vel2)&&(vel2>vel1)){ExtMapBuffer[bar2]=0;bar=bar3+1;}
if ((vel2==vel1)&&(vel1!=0 )){ExtMapBuffer[bar1]=0;bar=bar3+1;}
}
}

time2=Time[bar2];
time3=Time[bar3];
time4=Time[bar4];
ZigZag2=vel2;
ZigZag3=vel3;
ZigZag4=vel4;
//+---


// Basic Modification ==>

i=0;
for(h=0; h<Bars && i<MaxAnalyze; h++)
{
if ((ExtMapBuffer[h]!=0) || (ExtMapBuffer2[h]!=0)) {
Peak= h;
i++;
}
}

for(j=0;j<i && j<MaxAnalyze && found==false;j++) {
Wolf[1]=Peak[j+4]; // 1 High
Wolf[2]=Peak[j+3]; // 2 Low
Wolf[3]=Peak[j+2]; // 3 High
Wolf[4]=Peak[j+1]; // 4 Low
Wolf[5]=Peak[j+0]; // 5 High
if ( // Buy Wolfwave
Low[Wolf[1]]<High[Wolf[2]] && // 1. + 3.a.//1-3
Low[Wolf[3]]<Low[Wolf[1]] && // 2. + 3.b. //3-1
Low[Wolf[3]]<High[Wolf[4]] // 4//3-4
) {
WolfWave="Buy";
} else if
( // Sell Wolfwave
High[Wolf[1]]>Low[Wolf[2]] && // 1. + 3.a.
High[Wolf[3]]>High[Wolf[1]] && // 2. + 3.b.
High[Wolf[3]]>Low[Wolf[4]] // 4
) {
WolfWave="Sell";
} else {
WolfWave="Not";
}

if(WolfWave=="Buy") {
ObjectCreate(Title + "Line-1-3", OBJ_TREND, 0, Time[Wolf[1]],Low[Wolf[1]], Time[Wolf[3]],Low[Wolf[3]] );
// ObjectSet(Title + "Line-1-3",OBJPROP_RAY,0);
ObjectSet(Title + "Line-1-3", OBJPROP_COLOR, Lime);
ObjectSet(Title + "Line-1-3", OBJPROP_WIDTH, 2);
// ObjectSet(Title + "Line-1-3",OBJPROP_BACK,1);

if (ObjectGetValueByShift(Title + "Line-1-3", Wolf[3]) >= Low[Wolf[3]]) {
ObjectCreate(Title + "1", OBJ_TEXT, 0, Time[Wolf[1]],Low[Wolf[1]]-LowerDistance*Point );
ObjectSetText(Title + "1", ""+DoubleToStr(1,0), 14, "Arial", DeepSkyBlue);
ObjectCreate(Title + "2", OBJ_TEXT, 0, Time[Wolf[2]],High[Wolf[2]]+UpperDistance*Point );
ObjectSetText(Title + "2", ""+DoubleToStr(2,0), 14, "Arial", DeepSkyBlue);
ObjectCreate(Title + "3", OBJ_TEXT, 0, Time[Wolf[3]],Low[Wolf[3]]-LowerDistance*Point );
ObjectSetText(Title + "3", ""+DoubleToStr(3,0), 14, "Arial", DeepSkyBlue);
ObjectCreate(Title + "4", OBJ_TEXT, 0, Time[Wolf[4]],High[Wolf[4]]+UpperDistance*Point );
ObjectSetText(Title + "4", ""+DoubleToStr(4,0), 14, "Arial", DeepSkyBlue);
ObjectCreate(Title + "5", OBJ_TEXT, 0, Time[Wolf[5]],Low[Wolf[5]]-LowerDistance*Point );
ObjectSetText(Title + "5", ""+DoubleToStr(5,0), 14, "Arial", DeepSkyBlue);

ObjectCreate(Title + "Line-1-4", OBJ_TREND, 0, Time[Wolf[1]],Low[Wolf[1]], Time[Wolf[4]],High[Wolf[4]] );
ObjectSet(Title + "Line-1-4", OBJPROP_COLOR, Red);
// ObjectSet(Title + "Line-1-4",OBJPROP_RAY,0);
ObjectSet(Title,OBJPROP_STYLE,STYLE_DOT);
ObjectSet(Title + "Line-1-4", OBJPROP_WIDTH, 0);
ObjectSet(Title + "Line-1-4",OBJPROP_BACK,0);
ObjectSet(Title + "Line-2-5",OBJPROP_BACK,0);

// ObjectCreate(Title + "Line-2-5", OBJ_TREND, 0, Time[Wolf[2]],High[Wolf[2]], Time[Wolf[5]],Low[Wolf[5]] );
//ObjectSet(Title,OBJPROP_STYLE,STYLE_DOT);
// ObjectSet(Title + "Line-2-5",OBJPROP_RAY,0);
//ObjectSet(Title + "Line-2-5", OBJPROP_COLOR, Lime);
// ObjectSet(Title + "Line-2-5", OBJPROP_WIDTH, 1);

// Comment("Buy Wolfwave (" + TimeToStr(Time[Wolf[5]],TIME_DATE|TIME_MINUTES) + ") at " + (ObjectGetValueByShift("Line-1-3", Wolf[5])-5*Point) + " SL " + High[Wolf[5]]);
// found=true;
//} //else {
// ObjectDelete(Title + "Line-1-3");
}
} else if (WolfWave=="Sell") {
ObjectCreate(Title + "Line-1-3", OBJ_TREND, 0, Time[Wolf[1]],High[Wolf[1]], Time[Wolf[3]],High[Wolf[3]] );
//ObjectSet(Title + "Line-1-3",OBJPROP_RAY,0);
ObjectSet(Title + "Line-1-3", OBJPROP_COLOR, Lime);
ObjectSet(Title + "Line-1-3", OBJPROP_WIDTH, 2);
// ObjectSet(Title + "Line-1-3",OBJPROP_BACK,1);

if ( ObjectGetValueByShift(Title + "Line-1-3", Wolf[3]) <= High[Wolf[3]] ) {
ObjectCreate(Title + "1", OBJ_TEXT, 0, Time[Wolf[1]],High[Wolf[1]]+UpperDistance*Point );
ObjectSetText(Title + "1", ""+DoubleToStr(1,0), 14, "Arial", DeepSkyBlue);
ObjectCreate(Title + "2", OBJ_TEXT, 0, Time[Wolf[2]],Low[Wolf[2]]-LowerDistance*Point );
ObjectSetText(Title + "2", ""+DoubleToStr(2,0), 14, "Arial", DeepSkyBlue);
ObjectCreate(Title + "3", OBJ_TEXT, 0, Time[Wolf[3]],High[Wolf[3]]+UpperDistance*Point );
ObjectSetText(Title + "3", ""+DoubleToStr(3,0), 14, "Arial", DeepSkyBlue);
ObjectCreate(Title + "4", OBJ_TEXT, 0, Time[Wolf[4]],Low[Wolf[4]]-LowerDistance*Point );
ObjectSetText(Title + "4", ""+DoubleToStr(4,0), 14, "Arial", DeepSkyBlue);
ObjectCreate(Title + "5", OBJ_TEXT, 0, Time[Wolf[5]],High[Wolf[5]]+UpperDistance*Point );
ObjectSetText(Title + "5", ""+DoubleToStr(5,0), 14, "Arial", DeepSkyBlue);

ObjectCreate(Title + "Line-1-4", OBJ_TREND, 0, Time[Wolf[1]],High[Wolf[1]], Time[Wolf[4]],Low[Wolf[4]] );
ObjectSet(Title + "Line-1-4", OBJPROP_COLOR, Red);
//ObjectSet(Title + "Line-1-4",OBJPROP_RAY,0);
ObjectSet(Title,OBJPROP_STYLE,STYLE_DOT);
ObjectSet(Title + "Line-1-4", OBJPROP_WIDTH, 0);
ObjectSet(Title + "Line-1-4",OBJPROP_BACK,0);


// ObjectCreate(Title + "Line-2-5", OBJ_TREND, 0, Time[Wolf[2]],Low[Wolf[2]], Time[Wolf[5]],High[Wolf[5]] );
//// ObjectSet(Title,OBJPROP_STYLE,STYLE_DOT);
// ObjectSet(Title + "Line-2-5",OBJPROP_RAY,0);
// ObjectSet(Title + "Line-2-5", OBJPROP_COLOR, Yellow);
// ObjectSet(Title + "Line-2-5", OBJPROP_WIDTH, 1);
// ObjectSet(Title + "Line-2-5",OBJPROP_BACK,0);


// found=true;
} //else {
//ObjectDelete(Title + "Line-1-3");
ObjectsRedraw();
//}
}
}
return(0);
}
//+------------------------------------------------------------------+
Second one is Targets,mq4

//+------------------------------------------------------------------+
//| Targets.mq4 |
//| // |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Blue
//---- input parameters
extern int barn=300;
extern int Length=6;
//---- buffers
double ExtMapBuffer1[];
//double ExtMapBuffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexEmptyValue(0,0.0);
//SetIndexDrawBegin(0, barn);
//SetIndexStyle(0,DRAW_SECTION);
SetIndexBuffer(0,ExtMapBuffer1);

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{

ObjectDelete("Start line");
ObjectDelete("Stop line");
ObjectDelete("Target1 line");
ObjectDelete("Target2 line");
ObjectDelete("Target3 line");
ObjectDelete("Fantasy line");
ObjectDelete("CrazyDream line");
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
int shift,Swing,Swing_n,uzl,i,zu,zd,mv;
double PointA,PointB,PointC,Target1,Target2,Target3,Fantasy,CrazyDream,Start,Stop;
double LL,HH,BH,BL,NH,NL;
double Uzel[10000][3];
string text;
// loop from first bar to current bar (with shift=0)
Swing_n=0;Swing=0;uzl=0;
BH =High[barn];BL=Low[barn];zu=barn;zd=barn;

for (shift=barn;shift>=0;shift--) {
LL=10000000;HH=-100000000;
for (i=shift+Length;i>=shift+1;i--) {
if (Low< LL) {LL=Low;}
if (High>HH) {HH=High;}
}


if (Low[shift]HH){
Swing=2;
if (Swing_n==1) {zu=shift+1;}
if (Swing_n==-1) {zd=shift+1;}
} else {
if (Low[shift]<LL) {Swing=-1;}
if (High[shift]>HH) {Swing=1;}
}

if (Swing!=Swing_n && Swing_n!=0) {
if (Swing==2) {
Swing=-Swing_n;BH = High[shift];BL = Low[shift];
}
uzl=uzl+1;
if (Swing==1) {
Uzel[uzl][1]=zd;
Uzel[uzl][2]=BL;
}
if (Swing==-1) {
Uzel[uzl][1]=zu;
Uzel[uzl][2]=BH;
}
BH = High[shift];
BL = Low[shift];
}

if (Swing==1) {
if (High[shift]>=BH) {BH=High[shift];zu=shift;}}
if (Swing==-1) {
if (Low[shift]<=BL) {BL=Low[shift]; zd=shift;}}
Swing_n=Swing;
}
for (i=1;i<=uzl;i++) {
//text=DoubleToStr(Uzel[1],0);
//text=;
mv=StrToInteger(DoubleToStr(Uzel[1],0));
//ExtMapBuffer1[mv]=Uzel[2];
}

PointA = Uzel[uzl-2][2];
PointB = Uzel[uzl-1][2];
PointC = Uzel[uzl][2];

Comment(PointA," ",PointB," ",PointC);

Target1=NormalizeDouble((PointB-PointA)*0.618+PointC,4);
Target2=PointB-PointA+PointC;
Target3=NormalizeDouble((PointB-PointA)*1.618+PointC,4);
Fantasy=NormalizeDouble((PointB-PointA)*2.618+PointC,4);
CrazyDream=NormalizeDouble((PointB-PointA)*4.618+PointC,4);
if (PointB<PointC)
{
Start= NormalizeDouble((PointB-PointA)*0.318+PointC,4)-(Ask-Bid);
Stop=PointC+2*(Ask-Bid);
}
if (PointB>PointC)
{
Start= NormalizeDouble((PointB-PointA)*0.318+PointC,4)+(Ask-Bid);
Stop=PointC-2*(Ask-Bid);
}


if (ObjectFind("Start Line") != 0)
{
ObjectCreate("Start line",OBJ_HLINE,0,Time[0],Start);
ObjectSet("Start line",OBJPROP_COLOR,Honeydew);
ObjectSet("Start line",OBJPROP_WIDTH,1);
ObjectSet("Start line",OBJPROP_STYLE,STYLE_DOT);
}
else
{
ObjectMove("Start line", 0,Time[0],Start);
}

if (ObjectFind("Stop Line") != 0)
{
ObjectCreate("Stop line",OBJ_HLINE,0,Time[0],Stop);
ObjectSet("Stop line",OBJPROP_COLOR,Red);
ObjectSet("Stop line",OBJPROP_WIDTH,1);
ObjectSet("Stop line",OBJPROP_STYLE,STYLE_DOT);
}
else
{
ObjectMove("Stop line", 0,Time[0],Stop);
}

if (ObjectFind("Target1 Line") != 0)
{
ObjectCreate("Target1 line",OBJ_HLINE,0,Time[0],Target1);
ObjectSet("Target1 line",OBJPROP_COLOR,Green);
ObjectSet("Target1 line",OBJPROP_WIDTH,1);
ObjectSet("Target1 line",OBJPROP_STYLE,STYLE_DOT);

}
else
{
ObjectMove("Target1 line", 0,Time[0],Target1);
}

if (ObjectFind("Target2 Line") != 0)
{
ObjectCreate("Target2 line",OBJ_HLINE,0,Time[0],Target2);
ObjectSet("Target2 line",OBJPROP_COLOR,Yellow);
ObjectSet("Target2 line",OBJPROP_WIDTH,1);
ObjectSet("Target2 line",OBJPROP_STYLE,STYLE_DOT);

}
else
{
ObjectMove("Target2 line", 0,Time[0],Target2);
}

if (ObjectFind("Target3 Line") != 0)
{
ObjectCreate("Target3 line",OBJ_HLINE,0,Time[0],Target3);
ObjectSet("Target3 line",OBJPROP_COLOR,DarkOrchid);
ObjectSet("Target3 line",OBJPROP_WIDTH,1);
ObjectSet("Target3 line",OBJPROP_STYLE,STYLE_DOT);

}
else
{
ObjectMove("Target3 line", 0,Time[0],Target3);
}

if (ObjectFind("Fantasy Line") != 0)
{
ObjectCreate("Fantasy line",OBJ_HLINE,0,Time[0],Fantasy);
ObjectSet("Fantasy line",OBJPROP_COLOR,Lime);
ObjectSet("Fantasy line",OBJPROP_WIDTH,1);
ObjectSet("Fantasy line",OBJPROP_STYLE,STYLE_DOT);

}
else
{
ObjectMove("Fantasy line", 0,Time[0],Fantasy);
}


if (ObjectFind("CrazyDream Line") != 0)
{
ObjectCreate("CrazyDream line",OBJ_HLINE,0,Time[0],CrazyDream);
ObjectSet("CrazyDream line",OBJPROP_COLOR,DarkTurquoise);
ObjectSet("CrazyDream line",OBJPROP_WIDTH,1);
ObjectSet("CrazyDream line",OBJPROP_STYLE,STYLE_DOT);

}
else
{
ObjectMove("CrazyDream line", 0,Time[0],CrazyDream);
}





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

lex3879
139
lex3879  

VQ indicator




//+------------------------------------------------------------------+
//| VQ.mq4 |
//| Volatility Quality |
//| //+------------------------------------------------------------------+

#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1 Yellow
#property indicator_color2 Green
#property indicator_color3 Red
#property indicator_color4 Yellow
#property indicator_color5 Cyan
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2

extern bool Crash = false;

extern int TimeFrame = 0;
extern int Length = 5;
extern int Method = 3;
extern int Smoothing = 1;
extern int Filter = 5;

extern bool RealTime = true;
extern bool Steady = false;
extern bool Color = true;
extern bool Alerts = true;
extern bool EmailON = false;
extern bool SignalPrice = true;
extern color SignalPriceBUY = Yellow;
extern color SignalPriceSELL = Aqua;
extern int CountBars = 1485;


double VQ[];
double SumVQ[];
double SumVQ_MTF[];
double DIR[];
double UpBuffer[];
double DnBuffer[];
double UpArrow[];
double DnArrow[];

bool TurnedUp = false;
bool TurnedDn = false;
datetime timeprev1=0;
datetime timeprev2=0;
int p=0;
//+------------------------------------------------------------------+

int init()
{
IndicatorBuffers(8);
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID);
SetIndexBuffer(0,SumVQ);
SetIndexStyle(1,DRAW_LINE,STYLE_SOLID);
SetIndexBuffer(1,UpBuffer);
SetIndexStyle(2,DRAW_LINE,STYLE_SOLID);
SetIndexBuffer(2,DnBuffer);
SetIndexStyle(3,DRAW_ARROW);
SetIndexArrow(3,233);
SetIndexBuffer(3,UpArrow);
SetIndexStyle(4,DRAW_ARROW);
SetIndexArrow(4,234);
SetIndexBuffer(4,DnArrow);
SetIndexBuffer(5,VQ);
SetIndexBuffer(6,DIR);
SetIndexBuffer(7,SumVQ_MTF);

if (Length < 2) Length = 2;
if (Method < 0) Method = 0;
if (Method > 3) Method = 3;
if (Smoothing < 0) Smoothing = 0;
if (Filter < 0) Filter = 0;

if ((TimeFrame < Period()) && (TimeFrame != 0)) TimeFrame = Period();
switch(TimeFrame)
{
case 1: string TimeFrameStr = "M1"; break;
case 5: TimeFrameStr = "M5"; break;
case 15: TimeFrameStr = "M15"; break;
case 30: TimeFrameStr = "M30"; break;
case 60: TimeFrameStr = "H1"; break;
case 240: TimeFrameStr = "H4"; break;
case 1440: TimeFrameStr = "D1"; break;
case 10080: TimeFrameStr = "W1"; break;
case 43200: TimeFrameStr = "MN1"; break;
default: TimeFrameStr = "";
}

string short_name = "VQ" + TimeFrameStr + " | " + Length + " , " + Method + " , " + Smoothing + " , " + Filter + " | ";
IndicatorShortName(short_name);

return(0);
}

//+------------------------------------------------------------------+

int start()
{
if (Bars < 100) {IndicatorShortName("Bars less than 100"); return(0);}

if(timeprev1<iTime(NULL,TimeFrame,0)) {TurnedDn = false; TurnedUp = false; timeprev1=iTime(NULL,TimeFrame,0);}

if (!RealTime)
{
if(timeprev2==iTime(NULL,TimeFrame,0)) return(0);
timeprev2=iTime(NULL,TimeFrame,0);
p=TimeFrame/Period()+1; if (p==0) p=1;
}

double TR = 0, MH = 0, ML = 0, MO = 0, MC = 0, MC1 = 0;

if (CountBars>iBars(NULL,TimeFrame) || CountBars>Bars-Length-1) CountBars=MathMin(Bars-Length-1,iBars(NULL,TimeFrame)-Length-1);
if (Crash && CountBars>0){CountBars=CountBars-10; IndicatorShortName("Crash: "+CountBars+" ");}
if (Crash && CountBars<0) IndicatorShortName("Crash");

int i = CountBars;
SumVQ = Close;
SumVQ_MTF = Close;
while (i >= 0)
{
MH = iMA(NULL,TimeFrame,Length,0,Method,PRICE_HIGH,i);
ML = iMA(NULL,TimeFrame,Length,0,Method,PRICE_LOW,i);
MO = iMA(NULL,TimeFrame,Length,0,Method,PRICE_OPEN,i);
MC = iMA(NULL,TimeFrame,Length,0,Method,PRICE_CLOSE,i);
MC1 = iMA(NULL,TimeFrame,Length,0,Method,PRICE_CLOSE,i + Smoothing);
if (Steady==true) {MC=iMA(NULL,TimeFrame,Length,0,Method,PRICE_MEDIAN,i); MC1=iMA(NULL,TimeFrame,Length,0,Method,PRICE_MEDIAN,i+Smoothing);}
VQ = MathAbs(((MC - MC1) / MathMax(MH - ML,MathMax(MH - MC1,MC1 - ML)) + (MC - MO) / (MH - ML)) * 0.5) * ((MC - MC1 + (MC - MO)) * 0.5);
SumVQ = SumVQ + VQ;
if (Filter > 0) if (MathAbs(SumVQ - SumVQ) < Filter * Point) SumVQ = SumVQ;
if (TimeFrame > Period()) SumVQ_MTF = SumVQ;
i--;
}

if (TimeFrame>Period())
{
datetime TimeArray1[];
ArrayCopySeries(TimeArray1,MODE_TIME,Symbol(),TimeFrame);
int limit=CountBars+TimeFrame/Period();
for(i=0, int y=0;i<limit;i++) {if (Time<TimeArray1[y]) y++; SumVQ=SumVQ_MTF[y];}
}


for (i = CountBars; i >= 0; i--)
{
DIR = DIR;
if (SumVQ - SumVQ > 0) DIR = 1;
if (SumVQ - SumVQ > 0) DIR = -1;
if (Color == true)
{
if (DIR > 0)
{
UpBuffer = SumVQ;
if (DIR < 0) UpBuffer = SumVQ;
DnBuffer = EMPTY_VALUE;
}
else
{
if (DIR < 0)
{
DnBuffer = SumVQ;
if (DIR > 0) DnBuffer = SumVQ;
UpBuffer = EMPTY_VALUE;
}
}
}
if (Alerts == true)
{
UpArrow = EMPTY_VALUE; DnArrow = EMPTY_VALUE;
if ((DIR == 1) && (DIR == -1)) UpArrow = SumVQ - (Ask - Bid);
if ((DIR == -1) && (DIR == 1)) DnArrow = SumVQ + (Ask - Bid);
}
}


if (Alerts == true)
{
string AlertTXT;
if (UpArrow[0+p]!=EMPTY_VALUE && TurnedUp==false)
{
AlertTXT="VQ BUY: "+Symbol()+" - "+Period()+" at "+ DoubleToStr(Close[0],Digits)+ " - "+ TimeToStr(CurTime(),TIME_SECONDS);
Alert(AlertTXT); if (EmailON) SendMail(AlertTXT,AlertTXT);
if (SignalPrice == true)
{
ObjectCreate("BUY SIGNAL: " + DoubleToStr(Time[0],0),OBJ_ARROW,0,Time[0],Close[0]);
ObjectSet("BUY SIGNAL: " + DoubleToStr(Time[0],0),OBJPROP_ARROWCODE,5);
ObjectSet("BUY SIGNAL: " + DoubleToStr(Time[0],0),OBJPROP_COLOR,SignalPriceBUY);
}
TurnedDn = false; TurnedUp = true;
}
if (DnArrow[0+p]!=EMPTY_VALUE && TurnedDn==false)
{
AlertTXT="VQ SELL: "+Symbol()+" - "+Period()+" at "+ DoubleToStr(Close[0],Digits)+ " - "+ TimeToStr(CurTime(),TIME_SECONDS);
Alert(AlertTXT); if (EmailON) SendMail(AlertTXT,AlertTXT);
if (SignalPrice == true)
{
ObjectCreate("SELL SIGNAL: " + DoubleToStr(Time[0],0),OBJ_ARROW,0,Time[0],Close[0]);
ObjectSet("SELL SIGNAL: " + DoubleToStr(Time[0],0),OBJPROP_ARROWCODE,5);
ObjectSet("SELL SIGNAL: " + DoubleToStr(Time[0],0),OBJPROP_COLOR,SignalPriceSELL);
}
TurnedUp = false; TurnedDn = true;
}
}


return(0);
}


BUY SELL EVERY ARROQ DRAWN

starfin07
7
starfin07  

please anybody kind me for Converting custom Indicators code to EA code

Thanks ..

//+------------------------------------------------------------------+

//| TMA+CG.mq4 |

//| mladen |

//| arrowse coded acording to idea presented by rajiv |

//+------------------------------------------------------------------+

#property copyright "rajivxxx"

#property link "rajivxxx@gmail.com"

#property indicator_chart_window

#property indicator_buffers 5

#property indicator_color1 DimGray

#property indicator_color2 Red

#property indicator_color3 LimeGreen

#property indicator_color4 Red

#property indicator_color5 Blue

#property indicator_style1 STYLE_DOT

//

//

//

//

//

extern string TimeFrame = "current time frame";

extern int HalfLength = 56;

extern int Price = PRICE_WEIGHTED;

extern double BandsDeviations = 2.5;

extern bool Interpolate = true;

extern bool alertsOn = false;

extern bool alertsOnCurrent = false;

extern bool alertsOnHighLow = true;

extern bool alertsMessage = true;

extern bool alertsSound = false;

extern bool alertsEmail = false;

//

//

//

//

//

double tmBuffer[];

double upBuffer[];

double dnBuffer[];

double wuBuffer[];

double wdBuffer[];

double upArrow[];

double dnArrow[];

//

//

//

//

//

string IndicatorFileName;

bool calculatingTma = false;

bool returningBars = false;

int timeFrame;

//+------------------------------------------------------------------+

//| |

//+------------------------------------------------------------------+

//

//

//

//

int init()

{

timeFrame = stringToTimeFrame(TimeFrame);

HalfLength = MathMax(HalfLength,1);

IndicatorBuffers(7);

SetIndexBuffer(0,tmBuffer); SetIndexDrawBegin(0,HalfLength);

SetIndexBuffer(1,upBuffer); SetIndexDrawBegin(1,HalfLength);

SetIndexBuffer(2,dnBuffer); SetIndexDrawBegin(2,HalfLength);

SetIndexBuffer(3,dnArrow); SetIndexStyle(3,DRAW_ARROW); SetIndexArrow(5,242);

SetIndexBuffer(4,upArrow); SetIndexStyle(4,DRAW_ARROW); SetIndexArrow(6,241);

SetIndexBuffer(5,wuBuffer);

SetIndexBuffer(6,wdBuffer);

if (TimeFrame=="calculateTma") { calculatingTma=true; return(0); }

if (TimeFrame=="returnBars") { returningBars=true; return(0); }

IndicatorFileName = WindowExpertName();

return(0);

}

int deinit() { return(0); }

//+------------------------------------------------------------------+

//| |

//+------------------------------------------------------------------+

//

//

//

//

//

int start()

{

int counted_bars=IndicatorCounted();

int i,limit;

if(counted_bars<0) return(-1);

if(counted_bars>0) counted_bars--;

limit=MathMin(Bars-1,Bars-counted_bars+HalfLength);

if (returningBars) { tmBuffer[0] = limit; return(0); }

if (calculatingTma) { calculateTma(limit); return(0); }

if (timeFrame > Period()) limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,IndicatorFileName,"returnBars",0,0)*timeFrame/Period()));

//

//

//

//

//

for(i = limit; i >= 0; i--)

{

int shift1 = iBarShift(NULL,timeFrame,Time);
datetime time1 = iTime (NULL,timeFrame,shift1);

//
//
//
//
//

tmBuffer = iCustom(NULL,timeFrame,IndicatorFileName,"calculateTma",HalfLength,Price,BandsDeviations,0,shift1);
upBuffer = iCustom(NULL,timeFrame,IndicatorFileName,"calculateTma",HalfLength,Price,BandsDeviations,1,shift1);
dnBuffer = iCustom(NULL,timeFrame,IndicatorFileName,"calculateTma",HalfLength,Price,BandsDeviations,2,shift1);

upArrow = EMPTY_VALUE;
dnArrow = EMPTY_VALUE;
if (High[i+1]>upBuffer[i+1] && Close[i+1]>Open[i+1] && Close<Open) upArrow = High+iATR(NULL,0,20,i);
if ( Low[i+1]<dnBuffer[i+1] && Close[i+1]<Open[i+1] && Close>Open) dnArrow = High-iATR(NULL,0,20,i);

if (timeFrame <= Period() || shift1==iBarShift(NULL,timeFrame,Time[i-1])) continue;
if (!Interpolate) continue;

//
//
//
//
//

for(int n = 1; i+n = time1; n++) continue;
double factor = 1.0 / n;
for(int k = 1; k < n; k++)
{
tmBuffer[i+k] = k*factor*tmBuffer[i+n] + (1.0-k*factor)*tmBuffer;
upBuffer[i+k] = k*factor*upBuffer[i+n] + (1.0-k*factor)*upBuffer;
dnBuffer[i+k] = k*factor*dnBuffer[i+n] + (1.0-k*factor)*dnBuffer;
}
}

//
//
//
//
//

if (alertsOn)
{
if (alertsOnCurrent)
int forBar = 0;
else forBar = 1;
if (alertsOnHighLow)
{
if (High[forBar] > upBuffer[forBar] && High[forBar+1] < upBuffer[forBar+1]) doAlert("high penetrated upper bar");
if (Low[forBar] dnBuffer[forBar+1]) doAlert("low penetrated lower bar");
}
else
{
if (Close[forBar] > upBuffer[forBar] && Close[forBar+1] < upBuffer[forBar+1]) doAlert("close penetrated upper bar");
if (Close[forBar] dnBuffer[forBar+1]) doAlert("close penetrated lower bar");
}
}

return(0);
}

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//

void calculateTma(int limit)
{
int i,j,k;
double FullLength = 2.0*HalfLength+1.0;

//
//
//
//
//

for (i=limit; i>=0; i--)
{
double sum = (HalfLength+1)*iMA(NULL,0,1,0,MODE_SMA,Price,i);
double sumw = (HalfLength+1);
for(j=1, k=HalfLength; j<=HalfLength; j++, k--)
{
sum += k*iMA(NULL,0,1,0,MODE_SMA,Price,i+j);
sumw += k;

if (j<=i)
{
sum += k*iMA(NULL,0,1,0,MODE_SMA,Price,i-j);
sumw += k;
}
}
tmBuffer = sum/sumw;

//
//
//
//
//

double diff = iMA(NULL,0,1,0,MODE_SMA,Price,i)-tmBuffer;
if (i> (Bars-HalfLength-1)) continue;
if (i==(Bars-HalfLength-1))
{
upBuffer = tmBuffer;
dnBuffer = tmBuffer;
if (diff>=0)
{
wuBuffer = MathPow(diff,2);
wdBuffer = 0;
}
else
{
wdBuffer = MathPow(diff,2);
wuBuffer = 0;
}
continue;
}

//
//
//
//
//

if(diff>=0)
{
wuBuffer = (wuBuffer[i+1]*(FullLength-1)+MathPow(diff,2))/FullLength;
wdBuffer = wdBuffer[i+1]*(FullLength-1)/FullLength;
}
else
{
wdBuffer = (wdBuffer[i+1]*(FullLength-1)+MathPow(diff,2))/FullLength;
wuBuffer = wuBuffer[i+1]*(FullLength-1)/FullLength;
}
upBuffer = tmBuffer + BandsDeviations*MathSqrt(wuBuffer);
dnBuffer = tmBuffer - BandsDeviations*MathSqrt(wdBuffer);
}
}



//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//

void doAlert(string doWhat)
{
static string previousAlert="";
static datetime previousTime;
string message;

//
//
//
//
//

if (previousAlert!=doWhat || previousTime!=Time[0])
{
previousAlert = doWhat;
previousTime = Time[0];

message= StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," THA : ",doWhat);
if (alertsMessage) Alert(message);
if (alertsEmail) SendMail(StringConcatenate(Symbol(),"TMA "),message);
if (alertsSound) PlaySound("alert2.wav");
}
}

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
for(int l = StringLen(tfs)-1; l >= 0; l--)
{
int char = StringGetChar(tfs,l);
if((char > 96 && char 223 && char < 256))
tfs = StringSetChar(tfs, 1, char - 32);
else
if(char > -33 && char < 0)
tfs = StringSetChar(tfs, 1, char + 224);
}
int tf=0;
if (tfs=="M1" || tfs=="1") tf=PERIOD_M1;
if (tfs=="M5" || tfs=="5") tf=PERIOD_M5;
if (tfs=="M15"|| tfs=="15") tf=PERIOD_M15;
if (tfs=="M30"|| tfs=="30") tf=PERIOD_M30;
if (tfs=="H1" || tfs=="60") tf=PERIOD_H1;
if (tfs=="H4" || tfs=="240") tf=PERIOD_H4;
if (tfs=="D1" || tfs=="1440") tf=PERIOD_D1;
if (tfs=="W1" || tfs=="10080") tf=PERIOD_W1;
if (tfs=="MN" || tfs=="43200") tf=PERIOD_MN1;
if (tf==0 || tf<Period()) tf=Period();
return(tf);
}

Files:
MY_TMA.ex4 13 kb
To add comments, please log in or register