Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 138

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
Tell me how to find out the date and time of yesterday
Well, it's got everything you need, just read it. Like this one.
Can you tell me how to find out yesterday's date and time programmatically?
calendar yesterday:
datetime tim=TimeCurrent()-24*60*60;
MqlDateTime ts;
TimeToStruct(tim,ts);
ts.hour=0;ts.min=0;ts.sec=0;
tim=StructToTime(ts);
now in tim - start of yesterday's calendar day (!!! not opening time of pre-bar D1)
Opening time of the pre-bar D1 - iTime(0,PERIOD_D1,1); with small fix - if it is Saturday, then add 24 hours, if Sunday, then 48 hours.
Well, it's got everything you need, just read it. Like this one.
I have been trying to figure out how to do it since yesterday, but I can't, so I'm asking for help, because I'm new to it.
Please help me find the index of yesterday's bar at 15:00 and the index of today's bar at 7:00 only without explicitly specifying the date as shown in the example
calendar yesterday:
datetime tim=TimeCurrent()-24*60*60;
MqlDateTime ts;
TimeToStruct(tim,ts);
ts.hour=0;ts.min=0;ts.sec=0;
tim=StructToTime(ts);
now in tim - start of yesterday's calendar day (!!! not the opening time of the D1 pre-bar)
Opening time of the pre-bar D1 - iTime(0,PERIOD_D1,1); with small fix - add 24 hours if it is Saturday, 48 hours if it is Sunday.
function to transfer the stopband to lossless, the order ticket and the distance in pips are passed to the function
{
double sl=0.0;
if(OrderSelect(ticket,SELECT_BY_TICKET))
{
if(OrderType()==OP_BUY)
{
if(Bid>=OrderOpenPrice() && Bid-OrderOpenPrice()>=distance*_Point) sl=OrderOpenPrice();
if(OrderStopLoss()!=0 && OrderStopLoss()>=OrderOpenPrice())return;
}
if(OrderType()==OP_SELL)
{
if(Ask<=OrderOpenPrice() && OrderOpenPrice()-Ask>=distance*_Point) sl=OrderOpenPrice();
if(OrderStopLoss()!=0 && OrderStopLoss()<=OrderOpenPrice())return;
}
ResetLastError();
if(sl<=0)return;
if(!OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0))
{
int error=GetLastError();
rezult=StringConcatenate(OrderSymbol(),": error modifying StopLoss order ",OrderTicket()," ",TypeToStr(OrderType())," №- ",error);
Print(rezult);
}
}
}
...thanks sergey!!!!!
Cool, I'll try to pull the functions into the EA. I'll post the result later.
Thanks!!!
Added a second search method.
//| iFreeNumFractals.mq4 |
//| Copyright 2017, Artem A. Trishkin, Skype artmedia70 |
//| https://login.mql5.com/ru/users/artmedia70 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Artem A. Trishkin, Skype artmedia70"
#property link "https://login.mql5.com/ru/users/artmedia70"
#property version "3.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2
//--- plot UpperFractal
#property indicator_label1 "Upper Fractal"
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- plot LowerFractal
#property indicator_label2 "Lower Fractal"
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrSteelBlue
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//---
enum ENUM_TYPE_FRACTAL
{
TYPE_FRACTAL_ACCURATE = 0, // Accurate fractal
TYPE_FRACTAL_INACCURATE = 1, // Inaccurate fractal
};
//--- input parameters
input ENUM_TYPE_FRACTAL TypeFractals = TYPE_FRACTAL_ACCURATE; // Type of fractal
input int LeftNumUp = 2; // The number of bars on the left for upper fractals
int leftNumUp; // Количество баров слева для верхнего фрактала
input int RightNumUp = 2; // The number of bars on the right for upper fractals
int rightNumUp; // Количество баров справа для верхнего фрактала
input int LeftNumDn = 2; // The number of bars on the left for lower fractals
int leftNumDn; // Количество баров слева для нижнего фрактала
input int RightNumDn = 2; // The number of bars on the right for lower fractals
int rightNumDn; // Количество баров справа для нижнего фрактала
//--- 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);
SetIndexEmptyValue(0,EMPTY_VALUE);
SetIndexEmptyValue(1,EMPTY_VALUE);
//---
leftNumUp=(LeftNumUp<1?1:LeftNumUp);
rightNumUp=(RightNumUp<1?1:RightNumUp);
leftNumDn=(LeftNumDn<1?1:LeftNumDn);
rightNumDn=(RightNumDn<1?1:RightNumDn);
string short_name=MQLInfoString(MQL_PROGRAM_NAME)+"("+(string)leftNumUp+","+(string)rightNumUp+")("+(string)leftNumDn+","+(string)rightNumDn+")";
IndicatorSetString(INDICATOR_SHORTNAME,short_name);
IndicatorSetInteger(INDICATOR_DIGITS,Digits());
//---
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<fmax(leftNumUp+rightNumUp,leftNumDn+rightNumDn)) return(0);
int limit=rates_total-prev_calculated;
if(limit>0) {
ArrayInitialize(BufferUpperFractal,0.0);
ArrayInitialize(BufferUpperFractal,0.0);
limit=rates_total-fmax(leftNumUp,leftNumDn)-1;
}
//---
for(int i=limit; i>fmin(rightNumUp,rightNumDn); i--) {
if(GetFreeUpperFractal(Symbol(),PERIOD_CURRENT,i,leftNumUp,rightNumUp)>0) BufferUpperFractal[i]=high[i];
if(GetFreeLowerFractal(Symbol(),PERIOD_CURRENT,i,leftNumDn,rightNumDn)>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);
if(TypeFractals==TYPE_FRACTAL_ACCURATE) {
for(int i=shift-1; i>=shift-right_dimension; i--) if(GetPriceLow(symbol_name,timeframe,i)<GetPriceLow(symbol_name,timeframe,i+1)) return(-1);
for(int i=shift+1; i<=shift+left_dimension; i++) if(GetPriceLow(symbol_name,timeframe,i)<GetPriceLow(symbol_name,timeframe,i-1)) return(-1);
}
else {
for(int i=shift-1; i>=shift-right_dimension; i--) if(GetPriceLow(symbol_name,timeframe,i)<GetPriceLow(symbol_name,timeframe,shift)) return(-1);
for(int i=shift+1; i<=shift+left_dimension; i++) if(GetPriceLow(symbol_name,timeframe,i)<GetPriceLow(symbol_name,timeframe,shift)) 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);
if(TypeFractals==TYPE_FRACTAL_ACCURATE) {
for(int i=shift-1; i>=shift-right_dimension; i--) if(GetPriceHigh(symbol_name,timeframe,i)>GetPriceHigh(symbol_name,timeframe,i+1)) return(-1);
for(int i=shift+1; i<=shift+left_dimension; i++) if(GetPriceHigh(symbol_name,timeframe,i)>GetPriceHigh(symbol_name,timeframe,i-1)) return(-1);
}
else {
for(int i=shift-1; i>=shift-right_dimension; i--) if(GetPriceHigh(symbol_name,timeframe,i)>GetPriceHigh(symbol_name,timeframe,shift)) return(-1);
for(int i=shift+1; i<=shift+left_dimension; i++) if(GetPriceHigh(symbol_name,timeframe,i)>GetPriceHigh(symbol_name,timeframe,shift)) 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);
}
//+----------------------------------------------------------------------------+
Added a second search method.
Thank you, it's just great.
Remains to be uploaded to kodobase)
Thank you, just everything is super.
Remains to be uploaded to the kodobase)
Hello.
I am on linux. I have several currency pairs open in MT4 - up to 10. How can I use MQL4 to arrange chart tabs in a certain sequence?
Working with profiles, studying 1_MQL4.pdf and the Internet has not shown any results.
Regards, Vladimir
Hello.
I am on linux. I have several currency pairs open in MT4 - up to 10. How can I use MQL4 to arrange chart tabs in a certain sequence?
Working with profiles, studying 1_MQL4.pdf and the Internet has not shown any results.
Regards, Vladimir.
Even on Windows, the tabs cannot be sorted by means of mql4.
You can only open charts in the sequence you need and set the necessary templates on them.