cm Trailing SL TP

Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategyIt automatically opens orders when conditions are reached
0 Views
0 Downloads
0 Favorites
cm Trailing SL TP
ÿþ//+------------------------------------------------------------------+

//|                               Copyright © 2011, %;KAB>2 ;048<8@ |

//|                                                cmillion@narod.ru |

//+------------------------------------------------------------------+

#property copyright   "Copyright © 2019, http://cmillion.ru"

#property link        "cmillion@narod.ru"

#property strict

#property description ""@59;8=3 SL 8 TP"

#property description ""@0;8B AB>?;>AA 2 AB>@>=C ?@81K;8 8 B59:?@>D8B 2 AB>@>=C C1KB:0 =0 @0AAB>O=88 C:070==>< 2 ?0@0<5B@0E"

#property description "A;8 AB>?;>AA 8;8 B59:?@>D8B 2 ?0@0<5B@0E C:070= 0 B> B@0;0 A>>B25BAB2CNI53> ?0@0<5B@0 =5 1C45B"

#property description "TS - <8=8<0;L=0O ?@81K;L 1;865 :>B>@>9 AB>?K =5 2KAB02;ONBAO. ;O 55 >B:;NG5=8O CAB0=>28B5 1>;LH>5 >B@8F0B5;L=>5 G8A;>"

//--------------------------------------------------------------------

extern int stoploss             = 100;

extern int takeprofit           = 50;

extern int TS=-10000;

//--------------------------------------------------------------------

double STOPLEVEL;

//--------------------------------------------------------------------

void OnTick()

{

   STOPLEVEL=MarketInfo(Symbol(),MODE_STOPLEVEL);

   double OSL,OTP,OOP,PR=0,SL=0,TP=0;

   int i,b=0,s=0,OT=-1;

   for (i=0; i<OrdersTotal(); i++)

   {    

      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

      { 

         if (OrderSymbol()==Symbol())

         { 

            OT = OrderType(); 

            OSL = NormalizeDouble(OrderStopLoss(),Digits);

            OTP = NormalizeDouble(OrderTakeProfit(),Digits);

            OOP = NormalizeDouble(OrderOpenPrice(),Digits);

            SL=OSL;TP=OTP;

            if (OT==OP_BUY)             

            {  

               b++;

               if (takeprofit>STOPLEVEL)

               {

                  PR = NormalizeDouble(Ask + takeprofit * Point,Digits);

                  if ((PR < OTP || OTP==0) && (PR - OOP)/Point>=TS) TP = PR;

               } 

               else TP=0;

               if (stoploss>STOPLEVEL)

               {

                  PR = NormalizeDouble(Bid - stoploss*Point,Digits); 

                  if (PR > OSL && (PR - OOP)/Point>=TS) SL = PR;

               }

               else SL=0;

               if (SL != OSL || TP != OTP)

               {  

                  if (!OrderModify(OrderTicket(),OOP,SL,TP,0,clrNONE)) 

                     Print("Error OrderModify <<",(GetLastError()),">> ");

               }

            }                                         

            if (OT==OP_SELL)        

            {

               s++;

               if (takeprofit>STOPLEVEL)

               {

                  PR = NormalizeDouble(Bid - takeprofit * Point,Digits);

                  if (PR > OTP && (OOP - PR)/Point>=TS) TP = PR;

               }

               else TP=0;

               if (stoploss>STOPLEVEL)

               {

                  PR = NormalizeDouble(Ask + stoploss*Point,Digits); 

                  if ((PR < OSL || OSL==0) && (OOP - PR)/Point>=TS) SL = PR;

               }

               else SL=0;

               if (SL != OSL || TP != OTP)

               {  

                  if (!OrderModify(OrderTicket(),OOP,SL,TP,0,clrNONE)) 

                     Print("Error OrderModify <<",(GetLastError()),">> ");

               }

            } 

         }

      }

   } 

   if (IsTesting())

   {

      if (b==0) SendOrder(OP_BUY, MarketInfo(Symbol(),MODE_MINLOT), NormalizeDouble(Ask,Digits)); 

      if (s==0) SendOrder(OP_SELL, MarketInfo(Symbol(),MODE_MINLOT), NormalizeDouble(Bid,Digits)); 

   }

}

//--------------------------------------------------------------------

bool SendOrder(int OT, double lots, double price, double sl=0, double tp=0)

{

   if (OT<2)

   {

      if (AccountFreeMarginCheck(Symbol(),OT,lots)<0) return(false);

   }

   for (int i=0; i<10; i++)

   {    

      if (OrderSend(Symbol(),OT, lots,price,100,sl,tp,NULL,0,0,clrNONE)!=-1) return(true);

      Sleep(500);

      RefreshRates();

   }

   return(false);

}

//------------------------------------------------------------------

string Strtip(int OT)

{

   switch(OT) 

   { 

   case OP_BUY:

      return("BUY"); 

   case OP_SELL:

      return("SELL"); 

   case OP_BUYSTOP:

      return("BUYSTOP"); 

   case OP_SELLSTOP:

      return("SELLSTOP"); 

   case OP_BUYLIMIT:

      return("BUYLIMIT"); 

   case OP_SELLLIMIT:

      return("SELLLIMIT"); 

   }

   return("error"); 

}

//------------------------------------------------------------------

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