Manual_Modify_StopLoss_TakeProfit

Author: Copyright � 2011, ������� ��������
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
Manual_Modify_StopLoss_TakeProfit
//+------------------------------------------------------------------+
//| Ðó÷íàÿ óñòàíîâêà è ïåðåìåùåíèå îðäåðîâ                           |
//|                     Manual Open and Modify Stop Limit orders.mq4 |
//|                               Copyright © 2011, Õëûñòîâ Âëàäèìèð |
//|                                                cmillion@narod.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Õëûñòîâ Âëàäèìèð"
#property link      "cmillion@narod.ru"
#property strict
//--------------------------------------------------------------------
extern int     Stoploss          = 0,     //ñòîïëîññ îðäåðîâ
               Takeprofit        = 10;     //òåéêïðîôèò îðäåðîâ

string Obj;
double SL,TP,STOPLEVEL;
//--------------------------------------------------------------------
int OnInit()
{
   STOPLEVEL = MarketInfo(Symbol(),MODE_STOPLEVEL);
   if (Stoploss <STOPLEVEL && Stoploss!=0) {Stoploss=0;Alert("Stoploss ",Stoploss,"  ìåíåå äîïóñòèìîãî óðîâíÿ ",STOPLEVEL," Stoploss îòêëþ÷åí");}
   if (Takeprofit <STOPLEVEL && Takeprofit!=0) {Takeprofit=0;Alert("Takeprofit ",Takeprofit,"  ìåíåå äîïóñòèìîãî óðîâíÿ ",STOPLEVEL," Takeprofit îòêëþ÷åí");}
   if (Stoploss!=0)
   {  
      Obj = "Sell SL";
      if (ObjectFind(Obj)==-1) {drawline(Obj,Red,NormalizeDouble(Ask+Stoploss*Point,Digits));drawtext(Obj,NormalizeDouble(Ask+Stoploss*Point,Digits));}
      Obj = "Buy SL";
      if (ObjectFind(Obj)==-1) {drawline(Obj,Blue,NormalizeDouble(Bid-Stoploss*Point,Digits));drawtext(Obj,NormalizeDouble(Bid-Stoploss*Point,Digits));}
   }
   else 
   {
      ObjectDelete("Sell SL");
      ObjectDelete("Buy SL");
      ObjectDelete("Sell SLn");
      ObjectDelete("Buy SLn");
   }
   if (Takeprofit!=0)
   {  
      Obj = "Sell TP";
      if (ObjectFind(Obj)==-1) {drawline(Obj,Red,NormalizeDouble(Bid-Takeprofit*Point,Digits));drawtext(Obj,NormalizeDouble(Bid-Takeprofit*Point,Digits));}
      Obj = "Buy TP";
      if (ObjectFind(Obj)==-1) {drawline(Obj,Blue,NormalizeDouble(Ask+Takeprofit*Point,Digits));drawtext(Obj,NormalizeDouble(Ask+Takeprofit*Point,Digits));}
   }
   else 
   {
      ObjectDelete("Sell TP");
      ObjectDelete("Buy TP");
      ObjectDelete("Sell TPn");
      ObjectDelete("Buy TPn");
   }
   return(INIT_SUCCEEDED);
}
//--------------------------------------------------------------------
void OnDeinit(const int reason)
{
   ObjectDelete("Sell SL");
   ObjectDelete("Buy SL");
   ObjectDelete("Sell TP");
   ObjectDelete("Buy TP");
   ObjectDelete("Sell SLn");
   ObjectDelete("Buy SLn");
   ObjectDelete("Sell TPn");
   ObjectDelete("Buy TPn");
}
//--------------------------------------------------------------------
void OnTick()
{
if (Takeprofit==0 && Stoploss==0) return;
   double OOP,OSL,OTP;
   int tip;
   double SLsell = NormalizeDouble(ObjectGet("Sell SL",OBJPROP_PRICE1),Digits);
   double SLbuy  = NormalizeDouble(ObjectGet("Buy SL",OBJPROP_PRICE1),Digits);
   double TPsell = NormalizeDouble(ObjectGet("Sell TP",OBJPROP_PRICE1),Digits);
   double TPbuy  = NormalizeDouble(ObjectGet("Buy TP",OBJPROP_PRICE1),Digits);
   if (SLsell!=0) drawtext("Sell SL", SLsell);
   if (SLbuy!=0 ) drawtext("Buy SL", SLbuy);
   if (TPsell!=0) drawtext("Sell TP", TPsell);
   if (TPbuy!=0)  drawtext("Buy TP", TPbuy);
   RefreshRates();
   for (int i=0; i<OrdersTotal(); i++)
   {    
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      { 
         if (OrderSymbol()==Symbol())
         { 
            tip = OrderType(); 
            OOP = NormalizeDouble(OrderOpenPrice(),Digits);
            OSL = NormalizeDouble(OrderStopLoss(),Digits);
            OTP = NormalizeDouble(OrderTakeProfit(),Digits);
            SL=0;TP=0;
            if (tip==OP_BUY)             
            {
               if ((Bid-STOPLEVEL*Point)<SLbuy) SL=OSL; else SL=SLbuy;
               if ((Ask+STOPLEVEL*Point)>TPbuy) TP=OTP; else TP=TPbuy;
               if (SL != OSL || TP != OTP)
               {  
                  if (!OrderModify(OrderTicket(),OOP,SL,TP,0,White)) Print("Error OrderModify ",GetLastError());
               }
            }                                         
            if (tip==OP_SELL)        
            {
               if ((Bid-STOPLEVEL*Point)<TPsell) TP=OTP; else TP=TPsell;
               if ((Ask+STOPLEVEL*Point)>SLsell) SL=OSL; else SL=SLsell;
               if (SL != OSL || TP != OTP)
               {  
                  if (!OrderModify(OrderTicket(),OOP,SL,TP,0,White)) Print("Error OrderModify ",GetLastError());
               }
            } 
         }
      }
   } 
}
//--------------------------------------------------------------------
void drawline(string NameL, color col, double Y1)
{
   ObjectCreate(NameL, OBJ_HLINE, 0,0,Y1,0,0);
   ObjectSet   (NameL, OBJPROP_COLOR, col);
   ObjectSet   (NameL, OBJPROP_STYLE, STYLE_SOLID);
   ObjectSet   (NameL, OBJPROP_WIDTH, 1);
   ObjectSet   (NameL, OBJPROP_BACK,  false);
   ObjectSet   (NameL, OBJPROP_RAY,   false);
}
//--------------------------------------------------------------------
void drawtext(string NameL, double Y1)
{
   string NameLine=StringConcatenate(NameL,"n");
   ObjectDelete (NameLine);
   ObjectCreate (NameLine, OBJ_TEXT,0,Time[WindowFirstVisibleBar()-WindowFirstVisibleBar()/4],Y1,0,0,0,0);
   ObjectSetText(NameLine, NameL ,8,"Arial");
   ObjectSet    (NameLine, OBJPROP_COLOR, ObjectGet(NameL,OBJPROP_COLOR));
}
//--------------------------------------------------------------------

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 ---