Trailing stop ONLY on the remaining open positions. - page 2

 
//+----------------------------------------------------------------------------------------------------------------------------------------+  
//Moving Average Trailing Stop Function
//+----------------------------------------------------------------------------------------------------------------------------------------+   
void MA_Trail()

{

   ATR = iATR(NULL,60,14,1);
   MA = iMA(NULL,60,MA_Period,0,1,0,1);
   
   double BuyStopPriceMath = MA - ATR;
   double SellStopPriceMath = MA + ATR;
   
   double new_BSL = NormalizeDouble(BuyStopPriceMath,Digits);
   double new_SSL = NormalizeDouble(SellStopPriceMath,Digits);

   for(int b=OrdersTotal()-1; b>=0; b--)
     {
      if( !OrderSelect(b,SELECT_BY_POS,MODE_TRADES))continue;
          if( OrderMagicNumber() == MagicNumber1 || OrderMagicNumber()== MagicNumber2 || 
              OrderMagicNumber()== MagicNumber3 || OrderMagicNumber()== MagicNumber4 )
            if(OrderSymbol() == Symbol())
               {

               if( OrderType()==OP_BUY  )
                  {
                    if( OrderStopLoss() > new_BSL - Point )continue; 
                    if( new_BSL > OrderStopLoss() - Point )
                    {
                     bool BuyModify = OrderModify(OrderTicket(),OrderOpenPrice(),new_BSL,OrderTakeProfit(),0,CLR_NONE);
                     if(BuyModify == True )Print("Trailing BUY Order Modified on: ", Symbol());
                     if(BuyModify != True )Print("Trailing BUY Order Modify Failed: ", GetLastError());
                    }
                  }     

    
               if( OrderType()==OP_SELL )
                  {
                    
                    if( new_SSL > OrderStopLoss() - Point )continue; 
                    if( OrderStopLoss() > new_SSL - Point )
                    {
                     bool SellModify = OrderModify(OrderTicket(),OrderOpenPrice(),new_SSL,OrderTakeProfit(),0,CLR_NONE);
                     if(SellModify == True )Print("Trailing SELL Order Modified on: ", Symbol());
                     if(SellModify != True )Print("Trailing SELL Order Modify Failed: ", GetLastError());
                    }
                  }
               }   
    
     }
}
Finally done it - Christ almighty. I feel so stupid looking at it now!
 
DomGilberto:
Finally done it - Christ almighty. I feel so stupid looking at it now!



Just something to think about

if( new_BSL > OrderStopLoss() - Point )


//What if new_BSL == OrderStopLoss() ? That would make your code the same as

if( OrderStopLoss() > OrderStopLoss() - Point )  //Which of course HAS to be true

//So wouldn't 

if( new_BSL > OrderStopLoss() + Point )  //have been better?

//Then you would have no need for

if( OrderStopLoss() > new_BSL - Point )continue; 
 

So you're right. To be perfectly honest, I have tried understanding the "Point" built in variable, but I actually don't understand its purpose to the if statement.

This will sound horrifying to some, but I use it because many moons ago I read a large article on comparing doubles (think it was by WHRoeder - could be wrong) and I noticed that in there. Then someone told me in a post to do it.

There isn't much information on Point within the referencing doc.

+ or - Point at the end of an 'double' equality statement... What does this change? Is it just telling the if statement to be "point size of the current symbol in the quote currency", i.e. in my case, 5 decimal places?

Thanks for your input Gumrai, have swapped it over. Works fine!

 
Last question on this thread, if I wanted to make a dynamic trailing stop, how would I go about it? I have got to this point so far but not working correctly?

It trails, but before the second target is hit by the bid, it actually jumps to the different MA trail? "MA2" below?


//+----------------------------------------------------------------------------------------------------------------------------------------+  
//Moving Average Trailing Stop Function
//+----------------------------------------------------------------------------------------------------------------------------------------+   
void MA_Trail()

{
   
static datetime Target_2_reached;
double          Target2_Stored_OpenTime = GlobalVariableGet(" Target 2 hit ");
   
ATR = iATR(NULL,low,ATR_Period,1);
MA = iMA(NULL,low,MA_Period,0,1,0,1); 
double BuyStopPriceMath = MA - ATR;
double SellStopPriceMath = MA + ATR;
double new_BSL_1 = NormalizeDouble(BuyStopPriceMath,Digits);
double new_SSL_1 = NormalizeDouble(SellStopPriceMath,Digits);
//----------------------------------------------------------//
double MA2 = iMA(NULL,low,21,0,1,0,1); 
double BuyStopPriceMath2 = MA2 - ATR;
double SellStopPriceMath2 = MA2 + ATR;
double new_BSL_2 = NormalizeDouble(BuyStopPriceMath2,Digits);
double new_SSL_2 = NormalizeDouble(SellStopPriceMath2,Digits);

for(int b=OrdersTotal()-1; b>=0; b--)
  {
  if( !OrderSelect(b,SELECT_BY_POS,MODE_TRADES))continue;
   if( OrderMagicNumber() == MagicNumber1 || OrderMagicNumber()== MagicNumber2 || 
      OrderMagicNumber()== MagicNumber3 || OrderMagicNumber()== MagicNumber4 )
      if(OrderSymbol() == Symbol())
      {
               
      if( OrderType()==OP_BUY  )
       {
       if( OrderMagicNumber() == MagicNumber2 ){
          TakeProfit_2 = OrderTakeProfit();
         }
                     
        if( new_BSL_1 > OrderStopLoss() + Point && OrderOpenTime() != Target2_Stored_OpenTime) 
           {
           bool BuyModify = OrderModify(OrderTicket(),OrderOpenPrice(),new_BSL_1,OrderTakeProfit(),0,CLR_NONE);
           if(BuyModify == True )Print("Original MA trail BUY Order Modified on: ", Symbol());
           if(BuyModify != True )Print("Original MA trail BUY Order Modify Failed: ", GetLastError());
           }
                    
        if( Bid > TakeProfit_2 - Point && OrderOpenTime() != Target2_Stored_OpenTime )
           {
            Target_2_reached = GlobalVariableSet(" Target 2 hit ", OrderOpenTime());
           }
                        
        if( OrderOpenTime() == Target2_Stored_OpenTime )
           {
            if( new_BSL_2 > OrderStopLoss() + Point )
           {
        
        if( OrderOpenTime() == Target2_Stored_OpenTime )
           {
           if( new_BSL_2 > OrderStopLoss() + Point )
              {
              bool BuyModify2 = OrderModify(OrderTicket(),OrderOpenPrice(),new_BSL_2,OrderTakeProfit(),0,CLR_NONE);
               if(BuyModify2 == True )Print("MA CHANGED - Trailing BUY Order Modified on: ", Symbol());
               if(BuyModify2 != True )Print("MA CHANGED - Trailing BUY Order Modify Failed: ", GetLastError());
              } 
          }

   }

...
 
(sorry for being a little spammy) but I have looked at this for an hour now!

I think what is happening is on the very first trade taken, on the FIRST loop, it selects OrderMagicNumber1 and therefore cannot find the TakeProfit_2 price. Therefore, it is set to "0". When it gets to:

if( Bid > TakeProfit_2 - Point && OrderOpenTime() != Target2_Stored_OpenTime )
     {
      Target_2_reached = GlobalVariableSet(" Target 2 hit ", OrderOpenTime());
     }


  1. Bid is always going to be > than TakeProfit2! How do I make sure on the very first loop that I obtain the TakeProfit price of the OrderMagicNumber2????
  2. Also, what happens if the particular pair has not traded in over 4 weeks. The globalvariableset will have been forgotten, right???

 
 TakeProfit_2 - Point
I think your math is a bit wrong. Do you know what and how to use Point ?
 
I think you're right. I do not entirely understand Point? The help file doesn't explain a lot?
 

Point in this case means how many digits after dot. If it is 5 digits means 1*Point = 0.00001

Use Print(Point); to get the point value. From there, what-is-the-distance-you-want * Point

The 5th digit means 1/10 of 1.

 
Ok nice one thanks. I think I understand. 

Would you mind taking a look at this? It seems to work flawlessly on the OP_BUY side by not on the OP_SELL side? Basic symmetry here but I cannot spot the difference. I apologize; it's a little messy!

To be specific, it almost ignores everything below on the sell side. As soon as the OP_SELLSTOP turns into OP_SELL it switches straight to the very last OrderModify function at the bottom with the "new_SSL_3" price!?

//+----------------------------------------------------------------------------------- 
//Moving Average Trailing Stop Function
//+-----------------------------------------------------------------------------------   
void MA_Trail()
{ 

  //- Original MA Trail Parameters -------------------------------------------------++
   ATR = iATR(NULL,low,ATR_Period,1);
   MA = iMA(NULL,low,MA_Period,0,1,0,1); 
   double BuyStopPriceMath = MA - ATR;
   double SellStopPriceMath = MA + ATR;
   double new_BSL_1 = NormalizeDouble(BuyStopPriceMath,Digits);
   double new_SSL_1 = NormalizeDouble(SellStopPriceMath,Digits);
  //- Dynamic MA2 Trail Parameters -------------------------------------------------++
   double MA2 = iMA(NULL,low,Dynamic_MA2,0,1,0,1); 
   double BuyStopPriceMath2 = MA2 - ATR;
   double SellStopPriceMath2 = MA2 + ATR;
   double new_BSL_2 = NormalizeDouble(BuyStopPriceMath2,Digits);
   double new_SSL_2 = NormalizeDouble(SellStopPriceMath2,Digits);

  //- Dynamic MA3 Trail Parameters -------------------------------------------------++
   double MA3 = iMA(NULL,low,Dynamic_MA3,0,1,0,1); 
   double BuyStopPriceMath3 = MA3 - ATR;
   double SellStopPriceMath3 = MA3 + ATR;
   double new_BSL_3 = NormalizeDouble(BuyStopPriceMath3,Digits);
   double new_SSL_3 = NormalizeDouble(SellStopPriceMath3,Digits);   
   
  
  //- Select BUY Trade with SECOND target ------------------------------------------++
  if(OrderSelect(BuyTicketOrder2, SELECT_BY_TICKET) == True){
      if( OrderMagicNumber() == MagicNumber1 && OrderSymbol() == Symbol() ){
      Buy_TakeProfit_2 = OrderTakeProfit();}
      }
   static datetime Buy_Target_2_reached;
   double          Target2_StoredBuy_OpenTime = GlobalVariableGet(" Buy Target 2 hit ");   
   
  //- Select SELL Trade with SECOND target -----------------------------------------++  
  if(OrderSelect(SellTicketOrder2, SELECT_BY_TICKET) == True){
      if( OrderMagicNumber() == MagicNumber1 && OrderSymbol() == Symbol() ){
      Sell_TakeProfit_2 = OrderTakeProfit();}
      }
   static datetime Sell_Target_2_reached;
   double          Target2_StoredSell_OpenTime = GlobalVariableGet(" Sell Target 2 hit ");
  
  
  
  //- Select BUY Trade with THIRD target ------------------------------------------++
  if(OrderSelect(BuyTicketOrder3, SELECT_BY_TICKET) == True){
      if( OrderMagicNumber() == MagicNumber1 && OrderSymbol() == Symbol() ){
      Buy_TakeProfit_3 = OrderTakeProfit();}
      }
   static datetime Buy_Target_3_reached;
   double          Target3_StoredBuy_OpenTime = GlobalVariableGet(" Buy Target 3 hit ");   
   
  //- Select SELL Trade with THIRD target -----------------------------------------++  
  if(OrderSelect(SellTicketOrder3, SELECT_BY_TICKET) == True){
      if( OrderMagicNumber() == MagicNumber1 && OrderSymbol() == Symbol() ){
      Sell_TakeProfit_3 = OrderTakeProfit();}
      }
  static datetime Sell_Target_3_reached;
  double          Target3_StoredSell_OpenTime = GlobalVariableGet(" Sell Target 3 hit ");
  //--------------------------------------------------------------------------------++  
  
  for(int b=OrdersTotal()-1; b>=0; b--)
     {
     if( !OrderSelect(b,SELECT_BY_POS,MODE_TRADES))continue;
      if( OrderMagicNumber() == MagicNumber1  )
        if(OrderSymbol() == Symbol())
        {
        RefreshRates();
               
      
      if( OrderType()==OP_BUY  )
          {
          
         if( new_BSL_1 > OrderStopLoss() + Point && OrderOpenTime() != Target2_StoredBuy_OpenTime &&
             OrderOpenTime() != Target3_StoredBuy_OpenTime ) 
           {
            bool BuyModify = OrderModify(OrderTicket(),OrderOpenPrice(),new_BSL_1,OrderTakeProfit(),0,CLR_NONE);
            if(BuyModify == True )Print("Original MA trail BUY Order Modified on: ", Symbol());
            if(BuyModify != True )Print("Original MA trail BUY Order Modify Failed: ", GetLastError());
           }
                    
          if( Bid - Buy_TakeProfit_2 > Point / 2. && OrderOpenTime() != Target2_StoredBuy_OpenTime )
            {
             Buy_Target_2_reached = GlobalVariableSet(" Buy Target 2 hit ", OrderOpenTime());
            }
          
          if( Bid - Buy_TakeProfit_3 > Point / 2. && OrderOpenTime() != Target3_StoredBuy_OpenTime )
            {
             Buy_Target_3_reached = GlobalVariableSet(" Buy Target 3 hit ", OrderOpenTime());
            }  
                        
         if( OrderOpenTime() == Target2_StoredBuy_OpenTime && OrderOpenTime() != Target3_StoredBuy_OpenTime)
           {
            if( new_BSL_2 > OrderStopLoss() + Point )
             {
              bool BuyModify2 = OrderModify(OrderTicket(),OrderOpenPrice(),new_BSL_2,OrderTakeProfit(),0,CLR_NONE);
              if(BuyModify2 == True )Print("MA CHANGED - Trailing BUY Order Modified on: ", Symbol());
              if(BuyModify2 != True )Print("MA CHANGED - Trailing BUY Order Modify Failed: ", GetLastError());
             } 
           }
          
        if( OrderOpenTime() == Target3_StoredBuy_OpenTime )
         {
          if( new_BSL_3 > OrderStopLoss() + Point )
           {
            bool BuyModify3 = OrderModify(OrderTicket(),OrderOpenPrice(),new_BSL_3,OrderTakeProfit(),0,CLR_NONE);
            if(BuyModify3 == True )Print("3rd Profit Target Hit. Dynamic MA changed again: ", Symbol());
            if(BuyModify3 != True )Print("MA3 tried to CHANGE - Trailing BUY Order Modify Failed: ", GetLastError());
           } 
         }   
        }     

      
      
     if( OrderType()==OP_SELL )
        {
            
       if( OrderStopLoss() > new_SSL_1 + Point && OrderOpenTime() != Target2_StoredSell_OpenTime &&
         OrderOpenTime() != Target3_StoredSell_OpenTime )
         {
         bool SellModify = OrderModify(OrderTicket(),OrderOpenPrice(),new_SSL_1,OrderTakeProfit(),0,CLR_NONE);
         if(SellModify == True )Print("Trailing SELL Order Modified on: ", Symbol());
         if(SellModify != True )Print("Trailing SELL Order Modify Failed: ", GetLastError());
         }
                   
       if( Sell_TakeProfit_2 - Ask > Point / 2. && OrderOpenTime() != Target2_StoredSell_OpenTime )
         {
          Sell_Target_2_reached = GlobalVariableSet(" Sell Target 2 hit ", OrderOpenTime());
         }
         
       if( Sell_TakeProfit_3 - Ask > Point / 2. && OrderOpenTime() != Target3_StoredSell_OpenTime )
        {
         Sell_Target_3_reached = GlobalVariableSet(" Sell Target 3 hit ", OrderOpenTime());
        }
                               
     if( OrderOpenTime() == Target2_StoredSell_OpenTime && OrderOpenTime() != Target3_StoredSell_OpenTime )
       {
        if( OrderStopLoss() > new_SSL_2 + Point )
         {
          bool SellModify2 = OrderModify(OrderTicket(),OrderOpenPrice(),new_SSL_2,OrderTakeProfit(),0,CLR_NONE);
          if(SellModify2 == True )Print("MA CHANGED - Trailing SELL Order Modified on: ", Symbol());
          if(SellModify2 != True )Print("MA CHANGED - Trailing SELL Order Modify Failed: ", GetLastError());
         } 
       }
          
      if( OrderOpenTime() == Target3_StoredSell_OpenTime )
       {
       if( OrderStopLoss() > new_SSL_3 + Point )
         {
         bool SellModify3 = OrderModify(OrderTicket(),OrderOpenPrice(),new_SSL_3,OrderTakeProfit(),0,CLR_NONE);
         if(SellModify3 == True )Print("MA CHANGED - Trailing SELL Order Modified on: ", Symbol());
         if(SellModify3 != True )Print("MA CHANGED - Trailing SELL Order Modify Failed: ", GetLastError());
         } 
       }                     
     }
  }   
 }
}
 
Wow this is BUGGING me!
Reason: