Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 34
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Show me your whole indicator - let's see what's wrong.
Thank you.
Here it is.
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 LawnGreen
#property indicator_color2 DeepPink
#property indicator_width1 1
#property indicator_width2 1
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
extern string Stochastic = "Configure Stochastic Settings";
extern int TimeFrame1=1;
extern int KPeriod = 5;
extern int DPeriod = 3;
extern int Slowing = 3;
extern int OverBought = 80;
extern int OverSold = 20;
extern int TimeFrame2=5;
extern int KPeriod1 = 5;
extern int DPeriod1 = 3;
extern int Slowing1 = 3;
extern int OverBought1 = 55;
extern int OverSold1 = 45;
extern string Alerts = "Configure Alerts";
extern bool PopUpAlert = true; //Popup Alert
extern bool EmailAlert = true; //Email Alert
extern bool PushAlert = true; //Push Notifications Alert
// UP and DOWN Buffers
double UP[];
double DOWN[];
// Distance of arrows from the high or low of a bar
int distance = 3;
double MyPoint;
datetime CTime;
int OnInit()
{
//--- indicator buffers mapping
//UP Arrow Buffer
SetIndexEmptyValue(0,0.0);
SetIndexStyle(0,DRAW_ARROW,0,EMPTY);
SetIndexArrow(0,233);
SetIndexBuffer(0,UP);
//DOWN Arrow Buffer
SetIndexEmptyValue(1,0.0);
SetIndexStyle(1,DRAW_ARROW,0,EMPTY);
SetIndexArrow(1,234);
SetIndexBuffer(1,DOWN);
//Auto Adjustment for broker digits
if (Digits()==5||Digits()==3){MyPoint=Point*10;} else{MyPoint=Point;}
CTime=Time[0];
//---
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
//--- delete an object from a chart
Comment("");
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
//---
Comment("");
int limit = rates_total;
int count=prev_calculated;
for(int i=limit-count; i>=1;i--)
{
//Getting Stochastic buffer values using the iCustom function
double Stoch1 = iStochastic(NULL,TimeFrame1,KPeriod,DPeriod,Slowing,MODE_SMA,0,MODE_MAIN,i);
double Stoch2 = iStochastic(NULL,TimeFrame1,KPeriod,DPeriod,Slowing,MODE_SMA,0,MODE_MAIN,i+1);
double Stoch50_1 = iStochastic(NULL,TimeFrame2,KPeriod1,DPeriod1,Slowing1,MODE_SMA,0,MODE_MAIN,i);
double Stoch50_2 = iStochastic(NULL,TimeFrame2,KPeriod1,DPeriod1,Slowing1,MODE_SMA,0,MODE_MAIN,i+1);
if(Stoch1>20 && Stoch2<20&&Stoch50_1>50&&Stoch50_1>Stoch50_2)
{
UP[i]=Low[i]-distance*MyPoint;
}
if(Stoch1<80 && Stoch2>80&&Stoch50_1<50&&Stoch50_1<Stoch50_2)
{
DOWN[i]=High[i]+distance*MyPoint;
}
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
Thank you.
Here it is
...
Just to clarify.
I want to put the arrow up if the indicator line has crossed level 20, on M1 period, and the indicator line is directed up, above level 50, on M5 period.
Let's see how to get the values from the higher timeframe, being on the lower one:
//| iTwoStoch.mq4 |
//| Copyright 2016, Artem A. Trishkin, Skype artmedia70 |
//| https://login.mql5.com/ru/users/artmedia70 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Artem A. Trishkin, Skype artmedia70"
#property link "https://login.mql5.com/ru/users/artmedia70"
#property version "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2
//--- plot UP
#property indicator_label1 "UP"
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrLawnGreen
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- plot DN
#property indicator_label2 "DN"
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrDeepPink
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//--- enums
enum enumYN
{
enYes=1, // Да
enNo=0, // Нет
};
//--- input parameters
extern string Stochastic1 = "Параметры стохастика 1";
input ENUM_TIMEFRAMES TimeFrame1 = PERIOD_M1; // Таймфрейм
input int KPeriod1 = 5; // Период %K
input int DPeriod1 = 3; // Период %D
input int Slowing1 = 3; // Замедление
input double OverBought1 = 80; // Уровень перекупленности
input double OverSold1 = 20; // Уровень перепроданности
extern string Stochastic2 = "Параметры стохастика 2";
input ENUM_TIMEFRAMES TimeFrame2 = PERIOD_M5; // Таймфрейм
input int KPeriod2 = 5; // Период %K
input int DPeriod2 = 3; // Период %D
input int Slowing2 = 3; // Замедление
input double OverBought2 = 55; // Уровень перекупленности
input double OverSold2 = 45; // Уровень перепроданности
extern string Alerts = "Configure Alerts";
input enumYN UsePopUpAlert = enYes; // Popup Alert
input enumYN UseEmailAlert = enYes; // Email Alert
input enumYN UsePushAlert = enYes; // Push Notifications Alert
//--- indicator buffers
double BufferUP[];
double BufferDN[];
//--- global variables
int kperiod1; // Период %K стох.1
int dperiod1; // Период %D стох.1
int slowing1; // Замедление стох.1
double overBought1; // Уровень перекупленности стох.1
double overSold1; // Уровень перепроданности стох.1
//---
int kperiod2; // Период %K стох.2
int dperiod2; // Период %D стох.2
int slowing2; // Замедление стох.2
double overBought2; // Уровень перекупленности стох.2
double overSold2; // Уровень перепроданности стох.2
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,BufferUP);
SetIndexBuffer(1,BufferDN);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
SetIndexArrow(0,233);
SetIndexArrow(1,234);
//---
kperiod1=(KPeriod1<1?1:KPeriod1);
kperiod2=(KPeriod2<1?1:KPeriod2);
dperiod1=(DPeriod1<1?1:DPeriod1);
dperiod2=(DPeriod2<1?1:DPeriod2);
slowing1=(Slowing1<1?1:Slowing1);
slowing2=(Slowing2<1?1:Slowing2);
//---
overBought1=(OverBought1>100.0?100.0:OverBought1<0.1?0.1:OverBought1);
overSold1=(OverSold1<0?0:OverSold1>99.9?99.9:OverSold1);
if(overBought1<=overSold1) overBought1=overSold1+0.1;
if(overSold1>=overBought1) overSold1=overBought1-0.1;
//---
overBought2=(OverBought2>100.0?100.0:OverBought2<0.1?0.1:OverBought2);
overSold2=(OverSold2<0?0:OverSold2>99.9?99.9:OverSold2);
if(overBought2<=overSold2) overBought2=overSold2+0.1;
if(overSold2>=overBought2) overSold2=overBought2-0.1;
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
//---
if(rates_total<2) return(0);
int limit=rates_total-prev_calculated;
if(limit>1) {
limit=rates_total-2;
ArrayInitialize(BufferUP,EMPTY_VALUE);
ArrayInitialize(BufferDN,EMPTY_VALUE);
}
for(int i=limit; i>=0; i--) {
int bar_sto2_0=iBarShift(Symbol(),TimeFrame2,iTime(Symbol(),TimeFrame2,i));
int bar_sto2_1=iBarShift(Symbol(),TimeFrame2,iTime(Symbol(),TimeFrame2,i+1));
double sto1_0=iStochastic(Symbol(),TimeFrame1,kperiod1,dperiod1,slowing1,MODE_SMA,STO_LOWHIGH,MODE_MAIN,i);
double sto1_1=iStochastic(Symbol(),TimeFrame1,kperiod1,dperiod1,slowing1,MODE_SMA,STO_LOWHIGH,MODE_MAIN,i+1);
double sto2_0=iStochastic(Symbol(),TimeFrame2,kperiod2,dperiod2,slowing2,MODE_SMA,STO_LOWHIGH,MODE_MAIN,bar_sto2_0);
double sto2_1=iStochastic(Symbol(),TimeFrame2,kperiod2,dperiod2,slowing2,MODE_SMA,STO_LOWHIGH,MODE_MAIN,bar_sto2_1);
Comment(
"\n",
"Stoch ",EnumToString(TimeFrame1),
", бар ",(i+1)," > время: ",TimeToString(time[i+1],TIME_MINUTES),", значение: ",DoubleToString(sto1_1,Digits()),
"; бар ",i," > время: ",TimeToString(time[i],TIME_MINUTES),", значение: ",DoubleToString(sto1_0,Digits()),"\n",
//---
"Stoch ",EnumToString(TimeFrame2),
", бар ",bar_sto2_1," > время: ",TimeToString(iTime(Symbol(),TimeFrame2,bar_sto2_1),TIME_MINUTES),", значение: ",DoubleToString(sto2_1,Digits()),
"; бар ",bar_sto2_0," > время: ",TimeToString(iTime(Symbol(),TimeFrame2,bar_sto2_0),TIME_MINUTES),", значение: ",DoubleToString(sto2_0,Digits())
);
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
Next, try doing the logic you want instead of Comment().
Let's see...
Let's see how to get values from a higher timeframe while on a lower one:
And then try to do the logic you want instead of Comment().
We will see...
Thanks, it worked )
In the Expert Advisor, when calling the indicator, I just need to specify the number of the desired timeframe, while the indicator "dances" with the shift. Where can I read more about it?
Thanks, it worked )
In the EA, when I call the indicator, I just need to specify the number of the desired timeframe, and the indicator "dances" with the shift. Where can I read more about it?
I have not read it anywhere - I have no idea.
Imagine this: you cycle through the bars on M1. You take indicator values from the bar number, to which cycle index i is referenced.
In the table in the header at the top the cycle index M1, below it is the value of the bar time to which the M1 index refers
At the very bottom the cycle index M5, above it is the value of the bar time to which the M5 index refers
When you pass the index value from the current timeframe, at which the cycle starts, into the iCustom() function, you get the values of the indicator from the older timeframe according to the index. But the index does not refer to the necessary bar, as it can be seen in the table.
And this should be done in expert advisors as well, otherwise - you're not taking values from the expected bar and think that you've got it correct. Well, you got it right, but not from the wrong bar. This must be known and taken into account.
I.e., at multitime data reception it is necessary to rely on time, and recalculate it to the bar number at the required timeframe.
Artyom, please help me too, I have problems with indicators.
I need to find these constructions and put arrows on them. The functions in the Expert Advisor work and are simple to connect, well, it's understandable - they only need current values, but how to transfer them to the indicator, to draw the coincidence on the history...?
The arrows should be drawn on a candlestick, as soon as the High[1] and Low[1] coincide.
//| iTwo.mq4 |
//| Copyright 2016, Artem A. Trishkin, Skype artmedia70 |
//| https://login.mql5.com/ru/users/artmedia70 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Artem A. Trishkin, Skype artmedia70"
#property link "https://login.mql5.com/ru/users/artmedia70"
#property version "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2
//--- plot UP
#property indicator_label1 "UP"
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrLawnGreen
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- plot DN
#property indicator_label2 "DN"
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrDeepPink
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//--- input parameters
input ENUM_TIMEFRAMES TimeFrame = PERIOD_H1;
input string dSymbol = "AUDUSD";
//--- indicator buffers
double BufferUP[];
double BufferDN[];
//--- global variables
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,BufferUP);
SetIndexBuffer(1,BufferDN);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
SetIndexArrow(0,233);
SetIndexArrow(1,234);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
//---
if(rates_total<2) return(0);
int limit=rates_total-prev_calculated;
if(limit>1) {
limit=rates_total-2;
ArrayInitialize(BufferUP,EMPTY_VALUE);
ArrayInitialize(BufferDN,EMPTY_VALUE);
}
int NumFrUp = GetFractalBar(dSymbol,PERIOD_CURRENT,0,MODE_UPPER); // номер бара Up
int NumFrDn = GetFractalBar(dSymbol,PERIOD_CURRENT,0,MODE_LOWER); // номер бара Dn
Comment( "Bear: " , GetBearBull(dSymbol, TimeFrame, 3) == 1, // Подряд 3 бара вверх
"\nBull: ", GetBearBull(dSymbol, TimeFrame, 3) == 2, // Подряд 3 бара вниз
"\nNumFrUp: " , NumFrUp, // номер бара Up
"\nNumFrDn: ", NumFrDn, // номер бара Dn
"\nPriceFrUp: " ,iHigh(dSymbol,PERIOD_CURRENT,NumFrUp), // Цена верхнего фрактала
"\nPriceDn: ", iLow(dSymbol,PERIOD_CURRENT,NumFrDn) // Цена нижнего фрактала
);
for(int i=limit; i>=0; i--) {
//--
if(/* что сюда */0)
{
BufferUP[i]=Low[i]-50*_Point;
}
//--
if(/* что сюда */0)
{
BufferDN[i]=High[i]+50*_Point;
}
} // end for
//--- return value of prev_calculated for next call
return(rates_total);
}
//===============================================================================================
//---------------------- Возвращает номер бара фрактала по его номеру --------------------------+
//===============================================================================================
int GetFractalBar(string symb="0", int tf=0, int nf=0, int mode=MODE_UPPER) {
if(symb=="0") { symb=Symbol();}
double f=0;
int kf=0;
for(int i=3; i<iBars(symb, tf)-1; i++) {
if(mode==MODE_LOWER){
f=iFractals(symb, tf, MODE_LOWER, i);
if(f!=0) {
kf++;
if(kf>nf) { return(i);}
}}
if(mode==MODE_UPPER){
f=iFractals(symb, tf, MODE_UPPER, i);
if(f!=0) {
kf++;
if(kf>nf) { return(i);}
}}}
return(-1);
}
//===============================================================================================
//------------------------ Функция возвращает направление баров подряд -------------------------+
//===============================================================================================
int GetBearBull(string symb="0", int tf=0, int ne=5) {
if(symb=="0") { symb=Symbol();}
double open1=0,close1=0,open2=0,close2=0;
int b=1,s=1;
for(int i=1; i<ne; i++) {
open1=iOpen(symb,tf,i);
close1=iClose(symb,tf,i);
open2= iOpen(symb,tf,i+1);
close2= iClose(symb,tf,i+1);
if(open2<close2 && open1<close1 && open2<open1) { // бычьи
b++;
if(b==ne) { return(1);}
}
if(open2>close2 && open1>close1 && open2>open1) { // медвежьи
s++;
if(s==ne) { return(2);}
}
}
return(0);
}
//===============================================================================================
Then I will try to enter other patterns following the analogy, so as not to disturb the Expert Advisor.
Thank you!
Artyom, please help me too, I have problems with indicators.
I need to find these constructions and put arrows on them. The functions in the Expert Advisor work and are simple to connect, well, it's understandable - they only need current values, but how to transfer them to the indicator to draw a coincidence on the history...?
The arrows are drawn on a candlestick, when the coincidence of High[1] and Low[1] occurs.
...
Then I will try to enter other patterns following the analogy, so as not to disturb the Expert Advisor.
Thank you!
There should be a different logic for different patterns, because not all of them are the same. And for one and the same pattern, the logic should be written differently by different programmers.
And what exactly, which pattern do you want to determine?
Let's start with this.
There must be different logic for different patterns, because not all patterns are the same. And for the same pattern, different programmers will write different logic.
And what exactly, which pattern do you want to define?
Let's start with this one.
I have attached a picture.
The condition: fractal "3" is higher than fractal "2" and fractal "1" is higher than "2" -FrDn3 > FrDn2&& FrDn2< FrDn1 We look at the candles, the fractals are already formed and wait for the confirmation of the candlestick pattern,
and if there are"3" increasing in a row - buy (arrow) -GetBearBull(dSymbol,0,3)==1(1 - buy)
For selling, it is vice versa.
It's easy to put it in an Expert Advisor, but in an indicator ...
I have attached the picture.
Required condition: fractal "3" is higher than fractal "2" and fractal "1" is higher than "2" -FrDn3 > FrDn2&& FrDn2< FrDn1 We look at the candlestick, the fractals have already been formed and wait for the confirmation of the candlestick pattern,
and if there are"3" rising ones in a row - buy (arrow) -GetBearBull(dSymbol,0,3)==1(1 - buy)
For selling, it is vice versa.
It's easy to put it in an Expert Advisor, but in an indicator...
In the indicator it is vice versa - if you start search in the depth of history, in the indicator - from its beginning. Look for the left fractal first, then the middle one, then the right one.
Manage the flags.
In the indicator, the logic is the opposite - if in the EA you start searching deep into the history, then in the indicator you start from the beginning of the history. Look for the left fractal first, then the middle one, then the right one.
Manage the flags.
I'm asking to show the ready variant. I may see it once and the questions will fall away, but now I have a thousand of them and cannot decide.
What to do with all this in an indicator - I have no idea(
//---------------------- Возвращает номер бара фрактала по его номеру --------------------------+
//===============================================================================================
int GetFractalBar(string symb="0", int tf=0, int nf=0, int mode=MODE_UPPER) {
if(symb=="0") { symb=Symbol();}
double f=0;
int kf=0;
for(int i=3; i<iBars(symb, tf)-1; i++) {
if(mode==MODE_LOWER){
f=iFractals(symb, tf, MODE_LOWER, i);
if(f!=0) {
kf++;
if(kf>nf) { return(i);}
}}
if(mode==MODE_UPPER){
f=iFractals(symb, tf, MODE_UPPER, i);
if(f!=0) {
kf++;
if(kf>nf) { return(i);}
}}}
return(-1);
}
//===============================================================================================
//------------------------ Функция возвращает направление баров подряд -------------------------+
//===============================================================================================
int GetBearBull(string symb="0", int tf=0, int ne=5) {
if(symb=="0") { symb=Symbol();}
double open1=0,close1=0,open2=0,close2=0;
int b=1,s=1;
for(int i=1; i<ne; i++) {
open1=iOpen(symb,tf,i);
close1=iClose(symb,tf,i);
open2= iOpen(symb,tf,i+1);
close2= iClose(symb,tf,i+1);
if(open2<close2 && open1<close1 && open2<open1) { // бычьи
b++;
if(b==ne) { return(1);}
}
if(open2>close2 && open1>close1 && open2>open1) { // медвежьи
s++;
if(s==ne) { return(2);}
}
}
return(0);
}
//===============================================================================================
If I could, then no questions, I understand the logic, but I cannot implement it in the code, so please show me a ready variant, I will see it once and the questions will disappear in the future, but now there are thousands of them and I cannot solve them myself.
What to do with all this in an indicator - no idea(
//---------------------- Возвращает номер бара фрактала по его номеру --------------------------+
//===============================================================================================
int GetFractalBar(string symb="0", int tf=0, int nf=0, int mode=MODE_UPPER) {
if(symb=="0") { symb=Symbol();}
double f=0;
int kf=0;
for(int i=3; i<iBars(symb, tf)-1; i++) {
if(mode==MODE_LOWER){
f=iFractals(symb, tf, MODE_LOWER, i);
if(f!=0) {
kf++;
if(kf>nf) { return(i);}
}}
if(mode==MODE_UPPER){
f=iFractals(symb, tf, MODE_UPPER, i);
if(f!=0) {
kf++;
if(kf>nf) { return(i);}
}}}
return(-1);
}
//===============================================================================================
//------------------------ Функция возвращает направление баров подряд -------------------------+
//===============================================================================================
int GetBearBull(string symb="0", int tf=0, int ne=5) {
if(symb=="0") { symb=Symbol();}
double open1=0,close1=0,open2=0,close2=0;
int b=1,s=1;
for(int i=1; i<ne; i++) {
open1=iOpen(symb,tf,i);
close1=iClose(symb,tf,i);
open2= iOpen(symb,tf,i+1);
close2= iClose(symb,tf,i+1);
if(open2<close2 && open1<close1 && open2<open1) { // бычьи
b++;
if(b==ne) { return(1);}
}
if(open2>close2 && open1>close1 && open2>open1) { // медвежьи
s++;
if(s==ne) { return(2);}
}
}
return(0);
}
//===============================================================================================
Do you need a function that returns a fractal bar number by its number?
You need a function that returns the presence of a fractal on the required bar (cycle index+3)