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

 

Is it possible to pass dynamic input parameters to the indicator via iCustom? When I try to do this, a new indicator window opens, for each new dynamic input parameter.

   if(NewBarCur())BulsPeriod++;
   double sep_window=iCustom(Symbol(),PERIOD_CURRENT,"Bulls",BulsPeriod,0,0);
 
And this question, is there any way to know if, for example, there is 1 bar or 3 minutes left on the test chart?
 
Artyom Trishkin:

In this thread I want to begin to help those who really want to understand and learn programming in the new MQL4 and want to easily switch to MQL5 - the languages are very similar.

This will be a good place to discuss tasks, algorithms to solve them, and any other questions, anyway, related to MT programming.

I hope other experienced members of our forum will join us and the thread will be interesting to all.


Artyom Trishkin:

In this branch, I would like to begin to help those who really want to understand and learn programming in the new MQL4 and want to easily switch to MQL5 - the languages are very similar.

This will be a good place to discuss problems, algorithms and any other questions concerning MT programming.

I hope other experienced members of our forum will join us in resolving the issues and the thread will be of interest to everyone.


Artyom Trishkin:

In this branch, I would like to begin to help those who really want to understand and learn programming in the new MQL4 and want to easily switch to MQL5 - the languages are very similar.

This will be a good place to discuss problems, algorithms and any other questions concerning MT programming.

I hope other experienced members of our forum will join us and this topic will be interesting to all.


Artyom Trishkin:

In this branch, I would like to begin to help those who really want to understand and learn programming in the new MQL4 and want to easily switch to MQL5 - the languages are very similar.

This will be a good place to discuss problems, algorithms and any other questions concerning MT programming.

I hope other experienced members of our forum will join us and this topic will be interesting to all.


Artyom Trishkin:

In this branch, I would like to begin to help those who really want to understand and learn programming in the new MQL4 and want to easily switch to MQL5 - the languages are very similar.

This will be a good place to discuss tasks, algorithms of their solution and any other questions, one way or another, related to MT programming.

I hope other experienced members of our forum will join us and this topic will be interesting to all.


Artyom

Artyom Trishkin:

I want to begin this chapter to help those who really want to understand and learn programming in the new MQL4 and want to easily switch to MQL5 - the languages are very similar.

This is a place to discuss tasks, algorithms to solve them and any other questions that are related to MT programming one way or another.

I hope that other experienced members of our forum will join us in resolving the issues and the thread will be of interest to everyone.


Help me, I have added trailing stop function to my EA, I have tested it and it gives me two errors. - I have no idea how to fix them. However trades are closed according to old strategy after the price has touched the upper limit of the channel and the lower one, respectively. I guess something has to be changed here as well. - Call back to the guys from the army of the dnr.

#property copyright "Copyright 2017, MetaQuotes Software Corp.

#property link "https://www.mql5.com"

#property version "1.00"

#property strict


//---------------------------------------------------------

extern double Lots = 0.01;

extern int TakeProfit = 600;

extern int StopLoss = 25;

extern int Magic = 0001;

extern int Slippage = 3;

extern int TralType = 0; // 0-SAR, 1-ATR, 2-HMA.

extern double SAR_Step = 0.02;

extern double SAR_Max = 0.2;

extern int ATR_Period = 14;

extern double ATR_K = 2.0;

extern inttern HMA_Period = 16;

extern intern HMA_Method = 3;

extern inttern HMA_Shift = 0;

datetime LBT;

//---------------------------------------------------------

extern string TMA = "TMA indicator parameters";

extern string TimeFrame = "current time frame";

extern int HalfLength = 56;

extern int Price = "PRICE_CLOSE;

extern double ATRMultiplier = 2.0;

extern inttern ATRPeriod = 100;

extern bool Interpolate = true;

//---------------------------------------------------------

double PriceHigh, PriceLow, SL, TP;

int ticket;


//+------------------------------------------------------------------+

//| Expert initialization function |

//+------------------------------------------------------------------+

int OnInit()

{

if (Digits == 3 || Digits == 5)

{

TakeProfit *= 10;

StopLoss *= 10;

Slippage *= 10;

}

return(INIT_SUCCEEDED);

}

//+------------------------------------------------------------------+

//| Expert deinitialization function |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

{


}

//+------------------------------------------------------------------+

//| expert tick function |

//+------------------------------------------------------------------+

void OnTick()

{

PriceHigh = iCustom(NULL, 0, "TMA_Fair", TimeFrame, HalfLength, Price, ATRMultiplier, ATRPeriod, Interpolate, 1, 0);

PriceLow = iCustom(NULL, 0, "TMA_Fair", TimeFrame, HalfLength, Price, ATRMultiplier, ATRPeriod, Interpolate, 2, 0);

if(CountSell() == 0 && Bid >= PriceHigh)

{

ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, "TMA robot", Magic, 0, Red);

if (ticket > 0)

{

SL = NormalizeDouble(Bid + StopLoss*Point, Digits);

TP = NormalizeDouble(Bid - TakeProfit*Point, Digits);

if (OrderSelect(ticket, SELECT_BY_TICKET))

if (!OrderModify(ticket, OrderOpenPrice(), SL, TP, 0))

Print(" Ordermodification error!)

}

}

if (CountBuy() == 0 && Ask <= PriceLow)

{

ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, "TMA robot", Magic, 0, Blue);

if (ticket > 0)

{

TP = NormalizeDouble(Ask + TakeProfit*Point, Digits);

SL = NormalizeDouble(Ask - StopLoss*Point, Digits);

if (OrderSelect(ticket, SELECT_BY_TICKET))

if(!OrderModify(ticket, OrderOpenPrice(), SL, TP, 0))

Print("Error modifying the buy order!)

} else Print("Error opening the Buy order");

}

//+------------------------------------------------------------------+

//| Expert initialization function |

//+------------------------------------------------------------------+

int init()

{

//--------


//--------

return (0);

}

//+------------------------------------------------------------------+

//| expert tick function |

//+------------------------------------------------------------------+

void OnTick()

{

PriceHigh = iCustom(NULL, 0, "TMA_Fair", TimeFrame, HalfLength, Price, ATRMultiplier, ATRPeriod, Interpolate, 1, 0);

PriceLow = iCustom(NULL, 0, "TMA_Fair", TimeFrame, HalfLength, Price, ATRMultiplier, ATRPeriod, Interpolate, 2, 0);

if(CountSell() == 0 && Bid >= PriceHigh)

{

ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, "TMA robot", Magic, 0, Red);

if (ticket > 0)

{

SL = NormalizeDouble(Bid + StopLoss*Point, Digits);

TP = NormalizeDouble(Bid - TakeProfit*Point, Digits);

if (OrderSelect(ticket, SELECT_BY_TICKET))

if (!OrderModify(ticket, OrderOpenPrice(), SL, TP, 0))

Print("Error modifying the sell order!)

} else Print("Error opening the sell order!)

}

if (CountBuy() == 0 && Ask <= PriceLow)

{

ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, "TMA robot", Magic, 0, Blue);

if (ticket > 0)

{

TP = NormalizeDouble(Ask + TakeProfit*Point, Digits);

SL = NormalizeDouble(Ask - StopLoss*Point, Digits);

if (OrderSelect(ticket, SELECT_BY_TICKET))

if(!OrderModify(ticket, OrderOpenPrice(), SL, TP, 0))

Print("Error modifying the buy order!)

} else Print("Error opening the Buy order");

}

if (Ask <= PriceLow && CountSell() > 0)

{

for (int i = OrdersTotal() -1; i>0; i--)

{

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

{

if (OrderMagicNumber() == Magic && OrderType() == OP_SELL)

if (!OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Black))

Print("Sell order close error!)

}

}

}

if (Bid >= PriceHigh && CountBuy() > 0)

{

for (int i = OrdersTotal() -1; i>0; i--)

{

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

{

if (OrderMagicNumber() == Magic && OrderType() == OP_BUY)

if(!OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Black))

Print("OrderClose Buy error!)

}

}

}

}

//+------------------------------------------------------------------+

int CountSell()

{

int count = 0;

for (int trade = OrdersTotal()-1; trade>=0; trade--)

{

if(OrderSelect(trade, SELECT_BY_POS, MODE_TRADES))

{

if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == OP_SELL)

count++;

}

}

return(count);

}//+------------------------------------------------------------------+

int CountBuy()

{

int count = 0;

for (int trade = OrdersTotal()-1; trade>=0; trade--)

{

if(OrderSelect(trade, SELECT_BY_POS, MODE_TRADES))

{

if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == OP_BUY)

count++;

}

}

return(count);

}

//+------------------------------------------------------------------+

//| Expert deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//+-------


//+-------

return (0)

}

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int Start()

{

//-----

bool error = fals;

if (LBT!=Time[0]) {

if (OrdersTotal()!=0) {

for (int i=0; i<OrdersTotal(); i++) {

if (OrderSelect(i,SELECT_BY_POS)&&OrderSymbol()==Symbol()&&OrderType()<2) {

double SL = OrderStopLoss();

if OrderType()==0) {

switch (TralType) {

case 0: SL = iSAR(NULL,0,SAR_Step,SAR_Max,0);

break;

case 1: SL = High[1] - iATR(NULL,0,ATR,Period,1)*ATR_K;

break;

case 2: SL = iCustom(NULL,0, "VininI_HMAsound&amp",HMA_Period,HMA_Method,3,HMA_Shift, fals,fals,",1,0,0);

break;

}

if (SL<OrderStopLoss())

SL = OrderStopLoss();

}

if (OrderType()==1) {

switch (TralType) {

case 0: SL = iSAR(NULL,0,SAR_Step,SAR_Max,0);

break;

case 1: SL = Low[1] + iATR(NULL,0,ATR,Period,1)*ATR_K;

break;

case 2: SL = iCustom(NULL,0, "VininI_HMAsound&amp",HMA_Period,HMA_Method,3,HMA_Shift, fals,fals,",1,0,0);

break;

}

if (SL>OrderStopLoss())

SL = OrderStopLoss();

}

if (SL!=OrderStopLoss()) {

if(!OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0))

error = true;

}

}

}

}

if (!error)

LBT = Time[0];

}

return (0);

}

//+------------------------------------------------------------------+

 
vkravtzov:

Learn how to insert code!!!


Learn to insert code!!!


Learn to insert code!!!


Learn how to insert code!!!


Learn how to insert code!!!


Learn how to insert code!!!


Learn how to insert code!!!


Learn how to insert code!!!


Learn how to insert code!!!


Learn to insert code!!!

 
vkravtzov: Help me to understand, I have added trailing stop function to Expert Advisor, I have tested it and it gives two errors. - I have cracked my brains, I cannot figure out how to fix them. However trades are closed according to old strategy after the price has touched the upper limit of the channel and the lower one, respectively. I guess something has to be changed here as well. - Call back to the guys in the army of the dnr.

Decided to help. But I have questions for the author - you have these two initialisation functions - what do they do?

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   if (Digits == 3 || Digits == 5)
   {
   TakeProfit *= 10;
   StopLoss   *= 10;
   Slippage   *= 10;
   }
   return(INIT_SUCCEEDED);
}


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//--------
//--------
    return (0);
    }

There are twoOnTick() functions.... And a third function int Start() - all of them are executed with the arrival of the tick. And in what order will the terminal start them?

Error: a closing curly bracket is missing at the end of the OnTick() function

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
  PriceHigh = iCustom(NULL, 0, "TMA_Fair", TimeFrame, HalfLength, Price, ATRMultiplier, ATRPeriod, Interpolate, 1, 0);
  PriceLow  = iCustom(NULL, 0, "TMA_Fair", TimeFrame, HalfLength, Price, ATRMultiplier, ATRPeriod, Interpolate, 2, 0);
  if(CountSell() == 0 && Bid >= PriceHigh)
  {
     ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, "TMA robot", Magic, 0, Red);
     if (ticket > 0)
     {
        SL = NormalizeDouble(Bid + StopLoss*Point, Digits);
        TP = NormalizeDouble(Bid - TakeProfit*Point, Digits);
        if (OrderSelect(ticket, SELECT_BY_TICKET))
           if (!OrderModify(ticket, OrderOpenPrice(), SL, TP, 0))
                Print("Ошибка модификации ордера на продажу!");
     }
  }
  if (CountBuy() == 0 && Ask <= PriceLow)
  {
     ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, "TMA robot", Magic, 0, Blue);
     if (ticket > 0)
     {
        TP = NormalizeDouble(Ask + TakeProfit*Point, Digits);
        SL = NormalizeDouble(Ask - StopLoss*Point, Digits);
        if (OrderSelect(ticket, SELECT_BY_TICKET))
            if(!OrderModify(ticket, OrderOpenPrice(), SL, TP, 0))
                Print("Ошибка модификации ордера на покупку!");
     }   else Print("Ошибка открытия ордера на покупку");
   }
}    // Эту скобку добавил !!!!!!!!!!!!!!!!!!!!!!!!!

Now the compiler has detected 8 errors. I deleted .

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//--------
//--------
    return (0);
    }


//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
//+-------
//+-------
   return (0);
   }

There are 7 errors now.

I replaced if(OrderType()==0) with a parenthesis after if: if(OrderType()==0) - 8 errors occurred

iATR(NULL,0,ATR,Period,1) - we need iATR(NULL,0,ATR_Period,1) - 2 errors occurred

bool error = fals; we need bool error = false; - errors became 3

Where can we get the VininI_HMAsound&amp indicator? What parameters are there? = the error would still be 1

And why would you ask a question in three places?

 

Help download and install free EAs and signals on mt4 not as neemogu find download vide only how to install, but no download. Tell me where to look

 
vovik36 Степанов:

Help download and install free EAs and signals on mt4 not as neemogu find download vide only how to install, but no download. Please advise where to look.

First graduate, then come.

 
Artyom Trishkin:

How about that?


   for(i=pos; i<rates_total && !IsStopped(); i++)
     {
      double dmin=1000000.0;
      double dmax=-1000000.0;
      for(k=i-InpKPeriod+1; k<=i; k++)
       {double ima=iMA(Symbol(),1,60,0,1,0,k);
         if(dmin>ima)  dmin=ima;
         if(dmax<ima)  dmax=ima;
                   Comment(ima);
        }
      ExtLowesBuffer[i]=dmin;
      ExtHighesBuffer[i]=dmax;
     }

Hi. Can you tell me why it doesn't seeima=iMA(Symbol(),1,60,0,1,0,k). The comment shows a much higher quote than the line itself. It's the reason why minimum and maximum values are not calculated correctly.

 
Rustam Bikbulatov:

Hi. Can you tell me why it doesn't seeima=iMA(NULL,60,24,0,1,0,k). Through the comment it shows a much higher quote than the line itself. This is the reason why minimum and maximum values are not calculated correctly.

iMA is numbering as in timeseries, 0 is the most relevant number. It may be vice versa in your case
 
Maxim Kuznetsov:
iMA is numbering as in timeseries, 0 is the most relevant number. And you probably have it the other way around.

It's an unintentional bet. It doesn't change anything. The very point is that it shows incorrectly with k!

Reason: