Modify_BUY-SL-TP

Author: Copyright � 2012 Matus German www.MTexperts.net
Modify_BUY-SL-TP
Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategy
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Modify_BUY-SL-TP
//+------------------------------------------------------------------+
//|            Copyright © 2011, Matus German, matusgerman@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012 Matus German www.MTexperts.net"
#property show_inputs

extern string descr   = "*** Modify BUY orders ***";
extern double SLpip   = 0;
extern double TPpip   = 0;
extern double SLprice = 0;
extern double TPprice = 0;

double pips2dbl, pips2point, minDistance,
       slPoints, tpPoints,
       itotal,
       sl, tp;

// script modifies stop loss and take profit
int start()
{
   minDistance=MarketInfo(Symbol(),MODE_STOPLEVEL)*Point;
   
   if (Digits == 5 || Digits == 3)    // Adjust for five (5) digit brokers.
   {            
      pips2dbl = Point*10; pips2point = 10;
   } 
   else 
   {    
      pips2dbl = Point;   pips2point = 1;
   }

   slPoints=SLpip*pips2dbl;
   tpPoints=TPpip*pips2dbl;

   itotal=OrdersTotal();
   for(int icnt=itotal-1;icnt>=0;icnt--) 
   {
      OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol()==Symbol() && OrderType()==OP_BUY)
      {
         RefreshRates();
         sl=0; tp=0;
         if(slPoints>0)
            sl=Bid-slPoints;
         else if(SLprice>0)
            sl=SLprice;
         else
            sl=OrderStopLoss();
            
         if(tpPoints>0)
            tp=Bid+tpPoints;
         else if(TPprice>0)
            tp=TPprice;
         else
            tp=OrderTakeProfit(); 
            
         if(Bid-sl<=MarketInfo(Symbol(),MODE_STOPLEVEL)*Point && sl>0)          // check broker stop levels
         {
            Alert("Stop Loss is too close to market price or on wrong side!!!");
            return(0);
         } 

         if(tp-Bid<=MarketInfo(Symbol(),MODE_STOPLEVEL)*Point && tp>0)
         {
            Alert("Take Profit is too close to market price or on wrong side!!!");
            return(0);
         } 
         
         if(OrderTakeProfit()!=tp || OrderStopLoss()!=sl)
         {
            while(OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,Green)==false)
            {
               RefreshRates();
               if(Bid-sl<=MarketInfo(Symbol(),MODE_STOPLEVEL)*Point && sl>0)          // check broker stop levels
               {
                  Alert("Stop Loss is too close to market price or on wrong side!!!");
                  return(0);
               }  

               if(tp-Bid<=MarketInfo(Symbol(),MODE_STOPLEVEL)*Point && tp>0)
               {
                  Alert("Take Profit is too close to market price or on wrong side!!!");
                  return(0);
               } 
            }   
         }                      
      } 
   }
   return(0);
}
//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---