Questions from Beginners MQL4 MT4 MetaTrader 4 - page 35

 

Hello!
To fix the indicator, you had to replace all the dots "." with a blank "".

Files:
supDem.zip  68 kb
 

Greetings.

I changed theorder selection type in the written function of certain order counting from selection by order to selection by ticket.

int BuyLimitCount(){
int count=0
for(int i=OrdersTotal()-1; i>=0; i--){
if(OrderSelect(i, SELECT_BY_POSMODE_TRADES)==true){
if(OrderMagicNumber()==MagicB){
if(OrderType()==OP_BUYLIMIT)
count++;}}}return(count);}  

int BuyLimitCount(){
int count=0
if(OrderSelect(ticketUP, SELECT_BY_TICKETMODE_TRADES)==true){
if(OrderMagicNumber()==MagicB){
if(OrderType()==OP_BUYLIMIT)
count++;}}return(count);}

After that, when running a few dozen candles, everything works correctly and after that there are repeated errors in the log:

2016.12.17 17:44:31.609 2016.12.07 00:27 test3 EURUSD,M1: unknown ticket 2 for OrderModify function

2016.12.17 17:44:31.608 2016.12.07 00:25 test3 EURUSD,M1: OrderModify error 4108 // Invalid ticket number.

Here is the full text, I changed it in all four functions:

extern int pointsl=100, pointtp=100, MagicB=1111, MagicS=2222, bars=10;  extern double lotB=0.1, lotS=0.1;
double slB, tpB, slS, tpS;  double x=0, z=0int ticketUP, ticketD;


void OnTick()  
{
double maxpr1=-9999double minpr1=9999;

for(int shift1=0; shift1<bars; shift1++)
{double i=iHigh(Symbol(), PERIOD_CURRENT, shift1);
if (i>maxpr1){maxpr1=i;}}

for(int shiftA1=0; shiftA1<bars; shiftA1++)
{double y=iLow(Symbol(), PERIOD_CURRENT, shiftA1);
if (y<minpr1) {minpr1=y;}} 

if (BuyLimitCount()==0 && BuyCount()==0){
slB=NormalizeDouble(minpr1-pointsl*Point,5);
tpB=NormalizeDouble(minpr1+pointtp*Point,5);
ticketUP=OrderSend(Symbol(), OP_BUYLIMIT, lotB, minpr1, 3, slB, tpB, "", MagicB, 0, Red);
if (ticketUP==-1Print("ERROR OP_BUY"); else Print("OP_BUY OK");}

if (SellLimitCount()==0 && SellCount() ==0){
slS=NormalizeDouble(maxpr1+pointsl*Point,5);
tpS=NormalizeDouble(maxpr1-pointtp*Point,5);
ticketD=OrderSend(Symbol(), OP_SELLLIMIT, lotS, maxpr1, 3, slS, tpS, "", MagicS, 0, Blue);
if (ticketD==-1Print("ERROR OP_SELL"); else Print("OP_SELL OK");}

if (x!=maxpr1){x=maxpr1;
slS=NormalizeDouble(maxpr1+pointsl*Point,5);
tpS=NormalizeDouble(maxpr1-pointtp*Point,5);
OrderModify(ticketD, maxpr1, slS, tpS, 0, Blue);}

if (z!=minpr1){z=minpr1;
slB=NormalizeDouble(minpr1-pointsl*Point,5);
tpB=NormalizeDouble(minpr1+pointtp*Point,5);
OrderModify(ticketUP, minpr1, slB, tpB, 0, Red);}

double maxpr=-9999double minpr=9999;

for(int shift=0; shift<bars; shift++)
{double e=iHigh(Symbol(), PERIOD_CURRENT, shift);
if (e>maxpr){maxpr=e;}}

for(int shiftA=0; shiftA<bars; shiftA++)
{double r=iLow(Symbol(), PERIOD_CURRENT, shiftA);
if (r<minpr) {minpr=r;}} 

string a;
if(bars==1)a="bar: ";
else a= IntegerToString(bars,1) + " bar's: ";
Comment("Last ", a, "max "DoubleToStr(maxpr, 5), ", min "DoubleToStr(minpr, 5),".");
}

int BuyLimitCount(){
int count=0
if(OrderSelect(ticketUP, SELECT_BY_TICKETMODE_TRADES)==true){
if(OrderMagicNumber()==MagicB){
if(OrderType()==OP_BUYLIMIT)
count++;}}return(count);}

int BuyCount(){
int count=0
if(OrderSelect(ticketUP, SELECT_BY_TICKETMODE_TRADES)==true){
if(OrderMagicNumber()==MagicB){
if(OrderType()==OP_BUY)
count++;}}return(count);}

int SellLimitCount(){
int count=0
if(OrderSelect(ticketD, SELECT_BY_TICKETMODE_TRADES)==true){
if(OrderMagicNumber()==MagicS){
if(OrderType()==OP_SELLLIMIT)
count++;}}return(count);}

int SellCount(){
int count=0
if(OrderSelect(ticketD, SELECT_BY_TICKETMODE_TRADES)==true){
if(OrderMagicNumber()==MagicS){
if(OrderType()==OP_SELL)
count++;}}return(count);}


And here there is one moment - after I changed the selection function, the order modify function started to generate an error and only after some time oftesting, at first everything was normal.

Why does this happen and how to deal with it?


 
spoiltboy:

Greetings.

I changed theorder selection type in the written function of certain order counting from selection by order to selection by ticket.

int BuyLimitCount(){
int count=0
for(int i=OrdersTotal()-1; i>=0; i--){
if(OrderSelect(i, SELECT_BY_POSMODE_TRADES)==true){
if(OrderMagicNumber()==MagicB){
if(OrderType()==OP_BUYLIMIT)
count++;}}}return(count);}  

int BuyLimitCount(){
int count=0
if(OrderSelect(ticketUP, SELECT_BY_TICKETMODE_TRADES)==true){
if(OrderMagicNumber()==MagicB){
if(OrderType()==OP_BUYLIMIT)
count++;}}return(count);}

After that, when running a few dozen candles, everything works correctly and after that there are repeated errors in the log:

2016.12.17 17:44:31.609 2016.12.07 00:27 test3 EURUSD,M1: unknown ticket 2 for OrderModify function

2016.12.17 17:44:31.608 2016.12.07 00:25 test3 EURUSD,M1: OrderModify error 4108 // Invalid ticket number.

Here is the full text, I changed it in all four functions:

extern int pointsl=100, pointtp=100, MagicB=1111, MagicS=2222, bars=10;  extern double lotB=0.1, lotS=0.1;
double slB, tpB, slS, tpS;  double x=0, z=0int ticketUP, ticketD;


void OnTick()  
{
double maxpr1=-9999double minpr1=9999;

for(int shift1=0; shift1<bars; shift1++)
{double i=iHigh(Symbol(), PERIOD_CURRENT, shift1);
if (i>maxpr1){maxpr1=i;}}

for(int shiftA1=0; shiftA1<bars; shiftA1++)
{double y=iLow(Symbol(), PERIOD_CURRENT, shiftA1);
if (y<minpr1) {minpr1=y;}} 

if (BuyLimitCount()==0 && BuyCount()==0){
slB=NormalizeDouble(minpr1-pointsl*Point,5);
tpB=NormalizeDouble(minpr1+pointtp*Point,5);
ticketUP=OrderSend(Symbol(), OP_BUYLIMIT, lotB, minpr1, 3, slB, tpB, "", MagicB, 0, Red);
if (ticketUP==-1Print("ERROR OP_BUY"); else Print("OP_BUY OK");}

if (SellLimitCount()==0 && SellCount() ==0){
slS=NormalizeDouble(maxpr1+pointsl*Point,5);
tpS=NormalizeDouble(maxpr1-pointtp*Point,5);
ticketD=OrderSend(Symbol(), OP_SELLLIMIT, lotS, maxpr1, 3, slS, tpS, "", MagicS, 0, Blue);
if (ticketD==-1Print("ERROR OP_SELL"); else Print("OP_SELL OK");}

if (x!=maxpr1){x=maxpr1;
slS=NormalizeDouble(maxpr1+pointsl*Point,5);
tpS=NormalizeDouble(maxpr1-pointtp*Point,5);
OrderModify(ticketD, maxpr1, slS, tpS, 0, Blue);}

if (z!=minpr1){z=minpr1;
slB=NormalizeDouble(minpr1-pointsl*Point,5);
tpB=NormalizeDouble(minpr1+pointtp*Point,5);
OrderModify(ticketUP, minpr1, slB, tpB, 0, Red);}

double maxpr=-9999double minpr=9999;

for(int shift=0; shift<bars; shift++)
{double e=iHigh(Symbol(), PERIOD_CURRENT, shift);
if (e>maxpr){maxpr=e;}}

for(int shiftA=0; shiftA<bars; shiftA++)
{double r=iLow(Symbol(), PERIOD_CURRENT, shiftA);
if (r<minpr) {minpr=r;}} 

string a;
if(bars==1)a="bar: ";
else a= IntegerToString(bars,1) + " bar's: ";
Comment("Last ", a, "max "DoubleToStr(maxpr, 5), ", min "DoubleToStr(minpr, 5),".");
}

int BuyLimitCount(){
int count=0
if(OrderSelect(ticketUP, SELECT_BY_TICKETMODE_TRADES)==true){
if(OrderMagicNumber()==MagicB){
if(OrderType()==OP_BUYLIMIT)
count++;}}return(count);}

int BuyCount(){
int count=0
if(OrderSelect(ticketUP, SELECT_BY_TICKETMODE_TRADES)==true){
if(OrderMagicNumber()==MagicB){
if(OrderType()==OP_BUY)
count++;}}return(count);}

int SellLimitCount(){
int count=0
if(OrderSelect(ticketD, SELECT_BY_TICKETMODE_TRADES)==true){
if(OrderMagicNumber()==MagicS){
if(OrderType()==OP_SELLLIMIT)
count++;}}return(count);}

int SellCount(){
int count=0
if(OrderSelect(ticketD, SELECT_BY_TICKETMODE_TRADES)==true){
if(OrderMagicNumber()==MagicS){
if(OrderType()==OP_SELL)
count++;}}return(count);}


And here there is one moment - after I changed the selection function, the order modify function started to generate an error and only after some time oftesting, at first everything was normal.

Why does this happen and how to deal with it?


Most probably, you are getting this error because you are trying to modify an order that has already been triggered or deleted. You should clear the variables that store pending order tickers, for example, when a pending order has been triggered.
 
Sergey Gritsay:
You probably get this error because you are trying to modify a pending order that has already been triggered or deleted. You need to reset the variables where the pending ticket is stored, for example, when the pending order has been triggered.
Do you know how to do it correctly? I have no idea how to do it.
 

Hi all, help me understand what's wrong.

my Expert Advisor opens ticket=OrderSend(Symbol(),OP_BUY,Lot,Ask,3,0,0, "AV2",1111,0,Green);

close МТ4, open МТ4 and check for previous work of the EA

for(int prev=0; prev<OrdersTotal();prev++) //Checking of previous EA operation

{

ViborOrdera=OrderSelect(prev,SELECT_BY_POS);

if(OrderSymbol()==Symbol())

//*************************************

//-------Check the Purchasing cycle--------

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

{

PriceAsk=OrderOpenPrice();//open price, needed for further work of the EA

}

question, where is the error, is there no check? The body of the loop is in the Inite.

 

No time to sort it out..........

Try to read and place in the right places.

boolRefreshRates();

///
 
ed3sss:

Hi all, help me understand what's wrong.

my Expert Advisor opens ticket=OrderSend(Symbol(),OP_BUY,Lot,Ask,3,0,0, "AV2",1111,0,Green);

close МТ4, open МТ4 and check for previous work of the EA


Question, where is the error, there is no check? The body of the loop is in Inite.

And prove that it is the loop that does not enter the loop. Also, why check ALL orders, from the very beginning of the account's life, if you need the last ones???
 
Mikhail Kozhemyako:

No time to sort it out..........

Try to read and place in the right places.

boolRefreshRates();

///
Didn't help(
 
Vitalie Postolache:
And prove that it is the cycle that is not going in. Besides, why should we check ALL orders from the very beginning of the account's life if we need the latest ones?

Proof: Print("Previous PriceAsk-",PriceAsk); the log is empty.

Besides, why should we check ALL orders from the very beginning of the account life, if we need the latest ones? - How else to do it, if there are 10 currency pairs open?


 

Everything was OK until we changed ViborOrdera=OrderSelect(prev,SELECT_BY_POS) in MQL;

It used to beOrderSelect(prev,SELECT_BY_POS);. It was a long time ago), but it worked.

Reason: