cm ea open 2 stopopders

Profit factor:
0.00
1 Views
1 Downloads
0 Favorites
cm ea open 2 stopopders
ÿþ//+-------------------------------6----------------------------------+

//|                                                      ProjectName |

//|                                      Copyright 2012, CompanyName |

//|                                       http://www.companyname.net |

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

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

#property link      "cmillion@narod.ru   WhatsApp +79283690333"

#property version   "1.00"

#property strict

#property description "!>25B=8: 2KAB02;O5B ?@>B82>?>;>6=K5 AB>? >@45@0 2 C:070==>5 2@5<O"

#property description ">A;5 A@010BK20=8O 2:;NG05BAO B@59;8=3AB>?"

#property description ">A;5 ?5@52>40 2 157C1KB>: C40;O5< ?@>B82>?>;>6=K9 >@45@"

/*--------------------------------------------------------------------

 >?@545;5==>5 2@5<O (7040QBAO 2 ?0@0<5B@0E) 2KAB02;ONBAO 420 >B;>65==KE >@45@0 109AB>? 8 A5;AB>? =0 @0AAB>O=88 2 ?C=:B0E (70405BAO 2 

?0@0<5B@0E level) >B B5:CI59 F5=K, A TP 8 SL 2 ?C=:B0E (70405BAO 2 ?0@0<5B@0E). 

0;55 A:@8?B B@0;8B AB>?;>AA ?@81K;L=>9 A45;:8. >A;5 A@010BK20=8O >4=>3> 87 >@45@>2 8 ?5@52>40 53> 2 157C1KB>: B@0;><, ?@>B82>?>;>6=K9 >@45@ C40;O5BAO.

//--------------------------------------------------------------------*/

sinput datetime TimeSet=D'2017.02.04 14:00'; //@5<O 2KAB02;5=8O >@45@>2, 5A;8 B5:CI55 2@5<O 1>;LH5 CAB0=>2;5==>3>, B> 2KAB02;ONBAO A@07C

extern int      level          = 10;       //@0AAB>O=85 >B B5:CI59 F5=K 4> >@45@>2

sinput int      Magic          = 0;        //C=8:0;L=K9 =><5@ >@45@0

extern double   Lot            = 0.1;      //>1J5< >@45@>2

extern int      Stoploss       = 500;      //AB>?;>AA (0->B:;NG5=)

extern int      Takeprofit     = 50;       //B59:?@>D8B (0->B:;NG5=)

extern int      TrailingStop   = 10;       //B@59;8=3AB>?, 5A;8 0, B> =5B B@59;8=30

extern int      TrailingStart  = 1;        //AB0@B B@59;8=30

extern int      TrailingStep   = 1;        //H03 B@0;0

extern bool     deleteapp      = true;     //>A;5 ?5@52>40 2 157C1KB>: C40;O5< ?@>B82>?>;>6=K9 >@45@

int slippage=3;

double STOPLEVEL;

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

int OnInit()

  {

   if(Digits==3 || Digits==5)

     {

      level*=10;

      Stoploss*=10;

      Takeprofit*=10;

      TrailingStop*=10;

      TrailingStart*=10;

      TrailingStep*=10;

     }

   return(INIT_SUCCEEDED);

  }

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

void OnTick()

  {

   STOPLEVEL=MarketInfo(Symbol(),MODE_STOPLEVEL);

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

   int i,b=0,s=0,tip,TicketB=0,TicketS=0;

   bool tralB=false;

   bool tralS=false;

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

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

        {

         if(OrderSymbol()==Symbol() && Magic==OrderMagicNumber())

           {

            tip = OrderType();

            OSL = NormalizeDouble(OrderStopLoss(),Digits);

            OTP = NormalizeDouble(OrderTakeProfit(),Digits);

            OOP = NormalizeDouble(OrderOpenPrice(),Digits);

            SL=OSL;TP=OTP;

            if(tip==OP_BUY)

              {

               b++;

               if(Stoploss!=0   && Bid<=OOP - Stoploss   * Point) {if(OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),slippage,clrNONE)) continue;}

               if(Takeprofit!=0 && Bid>=OOP + Takeprofit * Point) {if(OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),slippage,clrNONE)) continue;}

               if(OSL==0 && Stoploss>=STOPLEVEL && Stoploss!=0)

                 {

                  SL=NormalizeDouble(OOP-Stoploss  *Point,Digits);

                 }

               if(OTP==0 && Takeprofit>=STOPLEVEL && Takeprofit!=0)

                 {

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

                 }

               if(TrailingStop>=STOPLEVEL && TrailingStop!=0 && (Bid-OOP)/Point>=TrailingStart)

                 {

                  StLo=NormalizeDouble(Bid-TrailingStop*Point,Digits);

                  if(StLo>=OOP && StLo>OSL+TrailingStep*Point) SL=StLo;

                 }

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

                 {

                  if(!OrderModify(OrderTicket(),OOP,SL,TP,0,clrNONE)) Print("Error OrderModify <<",Error(GetLastError()),">> ");

                  else tralB=true;

                 }

              }

            if(tip==OP_SELL)

              {

               s++;

               if(Stoploss!=0   && Ask>=OOP + Stoploss   * Point) {if(OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),slippage,clrNONE)) continue;}

               if(Takeprofit!=0 && Ask<=OOP - Takeprofit * Point) {if(OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),slippage,clrNONE)) continue;}

               if(OSL==0 && Stoploss>=STOPLEVEL && Stoploss!=0)

                 {

                  SL=NormalizeDouble(OOP+Stoploss  *Point,Digits);

                 }

               if(OTP==0 && Takeprofit>=STOPLEVEL && Takeprofit!=0)

                 {

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

                 }

               if(TrailingStop>=STOPLEVEL && TrailingStop!=0 && (OOP-Ask)/Point>=TrailingStart)

                 {

                  StLo=NormalizeDouble(Ask+TrailingStop*Point,Digits);

                  if(StLo<=OOP && (StLo<OSL-TrailingStep*Point || OSL==0)) SL=StLo;

                 }

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

                 {

                  if(!OrderModify(OrderTicket(),OOP,SL,TP,0,clrNONE)) Print("Error OrderModify <<",Error(GetLastError()),">> ");

                  else tralS=true;

                 }

              }

            if(tip==OP_BUYSTOP)  TicketB=OrderTicket();

            if(tip==OP_SELLSTOP) TicketS=OrderTicket();

           }

        }

     }



   if(deleteapp)

     {

      if(TicketB>0 && tralS) if(!OrderDelete(TicketB)) Print("Error OrderDelete <<",Error(GetLastError()),">> ");

      if(TicketS>0 && tralB) if(!OrderDelete(TicketS)) Print("Error OrderDelete <<",Error(GetLastError()),">> ");

      if(TicketB>0 && TicketS+s==0) if(!OrderDelete(TicketB)) Print("Error OrderDelete <<",Error(GetLastError()),">> ");

      if(TicketS>0 && TicketB+b==0) if(!OrderDelete(TicketS)) Print("Error OrderDelete <<",Error(GetLastError()),">> ");

     }



//---



   if(b+s+TicketB+TicketS>0)

     {

      Comment(TimeToStr(TimeCurrent(),TIME_SECONDS),"\n",

              TicketB==0?"":StringConcatenate("Buy Stop ",TicketB,"\n"),

              TicketS==0?"":StringConcatenate("Sell Stop ",TicketS,"\n"),

              !tralB==0?"":"Trailing Buy\n",

              !tralS==0?"":"Trailing Sell\n");

      return;

     }



//---



   if(TimeCurrent()<TimeSet)

     {

      Comment(TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS),"\n!>25B=8: >B:@K205B ?@>B82>?>;>6=K5 AB>? >@45@0 2 ",

              TimeToStr(TimeSet,TIME_DATE|TIME_SECONDS),"\n",WindowExpertName(),"\n",

              "> 2KAB02;5=8O ?>78F89 >AB0;>AL ",TimeToStr(TimeSet-TimeCurrent(),TIME_SECONDS));

      return;

     }



//---



   string txt=NULL;

   RefreshRates();

   STOPLEVEL=MarketInfo(Symbol(),MODE_STOPLEVEL);

   if(level<STOPLEVEL) level=(int)STOPLEVEL;

   double Price=NormalizeDouble(Ask+level*Point,Digits);

   if(Takeprofit!=0)

     {

      if(Takeprofit<STOPLEVEL) TP=NormalizeDouble(Price+STOPLEVEL*Point,Digits);

      else  TP=NormalizeDouble(Price+Takeprofit*Point,Digits);

     }

   else TP=0;

   if(Stoploss!=0)

     {

      if(Stoploss<STOPLEVEL) SL=NormalizeDouble(Price-STOPLEVEL*Point,Digits);

      else  SL=NormalizeDouble(Price-Stoploss*Point,Digits);

     }

   else SL=0;

   if(SendOrder(OP_BUYSTOP,Lot,Price,SL,TP))

      txt=StringConcatenate(txt,"@45@ BUYSTOP CA?5H=> 2KAB02;5= ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS),"\n");



   Price=NormalizeDouble(Bid-level*Point,Digits);

   if(Takeprofit!=0)

     {

      if(Takeprofit<STOPLEVEL) TP=NormalizeDouble(Price-STOPLEVEL*Point,Digits);

      else  TP=NormalizeDouble(Price-Takeprofit*Point,Digits);

     }

   else TP=0;

   if(Stoploss!=0)

     {

      if(Stoploss<STOPLEVEL) SL=NormalizeDouble(Price+STOPLEVEL*Point,Digits);

      else  SL=NormalizeDouble(Price+Stoploss*Point,Digits);

     }

   else SL=0;

   if(SendOrder(OP_SELLSTOP,Lot,Price,SL,TP))

      txt=StringConcatenate(txt,"@45@ SELLSTOP CA?5H=> 2KAB02;5= ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS),"\n");

   Comment(txt);

  }

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

string Strtip(int tip)

  {

   switch(tip)

     {

      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");

  }

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

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

  {

   if(tip>1)

     {

      if(tip==OP_BUYSTOP)

        {

         if(AccountFreeMarginCheck(Symbol(),OP_BUY,lots)<=0 || GetLastError()==134)

           {

            return(false);

           }

        }

      if(tip==OP_SELLSTOP)

        {

         if(AccountFreeMarginCheck(Symbol(),OP_SELL,lots)<=0 || GetLastError()==134)

           {

            return(false);

           }

        }

     }

   else

     {

      if(AccountFreeMarginCheck(Symbol(),tip,lots)<=0 || GetLastError()==134)

        {

         Alert("54>AB0B>G=> A@54AB2");

         return(false);

        }

     }

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

     {

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

      Alert(" ?>?KB:0 ",i," H81:0 >B:@KB8O >@45@0 ",Strtip(tip)," <<",Error(GetLastError()),">>  lot=",lots,"  pr=",price," sl=",sl," tp=",tp);

      Sleep(500);

      RefreshRates();

      if(IsStopped()) return(false);

     }

   return(false);

  }

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

string Error(int code)

  {

   switch(code)

     {

      case 0:   return("5B >H81>:");

      case 1:   return("5B >H81:8, => @57C;LB0B =58725AB5=");

      case 2:   return("1I0O >H81:0");

      case 3:   return("5?@028;L=K5 ?0@0<5B@K");

      case 4:   return("">@3>2K9 A5@25@ 70=OB");

      case 5:   return("!B0@0O 25@A8O :;85=BA:>3> B5@<8=0;0");

      case 6:   return("5B A2O78 A B>@3>2K< A5@25@><");

      case 7:   return("54>AB0B>G=> ?@02");

      case 8:   return("!;8H:>< G0ABK5 70?@>AK");

      case 9:   return("54>?CAB8<0O >?5@0F8O =0@CH0NI0O DC=:F8>=8@>20=85 A5@25@0");

      case 64:  return("!G5B 701;>:8@>20=");

      case 65:  return("5?@028;L=K9 =><5@ AG5B0");

      case 128: return("AB5: A@>: >6840=8O A>25@H5=8O A45;:8");

      case 129: return("5?@028;L=0O F5=0");

      case 130: return("5?@028;L=K5 AB>?K");

      case 131: return("5?@028;L=K9 >1J5<");

      case 132: return(" K=>: 70:@KB");

      case 133: return("">@3>2;O 70?@5I5=0");

      case 134: return("54>AB0B>G=> 45=53 4;O A>25@H5=8O >?5@0F88");

      case 135: return("&5=0 87<5=8;0AL");

      case 136: return("5B F5=");

      case 137: return("@>:5@ 70=OB");

      case 138: return(">2K5 F5=K");

      case 139: return("@45@ 701;>:8@>20= 8 C65 >1@010BK205BAO");

      case 140: return(" 07@5H5=0 B>;L:> ?>:C?:0");

      case 141: return("!;8H:>< <=>3> 70?@>A>2");

      case 145: return(">48D8:0F8O 70?@5I5=0, B0: :0: >@45@ A;8H:>< 1;87>: : @K=:C");

      case 146: return(">4A8AB5<0 B>@3>2;8 70=OB0");

      case 147: return("A?>;L7>20=85 40BK 8AB5G5=8O >@45@0 70?@5I5=> 1@>:5@><");

      case 148: return(">;8G5AB2> >B:@KBKE 8 >B;>65==KE >@45@>2 4>AB83;> ?@545;0, CAB0=>2;5==>3> 1@>:5@><.");

      case 4000: return("5B >H81:8");

      case 4001: return("5?@028;L=K9 C:070B5;L DC=:F88");

      case 4002: return("=45:A <0AA820 - 2=5 480?07>=0");

      case 4003: return("5B ?0<OB8 4;O AB5:0 DC=:F89");

      case 4004: return("5@5?>;=5=85 AB5:0 ?>A;5 @5:C@A82=>3> 2K7>20");

      case 4005: return("0 AB5:5 =5B ?0<OB8 4;O ?5@540G8 ?0@0<5B@>2");

      case 4006: return("5B ?0<OB8 4;O AB@>:>2>3> ?0@0<5B@0");

      case 4007: return("5B ?0<OB8 4;O 2@5<5==>9 AB@>:8");

      case 4008: return("58=8F80;878@>20==0O AB@>:0");

      case 4009: return("58=8F80;878@>20==0O AB@>:0 2 <0AA825");

      case 4010: return("5B ?0<OB8 4;O AB@>:>2>3> <0AA820");

      case 4011: return("!;8H:>< 4;8==0O AB@>:0");

      case 4012: return("AB0B>: >B 45;5=8O =0 =>;L");

      case 4013: return("5;5=85 =0 =>;L");

      case 4014: return("58725AB=0O :><0=40");

      case 4015: return("5?@028;L=K9 ?5@5E>4");

      case 4016: return("58=8F80;878@>20==K9 <0AA82");

      case 4017: return("K7>2K DLL =5 @07@5H5=K");

      case 4018: return("52>7<>6=> 703@C78BL 181;8>B5:C");

      case 4019: return("52>7<>6=> 2K720BL DC=:F8N");

      case 4020: return("K7>2K 2=5H=8E 181;8>B5G=KE DC=:F89 =5 @07@5H5=K");

      case 4021: return("54>AB0B>G=> ?0<OB8 4;O AB@>:8, 2>72@0I05<>9 87 DC=:F88");

      case 4022: return("!8AB5<0 70=OB0");

      case 4050: return("5?@028;L=>5 :>;8G5AB2> ?0@0<5B@>2 DC=:F88");

      case 4051: return("54>?CAB8<>5 7=0G5=85 ?0@0<5B@0 DC=:F88");

      case 4052: return("=CB@5==OO >H81:0 AB@>:>2>9 DC=:F88");

      case 4053: return("H81:0 <0AA820");

      case 4054: return("5?@028;L=>5 8A?>;L7>20=85 <0AA820-B09<A5@88");

      case 4055: return("H81:0 ?>;L7>20B5;LA:>3> 8=48:0B>@0");

      case 4056: return("0AA82K =5A>2<5AB8<K");

      case 4057: return("H81:0 >1@01>B:8 3;>10;L=K5E ?5@5<5==KE");

      case 4058: return(";>10;L=0O ?5@5<5==0O =5 >1=0@C65=0");

      case 4059: return("$C=:F8O =5 @07@5H5=0 2 B5AB>2>< @568<5");

      case 4060: return("$C=:F8O =5 @07@5H5=0");

      case 4061: return("H81:0 >B?@02:8 ?>GBK");

      case 4062: return("68405BAO ?0@0<5B@ B8?0 string");

      case 4063: return("68405BAO ?0@0<5B@ B8?0 integer");

      case 4064: return("68405BAO ?0@0<5B@ B8?0 double");

      case 4065: return(" :0G5AB25 ?0@0<5B@0 >68405BAO <0AA82");

      case 4066: return("0?@>H5==K5 8AB>@8G5A:85 40==K5 2 A>AB>O=88 >1=>2;5=8O");

      case 4067: return("H81:0 ?@8 2K?>;=5=88 B>@3>2>9 >?5@0F88");

      case 4099: return(">=5F D09;0");

      case 4100: return("H81:0 ?@8 @01>B5 A D09;><");

      case 4101: return("5?@028;L=>5 8<O D09;0");

      case 4102: return("!;8H:>< <=>3> >B:@KBKE D09;>2");

      case 4103: return("52>7<>6=> >B:@KBL D09;");

      case 4104: return("5A>2<5AB8<K9 @568< 4>ABC?0 : D09;C");

      case 4105: return("8 >48= >@45@ =5 2K1@0=");

      case 4106: return("58725AB=K9 A8<2>;");

      case 4107: return("5?@028;L=K9 ?0@0<5B@ F5=K 4;O B>@3>2>9 DC=:F88");

      case 4108: return("525@=K9 =><5@ B8:5B0");

      case 4109: return("">@3>2;O =5 @07@5H5=0. 5>1E>48<> 2:;NG8BL >?F8N  07@5H8BL A>25B=8:C B>@3>20BL 2 A2>9AB20E M:A?5@B0.");

      case 4110: return(";8==K5 ?>78F88 =5 @07@5H5=K. 5>1E>48<> ?@>25@8BL A2>9AB20 M:A?5@B0.");

      case 4111: return(">@>B:85 ?>78F88 =5 @07@5H5=K. 5>1E>48<> ?@>25@8BL A2>9AB20 M:A?5@B0.");

      case 4200: return("1J5:B C65 ACI5AB2C5B");

      case 4201: return("0?@>H5=> =58725AB=>5 A2>9AB2> >1J5:B0");

      case 4202: return("1J5:B =5 ACI5AB2C5B");

      case 4203: return("58725AB=K9 B8? >1J5:B0");

      case 4204: return("5B 8<5=8 >1J5:B0");

      case 4205: return("H81:0 :>>@48=0B >1J5:B0");

      case 4206: return("5 =0945=> C:070==>5 ?>4>:=>");

      default:   return("H81:0 =58725AB=0 ");

     }

  }

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



void OnDeinit(const int reason)

  {

   Comment("");

  }

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

Comments