Coding help - page 320

 

Hi there,

Long time i was not around here...

I am working on an EA Idea, i need to add an indicator but i don't know how to include this into the EA, can you help me please?

Indicator is Slope Directional Line, i want it to work in daily chart and to trigger orders in lower TF chart, code of indicator is:

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

//| slope directional line D.L.M.mq4 |

//| DANIEL LUCHINGER 2014 |

//| MQL5: automated forex trading, strategy tester and custom indicators with MetaTrader |

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

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_color1 Blue

#property indicator_width1 3

#property indicator_color2 Red

#property indicator_width2 3

//---- input parameters

extern int period=14;

extern int method=3; // MODE_SMA

extern int price=0; // PRICE_CLOSE

//---- buffers

double Uptrend[];

double Dntrend[];

double ExtMapBuffer[];

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

//| Custom indicator initialization function |

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

int init()

{

IndicatorBuffers(3);

SetIndexBuffer(0, Uptrend);

SetIndexBuffer(1, Dntrend);

SetIndexBuffer(2, ExtMapBuffer);

ArraySetAsSeries(ExtMapBuffer, true);

SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,3);

SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,3);

IndicatorShortName("Slope Direction Line");

return(0);

}

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

//| Custom indicator deinitialization function |

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

int deinit(){return(0);}

double WMA(int x, int p)

{

return(iMA(NULL, 0, p, 0, method, price, x));

}

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

//| Custom indicator iteration function |

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

int start()

{

int counted_bars = IndicatorCounted();

if(counted_bars < 0)

return(-1);

int x = 0;

int p = MathSqrt(period);

int e = Bars - counted_bars + period + 1;

double vect[], trend[];

if(e > Bars)

e = Bars;

ArrayResize(vect, e);

ArraySetAsSeries(vect, true);

ArrayResize(trend, e);

ArraySetAsSeries(trend, true);

for(x = 0; x < e; x++)

{

vect[x] = 2*WMA(x, period/2) - WMA(x, period);

}

for(x = 0; x < e-period; x++)

ExtMapBuffer[x] = iMAOnArray(vect, 0, p, 0, method, x);

for(x = e-period; x >= 0; x--)

{

trend[x] = trend[x+1];

if (ExtMapBuffer[x]> ExtMapBuffer[x+1]) trend[x] =1;

if (ExtMapBuffer[x]< ExtMapBuffer[x+1]) trend[x] =-1;

if (trend[x]>0)

{ Uptrend[x] = ExtMapBuffer[x];

if (trend[x+1]<0) Uptrend[x+1]=ExtMapBuffer[x+1];

Dntrend[x] = EMPTY_VALUE;

}

else

if (trend[x]<0)

{

Dntrend[x] = ExtMapBuffer[x];

if (trend[x+1]>0) Dntrend[x+1]=ExtMapBuffer[x+1];

Uptrend[x] = EMPTY_VALUE;

}

}

return(0);

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

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

I have coded the following way:

double UpTrend = iCustom(NULL,1440,"slope directional line D.L.M",20,3,0,1); // where 0 = UptrendBuffer

double DnTrend = iCustom(NULL,1440,"slope directional line D.L.M",20,3,1,1); // where 1 = DntrendBuffer

double _sl = 0,_tp = 0;

{

if(haMode == 0 || haMode == 1)

{

if(UpTrend)

{label("LONG Condition OK");

if(Close[0] >= srDown && Close[0] < srUp)

//if(StopLoss > 0) _sl = Ask - StopLoss;

_sl = srDownHT-StopLossShift;

if(TakeProfit > 0) _tp = Ask + TakeProfit;

if(tt && orders<MaxTrades)

{

openOrder(getLots(),MagicNumber,OP_BUY, _sl,_tp);

}

}}

}

}

Can somebody correct me please because i'm shure this is not working

Thanks

Daniel1983

 

Thank you very much Mistertools for your help.

But ...

What I want to do is applying a moving average to the #_FullSSA_normalize correct mtf nmc.

This works without a problem when I do this on the M5 in a normal way with the help of the MA of MT4.

It's the only thing I use from MT4 ...

On the M1 it sometimes works, other times not. It might have to do something with the settings.

Just look at the picture, the SSA is ok, but not the MA ...

I have been trying to do it my self, but it's far too complicated for me, although I only got two errors, after 'my work' ..

Must have been lucky, but nevertheless, the SSA worked without a problem, but the MA was nowhere to be seen.

Of course, the SSA is an elite one ....

which I bought some time ago and it's worth it ...

Files:
ssa.png  72 kb
 
daniel1983:
Hi there,

Long time i was not around here...

I am working on an EA Idea, i need to add an indicator but i don't know how to include this into the EA, can you help me please?

Indicator is Slope Directional Line, i want it to work in daily chart and to trigger orders in lower TF chart, code of indicator is:

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

//| slope directional line D.L.M.mq4 |

//| DANIEL LUCHINGER 2014 |

//| MQL5: automated forex trading, strategy tester and custom indicators with MetaTrader |

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

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_color1 Blue

#property indicator_width1 3

#property indicator_color2 Red

#property indicator_width2 3

//---- input parameters

extern int period=14;

extern int method=3; // MODE_SMA

extern int price=0; // PRICE_CLOSE

//---- buffers

double Uptrend[];

double Dntrend[];

double ExtMapBuffer[];

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

//| Custom indicator initialization function |

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

int init()

{

IndicatorBuffers(3);

SetIndexBuffer(0, Uptrend);

SetIndexBuffer(1, Dntrend);

SetIndexBuffer(2, ExtMapBuffer);

ArraySetAsSeries(ExtMapBuffer, true);

SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,3);

SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,3);

IndicatorShortName("Slope Direction Line");

return(0);

}

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

//| Custom indicator deinitialization function |

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

int deinit(){return(0);}

double WMA(int x, int p)

{

return(iMA(NULL, 0, p, 0, method, price, x));

}

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

//| Custom indicator iteration function |

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

int start()

{

int counted_bars = IndicatorCounted();

if(counted_bars < 0)

return(-1);

int x = 0;

int p = MathSqrt(period);

int e = Bars - counted_bars + period + 1;

double vect[], trend[];

if(e > Bars)

e = Bars;

ArrayResize(vect, e);

ArraySetAsSeries(vect, true);

ArrayResize(trend, e);

ArraySetAsSeries(trend, true);

for(x = 0; x < e; x++)

{

vect[x] = 2*WMA(x, period/2) - WMA(x, period);

}

for(x = 0; x < e-period; x++)

ExtMapBuffer[x] = iMAOnArray(vect, 0, p, 0, method, x);

for(x = e-period; x >= 0; x--)

{

trend[x] = trend[x+1];

if (ExtMapBuffer[x]> ExtMapBuffer[x+1]) trend[x] =1;

if (ExtMapBuffer[x]< ExtMapBuffer[x+1]) trend[x] =-1;

if (trend[x]>0)

{ Uptrend[x] = ExtMapBuffer[x];

if (trend[x+1]<0) Uptrend[x+1]=ExtMapBuffer[x+1];

Dntrend[x] = EMPTY_VALUE;

}

else

if (trend[x]<0)

{

Dntrend[x] = ExtMapBuffer[x];

if (trend[x+1]>0) Dntrend[x+1]=ExtMapBuffer[x+1];

Uptrend[x] = EMPTY_VALUE;

}

}

return(0);

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

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

I have coded the following way:

double UpTrend = iCustom(NULL,1440,"slope directional line D.L.M",20,3,0,1); // where 0 = UptrendBuffer

double DnTrend = iCustom(NULL,1440,"slope directional line D.L.M",20,3,1,1); // where 1 = DntrendBuffer

double _sl = 0,_tp = 0;

{

if(haMode == 0 || haMode == 1)

{

if(UpTrend)

{label("LONG Condition OK");

if(Close[0] >= srDown && Close[0] < srUp)

//if(StopLoss > 0) _sl = Ask - StopLoss;

_sl = srDownHT-StopLossShift;

if(TakeProfit > 0) _tp = Ask + TakeProfit;

if(tt && orders<MaxTrades)

{

openOrder(getLots(),MagicNumber,OP_BUY, _sl,_tp);

}

}}

}

}

Can somebody correct me please because i'm shure this is not working

Thanks

Daniel1983

That is not working simply because slope directional line is a repainting indicator. Take any normal Hull average ("slope directional line" is Hull average) and all will be easier

 

Thank you Mladen, i appreciate your time..

I found through the posts about HMA, one you have created with speed control,

i would like to try it into the EA, the same idea as i wanted with the Slope Directional Indicator,

but with this HMA indicator you posted "Hull Moving Averages 2.0 &SR lines"

how would be the code for this to make it work in my EA?

I wrote:

input int IndicatorTF = 1440 //(indicator in Daily TimeFrame)

input int HMAPeriod = 35; //(allow me to change this parameters)

input ENUM_HMAPrice = PRICE_CLOSE; //(allow me to change this parameters)

input int HMASpeed = 2.0; //(allow me to change this parameters)

double trendUp = iCustom(Symbol(),IndicatorTF,"Hull moving average 2.0 & sr lines",HMAPeriod,ENUM_HMAPrice,HMASpeed,0,0);

double trendDn = iCustom(Symbol(),IndicatorTF,"Hull moving average 2.0 & sr lines",HMAPeriod,ENUM_HMAPrice,HMASpeed,1,0);

if(trendUp > 0)

{

Open Buy

}

Indicator code is:

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

//| Hull moving average |

//| mladen |

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

#property copyright "www.forex-tsd.com"

#property link "www.forex-tsd.com"

#property indicator_chart_window

#property indicator_buffers 3

#property indicator_color1 LimeGreen

#property indicator_color2 PaleVioletRed

#property indicator_color3 PaleVioletRed

#property indicator_width1 2

#property indicator_width2 2

#property indicator_width3 2

//

//

//

//

//

extern string TimeFrame = "Current time frame";

extern int HMAPeriod = 35;

extern int HMAPrice = PRICE_CLOSE;

extern double HMASpeed = 2.0;

extern bool LinesVisible = false;

extern int LinesNumber = 5;

extern color ColorUp = LimeGreen;

extern color ColorDown = PaleVioletRed;

extern string UniqueID = "HullLines1";

extern bool alertsOn = false;

extern bool alertsOnCurrent = true;

extern bool alertsMessage = true;

extern bool alertsSound = false;

extern bool alertsEmail = false;

//

//

//

//

//

double hma[];

double hmada[];

double hmadb[];

double work[];

double trend[];

int HalfPeriod;

int HullPeriod;

string indicatorFileName;

bool returnBars;

bool calculateValue;

int timeFrame;

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

//

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

//

//

//

//

//

int init()

{

IndicatorBuffers(5);

SetIndexBuffer(0,hma);

SetIndexBuffer(1,hmada);

SetIndexBuffer(2,hmadb);

SetIndexBuffer(3,trend);

SetIndexBuffer(4,work);

//

//

//

//

//

HMAPeriod = MathMax(2,HMAPeriod);

HalfPeriod = MathFloor(HMAPeriod/HMASpeed);

HullPeriod = MathFloor(MathSqrt(HMAPeriod));

indicatorFileName = WindowExpertName();

calculateValue = TimeFrame=="calculateValue"; if (calculateValue) { return(0); }

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

timeFrame = stringToTimeFrame(TimeFrame);

//

//

//

//

//

IndicatorShortName(timeFrameToString(timeFrame)+" HMA ("+HMAPeriod+")");

return(0);

}

void deinit()

{

deleteLines();

}

void deleteLines()

{

int lookForLength = StringLen(UniqueID);

for (int i= ObjectsTotal()-1; i>=0; i--)

{

string name = ObjectName(i);

if (StringSubstr(name,0,lookForLength)==UniqueID) ObjectDelete(name);

}

}

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

//

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

//

//

//

//

//

int start()

{

int i,counted_bars = IndicatorCounted();

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

if(counted_bars>0) counted_bars--;

int limit=MathMin(Bars-counted_bars,Bars-1);

if (returnBars) { hma[0] = MathMin(limit+1,Bars-1); return(0); }

//

//

//

//

//

if (calculateValue || timeFrame == Period())

{

if (trend[limit] == -1) CleanPoint(limit,hmada,hmadb);

for(i=limit; i>=0; i--) work = 2.0*iMA(NULL,0,HalfPeriod,0,MODE_LWMA,HMAPrice,i)-iMA(NULL,0,HMAPeriod,0,MODE_LWMA,HMAPrice,i);

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

{

hma = iMAOnArray(work,0,HullPeriod,0,MODE_LWMA,i);

hmada = EMPTY_VALUE;

hmadb = EMPTY_VALUE;

trend = trend;

if (hma > hma) trend = 1;

if (hma < hma) trend = -1;

if (trend == -1) PlotPoint(i,hmada,hmadb,hma);

}

deleteLines();

if (LinesVisible)

{

int k = 0;

for (i=0; i<Bars && k<LinesNumber; i++)

if (trend!=trend)

{

string name = UniqueID+(string)Time;

ObjectCreate(name,OBJ_TREND,0,Time,hma,Time+Period()*60,hma);

if (trend==1)

ObjectSet(name,OBJPROP_COLOR,ColorUp);

else ObjectSet(name,OBJPROP_COLOR,ColorDown);

k++;

}

}

manageAlerts();

return(0);

}

//

//

//

//

//

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

if (trend[limit]==-1) CleanPoint(limit,hmada,hmadb);

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

{

int y = iBarShift(NULL,timeFrame,Time);

trend = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",HMAPeriod,HMAPrice,HMASpeed,LinesVisible,LinesNumber,ColorUp,ColorDown,UniqueID,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,3,y);

hma = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",HMAPeriod,HMAPrice,HMASpeed,LinesVisible,LinesNumber,ColorUp,ColorDown,UniqueID,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,0,y);

hmada = EMPTY_VALUE;

hmadb = EMPTY_VALUE;

}

for (i=limit;i>=0;i--) if (trend==-1) PlotPoint(i,hmada,hmadb,hma);

return(0);

}

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

//|

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

//

//

//

//

//

void manageAlerts()

{

if (alertsOn)

{

if (alertsOnCurrent)

int whichBar = 0;

else whichBar = 1;

if (trend[whichBar] != trend[whichBar+1])

{

if (trend[whichBar] == 1) doAlert(whichBar,"up");

if (trend[whichBar] == -1) doAlert(whichBar,"down");

}

}

}

//

//

//

//

//

void doAlert(int forBar, string doWhat)

{

static string previousAlert="nothing";

static datetime previousTime;

string message;

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

previousAlert = doWhat;

previousTime = Time[forBar];

//

//

//

//

//

message = Symbol()+" "+timeFrameToString(Period())+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" HMA trend changed to "+doWhat;

if (alertsMessage) Alert(message);

if (alertsEmail) SendMail(Symbol()+" HMA ",message);

if (alertsSound) PlaySound("alert2.wav");

}

}

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

//

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

//

//

//

//

//

void CleanPoint(int i,double& first[],double& second[])

{

if ((second != EMPTY_VALUE) && (second != EMPTY_VALUE))

second = EMPTY_VALUE;

else

if ((first != EMPTY_VALUE) && (first != EMPTY_VALUE) && (first == EMPTY_VALUE))

first = EMPTY_VALUE;

}

//

//

//

//

//

void PlotPoint(int i,double& first[],double& second[],double& from[])

{

if (first == EMPTY_VALUE)

{

if (first == EMPTY_VALUE) {

first = from;

first = from;

second = EMPTY_VALUE;

}

else {

second = from;

second = from;

first = EMPTY_VALUE;

}

}

else

{

first = from;

second = EMPTY_VALUE;

}

}

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

//

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

//

//

//

//

//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};

int iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//

//

//

//

//

int stringToTimeFrame(string tfs)

{

tfs = stringUpperCase(tfs);

for (int i=ArraySize(iTfTable)-1; i>=0; i--)

if (tfs==sTfTable || tfs==""+iTfTable) return(MathMax(iTfTable,Period()));

return(Period());

}

string timeFrameToString(int tf)

{

for (int i=ArraySize(iTfTable)-1; i>=0; i--)

if (tf==iTfTable) return(sTfTable);

return("");

}

//

//

//

//

//

string stringUpperCase(string str)

{

string s = str;

for (int length=StringLen(str)-1; length>=0; length--)

{

int tchar = StringGetChar(s, length);

if((tchar > 96 && tchar 223 && tchar < 256))

s = StringSetChar(s, length, tchar - 32);

else if(tchar > -33 && tchar < 0)

s = StringSetChar(s, length, tchar + 224);

}

return(s);

}

 
mladen:
It is not a problem of other indicator interfering (unless you are using multiple instances of that same indicator on the same chart). In that case it can heppen since that indicator is not written to be used as multi instance indicator on the same chart

Hello MLaden , thank you so much but it is not a case where I'm using more than one instance on a chart, only one. just lines are shifting over .

if its not obvious don't worry I will keep as is, thanks for your input! have a nice day...

zigflip

 
zigflip:
Hello MLaden , thank you so much but it is not a case where I'm using more than one instance on a chart, only one. just lines are shifting over .

if its not obvious don't worry I will keep as is, thanks for your input! have a nice day...

zigflip

I did try the one that I posted and they were not shifting

Try it out, maybe it helps

 

How do i call an HMA indicator into an EA?

double (NULL,0,"HMA",??????)

And conditions are

if(trendUp) then buy, or if(trendUp =1) then buy or something like this?

thanks

 
daniel1983:
How do i call an HMA indicator into an EA?

double (NULL,0,"HMA",??????)

And conditions are

if(trendUp) then buy, or if(trendUp =1) then buy or something like this?

thanks

Depends on indicators parameters, but you have to use iCustom() for that

Much more information with quite a few examples you can find here : https://www.mql5.com/en/forum/173108

 
mladen:
Depends on indicators parameters, but you have to use iCustom() for that Much more information with quite a few examples you can find here : https://www.mql5.com/en/forum/173108

THank for guiding me, i still am confused, i found an HMA indicator similar code to slope line code, it is "HMA_Russian_Color", but i still have problems with the conditions, for filtering buy trend from sell trend.

i wrote

double Uptrend = iCustom(Symbol(),0,"HMA_Russian_Color",20,3,0,0); // where last 0 = UptrendBuffer

double Dntrend = iCustom(Symbol(),0,"HMA_Russian_Color",20,3,0,1); // where last 1 = DntrendBuffer

//period = 20 / type = 3 / price = 0 is correct?

if(Uptrend) //How do i write this??

{

Open Buy

}

Orders Buy still opened while HMA downtrend or red color ...

Please correct me, and or help me fix this...

Thanks

 
daniel1983:
THank for guiding me, i still am confused, i found an HMA indicator similar code to slope line code, it is "HMA_Russian_Color", but i still have problems with the conditions, for filtering buy trend from sell trend.

i wrote

double Uptrend = iCustom(Symbol(),0,"HMA_Russian_Color",20,3,0,0); // where last 0 = UptrendBuffer

double Dntrend = iCustom(Symbol(),0,"HMA_Russian_Color",20,3,0,1); // where last 1 = DntrendBuffer

//period = 20 / type = 3 / price = 0 is correct?

if(Uptrend) //How do i write this??

{

Open Buy

}

Orders Buy still opened while HMA downtrend or red color ...

Please correct me, and or help me fix this...

Thanks

For slope change try something like this :

double current = iCustom(Symbol(),0,"HMA_Russian_Color",20,3,0,0);

double previous1 = iCustom(Symbol(),0,"HMA_Russian_Color",20,3,0,1);

double previous2 = iCustom(Symbol(),0,"HMA_Russian_Color",20,3,0,2);

if (current>previous1 && previous1<previous2)

{

// slope changed to up

}

if (currentprevious2)

{

// slope changed to down

}