Tralling Stop

 

Hello!!!

I have a problem in the code. I tried everything, but nothing

ERROR: 130.

thanks for help.

 

 
//=========================POINT
      double MyPoint=Point;                                      
      if(Digits==3 || Digits==5) MyPoint=Point*10;   
for (int trade3=OrdersTotal()-1; trade3>=0; trade3--) 
    {
      if (OrderSelect(trade3,SELECT_BY_POS, MODE_TRADES)) 
      {
         if(OrderSymbol()==Symbol() && (OrderMagicNumber()==MagicNumber) )
         {  
               if(OrderType()==OP_BUY)
               {
                 
                  if(OrderStopLoss()==0 && Ask-10*MyPoint>OrderOpenPrice()) modify= OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+10*MyPoint,0,0,clrLightGreen);
                  if(modify<=0) error = GetLastError();
                  Comment(error);
                 
               }//FIM OP_BUY  

               
               
               
              
               
          }//FIM MAGIC
        }//FIM SELECT
    }//FIM CONTADOR  
 
if(OrderStopLoss()==0 && Ask-10*MyPoint>OrderOpenPrice())
 modify= OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+10*MyPoint,0,0,clrLightGreen);

when

 Ask-10*MyPoint>OrderOpenPrice()

is first true you are trying to modify the SL to what is the current Ask

This will not only likely fail because it is too close to the current price, it will be higher than the current bid and a Buy closes at bid.

 
GumRai:

when

is first true you are trying to modify the SL to what is the current Ask

This will not only likely fail because it is too close to the current price, it will be higher than the current bid and a Buy closes at bid.

GumRai thanks for anwser.

if(OrderStopLoss()==0 && Ask-10*MyPoint>OrderOpenPrice())
 modify= OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+7*MyPoint,0,0,clrLightGreen);

 correct ?

 

First you need to decide what you are trying to do.

This is not code for a trailing stop it is more like a move to break-even

As it is a buy, use Bid, not Ask, better to use OrderClosePrice() which is the last price at the time of the OrderSelect

So this is your code set to break-even. You will need to re-think for a trailing stop.

if(OrderStopLoss()==0 && OrderClosePrice()-10*MyPoint>OrderOpenPrice())
 modify= OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),0,0,clrLightGreen);
 
GumRai:

First you need to decide what you are trying to do.

This is not code for a trailing stop it is more like a move to break-even

As it is a buy, use Bid, not Ask, better to use OrderClosePrice() which is the last price at the time of the OrderSelect

So this is your code set to break-even. You will need to re-think for a trailing stop.

Gumrai, thank you for answer.

 the previous code was incomplete.  With your help, I made several tests and had no error

I believe that everything is OK.

 if(OrderStopLoss()==0 && OrderClosePrice()-10*MyPoint>stnewpricebuy) modify = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+5*MyPoint,0,0,clrLightGreen);
                  
                  if(SL>0 && (OrderStopLoss()>OrderStopLoss()+5*MyPoint))   modify = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()+2*MyPoint,0,0,clrLightGreen);

 

once again thank you for your help.

before sending my question looked at many forums. however you helped me. as always helps.

thank very much :) 

 

correction code:

if(SL==0 && OrderClosePrice()-10*MyPoint>stnewpricebuy) modify = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+5*MyPoint,0,0,clrLightGreen);
                  
                  if(SL>0 && (Bid>OrderStopLoss()+10*MyPoint))   modify = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()+5*MyPoint,0,0,clrLightGreen);
Reason: