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

 

@Artyom Trishkin@Konstantin Nikitin

Tell me, is it correct to poll thevoid NOTIFICATION() function through the int start? There's a constant loop there! Maybe it would be more correct to put this logic directly into start? So as not to overload the function! And I originally wrote it in such a way that it would be more pleasant to read it myself!

 
Rewerpool:

@Artyom Trishkin@Konstantin Nikitin

Tell me, is it correct to poll thevoid NOTIFICATION() function through the int start? There's a constant loop there! Maybe it would be more logical to put this logic into start at once?

Use OnTick(). It all depends on the required logic.
 
Can you please tell me where to insert this ?//+------------------------------------------------------------------+
//|  Проверяет объем ордера на корректность                          |
//+------------------------------------------------------------------+
bool CheckVolumeValue(double volume,string &description)
  {
//--- минимально допустимый объем для торговых операций
   double min_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);
   if(volume<min_volume)
     {
      description=StringFormat("Объем меньше минимально допустимого SYMBOL_VOLUME_MIN=%.2f",min_volume);
      return(false);
     }

//--- максимально допустимый объем для торговых операций
   double max_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);
   if(volume>max_volume)
     {
      description=StringFormat("Объем больше максимально допустимого SYMBOL_VOLUME_MAX=%.2f",max_volume);
      return(false);
     }

//--- получим минимальную градацию объема
   double volume_step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);

   int ratio=(int)MathRound(volume/volume_step);
   if(MathAbs(ratio*volume_step-volume)>0.0000001)
     {
      description=StringFormat("Объем не является кратным минимальной градации SYMBOL_VOLUME_STEP=%.2f, ближайший корректный объем %.2f",
                               volume_step,ratio*volume_step);
      return(false);
     }
   description="Корректное значение объема";
   return(true);
  }
 

The Expert Advisor code itself


enum t
{
e=1, // by Parabolic indicator
};
enum tf
{
af=0, // current
bf=1, // 1 minute
cf=2, // 5 minutes
df=3, // 15 minutes
ef=4, // 30 minutes
ff=5, // 1 hour
gf=6, // 4 hours
hf=7, // 1 day
};

extern int Magic = 123654789;
extern inttern Slippage = 0;
inttern double Lots = 1;
extern int int rsi_period = 14;
extern int rsi_high = 50;
int rsi_price = PRICE_CLOSE;
extern int rsi_low = 50;
int loss_stop = 5000;
extern int take_profit = 5000;
double sar_step = 0.03;
double sar_max = 0.03;

int GeneralNoLoss = false; // trailing from Breakeven point
int parameters_trailing = e; // trawl method
int StartTrall = -5000; // minimal trawl profit in points
int StepTrall = 1; //step trailing stop loss
input tf TF_Tralling = af; // indicators timeframe (0-current)
extern int delta = 50; // indent from the calculated stop loss level

double Step = 0.03; //Parabolic Step (method 4)
double Maximum = 0.03; //Parabolic Maximum (method 4)


color text_color = Lime; //color of information output

double ma, rsi, sar_1, sar_2, ma_1, ma_2, ma_3, ma_4, ma_5, ma_6;
datetime LastBuyTime, LastSellTime;

int STOPLEVEL;
int TF[10]={0,1,5,15,30,60,240,1440,10080,43200};
double SLB=0,SLS=0;


int OnInit()

{
//---

//---
return(0);
}

void OnTick(){
STOPLEVEL=(int)MarketInfo(Symbol(),MODE_STOPLEVEL);

int b=0,s=0;
double OOP,price_b=0,price_s=0,lot=0,NLb=0,NLs=0,LS=0,LB=0;
int tip,Ticket;
bool error;
double SL,OSL;
int n=0;
if (b==0) SLB=0;
if (s==0) SLS=0;
for (int i=OrdersTotal(); i>=0; i--)
{
if (OrderSelect(i, SELECT_BY_POS)==true)
{
tip = OrderType();
if (tip<2 && (OrderSymbol()==Symbol()) && (OrderMagicNumber()==Magic ))
{
OSL = OrderStopLoss();
OOP = OrderOpenPrice();
Ticket = OrderTicket();
n++;
if (tip==OP_BUY)
{
if (GeneralNoLoss)
{
SL = SlLastBar(OP_BUY,Bid,NLb);
if (SL < NLb+StartTrall*Point) continue;
}
else
{
SL = SlLastBar(OP_BUY,Bid,OOP);
if (SL < OOP+StartTrall*Point) continue;
}
//if (OSL >= OOP && only_NoLoss) continue;
if (SL >= OSL + StepTrall*Point && (Bid-SL)/Point>STOPLEVEL)
{
error=OrderModify(Ticket,OOP,SL,OrderTakeProfit(),0,White);
if (!error) Comment("TrailingStop Error ",GetLastError()," order ",Ticket," SL ",SL);
else Comment("TrailingStop ",Ticket," ",TimeToStr(TimeCurrent(),TIME_MINUTES));
}
}
if (tip==OP_SELL)
{
if (GeneralNoLoss)
{
SL = SlLastBar(OP_SELL,Ask,NLs);
if (SL > NLs-StartTrall*Point) continue;
}
else
{
SL = SlLastBar(OP_SELL,Ask,OOP);
if (SL > OOP-StartTrall*Point) continue;
}
//if (OSL <= OOP && only_NoLoss) continue;
if ((SL <= OSL-StepTrall*Point || OSL==0) && (SL-Ask)/Point>STOPLEVEL)
{
error=OrderModify(Ticket,OOP,SL,OrderTakeProfit(),0,White);
if (!error) Comment("TrailingStop Error ",GetLastError()," order ",Ticket," SL ",SL);
else Comment("TrailingStop ",Ticket," ",TimeToStr(TimeCurrent(),TIME_MINUTES));
}
}
}
}
}
ma = iMA(Symbol(), 0, 70, 0, MODE_SMA, PRICE_CLOSE, 1);
rsi = iRSI(Symbol(), 0, rsi_period, rsi_price, 1);
sar_1 = iSAR(Symbol(), 0, sar_step, sar_max, 1);
sar_2 = iSAR(Symbol(), 0, sar_step, sar_max, 2);


if ((sar_step))
{
LastBuyTime = Time[0];
if(OrderSend(Symbol(), OP_BUY, Lots, NormalizeDouble(Ask,Digits), Slippage, Ask - loss_stop*Point, Ask + take_profit*Point, "Order", Magic, 0, clrGreen) > 0){
Print("Buy Order has been sent!");
return;
}else Print("Buy Order Error");
}
if ((hhchhchhchhchhchhchh))
{
LastSellTime = Time[0];
if(OrderSend(Symbol(), OP_SELL, Lots, NormalizeDouble(Bid, Digits), Slippage, Bid + loss_stop*Point, Bid - take_profit*Point, "Order", Magic, 0, clrRed) > 0)
{
Print("Sell Order has been sent!");
return;
}else Print("Sell Order Error");
}
}

//--------------------------------------------------------------------
double SlLastBar(int tip,double price,double OOP)
{
double prc=0;
switch(parameters_trailing)
{
case 1: // by Parabolic indicator
prc = iSAR(Symbol(),TF[TF_Tralling],Step,Maximum,0);
if (tip==OP_BUY)
{
prc = NormalizeDouble(prc - delta*Point,Digits);
if(price-STOPLEVEL*Point < prc) prc=0;
ARROW("cm_SL_Buy", prc, 4, clrAqua);
}
if (tip==OP_SELL)
{
prc = NormalizeDouble(prc + delta*Point,Digits);
if(price+STOPLEVEL*Point > prc) prc=0;
ARROW("cm_SL_Sell", prc, 4, clrRed);
}
break;
}
return(prc);
}

//--------------------------------------------------------------------
string StrPer(int per)
{
if (per == 0) per=Period();
if (per == 1) return("M1");
if (per == 5) return("M5");
if (per == 15) return("M15");
if (per == 30) return("M30");
if (per == 60) return("H1");
if (per == 240) return("H4");
if (per == 1440) return("D1");
if (per == 10080) return("W1")
if (per == 43200) return("MN1")
return("period error");
}
//+------------------------------------------------------------------+
void ARROW(string Name, double Price, int ARROWCODE, color c)
{
ObjectDelete(Name);
ObjectCreate(Name,OBJ_ARROW,0,Time[0],Price,0,0,0);
ObjectSetInteger(0,Name,OBJPROP_ARROWCODE,ARROWCODE);
ObjectSetInteger(0,Name,OBJPROP_SELECTABLE, false);
ObjectSetInteger(0,Name,OBJPROP_SELECTED, false);
ObjectSetInteger(0,Name,OBJPROP_COLOR, c);
ObjectSetInteger(0,Name,OBJPROP_WIDTH, 1);
}
//--------------------------------------------------------------------
color Color(bool P,color a,color b)
{
if (P) return(a);
return(b);
}
//--------------------------------------------------------------------

 
Vladimir Ozharovskiy:
Can you please tell me where to insert this ?//+------------------------------------------------------------------+

Well.... if so critical

make it so:

//+------------------------------------------------------------------+
//|  Проверяет объем ордера на корректность                          |
//+------------------------------------------------------------------+

nice...)))

 
Alekseu Fedotov:

Well.... if so critical

make it so:

(beautiful...)))

But seriously ?)

 
Vladimir Ozharovskiy:

But seriously?)

If you mean the function itself, copy and paste it to the very end of the program,

necessarily outside the void OnTick() function

 
Vladimir Ozharovskiy:

But seriously ?)

I take it this function has no bidding, checks andreports

 
Alekseu Fedotov:

If you mean the function itself, copy and paste it to the end of the program,

make sure it's outside the void OnTick() function.

And then what should a man do? You have to call it correctly!

 
It seems to have helped... Thank you!
Reason: