Get a line object to substitute the trailing stop

 

Hi,

 Do anyone know how to get a line object to have a behaviour like trailing stop. What I mean is a way to substitute the stop loss modified  with a line object and here this line should have to go in the market direction only, once price bounce back and hit this line then a close hill happen.  

The reason why I'm asking have to do to get a way to avoid stoplevel. Make sense or not ?

Any clarification provided will be welcome.

Luis 

 
luisneves:

Hi,

 Do anyone know how to get a line object to have a behaviour like trailing stop. What I mean is a way to substitute the stop loss modified  with a line object and here this line should have to go in the market direction only, once price bounce back and hit this line then a close hill happen.  

The reason why I'm asking have to do to get a way to avoid stoplevel. Make sense or not ?

Any clarification provided will be welcome.

Luis 

think makes no sense in most cases.....

how big is your stoplevel 

and what size is the trailingstop  ?? 

 
deVries:

think makes no sense in most cases.....

how big is your stoplevel 

and what size is the trailingstop  ?? 


Good morning deVries,

Thank you to keep your attention to my issue.

So, what I'm looking for is to start trail when account gets a profit of 1.5 (15 for 5 digits).

Regarding stop level and spread as for stop level I do not know what the value is and as spread is always changing decided to include that information at the code for trail as you can see in the code below: 

void Trail()
    {        
     double StopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL) * Point;
     double Spread = MarketInfo(Symbol(),MODE_SPREAD);
     double NewStop = StopLevel + Spread;
                 
      for(int OrderCounter = OrdersTotal()-1; OrderCounter >= 0; OrderCounter--)                   
         {//23
        if(!OrderSelect(OrderCounter,SELECT_BY_POS))continue;
         if(OrderType()== OP_BUY && OrderSymbol()== Symbol() 
            && OrderMagicNumber()== MagicNumber)                                                                        
            {//24
               RefreshRates();
            if(OrderStopLoss() < Bid - (NewStop * pips2dbl) && Bid - OrderOpenPrice() > (MinimumProfit * pips2dbl)) 
               {//25
                  RefreshRates();
               if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid - (NewStop * pips2dbl), OrderTakeProfit(),0,Yellow))
                Print("Buy TrailingStop Failed, order number:",OrderTicket(),"Error:",GetLastError());
               }//25
             }//24
           }//23          
           if(OrderType()==OP_SELL && OrderSymbol()== Symbol() 
              && OrderMagicNumber()== MagicNumber)
              {//26
                 RefreshRates();
              if(OrderStopLoss() > Ask + (NewStop * pips2dbl) && OrderOpenPrice() - Ask > (MinimumProfit * pips2dbl))  
                 {//2
                    RefreshRates();
                 if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask + (NewStop * pips2dbl) ,OrderTakeProfit(),0,Yellow))              
                 Print("Sell TrailingStop Failed, order number:",OrderTicket(),"Error:",GetLastError());
               }//27                                         
            return(0);     
          }//26   
    }

 

While you said before I should use pending orders instead because I said that some times trail do not start, but even so the trail sometimes do not start with pending orders. So In my mind comes the idea that the traditional system of trailing stop could fail because limit issues imposed by brokers when one want to put orders that run few pips. so to try to avoid those limits if they exist, if one could use another way to trail perhaps that could be one solution. 

 thank you in advance for any clarification.

Luis 

 
luisneves:

Regarding stop level and spread as for stop level I do not know what the value is 

what values do you get for this on your account  for EURUSD ???

 

Print

See also Alert() and Comment() functions.
 
deVries:

what values do you get for this on your account  for EURUSD ???

 

Print

See also Alert() and Comment() functions.


Hi deVries,

 Thank you for the clue. The stop level is 0. The code  I used to get it is :

   RefreshRates();
   if(Ask > BuyTrigger && BuyAllowed)     
          {//5
          while(IsTradeContextBusy()) 
                Sleep(10);
                RefreshRates();                        
     BuyTicket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage*pips2points,0,0,"Initial Buy Order",MagicNumber,0,Green);                
     if(BuyTicket > -1)
        {//6
         BuyAllowed = false;SellAllowed = true;                                               
         Print("Initial Buy order placed # ",BuyTicket);
         Print("Account StopLevel is ", MarketInfo(Symbol(),MODE_STOPLEVEL));//<----------------------------------------------- Get Stop Level value
         
         AddLimitsBuy(); 
         Trail();                
         }//6
      else
         {//7 
         Print("Initial Buy Order Send failed, order number:",OrderTicket(),"Error:", GetLastError());
         }//7 
     return(0);
     }//5

 Sorry the question, but how do I delete the comment from the chart ?

 
luisneves:


Hi deVries,

 Thank you for the clue. The stop level is 0. The code  I used to get it is :

 Sorry the question, but how do I delete the comment from the chart ?

 

int deinit()
  {
//---- 
   Comment("");
//----
   return(0);
  }

Why do you need trailingfunction if it has room to trail for only the spread of the currency you are trading ??

why do you think you need a line object to substitute the trailing

 you don't allow your trade to trail at all.....

 
deVries:

Why do you need trailingfunction if it has room to trail for only the spread of the currency you are trading ??

why do you think you need a line object to substitute the trailing

 you don't allow your trade to trail at all.....


Hi again deVries;

Thank you for your patience with my issues.

So, regarding your first comment the logic is ; as soon the account is say, 1.5 (15) in profit, the trail will move the stop loss to  a point where if market bounce back the profit is guaranteed an if price keeps going up for a buy or going down for a sell then one can get an extra profit. Once order close by mean of trail then a close all will happen.

Regarding your second comment the reason why I though to use a line object is because that sometimes the trail do not start as  do not know why so in my mind I was afraid that was because a broker limit. So using another way to get the trailing perhaps I could use an order with a little pips ( I'm an ignorant, I know...)

 Last for your third comment, So as far I understand I need to use more pips before start trail, right ?

Thanks once more for your support

Luis 

 
luisneves:


Hi again deVries;

Thank you for your patience with my issues.

So, regarding your first comment the logic is ; as soon the account is say, 1.5 (15) in profit, the trail will move the stop loss to  a point where if market bounce back the profit is guaranteed an if price keeps going up for a buy or going down for a sell then one can get an extra profit. Once order close by mean of trail then a close all will happen.

Regarding your second comment the reason why I though to use a line object is because that sometimes the trail do not start as  do not know why so in my mind I was afraid that was because a broker limit. So using another way to get the trailing perhaps I could use an order with a little pips ( I'm an ignorant, I know...)

 Last for your third comment, So as far I understand I need to use more pips before start trail, right ?

Thanks once more for your support

Luis 


This is what i understand of the situation......

As soon your account  ( i think you mean trade )  is 1.5 in profit you want to trail the stoploss

What happens ??

You open a   Buy trade at 1.33500  EURUSD  place a Stoploss and a TakeProfit when it succeeding opening   

What values does it get after modification if orderopenprice = 1.33500 ??

OrderStopLoss() = ............  ????

OrderTakeProfit() = ............  ????     PLEASE ANSWER

.

Now you wait till Price for EURUSD  becomes   1.33515  and you want it to modify OrderStopLoss()   somewhere between 1.33500 and 1.33515  ???

and you want to try to modify it with every 0.00001 pricedifference    

You get stress looking to your chart i think 

You need to change your strategy  

.

You not only need more pips for starting the trail

you also have to trail with more pips

 
deVries:


This is what i understand of the situation......

As soon your account  ( i think you mean trade )  is 1.5 in profit you want to trail the stoploss

What happens ??

You open a   Buy trade at 1.33500  EURUSD  place a Stoploss and a TakeProfit when it succeeding opening   

What values does it get after modification if orderopenprice = 1.33500 ??

OrderStopLoss() = ............  ????

OrderTakeProfit() = ............  ????     PLEASE ANSWER

.

Now you wait till Price for EURUSD  becomes   1.33515  and you want it to modify OrderStopLoss()   somewhere between 1.33500 and 1.33515  ???

and you want to try to modify it with every 0.00001 pricedifference    

You get stress looking to your chart i think 

You need to change your strategy  

.

You not only need more pips for starting the trail

you also have to trail with more pips






Hi deVries,

Sorry for delay to reply.

So, now seems that the Trail is working fine. Now the only part that is not working as I would like to is the opening of opposite orders at same distance. As far I understood from later support you and WHRoeder provided seems that is not possible to get open orders that are say 2 pips(20) all times.

Is that correct ?

Nevertheless here is the code used to get the opposite orders. Is that possible that some of the code is incorrect ? 

Thank you in advance for any support

Luis

 

void OpenOppositeOrder()
    {     
    int Op;
    
   for(int Counter = OrdersTotal()-1; Counter >= 0; Counter--)
      {//
      if(!OrderSelect(Counter,SELECT_BY_POS,MODE_TRADES))continue;          
         {//
         if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)           
            {//16
             Op = OrderType();
             RefreshRates();            
            if(Op == OP_BUY && Ask < (OrderOpenPrice() - (OppositeDistance*pips2dbl)) && SellAllowed)                      
               {//  
                while(IsTradeContextBusy()) 
                      Sleep(10);
                      RefreshRates();                             
                SellTicket = OrderSend(Symbol(), OP_SELL, MLots, Bid, Slippage*pips2points, 0, 0, "Open Opposite Sell Order", MagicNumber, 0, Red);
                if(SellTicket > -1) 
                  {//
                  BuyAllowed=true;SellAllowed=false;                                          
                  Print("Opposite Sell order placed # ", SellTicket);                
                  AddLimitsSell();                                    
                  return;
                  }//
               else
                  {//
                  Print("Opposite Sell Order Failed,order number:",OrderTicket(),"Error:",GetLastError());
                  }//
                }// 
                RefreshRates();           
            if(Op == OP_SELL &&  Bid > OrderOpenPrice() + (OppositeDistance*pips2dbl) && BuyAllowed)           
              {//  
               while(IsTradeContextBusy()) 
                    Sleep(10);
                         RefreshRates();                                
               BuyTicket = OrderSend(Symbol(), OP_BUY, MLots, Ask, Slippage*pips2points, 0, 0, " Open Opposite Buy Order", MagicNumber, 0, Green); 
               if(BuyTicket > -1)
                  {//
                  BuyAllowed=false;SellAllowed=true;                                                          
                  Print("Opposite Buy order placed # ", BuyTicket);                             
                  AddLimitsBuy();
               return;
                  }//
               else
                  {//  
                  Print("Opposite Buy Order Failed, order number:",OrderTicket(),"Error:",GetLastError());
                  }//   
               }//
            }//
         }//    
      }//      
    }//
 
luisneves: as soon the account is say, 1.5 (15) in profit, the trail will move the stop loss to  a point where if market bounce back the profit is guaranteed an if price keeps going up for a buy or going down for a sell then one can get an extra profit.
No line needed, just close when it reverses. Use a reasonable SL (10+pips) in case of disconnection.
Not Compiled, Not tested
#define SLpips 1.5
if( OrderSelect ..){
   datetime oot = OrderOpenTime();
   int      iOot = iBarShift(NULL,0, oot),
            iHH  = iHighest(NULL,0, MODE_HIGH, iOOt+1, 0);
   double   HH   = High[iHH]
            tsl  = HH - SLpips * pips2dbl;
   if (tsl > OrderOpenPrice() && Bid < tsl)
     if(!OrderClose(..
Not Compiled, Not tested
 

Please explain what kind of martingale strategy are you trying to make ???

Your code is full of strange coding....  that i get problems to understand what you try to do

you don't tell how your trailing is coded now

and now we get an opposite strategy where you in the loop of your orderstotal open new trades and refresh fresh fresh rates

this is the begin if the ea restarts what has there to happen  ???

  

bool BuyAllowed = true,SellAllowed = true; 
 //+------------------------------------------------------------------------------------------------------------------+ 
 int init ()// Adjust for 4 or 5 digits.
   {
    if(Digits % 2 == 1)  // DE30=1/JPY=3/EURUSD=5 forum.mql4.com/43064#515262
     {pips2dbl = Point*10; pips2points = 10;   Digits.pips = 1;}
     else {pips2dbl = Point;    pips2points =  1;   Digits.pips = 0;}
     // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl       
  
           
      BuyTrigger = Ask + OpenDistance * pips2dbl;     
      SellTrigger = Bid - OpenDistance * pips2dbl;
      
   //+------------------------------------------------------------------------------------------------------------------+      
  // Get Current Market Status of the EA
  
   int LastClosedTicket = GetTicketFromHistory(Symbol(),MagicNumber);
     
   if(LastClosedTicket > 0)
    {                                                                 
      Print("LastClosedTicket=",LastClosedTicket);                   
      if(OrderSelect(LastClosedTicket,SELECT_BY_TICKET))                 
       {            
         if(OrderType()== OP_BUY)
         {
         BuyAllowed = false;SellAllowed = true;
         } 
         else                   
         {
         BuyAllowed = true;SellAllowed = false;
         } 
       }   
     }                
 return(0);   
   }
 

Do you look in the closed trades for last closed trade if it is buy or sell if buy you want open sell

if last closed sell you want to open new buy trade ??

Question(s)

don't you have to know if there is still trades running ?? 

and what has to happen if there is no history of the EA in historytrades

 

So what did you wanna make the moment you started to code this ??

Reason: