[Archive!] Any rookie question, so as not to clutter up the forum. Professionals, don't pass it by. Couldn't go anywhere without you - 2. - page 81

 

extern bool BUY = true;
extern int Magic = 0;
extern double Lot = 0.1;
extern int takeprofit = 0;
extern int stoploss = 0;
extern int slippage = 3;
double SL,TP;
int init(){
return(0);
}
int start()
{
if (BUY)
{
if (takeprofit!=0) TP = NormalizeDouble(Ask + takeprofit*Point,Digits); else TP=0;
if (stoploss!=0) SL = NormalizeDouble(Ask - stoploss*Point,Digits); else SL=0;

BUY=false;
}

return(0);
}
void OPENORDER(string ord)
{
int error;
while (true)
{ error=true;
if (ord=="Buy") error=OrderSend(Symbol(),OP_BUY, Lot,NormalizeDouble(Ask,Digits),slippage,SL,TP,",Magic,0){BUY = false;}
}
return;
}

 
nemo811:

Please give me an example. I'm just learning, and haven't faced the task of setting up a flag yet. )
Just change the variable to false and that's it, on the next tick the EA will not enter this block and it will not open anything else until it is restarted.
 
nemo811:

Please advise how to allow a single execution of a condition in an EA (e.g. opening an order), rather than repeating it with every tick. In this case, when closing a position, the EA should not open a new one. I have tried to do it, but it doesn't work as intended.

Thanks in advance.



why did you name the variable where the order ticket will be saved error ?

if (ord=="Buy") error=OrderSend(Symbol(),OP_BUY, Lot,NormalizeDouble(Ask,Digits),slippage,SL,TP,"",Magic,0){BUY = false;}

Read about OrderSend(), I think the questions will go away, I would write it like this:

void OPENORDER(string ord)
{
int ticket = -1;
 while (ticket<0)
 {ticket=OrderSend(Symbol(),OP_BUY, Lot,NormalizeDouble(Ask,Digits),slippage,SL,TP,"",Magic,0);
  sleep(500);
}
return;
}
 
IgorM:



Maybe you read earlier I voiced my problem. They seem to have found a solution, but I can't find in the book how to work with string in mql(

how do I take the numbers out of the line by line?

 
NameLess:


Maybe you read earlier I voiced my problem. They seem to have found a solution, but I can't find in the book how to work with string in mql(

how do I take the numbers out of the line by line?


Create a script to experiment with MQL4 string functions - see here https://docs.mql4.com/ru/strings
 

ZERO

under what rules, for example?

 
drknn:

Create a script that experiments with MQL4 string functions - see here https://docs.mql4.com/ru/strings/StringSubstr

Thank you!
 
NameLess:


Maybe you read a little earlier I voiced my problem. It seems to be a solution, but I can't find a way to work with strings in mql in the book (

how do I take a number out of a string of numbers?

to convert a number to a string https://docs.mql4.com/ru/convert/DoubleToStr

cut off a part of the string https://docs.mql4.com/ru/strings/StringSubstr from the beginning https://docs.mql4.com/ru/strings/StringLen

and convert it back to the real type https://docs.mql4.com/ru/convert/StrToDouble

that's all you need to do with strings, maybe StrToDouble() will be enough for you.

but this "ugly way" will cut off the number, not round it. Another way: multiply a real number to power of x by 10, and give the result to the int type - if I'm not mistaken, there will be no rounding, then divide int by 10 to power of x.

 
IgorM:

translate a number into the string https://docs.mql4.com/ru/convert/DoubleToStr

cut off a part of the string https://docs.mql4.com/ru/strings/StringSubstr from the beginning of https://docs.mql4.com/ru/strings/StringLen

and convert everything back to a real type https://docs.mql4.com/ru/convert/StrToDouble

that's all you need to do with strings, maybe StrToDouble() is enough for you.

but this "ugly way" will cut off the number, not round it. Another way: multiply a real number by 10 to the power of x, and give the result to the int type - if I'm not mistaken, there will be no rounding, then divide int by 10 to the power of x


at least *cut off*, as the number can only be taken out of the indicator to a string... thanks for the advice, I'll experiment)
 

IgorM:

NameLess :

Thanks for the help. I understand the logic with setting the flag and the function of returning the number of orders. But I'm also interested in the moment when the order is closed. It looks like the flag condition tries to place an order again but the function returning the order number says there is 1 less order and initializes opening of a position as well. Correct me if I have missed something.