[ARCHIVE!] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Can't go anywhere without you - 4. - page 563

 

Can you tell me what could be wrong with this code? It's a trailing stop, but when tested it generates error 1, when modified. Why is the order not modified?

When I was correcting this error (so far unsuccessfully), when compiling the Expert Advisor's main code into the print function, it started returning an error, saying that it needs inverted commas, but everything there is correct, and to correct this error I have to remove and put a new inverted comma...

bool trailingstop()
{
if ((OrderMagicNumber()==11111)==true)
{
Print("Keep Buy position");
while(OrderCloseTime()==0)
{
if ((Bid-OrderStopLoss())>(StopLevel*Point))
{
if (OrderModify(OrderTicket(),OrderOpenPrice(),ND(Bid-StopLevel*Point),0,0))
Print("Stop Loss of the order with the number ",OrderTicket()," and the magic number ",OrderMagicNumber()," successfully changed to ",OrderStopLoss());
else Print("The Stop Loss level of the order with the number ",OrderTicket()," and magic number ",OrderMagicNumber(),". Error ",GetLastError());
}
}
if (OrderCloseTime()>0)
{
Print("The order with the number ",OrderTicket()," and the magic number ",OrderMagicNumber()," closed at price ",OrderClosePrice()," with a profit/loss ",OrderProfit());
return(true);
}
}
else
{
if ((OrderMagicNumber()==22222)==true)
{
Print((Sell position);
while(OrderCloseTime()==0)
{
if ((OrderStopLoss()-Ask)>(StopLevel*Point))
{
if (OrderModify(OrderTicket(),OrderOpenPrice(),ND(Ask+StopLevel*Point),0,0))
Print("Stop Loss of the order with the number ",OrderTicket()," and the magic number ",OrderMagicNumber()," successfully changed to ",OrderStopLoss());
else Print("The Stop Loss level of the order with the number ",OrderTicket()," and magic number ",OrderMagicNumber(),". Error ",GetLastError());
}
}
if (OrderCloseTime()>0)
{
Print("The order with the number ",OrderTicket()," and the magic number ",OrderMagicNumber()," closed at price ",OrderClosePrice()," with a profit/loss ",OrderProfit());
return(true);
}
}
else return(false);
}
}
 
Thank you!!!
 
Ekburg:

Can you tell me what could be wrong with this code? It's a trailing stop, but when tested it generates error 1, when modified. Why is the order not modified?

When I was correcting this error (so far unsuccessfully), when compiling the Expert Advisor's main code into the print function, it started returning an error, saying that it needs inverted commas, but everything there is correct, and to correct this error I have to remove and put a new inverted comma...

bool trailingstop()
{
if ((OrderMagicNumber()==11111)==true)
{
Print("Keep Buy position");
while(OrderCloseTime()==0)
{
if ((Bid-OrderStopLoss())>(StopLevel*Point))
{
if (OrderModify(OrderTicket(),OrderOpenPrice(),ND(Bid-StopLevel*Point),0,0))
Print("Stop Loss of the order with the number ",OrderTicket()," and the magic number ",OrderMagicNumber()," successfully changed to ",OrderStopLoss());
else Print("The Stop Loss level of the order with the number ",OrderTicket()," and magic number ",OrderMagicNumber(),". Error ",GetLastError());
}
}
if (OrderCloseTime()>0)
{
Print("The order with the number ",OrderTicket()," and the magic number ",OrderMagicNumber()," closed at price ",OrderClosePrice()," with a profit/loss ",OrderProfit());
return(true);
}
}
else
{
if ((OrderMagicNumber()==22222)==true)
{
Print((Sell position);
while(OrderCloseTime()==0)
{
if ((OrderStopLoss()-Ask)>(StopLevel*Point))
{
if (OrderModify(OrderTicket(),OrderOpenPrice(),ND(Ask+StopLevel*Point),0,0))
Print("Stop Loss of the order with the number ",OrderTicket()," and the magic number ",OrderMagicNumber()," successfully changed to ",OrderStopLoss());
else Print("The Stop Loss level of the order with the number ",OrderTicket()," and the magic number ",OrderMagicNumber(),". Error ",GetLastError());
}
}
if (OrderCloseTime()>0)
{
Print("The order with the number ",OrderTicket()," and the magic number ",OrderMagicNumber()," closed at price ",OrderClosePrice()," with a profit/loss ",OrderProfit());
return(true);
}
}
else return(false);
}
}


Error 1 is not an error. this means that the order has already been modified. insert check for modification parameters before OrderModify() (if the order stoploop is not equal to stoploop , (ND(Bid-StopLevel*Point)or ND(Ask+StopLevel*Point)), thenOrderModify(), otherwise return).
 
rigonich:

Error 1 is not an error. This means that the order has already been modified. Insert check for the modification parameters before OrderModify() (if the stop loss of the order is not equal to the stop loss, (ND(Bid-StopLevel*Point) orND(Ask+StopLevel*Point)), thenOrderModify(), otherwise return).


This check is above: if ((Bid-OrderStopLoss())>(StopLevel*Point)), or I got you wrong

And I tested it on a sharp movement, even visually, it is clearly visible that the stop loss of the order and the expression are not equal

 
Usual_Trader:


At a glance, what I see in the function of closing orders, you need to select the order by the ticketOrderSelect(Ticket, SELECT_BY_TICKET), the number of lots indicate not the variable Lts, because it is recalculated every time, but use OrderLots() And another thing, you keep working on the 0 bar, respectively, the criteria on one bar may disappear, your orders may not open where you would like. Replace bar 0 with 1 and 1 with 2 in the indicator parameters



Thanks a lot for the reply), but the criteria does not work as I would like it to do (not all crossings of stochastics show orders ( ) and I would like to open orders on 0 bar, but I guess I need an additional condition: after opening an order on the current bar I have to do nothing till the bar closes, but I have no idea how to do that (may be using time series arrays???) Please advise.
Files:
 
Ekburg:


This check is above: if ((Bid-OrderStopLoss())>(StopLevel*Point)), or I misunderstood you.

I checked it on a sharp movement where you can clearly see that the stop loss of the order and the expression are not equal


The problem is as follows: since you don't have OrderSelect() in this function, it must be used somewhere in the loop with the search of orders. If this loop organizes the search from zero order to the last one, then after the modification of the first order, or if any order has been already found, when OrderSelect() is called next time, the order order order order changes and the function may select an order that has already been modified. Therefore, we should check if the value of the order StopLoss is equal to the one we send to the OrderModify() function

if(ND(Bid-StopLevel*Point)==OrderStopLoss())return(true);
 

abeiks:

For some time I have been trying to redo the buy close block to close only the last two buy positions, but it fails. Can you tell how to redo the block?

//+------------------------------------------------------------------+
//|                                                         0000.mq4 |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""
void Close_2buy()
   {
   bool     result;
   int      Count=2,Ticket=-1,error;
   datetime time=0;
   while(Count>0)
      {
      for (int f=OrdersTotal()-1; f>=0; f--) // 
         {
         OrderSelect(f, SELECT_BY_POS);
         if (OrderSymbol()==Symbol() &&(OrderMagicNumber()==magic ) 
         && (OrderType() == OP_BUY )) 
            {
            if(OrderOpenTime()>time)
               {
               time=OrderOpenTime();Ticket=OrderTicket();
            }
         }
      }
      if(OrderSelect(Ticket,SELECT_BY_TICKET)==True);
         {
         result=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),0,CLR_NONE);
         if(result!=TRUE){error=GetLastError();Print("LastError = ", error);}
      }
      else{error=GetLastError();Print("LastError = ", error);} 
      Ticket=-1;time=0;Count--;
   }
   return;
} 
 
ametist444:

Hello!

Can you tell me which files need to be downloaded and where to install them in mt4 to get this window?

I don't understand how to do it in theSimulation on different timeframes of the instrument under test

I want to see only three daily charts, four-hour and hourly

i want to see three daily, four-hour and hourly charts. Please help, i have not managed to do it all night long!



The windows like (visual) open when testing the EA in visual mode.


 
peshihod:

What kind of StopLoss?

I must have phrased it wrong :(

We have an algorithm for opening orders.

I need it to open the next order in another direction (when conditions appear), if the last order was closed in any way.

All works, but if I have closed by a stop loss, for example, for sell, and the conditions to open a sell in this direction, the opening continues. What we need is to wait for conditions to buy.

That is, we need to check the last closed order, so it would not coincide with the opening one (buy-sell-buy-sell alternation). Only one order or waiting for a signal is in operation.

I understand that we should probably use OrdersHistoryTotal(), but how would it look in a working EA?

Thank you.

 
delf:

There is an algorithm for opening orders.

We need any variant of the last order to open the next one in a different direction (when conditions appear).

In other words, we need to check the last closed order so that it does not coincide with the opening one (buy-sell-buy-sell alternation). There is only one order or signal waiting to be opened.

Does this alternation always take place in any case, and only one order is always in operation?

Then it is easier to add control over the number of orders to the condition:

if(OrdersTotal()==0)
{
	if(OldOrder==OrderBuy)
		{
		...
		}
	else
		{
		...
		}
}
Reason: