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

 
forexman77:

I tried it on another DC and it seems to be OK. The question is why the call:

outputs 0.01, but prints 131 on the bot?

The lots need to be normalised when passed to the function.

 
Artyom Trishkin:

Lots need to be normalised when they are transferred to the function.


I actually wrote that I was normalising. There is a limit to the number of lots in auto-trading.

You don't need to be Captain Obvious here.
 
forexman77:

Actually, I wrote that I was normalising. There is some kind of limit on the number of lots in auto trading.

Don't go all Captain Obvious here.

Well, look for yourself, if that's how you want to communicate.

 
rabanik:

Thank you very much for the reply.

Here's the whole function, the point is that it opens an order and then adds constant stop loss and take profit values through modification.

The order is opened but stop loss and take profit are not set (the calculation function in red, in whichOrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES) gives an error ((() )

//Open Order function, variables: symbol, order type, lot, open price, TP and SL calculation method (0 - set in the price, 1 - set in points that should be calculated from the open price), stop loss, take profit)

//Symbol/Pair, open order type - pending, open price, switch = 1, SL B TP - stop loss and take profit values are constant!

bool SendOrder(string Symb,int Type, double OP,int Mode) {
color CL;
double Pp = MarketInfo(Symb,MODE_POINT);
if (Type==0) CL=Blue;
if (Type==1) CL=Red;
if (Type==2 || Type==4) CL=DarkTurquoise;
if (Type==3 || Type==5) CL=Orange;
//verification of volume
if(MarketInfo(Symb,MODE_LOTSTEP)==0.01) DG=2; else DG=1;
if (Lot<MarketInfo(Symb,MODE_MINLOT)) Lot=MarketInfo(Symb,MODE_MINLOT);
if (Lot>MarketInfo(Symb,MODE_MAXLOT)) Lot=MarketInfo(Symb,MODE_MAXLOT);
if (Lot*MarketInfo(Symb,MODE_MARGINREQUIRED)>AccountEquity()) {
PnC(StringConcatenate("Not enough money to open trade",Type," volume: ",DoubleToStr(Lot,DG)),0);
return;
}
// check pending orders
/*
double Slv = MarketInfo(Symb,MODE_STOPLEVEL)*Pp;
if (Type>1) {
if (Type==2 && MarketInfo(Symb,MODE_ASK)-OP<Slv) OP=MarketInfo(Symb,MODE_ASK)-Slv;
if (Type==3 && OP-MarketInfo(Symb,MODE_BID)<Slv) OP=MarketInfo(Symb,MODE_BID)+Slv;
if (Type==4 && OP-MarketInfo(Symb,MODE_ASK)<Slv) OP=MarketInfo(Symb,MODE_ASK)+Slv;
if (Type==5 && MarketInfo(Symb,MODE_BID)-OP<Slv) OP=MarketInfo(Symb,MODE_BID)-Slv;
}
*/
RefreshRates();
int Min_Dist = MarketInfo(Symb,MODE_STOPLEVEL); //Limit stop loss/stake profit level in pips. Min. distance
double Tek_Ask = MarketInfo(Symb,MODE_ASK); //Last trade price received. It is stored in the predefined variable Ask for the current symbol
double Tek_Bid = MarketInfo(Symb,MODE_BID); //The last bid price received. It is stored in the predefined variable Bid for the current symbol
double Tek_Point = MarketInfo(Symb,MODE_POINT); //Point size in the quote currency. It is stored in the predefined variable Point for the current symbol
int Tek_Digits = MarketInfo(Symb,MODE_DIGITS);//The number of digits after the decimal point in the symbol price. For the current symbol it is stored in the predefined variable Digits

double Slv = NormalizeDouble(Min_Dist*Tek_Point,Tek_Digits);
if (Type>1) {
if (Type==2 && Tek_Ask - OP < Slv) OP = Tek_Ask - Slv;
if (Type==3 && OP - Tek_Bid < Slv) OP = Tek_Bid + Slv;
if (Type==4 && OP - Tek_Ask < Slv) OP = Tek_Ask + Slv;
if (Type==5 && Tek_Bid - OP < Slv) OP = Tek_Bid - Slv;
}
// check stops
if (SL!=0 || TP!=0) {
if (Mode==0) {
if (MathAbs(OP-SL)<Slv && SL!=0) {
if (Type==0 || Type==2 || Type==4) SL=OP-Slv; else SL=OP+Slv;
}
if (MathAbs(OP-TP)<Slv && TP!=0) {
if (Type==0 || Type==2 || Type==4) TP=OP+Slv; else TP=OP-Slv;
}
}else{
if (SL*Pp<Slv && SL!=0) SL=Slv/Pp;
if (TP*Pp<Slv && TP!=0) TP=Slv/Pp;
}
}
//opening
for(int k=0;k<=TryToTrade;k++) {
if (Type==0) OP=MarketInfo(Symb,MODE_ASK);
if (Type==1) OP=MarketInfo(Symb,MODE_BID);
PnC(StringConcatenate("Attempting to open an order, type:",Type," volume:",Lot," price:",OP),0);
if (IsTradeAllowed()) {
int Ticket=OrderSend(Symb,Type,Lot,NormalizeDouble(OP,MarketInfo(Symb,MODE_DIGITS)),3,0,0,NULL,Tek_Magic,0,CL);
}else{ PnC(StringConcatenate("Cannot open order ",k),0); Sleep(3000); continue; }
if (Ticket >= 0) { PnC(StringConcatenate("Order opened ",Ticket),0); break; }
LastError=Fun_Error(GetLastError());
if (LastError==0) {
RefreshRates(); Sleep(WaitTime);
if (k==TryToTrade) return(false); }
if (LastError==1) return(false);
if (LastError==2) { Work=false; return(false); }
}
// set stops
OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES);
if (SL==0 && TP==0) return;
if (Mode==1) {
if (SL!=0) {
if (Type==0 || Type==2 || Type==4) SL=OrderOpenPrice()-SL*Pp;
if (Type==1 || Type==3 || Type==5) SL=OrderOpenPrice()+SL*Pp;
}
if (TP!=0) {
if (Type==0 || Type==2 || Type==4) TP=OrderOpenPrice()+TP*Pp;
if (Type==1 || Type==3 || Type==5) TP=OrderOpenPrice()-TP*Pp;
}
}

for(k=0;k<=TryToTrade;k++) {
PnC(StringConcatenate("Trying to set stops on order: ",Ticket," s/l: ",SL," t/p: ",TP),0);
if (IsTradeAllowed()) {
//TickeT=OrderModify(Ticket,NormalizeDouble(OrderOpenPrice(),MarketInfo(Symb,MODE_DIGITS)),NormalizeDouble(Symb,MarketInfo(Symb,MODE_DIGITS)),NormalizeDouble(TP,MarketInfo(Symb,MODE_DIGITS)),0,CLR_NONE);
TickeT=OrderModify(Ticket,NormalizeDouble(OrderOpenPrice(),MarketInfo(Symb,MODE_DIGITS)),NormalizeDouble(SL,MarketInfo(Symb,MODE_DIGITS)),NormalizeDouble(TP,MarketInfo(Symb,MODE_DIGITS)),0,CLR_NONE);
}else{ PnC(StringConcatenate("Cannot modify order ",k),0); Sleep(3000); continue; }
if (TickeT == true) { PnC(StringConcatenate("Order modified ",Ticket),0); break; }
LastError=Fun_Error(GetLastError());
if (LastError==0) {
RefreshRates(); Sleep(WaitTime);
if (k==TryToTrade) return(false); }
if (LastError==1) return(false);
if (LastError==2) { Work=false; return(false); }
}
return(true);

}

Now the next error on the lineif (Type==0 || Type==2 || Type==4) SL=OrderOpenPrice()-SL*Pp;. The OrderOpenPrice() function returns the same error - Expression could not be evaluated. What a mess!!!
 
rabanik:
Now here is the next error on the lineif (Type==0 || Type==2 || Type==4) SL=OrderOpenPrice()-SL*Pp;. The OrderOpenPrice() function returns the same error - Expression could not be evaluated. What a mess!!!

It's all sorted. Sorry for the stupid question. I have a main variable SL of int type.

And I'm trying to get SL with type double again. But the system returns 1.

Thanks.

 

Can anyone suggest how to make an EA work from a certain place back to its beginning, without continuing to execute further operators.

 

Hello. The question is as follows, the EA does not open a sell trade. Can you take a look at it?

 if (Digits==3||Digits==5) {sl=SL*10*Point;}
         else if (Digits==4||Digits==2) {sl=SL*Point;}
            if (OrdersTotal()==0)
              {
                if (Close[1]>TD2)
                OrderSend (Symbol(),OP_BUY,Lot,Ask,10,Ask-sl,0,NULL,Magic,0,clrBlue);
                else if (Close[1]<TD1)
                OrderSend (Symbol(),OP_SELL,Lot,Bid,10,Bid+sl,0,NULL,Magic,0,clrRed);
              }
 
6737998:

Can anyone suggest how to make an EA work from a certain place back to its beginning, without continuing to execute further operators.

Оператор возврата return - Операторы - Основы языка - Справочник MQL4
Оператор возврата return - Операторы - Основы языка - Справочник MQL4
  • docs.mql4.com
Оператор возврата return - Операторы - Основы языка - Справочник MQL4
 
danil77783:

Hello. The question is as follows, the EA does not open a sell trade. Can you take a look at it?

What is TD1 and where is it assigned a value?
 

Friends, how to get out of this situation correctly... The indicator draws two horizontal lines from CSV (Red - from the beginning of the day to the end of the day) Blue - Beam coming from the past days, at different price levels. But there are days when these levels have the same value/price. What is the right way out of this situation, so that when the lines coincide, the Ray(Blue) is displayed, and the Red line is not visible on the chart, but physically present...i.e., it is transparent.

Now - Draw_Level("Red", red, DRAW_NONE, STYLE_SOLID,0); it stops drawing the line, but the Ray is not visible from under it either. Thanks.