Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 5
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
But how to do it step by step? ..... graphical objects (what are they and how to put them), put data labels on the graph (where to enter and with what data)....... sorry for stupid questions.
Software.
You need to write an information indicator with the data you want.
Next, if RSI crossed the level 20 minutes ago we check the price, i.e. to go short the price should be lower than 20 minutes ago. Thank you very much. If it works, I owe you a promise))
Since we create it maximally close to MQL5, we won't use MQL4 functions for getting bar shift by time and closing price (iBarShift() and iClose() respectively).
We do our own functions for this: GetBarShift() and GetPriceClose(). In addition, we will need to get the bar time using GetTime() function instead of iTime(), since we reject standard MQL4 functions.
Here is a test Expert Advisor:
//| exTestValueRSI.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
//--- input parameters
input ENUM_TIMEFRAMES TimeframeRSI = PERIOD_M15; // Таймфрейм RSI
input int PeriodRSI = 14; // Период расчёта RSI
input ENUM_APPLIED_PRICE PriceRSI = PRICE_CLOSE; // Цена расчёта RSI
input int MinutesBefore =20; // Количество минут назад
//--- global variables
int minutesBefore; // Количество минут назад
int periodRSI; // Период расчёта RSI
//---
double prevRSIvalue0; // Значение RSI для заданного тф xxx минут назад
double prevRSIvalue1; // Значение RSI для заданного тф xxx минут назад-x минут
//---
double prevClose_0; // Значение Close для заданного тф xxx минут назад
double prevClose_1; // Значение Close для заданного тф xxx минут назад-x минут
//---
datetime timeBefore; // Время ххх минут назад
datetime timePrevBefore; // Время ххх минут назад-x минут
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
minutesBefore=(MinutesBefore<1?1:MinutesBefore); // Количество минут назад
periodRSI=(PeriodRSI<1?1:PeriodRSI);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
int shift_0;
int shift_1;
//---
MqlDateTime server_time;
TimeToStruct(TimeCurrent(),server_time);
string tf=EnumToString(TimeframeRSI);
//--- если значение минут серверного времени кратно заданному значению, в частности 20-ти минутам или равно 0
if(server_time.min%minutesBefore==0 || server_time.min==0) {
//--- время 1x и 2x минут назад
timeBefore=TimeCurrent()-minutesBefore*PeriodSeconds(PERIOD_M1);
timePrevBefore=TimeCurrent()-2*minutesBefore*PeriodSeconds(PERIOD_M1);
//--- смещение в барах времени 1х и 2х для заданного таймфрейма RSI (тф М15)
shift_0=GetBarShift(Symbol(),TimeframeRSI,timeBefore);
shift_1=GetBarShift(Symbol(),TimeframeRSI,timePrevBefore);
//--- значения RSI на барах 1х и 2х минут назад для заданного таймфрейма RSI (тф М15)
prevRSIvalue0=GetLastDataRSI(Symbol(),TimeframeRSI,shift_0);
prevRSIvalue1=GetLastDataRSI(Symbol(),TimeframeRSI,shift_1);
//--- значения цен закрытия баров 1х и 2х минут назад
prevClose_0=GetPriceClose(Symbol(),TimeframeRSI,shift_0);
prevClose_1=GetPriceClose(Symbol(),TimeframeRSI,shift_1);
}
Comment("\nТекущее время: ",TimeCurrent(),
"\nВремя ",minutesBefore," минут назад: ",TimeCurrent()-minutesBefore*PeriodSeconds(PERIOD_M1),
"\nБар ",tf,"_0=",shift_0,", бар ",tf,"_1=",shift_1,
"\nМинуты текущего времени: ",server_time.min,
"\nЗначение RSI ",minutesBefore," минут назад на ",tf," : ",DoubleToString(prevRSIvalue0,4),
"\nЗначение RSI ",minutesBefore*2," минут назад на ",tf," : ",DoubleToString(prevRSIvalue1,4),
//---
"\nЗначение Close ",minutesBefore," минут назад > ",tf," : ",DoubleToString(prevClose_0,Digits()),
"\nЗначение Close ",minutesBefore*2," минут назад > ",tf," : ",DoubleToString(prevClose_1,Digits())
);
}
//+------------------------------------------------------------------+
double GetLastDataRSI(string symbol_name, ENUM_TIMEFRAMES timeframe, int shift, int period_rsi=14, ENUM_APPLIED_PRICE price_rsi=PRICE_CLOSE) {
return(iRSI(symbol_name,timeframe,period_rsi,price_rsi,shift));
}
//+------------------------------------------------------------------+
double GetPriceClose(string symbol_name,ENUM_TIMEFRAMES timeframe, int shift){
double array[1];
if(CopyClose(symbol_name,timeframe,shift,1,array)==1) return(array[0]);
return(-1);
}
//+------------------------------------------------------------------+
int GetBarShift(string symbol_name,ENUM_TIMEFRAMES timeframe,datetime time) {
if(time<0) return(-1);
//---
datetime array[], time0;
if(CopyTime(symbol_name,timeframe,0,1,array)<0) return(-1);
time0=array[0];
if(CopyTime(symbol_name,timeframe,time0,time,array)<0) return(-1);
datetime temptime=GetTime(symbol_name,timeframe,ArraySize(array)-1);
if(array[0]==temptime && temptime<=time) return(ArraySize(array)-1);
else return(ArraySize(array));
}
//+------------------------------------------------------------------+
datetime GetTime(string symbol_name,ENUM_TIMEFRAMES timeframe,int bar) {
if(bar<0) return(-1);
datetime array[];
if(CopyTime(symbol_name,timeframe,bar,1,array)>0) return(array[0]);
return(-1);
}
//+------------------------------------------------------------------+
Now we have to make a separate function to receive RSI data for the required time and compare prices and RSI values
Since we create it maximally close to MQL5, we won't use MQL4 functions for getting bar shift by time and closing price (iBarShift() and iClose() respectively).
We do our own functions for this: GetBarShift() and GetPriceClose(). In addition, we will need to get the bar time using GetTime() function instead of iTime(), since we reject standard MQL4 functions.
Here is a test Expert Advisor:
//| exTestValueRSI.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
//--- input parameters
input ENUM_TIMEFRAMES TimeframeRSI = PERIOD_M15; // Таймфрейм RSI
input int PeriodRSI = 14; // Период расчёта RSI
input ENUM_APPLIED_PRICE PriceRSI = PRICE_CLOSE; // Цена расчёта RSI
input int MinutesBefore =20; // Количество минут назад
//--- global variables
int minutesBefore; // Количество минут назад
int periodRSI; // Период расчёта RSI
//---
double prevRSIvalue0; // Значение RSI для заданного тф xxx минут назад
double prevRSIvalue1; // Значение RSI для заданного тф xxx минут назад-x минут
//---
double prevClose_0; // Значение Close для заданного тф xxx минут назад
double prevClose_1; // Значение Close для заданного тф xxx минут назад-x минут
//---
datetime timeBefore; // Время ххх минут назад
datetime timePrevBefore; // Время ххх минут назад-x минут
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
minutesBefore=(MinutesBefore<1?1:MinutesBefore); // Количество минут назад
periodRSI=(PeriodRSI<1?1:PeriodRSI);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
int shift_0;
int shift_1;
//---
MqlDateTime server_time;
TimeToStruct(TimeCurrent(),server_time);
string tf=EnumToString(TimeframeRSI);
//--- если значение минут серверного времени кратно заданному значению, в частности 20-ти минутам или равно 0
if(server_time.min%minutesBefore==0 || server_time.min==0) {
//--- время 1x и 2x минут назад
timeBefore=TimeCurrent()-minutesBefore*PeriodSeconds(PERIOD_M1);
timePrevBefore=TimeCurrent()-2*minutesBefore*PeriodSeconds(PERIOD_M1);
//--- смещение в барах времени 1х и 2х для заданного таймфрейма RSI (тф М15)
shift_0=GetBarShift(Symbol(),TimeframeRSI,timeBefore);
shift_1=GetBarShift(Symbol(),TimeframeRSI,timePrevBefore);
//--- значения RSI на барах 1х и 2х минут назад для заданного таймфрейма RSI (тф М15)
prevRSIvalue0=GetLastDataRSI(Symbol(),TimeframeRSI,shift_0);
prevRSIvalue1=GetLastDataRSI(Symbol(),TimeframeRSI,shift_1);
//--- значения цен закрытия баров 1х и 2х минут назад
prevClose_0=GetPriceClose(Symbol(),TimeframeRSI,shift_0);
prevClose_1=GetPriceClose(Symbol(),TimeframeRSI,shift_1);
}
Comment("\nТекущее время: ",TimeCurrent(),
"\nВремя ",minutesBefore," минут назад: ",TimeCurrent()-minutesBefore*PeriodSeconds(PERIOD_M1),
"\nБар ",tf,"_0=",shift_0,", бар ",tf,"_1=",shift_1,
"\nМинуты текущего времени: ",server_time.min,
"\nЗначение RSI ",minutesBefore," минут назад на ",tf," : ",DoubleToString(prevRSIvalue0,4),
"\nЗначение RSI ",minutesBefore*2," минут назад на ",tf," : ",DoubleToString(prevRSIvalue1,4),
//---
"\nЗначение Close ",minutesBefore," минут назад > ",tf," : ",DoubleToString(prevClose_0,Digits()),
"\nЗначение Close ",minutesBefore*2," минут назад > ",tf," : ",DoubleToString(prevClose_1,Digits())
);
}
//+------------------------------------------------------------------+
double GetLastDataRSI(string symbol_name, ENUM_TIMEFRAMES timeframe, int shift, int period_rsi=14, ENUM_APPLIED_PRICE price_rsi=PRICE_CLOSE) {
return(iRSI(symbol_name,timeframe,period_rsi,price_rsi,shift));
}
//+------------------------------------------------------------------+
double GetPriceClose(string symbol_name,ENUM_TIMEFRAMES timeframe, int shift){
double array[1];
if(CopyClose(symbol_name,timeframe,shift,1,array)==1) return(array[0]);
return(-1);
}
//+------------------------------------------------------------------+
int GetBarShift(string symbol_name,ENUM_TIMEFRAMES timeframe,datetime time) {
if(time<0) return(-1);
//---
datetime array[], time0;
if(CopyTime(symbol_name,timeframe,0,1,array)<0) return(-1);
time0=array[0];
if(CopyTime(symbol_name,timeframe,time0,time,array)<0) return(-1);
datetime temptime=GetTime(symbol_name,timeframe,ArraySize(array)-1);
if(array[0]==temptime && temptime<=time) return(ArraySize(array)-1);
else return(ArraySize(array));
}
//+------------------------------------------------------------------+
datetime GetTime(string symbol_name,ENUM_TIMEFRAMES timeframe,int bar) {
if(bar<0) return(-1);
datetime array[];
if(CopyTime(symbol_name,timeframe,bar,1,array)>0) return(array[0]);
return(-1);
}
//+------------------------------------------------------------------+
Now we have to make a separate function to get data for RSI from desired time and compare prices and RSI values
{
ticket=OrderSend(Symbol(),OP_SELL, 0.1, Ask, Slippage,0, 0, NULL, Magic, 0, Blue);
return(0);
}
{
int ticket;
if (OrdersTotal() == 0)
{
ticket=OrderSend(Symbol(),OP_SELL, 1, Bid, 0, 0, 0, NULL, 1234, 0, Red);
}
return(0);
}
I put it at the end of the code to check:
{
int ticket;
if (OrdersTotal() == 0)
{
ticket=OrderSend(Symbol(),OP_SELL, 1, Bid, 0, 0, 0, NULL, 1234, 0, Red);
}
return(0);
}
Who is stopping you from displaying the error code?
Who is stopping you from displaying the error code?
Dear forum users, Here is the question:
Is it possible in a tick block
int start() {}
Create an event which, when executed, will display a pop-up window in which you can manually set the value of a variable.
This is similar to what happens when you set a variable via Extern, but automatically during the execution of the program, rather than once at startup.
I would be grateful if you could at least tell me which way to dig.
Thank you!
Dear forum users, Here is the question:
Is it possible in a tick block
int start() {}
Create an event which, when executed, will display a pop-up window in which you can manually set the value of a variable.
This is similar to what happens when you set a variable via Extern, but automatically during the execution of the program, rather than once at startup.
I would be grateful if you could at least tell me which way to dig.
Thank you!
Dear forum users, Here is the question:
Is it possible in a tick block
int start() {}
Create an event which, when executed, will display a pop-up window in which you can manually set the value of a variable.
This is similar to what happens when you set a variable with Extern, but automatically during the execution of the program rather than once at startup.
I will be grateful if you tell me at least in what direction to dig.
Thank you!
Dig towards graphical objects. The input field in particular is OBJ_EDIT.
Here is a function to create it on a chart:
void SetEditField(const long chart_id=0, // ID графика
const string name="Edit", // имя объекта
const int sub_window=0, // номер подокна
const int x=0, // координата по оси X
const int y=0, // координата по оси Y
const int width=50, // ширина поля ввода
const int height=18, // высота поля ввода
const string text="Text", // текст
const string font="Calibri", // шрифт
const int font_size=8, // размер шрифта
const ENUM_ALIGN_MODE align=ALIGN_CENTER, // способ выравнивания
const ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER, // угол графика для привязки
const long z_order=0, // приоритет на нажатие мышью
const color clr=clrBlack, // цвет текста
const color back_clr=clrWhite, // цвет фона
const color border_clr=clrNONE, // цвет границы
const bool back=false, // на заднем плане
const bool read_only=false, // возможность редактировать
const bool selection=false, // выделить для перемещений
const bool hidden=true) // скрыт в списке объектов
{
if(ObjectFind(chart_id,name)<0) ObjectCreate(chart_id,name,OBJ_EDIT,sub_window,0,0);
ObjectSetInteger(chart_id,name,OBJPROP_XDISTANCE,x);
ObjectSetInteger(chart_id,name,OBJPROP_YDISTANCE,y);
ObjectSetInteger(chart_id,name,OBJPROP_XSIZE,width);
ObjectSetInteger(chart_id,name,OBJPROP_YSIZE,height);
ObjectSetString(chart_id,name,OBJPROP_TEXT,text);
ObjectSetString(chart_id,name,OBJPROP_FONT,font);
ObjectSetInteger(chart_id,name,OBJPROP_FONTSIZE,font_size);
ObjectSetInteger(chart_id,name,OBJPROP_ALIGN,align);
ObjectSetInteger(chart_id,name,OBJPROP_READONLY,read_only);
ObjectSetInteger(chart_id,name,OBJPROP_CORNER,corner);
ObjectSetInteger(chart_id,name,OBJPROP_COLOR,clr);
ObjectSetInteger(chart_id,name,OBJPROP_BGCOLOR,back_clr);
ObjectSetInteger(chart_id,name,OBJPROP_BORDER_COLOR,border_clr);
ObjectSetInteger(chart_id,name,OBJPROP_BACK,back);
ObjectSetInteger(chart_id,name,OBJPROP_SELECTABLE,selection);
ObjectSetInteger(chart_id,name,OBJPROP_SELECTED,selection);
ObjectSetInteger(chart_id,name,OBJPROP_HIDDEN,hidden);
ObjectSetInteger(chart_id,name,OBJPROP_ZORDER,z_order);
}
//+------------------------------------------------------------------+