Metatrader 5 coding questions / issues - page 15

 

Thank you for replying mladen. I don't think I can change the arrow type as I wanted, so I changed things to use nine buffers instead then, but nothing is displayed still, and I don't know why. Please could you have a look? Watching the variables in the debugger, I know the function CandleSentiment() definitely returns the values correctly, so it must be something else in the structure of my code. Also, I've read before that I shouldn't really be setting the price arrays as series, but I've gotten really confused now with which direction the buffers, price arrays, and candles are numbered from. For the candles, is it now left to right (oldest to newest) from zero? Do the price arrays by default match this? How would I do what I want without setting the arrays as series?

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

//| CandleSentiment.mq5 |

//| whitebloodcell |

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

#property copyright "whitebloodcell"

#property link "tmclayson@gmail.com"

#property version "1.00"

#property indicator_separate_window

#property indicator_buffers 9

#property indicator_plots 9

#property indicator_maximum 5

#property indicator_minimum -5

#property indicator_width1 2

#property indicator_label1 "HighCloseBullPattern"

#property indicator_label2 "MidCloseBullPattern"

#property indicator_label3 "LowCloseBullPattern"

#property indicator_label4 "HighCloseRangePattern"

#property indicator_label5 "MidCloseRangePattern"

#property indicator_label6 "LowCloseRangePattern"

#property indicator_label7 "HighCloseBearPattern"

#property indicator_label8 "MidCloseBearPattern"

#property indicator_label9 "LowCloseBearPattern"

double HighCloseBullPattern[];

double MidCloseBullPattern[];

double LowCloseBullPattern[];

double HighCloseRangePattern[];

double MidCloseRangePattern[];

double LowCloseRangePattern[];

double HighCloseBearPattern[];

double MidCloseBearPattern[];

double LowCloseBearPattern[];

#include

bool FirstRun = true;

double RangePercent;

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

//| Initialization

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

int OnInit() {

//--- name for DataWindow

IndicatorSetString(INDICATOR_SHORTNAME,"CandleSentiment("+_Symbol+")");

//--- Not sure if it was being calculated when declared as a global

RangePercent = 1.0/3.0;

//--- indicator buffers mapping

SetIndexBuffer(0,HighCloseBullPattern,INDICATOR_DATA);

SetIndexBuffer(1,MidCloseBullPattern,INDICATOR_DATA);

SetIndexBuffer(2,LowCloseBullPattern,INDICATOR_DATA);

SetIndexBuffer(3,HighCloseRangePattern,INDICATOR_DATA);

SetIndexBuffer(4,MidCloseRangePattern,INDICATOR_DATA);

SetIndexBuffer(5,LowCloseRangePattern,INDICATOR_DATA);

SetIndexBuffer(6,HighCloseBearPattern,INDICATOR_DATA);

SetIndexBuffer(7,MidCloseBearPattern,INDICATOR_DATA);

SetIndexBuffer(8,LowCloseBearPattern,INDICATOR_DATA);

//--- indicator arrow styles

PlotIndexSetInteger(0,PLOT_ARROW,241);//Up

PlotIndexSetInteger(1,PLOT_ARROW,246);//Diagonal Up

PlotIndexSetInteger(3,PLOT_ARROW,240);//Sidewards

PlotIndexSetInteger(4,PLOT_ARROW,244);// Up Down

PlotIndexSetInteger(5,PLOT_ARROW,244);// Up Down

PlotIndexSetInteger(5,PLOT_ARROW,244);// Up Down

PlotIndexSetInteger(6,PLOT_ARROW,240);//Sidewards

PlotIndexSetInteger(7,PLOT_ARROW,248);//Diagonal Down

PlotIndexSetInteger(8,PLOT_ARROW,242);//Down

//---- sets drawing line empty value--

PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);

PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);

PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,EMPTY_VALUE);

PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,EMPTY_VALUE);

PlotIndexSetDouble(4,PLOT_EMPTY_VALUE,EMPTY_VALUE);

PlotIndexSetDouble(5,PLOT_EMPTY_VALUE,EMPTY_VALUE);

PlotIndexSetDouble(6,PLOT_EMPTY_VALUE,EMPTY_VALUE);

PlotIndexSetDouble(7,PLOT_EMPTY_VALUE,EMPTY_VALUE);

PlotIndexSetDouble(8,PLOT_EMPTY_VALUE,EMPTY_VALUE);

//--- set accuracy

IndicatorSetInteger(INDICATOR_DIGITS,1);

//--- sets first bar from what index will be drawn

PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,1);

PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,1);

PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,1);

PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,1);

PlotIndexSetInteger(4,PLOT_DRAW_BEGIN,1);

PlotIndexSetInteger(5,PLOT_DRAW_BEGIN,1);

PlotIndexSetInteger(6,PLOT_DRAW_BEGIN,1);

PlotIndexSetInteger(7,PLOT_DRAW_BEGIN,1);

PlotIndexSetInteger(8,PLOT_DRAW_BEGIN,1);

//--- return if completed successfully.

return(INIT_SUCCEEDED);

}

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

//| Main Indicator Logic

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

int OnCalculate(const int _BarsTotal,

const int _BarsCalculated,

const datetime &T[],

const double &O[],

const double &H[],

const double &L[],

const double &C[],

const long &TV[],

const long &V[],

const int &S[]) {

int StartIndex = MathMax(_BarsCalculated-1,1);

int FinishIndex =_BarsTotal-1;

int Sentiment;

if(_BarsTotal<FinishIndex) {

return(0);

}

else {

if(FirstRun) {

ArrayInitialize(HighCloseBullPattern,EMPTY_VALUE);

ArrayInitialize(MidCloseBullPattern,EMPTY_VALUE);

ArrayInitialize(LowCloseBullPattern,EMPTY_VALUE);

ArrayInitialize(HighCloseRangePattern,EMPTY_VALUE);

ArrayInitialize(MidCloseRangePattern,EMPTY_VALUE);

ArrayInitialize(LowCloseRangePattern,EMPTY_VALUE);

ArrayInitialize(HighCloseBearPattern,EMPTY_VALUE);

ArrayInitialize(MidCloseBearPattern,EMPTY_VALUE);

ArrayInitialize(LowCloseBearPattern,EMPTY_VALUE);

// Set the price arrays to match with the indicator arrays

ArraySetAsSeries(T,true);

ArraySetAsSeries(O,true);

ArraySetAsSeries(H,true);

ArraySetAsSeries(L,true);

ArraySetAsSeries(C,true);

ArraySetAsSeries(TV,true);

ArraySetAsSeries(V,true);

ArraySetAsSeries(S,true);

FirstRun=false;

}

}

for(int Bar=StartIndex; Bar<FinishIndex; Bar++) {

Sentiment = CandleSentiment(Bar,Bar-1,RangePercent,RangePercent,O,H,L,C);

switch(Sentiment) {

case 4:

HighCloseBullPattern=Sentiment;

break;

case 3:

MidCloseBullPattern=Sentiment;

break;

case 2:

LowCloseBullPattern=Sentiment;a

break;

case 1:

HighCloseRangePattern=Sentiment;

break;

case 0:

MidCloseRangePattern=Sentiment;

break;

case -1:

LowCloseRangePattern=Sentiment;

break;

case -2:

HighCloseBearPattern=Sentiment;

break;

case -3:

MidCloseBearPattern=Sentiment;

break;

case -4:

LowCloseBearPattern=Sentiment;

break;

default: Print("Unexpected Sentiment"); break;

}

}

//--- return value of prev_calculated for next call

return(_BarsCalculated);

}
 
whitebloodcell:
Thank you for replying mladen. I don't think I can change the arrow type as I wanted, so I changed things to use nine buffers instead then, but nothing is displayed still, and I don't know why. Please could you have a look? Watching the variables in the debugger, I know the function CandleSentiment() definitely returns the values correctly, so it must be something else in the structure of my code. Also, I've read before that I shouldn't really be setting the price arrays as series, but I've gotten really confused now with which direction the buffers, price arrays, and candles are numbered from. For the candles, is it now left to right (oldest to newest) from zero? Do the price arrays by default match this? How would I do what I want without setting the arrays as series?
//+------------------------------------------------------------------+

//| CandleSentiment.mq5 |

//| whitebloodcell |

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

#property copyright "whitebloodcell"

#property link "tmclayson@gmail.com"

#property version "1.00"

#property indicator_separate_window

#property indicator_buffers 9

#property indicator_plots 9

#property indicator_maximum 5

#property indicator_minimum -5

#property indicator_width1 2

#property indicator_label1 "HighCloseBullPattern"

#property indicator_label2 "MidCloseBullPattern"

#property indicator_label3 "LowCloseBullPattern"

#property indicator_label4 "HighCloseRangePattern"

#property indicator_label5 "MidCloseRangePattern"

#property indicator_label6 "LowCloseRangePattern"

#property indicator_label7 "HighCloseBearPattern"

#property indicator_label8 "MidCloseBearPattern"

#property indicator_label9 "LowCloseBearPattern"

double HighCloseBullPattern[];

double MidCloseBullPattern[];

double LowCloseBullPattern[];

double HighCloseRangePattern[];

double MidCloseRangePattern[];

double LowCloseRangePattern[];

double HighCloseBearPattern[];

double MidCloseBearPattern[];

double LowCloseBearPattern[];

#include

bool FirstRun = true;

double RangePercent;

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

//| Initialization

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

int OnInit() {

//--- name for DataWindow

IndicatorSetString(INDICATOR_SHORTNAME,"CandleSentiment("+_Symbol+")");

//--- Not sure if it was being calculated when declared as a global

RangePercent = 1.0/3.0;

//--- indicator buffers mapping

SetIndexBuffer(0,HighCloseBullPattern,INDICATOR_DATA);

SetIndexBuffer(1,MidCloseBullPattern,INDICATOR_DATA);

SetIndexBuffer(2,LowCloseBullPattern,INDICATOR_DATA);

SetIndexBuffer(3,HighCloseRangePattern,INDICATOR_DATA);

SetIndexBuffer(4,MidCloseRangePattern,INDICATOR_DATA);

SetIndexBuffer(5,LowCloseRangePattern,INDICATOR_DATA);

SetIndexBuffer(6,HighCloseBearPattern,INDICATOR_DATA);

SetIndexBuffer(7,MidCloseBearPattern,INDICATOR_DATA);

SetIndexBuffer(8,LowCloseBearPattern,INDICATOR_DATA);

//--- indicator arrow styles

PlotIndexSetInteger(0,PLOT_ARROW,241);//Up

PlotIndexSetInteger(1,PLOT_ARROW,246);//Diagonal Up

PlotIndexSetInteger(3,PLOT_ARROW,240);//Sidewards

PlotIndexSetInteger(4,PLOT_ARROW,244);// Up Down

PlotIndexSetInteger(5,PLOT_ARROW,244);// Up Down

PlotIndexSetInteger(5,PLOT_ARROW,244);// Up Down

PlotIndexSetInteger(6,PLOT_ARROW,240);//Sidewards

PlotIndexSetInteger(7,PLOT_ARROW,248);//Diagonal Down

PlotIndexSetInteger(8,PLOT_ARROW,242);//Down

//---- sets drawing line empty value--

PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);

PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);

PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,EMPTY_VALUE);

PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,EMPTY_VALUE);

PlotIndexSetDouble(4,PLOT_EMPTY_VALUE,EMPTY_VALUE);

PlotIndexSetDouble(5,PLOT_EMPTY_VALUE,EMPTY_VALUE);

PlotIndexSetDouble(6,PLOT_EMPTY_VALUE,EMPTY_VALUE);

PlotIndexSetDouble(7,PLOT_EMPTY_VALUE,EMPTY_VALUE);

PlotIndexSetDouble(8,PLOT_EMPTY_VALUE,EMPTY_VALUE);

//--- set accuracy

IndicatorSetInteger(INDICATOR_DIGITS,1);

//--- sets first bar from what index will be drawn

PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,1);

PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,1);

PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,1);

PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,1);

PlotIndexSetInteger(4,PLOT_DRAW_BEGIN,1);

PlotIndexSetInteger(5,PLOT_DRAW_BEGIN,1);

PlotIndexSetInteger(6,PLOT_DRAW_BEGIN,1);

PlotIndexSetInteger(7,PLOT_DRAW_BEGIN,1);

PlotIndexSetInteger(8,PLOT_DRAW_BEGIN,1);

//--- return if completed successfully.

return(INIT_SUCCEEDED);

}

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

//| Main Indicator Logic

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

int OnCalculate(const int _BarsTotal,

const int _BarsCalculated,

const datetime &T[],

const double &O[],

const double &H[],

const double &L[],

const double &C[],

const long &TV[],

const long &V[],

const int &S[]) {

int StartIndex = MathMax(_BarsCalculated-1,1);

int FinishIndex =_BarsTotal-1;

int Sentiment;

if(_BarsTotal<FinishIndex) {

return(0);

}

else {

if(FirstRun) {

ArrayInitialize(HighCloseBullPattern,EMPTY_VALUE);

ArrayInitialize(MidCloseBullPattern,EMPTY_VALUE);

ArrayInitialize(LowCloseBullPattern,EMPTY_VALUE);

ArrayInitialize(HighCloseRangePattern,EMPTY_VALUE);

ArrayInitialize(MidCloseRangePattern,EMPTY_VALUE);

ArrayInitialize(LowCloseRangePattern,EMPTY_VALUE);

ArrayInitialize(HighCloseBearPattern,EMPTY_VALUE);

ArrayInitialize(MidCloseBearPattern,EMPTY_VALUE);

ArrayInitialize(LowCloseBearPattern,EMPTY_VALUE);

// Set the price arrays to match with the indicator arrays

ArraySetAsSeries(T,true);

ArraySetAsSeries(O,true);

ArraySetAsSeries(H,true);

ArraySetAsSeries(L,true);

ArraySetAsSeries(C,true);

ArraySetAsSeries(TV,true);

ArraySetAsSeries(V,true);

ArraySetAsSeries(S,true);

FirstRun=false;

}

}

for(int Bar=StartIndex; Bar<FinishIndex; Bar++) {

Sentiment = CandleSentiment(Bar,Bar-1,RangePercent,RangePercent,O,H,L,C);

switch(Sentiment) {

case 4:

HighCloseBullPattern=Sentiment;

break;

case 3:

MidCloseBullPattern=Sentiment;

break;

case 2:

LowCloseBullPattern=Sentiment;a

break;

case 1:

HighCloseRangePattern=Sentiment;

break;

case 0:

MidCloseRangePattern=Sentiment;

break;

case -1:

LowCloseRangePattern=Sentiment;

break;

case -2:

HighCloseBearPattern=Sentiment;

break;

case -3:

MidCloseBearPattern=Sentiment;

break;

case -4:

LowCloseBearPattern=Sentiment;

break;

default: Print("Unexpected Sentiment"); break;

}

}

//--- return value of prev_calculated for next call

return(_BarsCalculated);

}

Try the one from the attachment

I tested it with random values and it works (un-comment the include line and the the line where the real value should be retrieved, and comment or delete the line that calculates random values)

Files:
_test1.mq5  6 kb
 

If you are using spread information in metatrader 5 and you are using it from the rates field, don't. Metatrader 5 shows one spread on one time frame for the current bar and completely different spread on another time frame for the same current bar. Information in that field is completely useless

 

One issue of metatrader 5 that can affect seriously any code execution

If you use any operation related to time frame different than the current chart time frame (CopyBuffer(), CopyRates() ... ) from time to time you might end up with an "invalid time frame" error. No need to look for coding error : that is an error that happens in metatrader 5 engine. A perfectly legal time frame is declared as invalid on one tick and then on the next tick, that same time frame is accepted as perfectly normal (is it should have been from the start)

That issue can be dangerous if you use those operations in EAs (making unexplainable errors in EAs) and you should ALWAYS check if there was an error in such operations. The things that we are used as completely normal are not normal any more in metatrader 5 and s coder should always code with an idea that nothing is guaranteed in that kind of code execution and that errors should always be checked before proceeding with normal execution

 

And one example what happens frequently when you start metatrader 5 (even after 5 years of development it sometimes does this what you can see on the picture - no need to explain more)

Files:
example.gif  44 kb
 

hi

I am alim

can you help me to find DT oscillator for meta trader 5?

thank's

 

To everyone, I've been trying to add alarm to various mq5 indicators - MACD, ADX, and several others. I started on MACD first. My intention is to get alert/alarm whenever MACD histogram crosses 0 and MACD greater/lesser than its signal line. It appears that the signal comes at every bar and also it gives sell signal every time. I need help please. If I can get this right, adding alert/alarm to other indicators should be easier. Please find the mq5 indicator attached. Thanks.

Files:
macdalarm.mq5  7 kb
 
funayot:
To everyone, I've been trying to add alarm to various mq5 indicators - MACD, ADX, and several others. I started on MACD first. My intention is to get alert/alarm whenever MACD histogram crosses 0 and MACD greater/lesser than its signal line. It appears that the signal comes at every bar and also it gives sell signal every time. I need help please. If I can get this right, adding alert/alarm to other indicators should be easier. Please find the mq5 indicator attached. Thanks.

funayot

Try changing the lines with conditions to these 2:

if((ExtMacdBuffer[rates_total-2] > level) && (ExtMacdBuffer[rates_total-2] > ExtSignalBuffer[rates_total-2]) && (ExtMacdBuffer[rates_total-3] < ExtSignalBuffer[rates_total-3]) && (counter<=NumberofAlerts))

[/PHP]

and this

[PHP] if((ExtMacdBuffer[rates_total-2] < level) && (ExtMacdBuffer[rates_total-2] ExtSignalBuffer[rates_total-3]) && (counter<=NumberofAlerts))

__________________________

When not specified otherwise (if the buffer is not explicitly set to be serries), in metatrader 5 buffers indexes start from 0 for OLDEST (not for the current as we are used to in metatrader 4). That is why then you have to test the indexes specified in the above example and not 1 (bar 1 in that case means the first bar after the oldes bar, not the first bar closed bar from now)

 

Please find completed MACDAlarm attached. It's part of my contribution to this forum.

mladen:
funayot

Try changing the lines with conditions to these 2:

if((ExtMacdBuffer[rates_total-2] > level) && (ExtMacdBuffer[rates_total-2] > ExtSignalBuffer[rates_total-2]) && (ExtMacdBuffer[rates_total-3] < ExtSignalBuffer[rates_total-3]) && (counter<=NumberofAlerts))

[/PHP]

and this

[PHP] if((ExtMacdBuffer[rates_total-2] < level) && (ExtMacdBuffer[rates_total-2] ExtSignalBuffer[rates_total-3]) && (counter<=NumberofAlerts))

__________________________

When not specified otherwise (if the buffer is not explicitly set to be serries), in metatrader 5 buffers indexes start from 0 for OLDEST (not for the current as we are used to in metatrader 4). That is why then you have to test the indexes specified in the above example and not 1 (bar 1 in that case means the first bar after the oldes bar, not the first bar closed bar from now)
Files:
 
hjklfdsa:
hi

I am alim

can you help me to find DT oscillator for meta trader 5?

thank's

alim

Take a look at this post : https://www.mql5.com/en/forum/181297/page53