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

 
Top2n:

How about this?

if( number of indicator bars == number of EA bars )

 
Alexey Kozitsyn:

if( number of indicator bars == number of EA bars )


So how do I get the number of indicator bars in the EA?

 
Top2n:

So how do I get the number of bars of the indicator in the EA?

BarsCalculated().
 

Hello, could you please tell me what to write in brackets for the return function?

in the old version you may not write anything, but in the newer version you need to write something

I will send you a screenshot of the part of the EA as an example

 
kirill1405:

Hello, could you please tell me what to write in brackets for the return function?

In the old version you may not write anything, but in the newer version you need to write something

I will send you a screenshot of the part of the EA as an example

You have to write the value/variable that corresponds to the function type in brackets. If the function is a bool, then in brackets should be true or false, or a variable of the bool type. For other function types, the same way. If the function is void - just return;
 
Alexey Kozitsyn:
The value/variable corresponding to the function type should be written in brackets. If the function is a bool, it should be true or false, or a variable of the bool type in brackets. For other function types - similarly. If the function is void - just return;

Could you tell me from the picture I dropped what is needed for the return function?

to get a better idea of what it should look like.

Thank you!

 
kirill1405:

Could you tell me from the picture I dropped what is needed for the return function?

to get a better idea of what it should look like.

Thank you!

As I said, in order to return a correct value, you need to know the function type. And the picture doesn't show it.
 

Here you go, if it's not too much trouble

Files:
lmdf9hx.mq4  10 kb
 
kirill1405:

Here you go, if it's not too much trouble

Paste the code via the SRC button, no desire to download it, look at it and delete it.
 
Alexey Kozitsyn:
Paste the code via the SRC button, no desire to download it, look at it and delete it.

extern double StopLoss = 100;
extern double TakeProfit = 400;
extern int Period_MA_1 = 100;
extern int Period_MA_2 = 150;
extern double  Rasst = 40;
extern double Lots = 0.5;


bool Work = true;
string Symb;
//------------------------------------------------2
int start()
{
int
Total,     //количество ордеров в окне
Tip = -1,
Ticket;    // тип выбранного ордера 
double
MA_1_t,    // текущее значение средней №1 
MA_2_t,    // текущее значение средней №2
Lot,       // количество лотов в выбранном ордере
Lts,       // Количество лотов в открыв. ордере 
Min_Lot,   // минимальное количество лотов
Step,      // шаг изменения размера лота
Free,     // текущие свободные средства
One_Lot,  // стоимость одного лота
Price,    // цена выбранного ордера
SL,      // стоплосс выбранного ордера 
TP;      // тейкпрофит выбранного ордера 
bool
Ans = false,
Cls_B = false,
Cls_S = false,
Opn_B = false,
Opn_S = false;
//----------------------------------------------  3
// предварительная обработка
if (Bars < Period_MA_2)
{
Alert ("Недостаточно баров в окне. Эксперт не работает");
}
return;
if (Work==false)
{
Alert ("Критическая ошибка. Эксперт не работает");
}
return;
//здесь можно указать разные параметры например задать время работы эксперта
//----------------------------------------------  4
// учет ордеров
Symb = Symbol();
Total =0;
for (int i=1;i<=OrdersTotal();i++)
{
if (OrderSelect(i-1,SELECT_BY_POS)==true)
{
if (OrderSymbol()!= Symb) continue;
if ( OrderType()>1)
{
Alert ("Обнаружен отложенный ордер. Эксперт не работает");
return  ;
}
Total++;     //счетчик рыночных ордеров
if (Total>1)
{
Alert ("Несколько рыночных ордеров. Эксперт не работает.");
return ;
}
Ticket=OrderTicket();
Tip=OrderType();
Price=OrderOpenPrice();
SL=OrderStopLoss();
TP=OrderTakeProfit();
Lot=OrderLots();
}
}
// Торговые критерии
MA_1_t=iMA(NULL,0,Period_MA_1,0,MODE_EMA,PRICE_CLOSE,0);
MA_2_t=iMA(NULL,0,Period_MA_2,0,MODE_EMA,PRICE_CLOSE,0);
if (MA_1_t>MA_2_t+Rasst*Point)
{
Opn_B=true;
Cls_S=true;
}
if (MA_1_t<MA_2_t-Rasst*Point)
{
Opn_S=true;
Cls_B=true;
}
//--------------------------------------------
// закрытие ордеров
while (true)
{
if (Tip==0 && Cls_B==true)
{
Alert ("Попытка закрыть Buy ",Ticket,"ожидание ответа..");
RefreshRates();
Ans=OrderClose(Ticket,Lot,Bid,2);
if (Ans==true)
{
Alert ("Закрыт ордер Buy" ,Ticket);
break;
}
if  (Fun_Error(GetLastError())==1)
continue;
return;
}
if (Tip==1 && Cls_S==true)
{
Alert ("Попытка закрыть Sell ",Ticket,"ожидание ответа..");
RefreshRates();
Ans=OrderClose(Ticket,Lot,Ask,2);
if (Ans==true)
{
Alert ("Закрыт ордер Sell" ,Ticket);
break;
}
if (Fun_Error(GetLastError())==1)
continue;
return;
}
break;
}
//-------------------------------------------------
// расчет размера позиций
RefreshRates();
Min_Lot=MarketInfo(Symb,MODE_MINLOT);
Free = AccountFreeMargin();
One_Lot = MarketInfo(Symb,MODE_MARGINREQUIRED);
Step = MarketInfo(Symb,MODE_LOTSTEP);
if (Lots>0)
Lts=Lots;
if (Lts<Min_Lot)
Lts=Min_Lot;
if (Lts*One_Lot<Free)
{
Alert ("Не хватает денежных средств на ",Lts," лотов");
return;
}
//--------------------------------------------
// блок открытия ордеров
while (true)
{
if (Total==0 && Opn_B==true)
{
RefreshRates();
SL=Bid-New_Stop(StopLoss)*Point;
TP=Bid+New_Stop(TakeProfit)*Point;
Alert ("Попытка открыть ордер BUY. Ожидаем ответа..");
Ticket = OrderSend(Symb,OP_BUY,Lts,Ask,2,SL,TP);
if (Ticket>0)
{
Alert (" Открыт ордер BUY" ,Ticket);
return ;
}
if (Fun_Error(GetLastError())==1)
continue;
return ;
}
if (Total==0 && Opn_S==true)
{
RefreshRates();
SL=Ask-New_Stop(StopLoss)*Point;
TP=Ask+New_Stop(TakeProfit)*Point;
Alert ("Попытка открыть ордер Sell. Ожидаем ответа..");
Ticket = OrderSend(Symb,OP_SELL,Lts,Bid,2,SL,TP);
if (Ticket>0)
{
Alert (" Открыт ордер Sell" ,Ticket);
return ;
}
if (Fun_Error(GetLastError())==1)
continue;
return ;
}
break;
}
//-----------------------------------------------------------
return;
}
//---------------------------------------------------------
int Fun_Error (int Error)
{
switch (Error)
{
default : Alert ("Возникла ошибка ",Error);
return(0);
}
}
//-------------------------------------------------------------
int New_Stop (int Parametr)
{
int Min_Dist=MarketInfo(Symb,MODE_STOPLEVEL);
if (Parametr<Min_Dist)
{
Parametr=Min_Dist;
Alert ("Увеличена дистанция стоп приказа");
}
return(Parametr);
}
//------------------------------------------------------------