Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 3
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
We need to look deeper...
Deeper, where is that?
All attempts, with the new indicator, lead to a hang-up.
And with the indicator, which is unchanged, there's a momentary hiccup.
What could be the solution?
Deeper, where is that?
All attempts, with the new indicator, result in a hang-up.
And with the unchanged indicator, there is a momentary hiccup.
What could be the solution?
Just make functions to find such fractals and work with them.
For you, it's a simple thing to do.)
but for me, it's an impossible task.
A function like this?
{
//---
for(int i=1; i<=max; i++) {
if(i<=leftSide && low[bar]>low[bar-i]) return(false);
if(i<=rightSide && low[bar]>=low[bar+i]) return(false);
}
//---
return(true);
}
It's a lower fractal.
How to use it?
For you, it's easier to do.)
But for me, it's an impossible task.
How to implant the function from that indicator into mine?
{
//---
for(int i=1; i<=max; i++) {
if(i<=leftSide && low[bar]>low[bar-i]) return(false);
if(i<=rightSide && low[bar]>=low[bar+i]) return(false);
}
//---
return(true);
}
Well, you need it to return the price of a fractal on the required bar. Here, I made a simple indicator. It has two functions that you can take out of it and use in yours - I specially organized them as functions - with checks for invalid values.
//| iFreeNumFractals.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 UpperFractal
#property indicator_label1 "UpperFractal"
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- plot LowerFractal
#property indicator_label2 "LowerFractal"
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrSteelBlue
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//--- input parameters
input int LeftNum=2; // Количество баров слева
int leftNum; // Количество баров слева
input int RightNum=2; // Количество баров справа
int rightNum; // Количество баров справа
//--- indicator buffers
double BufferUpperFractal[];
double BufferLowerFractal[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,BufferUpperFractal);
SetIndexBuffer(1,BufferLowerFractal);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
PlotIndexSetInteger(0,PLOT_ARROW,159);
PlotIndexSetInteger(1,PLOT_ARROW,159);
SetIndexArrow(0,217);
SetIndexArrow(1,218);
//---
leftNum=(LeftNum<1?1:LeftNum);
rightNum=(RightNum<1?1:RightNum);
//---
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<leftNum+rightNum) return(0);
int limit=rates_total-prev_calculated;
if(limit>0) {
ArrayInitialize(BufferUpperFractal,0.0);
ArrayInitialize(BufferUpperFractal,0.0);
limit=rates_total-leftNum-1;
}
//---
for(int i=limit; i>rightNum; i--) {
if(GetFreeUpperFractal(i,limit,high,leftNum,rightNum)>0) BufferUpperFractal[i]=high[i];
if(GetFreeLowerFractal(i,limit,low ,leftNum,rightNum)>0) BufferLowerFractal[i]=low[i];
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+----------------------------------------------------------------------------+
double GetFreeLowerFractal(int shift,const int limit,const double &low[],int left_dimension=2,int right_dimension=2) {
if(left_dimension<1) left_dimension=1;
if(right_dimension<1) right_dimension=1;
if(shift-right_dimension<1 || shift+left_dimension>limit-1) return(-1);
for(int i=shift; i>shift-right_dimension; i--) if(low[i]>low[i-1]) return(-1);
for(int i=shift; i<shift+left_dimension; i++) if(low[i]>low[i+1]) return(-1);
return(low[shift]);
}
//+----------------------------------------------------------------------------+
double GetFreeUpperFractal(int shift,const int limit,const double &high[],int left_dimension=2,int right_dimension=2) {
if(left_dimension<1) left_dimension=1;
if(right_dimension<1) right_dimension=1;
if(shift-right_dimension<1 || shift+left_dimension>limit-1) return(-1);
for(int i=shift; i>=shift-right_dimension; i--) if(high[i]<high[i-1]) return(-1);
for(int i=shift; i<=shift+left_dimension; i++) if(high[i]<high[i+1]) return(-1);
return(high[shift]);
}
//+----------------------------------------------------------------------------+
To completely detach the functions for getting arbitrary fractals from the indicator, we should not pass by reference arrays high[] and low[] and limit value into them.
Since our code is very close to MQL5, we will have to refuse High[], Low[], iHigh() and iLow() functions. This is how it will look in this indicator:
//| iFreeNumFractals.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 UpperFractal
#property indicator_label1 "UpperFractal"
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- plot LowerFractal
#property indicator_label2 "LowerFractal"
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrSteelBlue
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//--- input parameters
input int LeftNum=2; // Количество баров слева
int leftNum; // Количество баров слева
input int RightNum=2; // Количество баров справа
int rightNum; // Количество баров справа
//--- indicator buffers
double BufferUpperFractal[];
double BufferLowerFractal[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,BufferUpperFractal);
SetIndexBuffer(1,BufferLowerFractal);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
// PlotIndexSetInteger(0,PLOT_ARROW,217);
// PlotIndexSetInteger(1,PLOT_ARROW,218);
SetIndexArrow(0,217);
SetIndexArrow(1,218);
//---
leftNum=(LeftNum<1?1:LeftNum);
rightNum=(RightNum<1?1:RightNum);
//---
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<leftNum+rightNum) return(0);
int limit=rates_total-prev_calculated;
if(limit>0) {
ArrayInitialize(BufferUpperFractal,0.0);
ArrayInitialize(BufferUpperFractal,0.0);
limit=rates_total-leftNum-1;
}
//---
for(int i=limit; i>rightNum; i--) {
if(GetFreeUpperFractal(Symbol(),PERIOD_CURRENT,i,leftNum,rightNum)>0) BufferUpperFractal[i]=high[i];
if(GetFreeLowerFractal(Symbol(),PERIOD_CURRENT,i,leftNum,rightNum)>0) BufferLowerFractal[i]=low[i];
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+----------------------------------------------------------------------------+
double GetFreeLowerFractal(const string symbol_name,ENUM_TIMEFRAMES timeframe,int shift,int left_dimension=2,int right_dimension=2) {
int bars=Bars(symbol_name,timeframe);
if(left_dimension<1) left_dimension=1;
if(right_dimension<1) right_dimension=1;
if(shift-right_dimension<1 || shift+left_dimension>bars-1) return(-1);
for(int i=shift; i>shift-right_dimension; i--) if(GetPriceLow(symbol_name,timeframe,i)>GetPriceLow(symbol_name,timeframe,i-1)) return(-1);
for(int i=shift; i<shift+left_dimension; i++) if(GetPriceLow(symbol_name,timeframe,i)>GetPriceLow(symbol_name,timeframe,i+1)) return(-1);
return(GetPriceLow(symbol_name,timeframe,shift));
}
//+----------------------------------------------------------------------------+
double GetFreeUpperFractal(const string symbol_name,ENUM_TIMEFRAMES timeframe,int shift,int left_dimension=2,int right_dimension=2) {
int bars=Bars(symbol_name,timeframe);
if(left_dimension<1) left_dimension=1;
if(right_dimension<1) right_dimension=1;
if(shift-right_dimension<1 || shift+left_dimension>bars-1) return(-1);
for(int i=shift; i>=shift-right_dimension; i--) if(GetPriceHigh(symbol_name,timeframe,i)<GetPriceHigh(symbol_name,timeframe,i-1)) return(-1);
for(int i=shift; i<=shift+left_dimension; i++) if(GetPriceHigh(symbol_name,timeframe,i)<GetPriceHigh(symbol_name,timeframe,i+1)) return(-1);
return(GetPriceHigh(symbol_name,timeframe,shift));
}
//+----------------------------------------------------------------------------+
double GetPriceHigh(const string symbol_name, ENUM_TIMEFRAMES timeframe, int shift){
double array[1];
if(CopyHigh(symbol_name,timeframe,shift,1,array)==1) return(array[0]);
return(-1);
}
//+----------------------------------------------------------------------------+
double GetPriceLow(const string symbol_name, ENUM_TIMEFRAMES timeframe, int shift){
double array[1];
if(CopyLow(symbol_name,timeframe,shift,1,array)==1) return(array[0]);
return(-1);
}
//+----------------------------------------------------------------------------+
Price2==Bid;
{
if (Hour()==9 && (Minute() == 40) && (Bid<Price2))
{
ticket=OrderSend(Symbol(),OP_SELL, Lts, Bid, SP,0,0, NULL, Magic, 0, Blue);
return(0);
}
}
Price2==Bid;
{
if (Hour()==9 && (Minute() == 40) && (Bid<Price2))
{
ticket=OrderSend(Symbol(),OP_SELL, Lts, Bid, SP,0,0, NULL, Magic, 0, Blue);
return(0);
}
}
:)
What if you run the Expert Advisor at 9.22?
And if there is a system or terminal failure? The price will be lost.
I.e., you have to look for what was 20 minutes ago when the time of this check comes. The time has come in which the minutes are greater than or equal to a multiple of twenty - check the state of RSI on the bar, which was 20 minutes ago. If it has the right crossover, then proceed as planned...
However, on M15 you cannot determine the exact time of crossing and the exact price, but you can look at the price on M1 - at least 15 times more accurate.
:)
What if you run the EA at 9.22 ?
What if there is a system or terminal failure? The price will be lost.
I.e., you have to look for what was 20 minutes ago when the time of this check comes. The time has come in which the minutes are greater than or equal to a multiple of twenty - check the state of RSI on the bar, which was 20 minutes ago. If it has the desired crossing, then we proceed as planned...
However, on M15 you cannot determine the exact time of crossing and the exact price, but you can look at the price on M1 - at least 15 times more accurate.
Okay, I understand that I can not cope with it)))) guys who can write this part of the code? I'll pay 1000 rubles
Comment("RSI = ",rci20);
And where's the thousand?
(just kidding)
Comment("RSI = ",rci20);
And where's the thousand?
(joking)