[ARCHIVE] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 3. - page 636

 

Good people, help, I can not understand, TrailingStop is not working as it should, if the deal is opened on the SELL trall works fine, but if you open BUY, then for some reason immediately modified stopper. Where is the error I can not understand.... HERE IS THE CODE:

extern int TP = 50;
extern int SL = 50;
extern int Trailing = 15;

int init()
{return(0);}
int deinit()
{return(0);}

int start()
{
//========= Calculate Breakeven level ======
int i;
double lotsBUY=0;
double lotsSEL=0;
double sumBUY=0;
double sumSEL=0;

for (i=0; i<OrdersTotal(); i++)
{
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
if (OrderSymbol()!=Symbol()) continue;
if (OrderType()==OP_BUY)
{
lotsBUY=lotsBUY+OrderLots();
sumBUY=sumBUY+OrderLots()*OrderOpenPrice();
}

if (OrderType()==OP_SELL)
{
lotsSEL=slotsSEL-OrderLots();
sumSEL=sumSEL-OrderLots()*OrderOpenPrice();
}
}
double priceBUY=0;
if (lotsBUY!=0) priceBUY=sumBUY/lotsBUY;
double priceSEL=0;
if (lotsSEL!=0) priceSEL=sumSEL/lotsSEL;

//======== Open position =====


double MA=iMA(NULL,0,14,0,MODE_SMMA,PRICE_CLOSE,0);

if (OrdersTotal() < 1 && Bid>MA)
OrderSend(Symbol(),OP_BUY,0.1,Ask,3,Ask-SL*Point,Ask+TP*Point,0,0,0,Green);
if (OrdersTotal() < 1 && Ask<MA)
OrderSend(Symbol(),OP_SELL,0.1,Bid,3,Bid+SL*Point,Bid-TP*Point,0,0,0,Red);


//======= Trailing Stop =======

for(i = 0; i <= OrdersTotal(); i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if (OrderType()==OP_SELL && Trailing!=0)
{
if(priceSEL-Ask>=Trailing*Point && OrderStopLoss()>(Ask+Trailing*Point))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Trailing*Point,OrderTakeProfit(),0,CLR_NONE);
}
}
if (OrderType()==OP_BUY && Trailing!=0)
{
if(Bid-priceBUY>=Trailing*Point && (Bid-Point*Trailing)>OrderStopLoss())
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*Trailing,OrderTakeProfit(),0,CLR_NONE)
}
}
}
return(0);

}

There is a moment, if I change priceBUY to OrderOpenPrice() in TrailingStop block, it works, but I need to trail from breakeven level, not from opening price..... On SELL works, but on buy for some reason not.... I don't understand where the mistake is.....SOS!!!!!!!!

 
Roman.:


This is not an error - it is a message that the minimum balance has been reached - i.e. the account is drained - the run is terminated and a new one is started with another set of input parameters - see checkboxes and set limit values in the Optimise Tester tab:

Thank you!
 

Good afternoon!!! My EA is retracting orders with this place.

 if(Summaprofita()>=Profit_v_balans )
        {
         for(int i=OrdersTotal()-1;i>=0;i--)
           {
              bool error=true;
            OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
            
            if( magic==OrderMagicNumber( ) && OrderSymbol()==Symbol())
              {
              
                 OrderClose( OrderTicket( ), OrderLots( ), OrderClosePrice( ) , 0, CLR_NONE); error=false;
                           Sleep(1000);
             OrderDelete(OrderTicket());error=false;
         } } }
        

My EA has been doing fine before, at least until I had 2-5 orders. Now we have as many orders as we want - 20, 50 on each side. In short, it does not clean. Well, two or three or four times it does, and then suddenly not!

I wonder how it looks in the tester - in general it removes several orders near the price, but not all! What kind of deviations can we envisage, invent, fancy, that it would surely close?

Really, it was fine before. It used to close, didn't it? What else does it need? ?????

 

Good people, help, I can not understand, TrailingStop is not working as it should, if the deal is opened on the SELL trall works fine, but if you open BUY, then for some reason immediately modified stopper. Where is the error I can not understand.... HERE IS THE CODE:

extern int TP = 100;
extern int SL = 100;
extern int Trailing = 15;

int init()
{return(0);}
int deinit()
{return(0);}

int start()
{
//========= Расчёт уровня безубытка ======
int i;
double lotsBUY=0;
double lotsSEL=0;
double sumBUY=0;
double sumSEL=0;

for (i=0; i<OrdersTotal(); i++)
{
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
if (OrderSymbol()!=Symbol()) continue;
if (OrderType()==OP_BUY)
{
lotsBUY=lotsBUY+OrderLots();
sumBUY=sumBUY+OrderLots()*OrderOpenPrice();
}

if (OrderType()==OP_SELL)
{
lotsSEL=lotsSEL-OrderLots();
sumSEL=sumSEL-OrderLots()*OrderOpenPrice();
}
}
double priceBUY=0;
if (lotsBUY!=0) priceBUY=sumBUY/lotsBUY;
double priceSEL=0;
if (lotsSEL!=0) priceSEL=sumSEL/lotsSEL;



//======== Открытие позиции =====


double MA=iMA(NULL,0,14,0,MODE_SMMA,PRICE_CLOSE,0);

if (OrdersTotal() < 1 && Bid>MA)
OrderSend(Symbol(),OP_BUY,0.1,Ask,3,Ask-SL*Point,Ask+TP*Point,0,0,0,Green);
if (OrdersTotal() < 1 && Ask<MA)
OrderSend(Symbol(),OP_SELL,0.1,Bid,3,Bid+SL*Point,Bid-TP*Point,0,0,0,Red);


//======= Trailing Stop =======

for(i = 0; i <= OrdersTotal(); i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if (OrderType()==OP_SELL && Trailing!=0)
{
if(priceSEL-Ask>=Trailing*Point && OrderStopLoss()>(Ask+Trailing*Point))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Trailing*Point,OrderTakeProfit(),0,CLR_NONE);
}
}
if (OrderType()==OP_BUY && Trailing!=0)
{
if(Bid-priceBUY>=Trailing*Point && (Bid-Point*Trailing)>OrderStopLoss())
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*Trailing,OrderTakeProfit(),0,CLR_NONE);
}
}
}
return(0);
}

There is a time, if I change priceBUY to OrderOpenPrice() in block TrailingStop, everything works, but I need a trailing stop from breakeven level, not from opening price..... On SELL works, but on buy for some reason not.... I don't understand where the mistake is.....SOS!!!!!!!!
 
MIR-ASOV:

Good people, help, I can not understand, TrailingStop is not working as it should, if the deal is opened on the SELL trall works fine, but if you open BUY, then for some reason immediately modified stopper. Where is the error I can not understand.... HERE IS THE CODE:

There is a time, if I change priceBUY to OrderOpenPrice() in block TrailingStop, everything works, but I need a trailing stop from breakeven level, not from opening price..... On SELL works, but on buy for some reason not.... I don't understand where the mistake is.....SOS!!!!!!!!

Try outputting with Print() the priceBUY value before modification to see if it meets expectations...
 
Try outputting with Print() the priceBUY value before modification to see if it meets expectations...
Yes, I checked, it corresponds fully and completely...... Essentially the breakeven level for BUY is equal to the opening price of the order, but for some reason it doesn't work, I put OrderOpenPrice() everything works, I put priceBUY, no......
 
Most likely you have zero in thepriceBUY variable at the time of opening, so Bid-priceBUY(0)==Bid, and therefore
 
Dimka-novitsek:

Good afternoon!!! My EA is retracting orders with this place.

My EA seems to have been doing fine before, at least until I had 2-5 orders. Now I have a new variant with as many orders as I want - 20, 50 on each side. In short, it does not clean. Well, two or three or four times it does, and then suddenly not!

I wonder how it looks in the tester - it removes several orders near the price, but not all of them! What kind of deviations can we invent, fancy, so that we would be able to close them without fail?

Really, everything was fine before? It used to close, didn't it? What else does he need? ?????

Uh,well,that's not gonna work.

  OrderClose( OrderTicket( ), OrderLots( ), OrderClosePrice( ) , 0, CLR_NONE);

While the order is not closed, its OrderClosePrice equals 0, and in this place you must substitute the real price at which you are closing the order, i.e. Ask in case of SELL order and Bid for BUY.

 
alsu:

Well, it doesn't work like that.

Until the order is closed, its OrderClosePrice equals 0, and in this place you have to put the real price at which you are closing the order, i.e. Ask in case of SELL order and Bid for BUY.


No,OrderClosePrice() for an order which is not closed is not equal to zero - check.OrderCloseTime() - yes
 
Thank you!!! I'll give it a try, I got it.
Reason: