cm Trailing 2SL

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 2SL
ÿþ//+------------------------------------------------------------------+

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

//|                                                cmillion@narod.ru |

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

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

#property link        "cmillion@narod.ru"

#property strict

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

#property description "@8 >B:@KB88 @K=>G=>3> >@45@0 02B><0B8G5A:8 2KAB02;O5BAO C@>25=L stoploss 2 A>>B25BAB288 A> 7=0G5=85< SL1 8 takeprofit 2 A>>B25BAB288 A> 7=0G5=85< TP."

#property description ";O Buy >@45@>2:"

#property description "@8 C25;8G5=88 F5=K, stoploss ?5@5CAB0=02;8205BAO 2 A>>B25AB288 A> 7=0G5=85< SL1, ?>:0 =5 1C45B 4>AB83=CB> 7040==>5 7=0G5=85 ?@81K;8 Pr (2 ?C=:B0E). "

#property description "@8 4>AB865=88 7040==>3> 7=0G5=8O ?@81K;8 Pr - stoploss 40;55 ?5@5CAB0=02;8205BAO 2 A>>B25AB288 A> 7=0G5=85< SL2"

#property description "@8 ?@8 C<5=LH5=88 F5=K stoploss =5 87<5=O5BAO."

#property description ";O Sell 75@:0;L=>"

 //3.1 @8 C<5=LH5=88 F5=K, stoploss ?5@5CAB0=02;8205BAO 2 A>>B25AB288 A> 7=0G5=85< SL1, ?>:0 =5 1C45B 4>AB83=CB> 7040==>5 7=0G5=85 ?@81K;8 Pr (2 ?C=:B0E).

 //3.2 @8 4>AB865=88 7040==>3> 7=0G5=8O ?@81K;8 Pr - stoploss 40;55 ?5@5CAB0=02;8205BAO 2 A>>B25AB288 A> 7=0G5=85< SL2

 //3.3 @8 C25;8G5=88 F5=K stoploss =5 87<5=O5BAO

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

extern int _TP  = 100; //C@>25=L 2KAB02;5=8O TP, 5A;8 0, B> TP =5 2KAB02;O5BAO

extern int _SL1 = 10; //C@>25=L 2KAB02;5=8O SL1, 5A;8 0, B> SL1 =5 2KAB02;O5BAO

extern int _SL2 = 50; //C@>25=L 2KAB02;5=8O SL2, 5A;8 0, B> SL2 =5 2KAB02;O5BAO

extern int _PR  = 20; //?@>D8B ?@8 :>B>@>< 2KAB02;O5< SL2

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

void OnTick()

{

   double STOPLEVEL=MarketInfo(Symbol(),MODE_STOPLEVEL);

   double stoploss,OSL,OTP,OOP,SL=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 (_TP!=0)

               {

                  if (NormalizeDouble(OOP + _TP * Point,Digits)>OOP+STOPLEVEL*Point) 

                     TP = NormalizeDouble(OOP + _TP * Point,Digits);

               } 

               else TP=0;

               if (_SL1!=0 || _SL2!=0)

               {

                  if ((Bid-OOP)/Point>_PR) stoploss = _SL2;

                  else stoploss = _SL1;

                  

                  if (stoploss>0)

                  {

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

                     if (sl > OSL) SL = sl;

                  }

                  if ((SL - Ask)/Point<STOPLEVEL) SL = OSL;

               }

               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 (_TP!=0)

               {

                  if (NormalizeDouble(OOP - _TP * Point,Digits)<OOP-STOPLEVEL*Point) 

                     TP = NormalizeDouble(OOP - _TP * Point,Digits);

               }

               else TP=0;

               if (_SL1!=0 && _SL2!=0)

               {

                  if ((OOP-Ask)/Point>_PR) stoploss = _SL2;

                  else stoploss = _SL1;

                  

                  if (stoploss>0)

                  {

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

                     if (sl < OSL || OSL==0) SL = sl;

                  }

                  if ((Bid - SL)/Point<STOPLEVEL) SL = OSL;

               }

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

               {  

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

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

               }

            } 

         }

      }

   } 

   if (IsTesting())

   {

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

      if (s==0 && CheckMoneyForTrade(_Symbol,MarketInfo(Symbol(),MODE_MINLOT),-1)) SendOrder(OP_SELL, MarketInfo(Symbol(),MODE_MINLOT), NormalizeDouble(Bid,Digits)); 

   }

}

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

bool CheckMoneyForTrade(string symb,double lots, int OT)

  {

   ENUM_ORDER_TYPE type;

   if (OT==1) type=ORDER_TYPE_BUY;

   else type=ORDER_TYPE_SELL;

   double free_margin=AccountFreeMarginCheck(symb,type,lots);

   if(free_margin<0)

     {

      string oper=(type==OP_BUY)? "Buy":"Sell";

      Print("Not enough money for ", oper," ",lots, " ", symb, " Error code=",GetLastError());

      return(false);

     }

   return(true);

  }

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

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