Spidery_Turn

Author: Copyright © 2010, Andres
Orders Execution
Checks for the total of open ordersChecks for the total of closed ordersIt Closes Orders by itself It automatically opens orders when conditions are reached
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Spidery_Turn
ÿþ//+------------------------------------------------------------------+

//|                                                 Spidery_Turn.mq4 |

//|                                         Copyright © 2010, Andres |

//|                                                  http://mql5.su/ |

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

#property copyright "Copyright © 2010, Andres"

#property link      "http://mql5.su/"

#property version   "1.0"



//------- =5H=85 ?0@0<5B@K A>25B=8:0 --------------------------------

extern double Lots                = 0.1;         //  07<5@ B>@3C5<>3> ;>B0

extern int    SLandTP             = 12;          //  07<5@ SL 8 TP

extern int    MinMultTPnextCycle  = 6;           //  07<5@ <8=. <=>68B5;O TP 4;O A;. F8:;0

extern int    OpenHour_1          = 8;           // '0A >B:@KB8O >@45@0 1

extern int    OpenMinute_1        = 0;           // 8=CBK >B:@KB8O >@45@0 1

extern int    OpenHour_2          = 16;          // '0A >B:@KB8O >@45@0 2

extern int    OpenMinute_2        = 30;          // 8=CBK >B:@KB8O >@45@0 2

 bool   InvisibleOrders = true;            // A?>;L7>20BL =52848<K5 >@45@0

extern int    Slippage            = 3;           // @>A:0;L7K20=85





//---- ;>10;L=K5 ?5@5<5==K5 A>25B=8:0 -------------------------------

int StopLoss;                                    // =0G5=85 SL

int TakeProfit;                                  // =0G5=85 TP

int dig_ret = 1;                                 // =>68B5;L 4;O 5-B8 7=0:0

string name = "Spidery Turn ";                   // 0720=85 A>25B=8:0

int magic;                                       // 038: =><5@ >@45@>2

string comment;                                  // ><<5=B0@89 : >@45@C

string opentime_1, opentime_2;                   // @5<O 2 D>@<0B5 B5:AB0

bool work=true;                                  // -:A?5@B 1C45B @01>B0BL

int Tip[2];                                      // 0AA82 B8?>2 >B:@K205<KE ?>7.

int distTP;                                      // 0:A8<0;L=0O 48AB. 4> "

int nocyc;                                       // !G5BG8: ?CABKE F8:;>2

int numcyc;                                      // 5@5<. ?>7. A =081. @0AAB. 4> "





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

//| expert initialization function                                   |

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

int init() {

  if (Digits==5 || Digits==3) dig_ret=10;        // @>25@:0 1@>:5@0 =0 5-B8 7=0:

  if (IsTesting()) name = "Test "+name;          // ;O B5AB8@>20=8O 87<5=O5< 8<O

  StopLoss = SLandTP;                            // @8A208205< 7=0G. SL

  TakeProfit = SLandTP;                          // @8A208205< 7=0G. TP

  comment = name + Symbol() + Period();          // 5=5@0F8O :><<5=B0@8O : >@45@C

  magic = makeMagicNumber(comment);              // 5=5@0F8O <038: >@45@0

  opentime_1 = TimeToText(OpenHour_1, OpenMinute_1);       // 5@52>4 2@5<5=8 2 B5:AB

  opentime_2 = TimeToText(OpenHour_2, OpenMinute_2);       // 5@52>4 2@5<5=8 2 B5:AB

  if (GlobalVariableGet(comment+" CY.#") > 2)    // A;8 5ABL ?>78F88

    ArrayResize(Tip, GlobalVariableGet(comment+" CY.#"));  // :BC0;878@C5< @07<5@ <0AA820

  return(0);

}

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

//| expert deinitialization function                                 |

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

int deinit() {

  Comment("");

  DelCommentForChart();

  if (IsTesting()) {

    for (int i=0; i<100-1; i++) {

      GlobalVariableDel(comment+" TM."+i);

      GlobalVariableDel(comment+" TP."+i);

    }

    GlobalVariableDel(comment+" CY.#");

  }

  return(0);

}

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

//| expert start function                                            |

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

int start() {

  int reduction_tp;                              // 5;8G8=0 C<5=LH5=8O "

  int newnum;                                    // ><5@ =>2>3> F8:;0

  int newsize;                                   // >2K9 @07<5@ <0AA820

  nocyc = 0;                                     // 1=C;O5< AG5BG8: ?CABKE F8:;>2

  if (work==false) {                             // @8B8G5A:0O >H81:0

      Alert("-:A?5@B ", comment, "=5 @01>B05B");

      return;                                    // KE>4 87 start()

  }

  for (int i=0; i<ArraySize(Tip)-1; i++) {

    if (ExistPosition(i)) {                      // @>25@O5< =0;8G85 >B:@. ?>7.

      if (PurposeStopLoss(StopLoss*dig_ret, i)) {// A;8 4>H;0 4> !

        if (ClosePosition(true, i)) {      // 0:@K205< ?>78F8N

          if (OpenPosition(Tip[i], i))           // B:@K205< 2AB@5G=CN ?>78F8N

            GlobalVariableSet(comment+" TP."+i, (GlobalVariableGet(comment+" TP."+i)-PurposeProfitLastOrder(i)));

          break;

        }

      }

      if (PurposeTakeProfit(GlobalVariableGet(comment+" TP."+i), i)) // A;8 4>H;0 4> "

        if (ClosePosition(false, i)) {    // 0:@K205< ?>78F8N

          numcyc=DefinitionMaxDistanceTP();      // 0E>48< ?>7. A =081. @0AAB. 4> "

          reduction_tp=TotalOrdersProfit(i);     // KG8A;O5< 25;. C<5=LH. "

          if (reduction_tp <= Slippage*dig_ret)

            reduction_tp=TakeProfit*dig_ret;     // A;8 >=0 ?>;>6. B> ?@8A208205< >B@.

          GlobalVariableSet(comment+" TP."+numcyc,

            (GlobalVariableGet(comment+" TP."+numcyc)-reduction_tp));

          GlobalVariableDel(comment+" TP."+i);   // KG8A;O5< C<5=LH5=85 " 8 C40;O5< B5:. ?5@5<.

          if (i!=0 && i==ArraySize(Tip)-2) {     // A;8 F8:; ?>A;54=89 8 =5 548=AB25==K9

            while(ArraySize(Tip)!=i+1) ArrayResize(Tip, i+1);        // #<5=LH05< @07<5@ <0AA820 Tip =0 1

            GlobalVariableSet(comment+" CY.#", i+1);

          }

          break;

        }

      numcyc=DefinitionMaxDistanceTP();          // 1=>2;O5< ?5@5<5==CN distTP

      if (GlobalVariableGet(comment+" TP."+i) >= MinMultTPnextCycle*TakeProfit*dig_ret ||

        distTP >= (MinMultTPnextCycle-1)*TakeProfit*dig_ret)         // 8;8 48AB. 4> <0:A. " 25;8:0

        if (i==CriterionOrNumber(TakeProfit*dig_ret, Slippage*dig_ret)) {      // A;8 5ABL :@8B. >B:@. 2AB@5G=>9 ?>78F88

          newnum=ArraySize(Tip)-1;               // 0?><8=05< =><5@ =>2>3> F8:;0

          Print("i=", i, " newnum=", newnum);

          if (OpenPosition(Tip[newnum], newnum)) {         // B:@K205< ?>7. =>2>3> F8:;0

            GlobalVariableSet(comment+" TP."+newnum, TakeProfit*dig_ret);

            GlobalVariableSet(comment+" TM."+newnum, TimeCurrent()); // !B028< B5:CI55 2@5<O ?5@5<5==>9

            newsize=ArraySize(Tip)+1;

            while(ArraySize(Tip)!=newsize) ArrayResize(Tip, newsize);          // #25;8G8205< @07<5@ <0AA820 Tip =0 1

            GlobalVariableSet(comment+" CY.#", ArraySize(Tip));

          }

        }

    }

    else nocyc+=1;

  }

  CommentForChart();

  if (ArraySize(Tip) == nocyc+1) {                         // A;8 2A5 F8:;K ?CABK5...

    for (int j=0; j<ArraySize(Tip)-1; j++)

      GlobalVariableSet(comment+" TM."+j, TimeCurrent());  // !B028< B5:CI55 2@5<O 2A5E ?5@5<5==KE

    while(ArraySize(Tip)!=2) ArrayResize(Tip, 2);          // >72@0I05< @07<5@ <0AA820 Tip @02=K< 2

    GlobalVariableSet(comment+" CY.#", 2);

    DelCommentForChart();

  }

  if (TimeToStr(TimeCurrent(), TIME_MINUTES)==opentime_1 && ArraySize(Tip)==2)

    if (OpenPosition(PositionDirection(), 0)) {

      GlobalVariableSet(comment+" TP."+0, TakeProfit*dig_ret);       // @8A208205< 3;. ?5@. 70G5=85 "

      GlobalVariableSet(comment+" TM."+0, TimeCurrent());  // !B028< B5:CI55 2@5<O ?5@5<5==>9

    }

  if (TimeToStr(TimeCurrent(), TIME_MINUTES)==opentime_2 && ArraySize(Tip)==2)

    if (OpenPosition(PositionDirection(), 0)) {

      GlobalVariableSet(comment+" TP."+0, TakeProfit*dig_ret);       // @8A208205< 3;. ?5@. 70G5=85 "

      GlobalVariableSet(comment+" TM."+0, TimeCurrent());  // !B028< B5:CI55 2@5<O ?5@5<5==>9

    }



   



  return(0);

}

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

//| @>25@:0 A@01>B:8 "59: @>D8B0                                   |

//| 0@0<5B@K:                                                       |

//|   TP   - Take Profit >@45@0                                      |

//|   cyc  - =><5@ F8:;0 ?>78F89                                     |

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

bool PurposeTakeProfit(double TP, int cyc) {

  double tp;

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

    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

      if (OrderSymbol()==Symbol() && OrderMagicNumber()==magic+cyc) {

        if (OrderType()==OP_BUY) {

          tp = OrderOpenPrice()+TP*Point;

          if (Bid >= tp) return(true);

        }

        if (OrderType()==OP_SELL) {

          tp = OrderOpenPrice()-TP*Point;

          if (Ask <= tp) return(true);

        }

      }

    }

  }

  return(false);

}  

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

//| @>25@:0 A@01>B:8 !B>? >AA0                                     |

//| 0@0<5B@K:                                                       |

//|   SL   - Stop Loss >@45@0                                        |

//|   cyc  - =><5@ F8:;0 ?>78F89                                     |

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

bool PurposeStopLoss(int SL, int cyc) {

  double sl;

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

    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

      if (OrderSymbol()==Symbol() && OrderMagicNumber()==magic+cyc) {

        if (OrderType()==OP_BUY) {

          sl = OrderOpenPrice()-SL*Point;

          if (Bid <= sl) return(true);

        }

        if (OrderType()==OP_SELL) {

          sl = OrderOpenPrice()+SL*Point;

          if (Ask >= sl) return(true);

        }

      }

    }

  }

  return(false);

}  

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

//| @>25@:0 @81K;8 ?>A;54=53> 70:@KB>3> >@45@0                     |

//| 0@0<5B@K:                                                       |

//|   cyc  - =><5@ F8:;0 ?>78F89                                     |

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

int PurposeProfitLastOrder(int cyc) {

  datetime tto;

  int tpo;

  for (int i=0; i<OrdersHistoryTotal(); i++) {

    if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) {

      Print ("H81:0 ?@8 4>ABC?5 : 8AB>@8G5A:>9 1075 #",GetLastError());

      return(-StopLoss*dig_ret);

    }

    if (OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic+cyc)

      continue;

    if (OrderSymbol()==Symbol() && OrderMagicNumber()==magic+cyc) {

      if (OrderCloseTime()>GlobalVariableGet(comment+" TM."+cyc)) {

        if (OrderCloseTime()>tto) {

          tto=OrderCloseTime();

          if (OrderType()==OP_BUY) tpo=NormalizeDouble((OrderClosePrice()-OrderOpenPrice())/Point,0);

          if (OrderType()==OP_SELL) tpo=NormalizeDouble((OrderOpenPrice()-OrderClosePrice())/Point,0);

        }

      }

    }

  }

  return(tpo);           

}

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

//| @>25@:0 @81K;8 2A5E >@45@>2 F8:;0                              |

//| 0@0<5B@K:                                                       |

//|   cyc  - =><5@ F8:;0 ?>78F89                                     |

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

int TotalOrdersProfit(int cyc) {

  int tpo, ttpo;

  for (int i=0; i<OrdersHistoryTotal(); i++) {

    if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) {

      Print ("H81:0 ?@8 4>ABC?5 : 8AB>@8G5A:>9 1075 #",GetLastError());

      return(TakeProfit*dig_ret);

    }

    if (OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic+cyc)

      continue;

    if (OrderSymbol()==Symbol() && OrderMagicNumber()==magic+cyc) {

      if (OrderCloseTime()>GlobalVariableGet(comment+" TM."+cyc)) {

        if (OrderType()==OP_BUY) {

          tpo=NormalizeDouble((OrderClosePrice()-OrderOpenPrice())/Point,0);

          ttpo=ttpo+tpo;

        }

        if (OrderType()==OP_SELL) {

          tpo=NormalizeDouble((OrderOpenPrice()-OrderClosePrice())/Point,0);

          ttpo=ttpo+tpo;

        }

      }

    }

  }

  return(ttpo);           

}

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

//| ?@545;5=85 F8:;0 A =081>;LH8< @0AAB>O=85< 4> "                 |

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

int DefinitionMaxDistanceTP() {

  double dtp;

  double tp;

  int number;

  for (int j=0; j<ArraySize(Tip)-1; j++) {

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

      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

        if (OrderSymbol()==Symbol() && OrderMagicNumber()==magic+j) {

          if (OrderType()==OP_BUY) {

            tp = OrderOpenPrice()+GlobalVariableGet(comment+" TP."+j)*Point;   // ?@545;O5< " ?>78F88

            if (dtp < tp-Bid) {                  // @>25@O5< @0AAB>O=85 4> " ?>7.

              dtp = tp - Bid;                    // 0?><8=05< =>2>5 @0AAB. 4> " ?>7.

              number = j;

            }

          }

          if (OrderType()==OP_SELL) {

            tp = OrderOpenPrice()-GlobalVariableGet(comment+" TP."+j)*Point;   // ?@545;O5< " ?>78F88

            if (dtp < Ask-tp) {                  // @>25@O5< @0AAB>O=85 4> " ?>7.

              dtp = Ask - tp;                    // 0?><8=05< =>2>5 @0AAB. 4> " ?>7.

              number = j;

            }

          }

        }

      }

    }

  }

  distTP = NormalizeDouble(dtp/Point,0);         // 0?><8=05< <0:A. 48AB 4> "

  return(number);

}

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

//| ?@54. :@8B5@8O =5>B:@KB8O 2AB@5G=>9 ?>78F88 8;8 =><5@0 F8:;0    |

//| 0@0<5B@K:                                                       |

//|   tp    - 8AB0=F8O 4;O >B:@KB8O 2AB@5G=>9 ?>78F88               |

//|   slp   - 5;LB0 4;O >B:@KB8O 2AB@5G=>9 ?>78F88                  |

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

int CriterionOrNumber(int tp, int slp) {

  double max, min;

  datetime tm;

  int number;

  for (int j=0; j<ArraySize(Tip)-1; j++) {

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

      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

        if (OrderSymbol()==Symbol() && OrderMagicNumber()==magic+j) {

          if (OrderType()==OP_BUY) {

            if (OrderOpenTime()>tm) {

              tm=OrderOpenTime();                // I5< ?>A;54=NN >B:@KBCN ?>7...

              number = j;                        // ...8 70?><8=05< 5Q F8:;

            }

            if (OrderOpenPrice()>max) max=OrderOpenPrice();          // I5< 25@E=89 >@45@ buy

          }

          if (OrderType()==OP_SELL) {

            if (OrderOpenTime()>tm) {

              tm=OrderOpenTime();                // I5< ?>A;54=NN >B:@KBCN ?>7...

              number = j;                        // ...8 70?><8=05< 5Q F8:;

            }

            if (OrderOpenPrice()<min || min==0) min=OrderOpenPrice();// I5< =86=89 >@45@ sell

          }

        }

      }

    }

  }

  if ((Bid > max+tp*Point && Bid < max+(tp+slp)*Point && min==0) ||

    (Ask < min-tp*Point && Ask > min-(tp+slp)*Point && max==0)) return(number);

  else return(-1);                               //  A;CG05 =5A>2?. :@8B. >B:@KB8O 2>72@. -1

}

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

//| ?@545;5=85 =0?@02;5=8O ?5@2>9 ?>78F88                           |

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

int PositionDirection() {

  if ((Close[1]-Open[1]) > 0) return(OP_BUY);

  else return(OP_SELL);

}

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

//| 5=5@0F8O <038: =><5@0                                           |

//| 0@0<5B@K:                                                       |

//|   str   - ;N1>9 C=8:0;L=K9 B5:AB                                 |

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

int makeMagicNumber(string str) {

  int i,s;

  for (i=0; i<StringLen(str); i++) {

    s = s + StringGetChar(str,i) * i * 32;

  }

  return(s);

}

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

//| @5>1@07>20=85 2@5<5=8 2 B5:AB                                   |

//| 0@0<5B@K:                                                       |

//|   Time_Hour     - G0AK                                           |

//|   Time_Minute   - <8=CBK                                         |

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

string TimeToText(int Time_Hour, int Time_Minute) {

  string Time_H_M, Time_H, Time_M;          // @5<O 2 B5:AB>2>< D>@<0B5



  if (Time_Hour >= 10 && Time_Hour <= 23) Time_H=DoubleToStr(Time_Hour, 0);

  else {

    if (Time_Hour >= 0 && Time_Hour <= 9) Time_H="0"+DoubleToStr(Time_Hour, 0);

    else {

      Alert("525@=K9 ?0@0<5B@ OpenHour");

      work=false;

    }

  }

  if (Time_Minute >= 10 && Time_Minute <= 59) Time_M=DoubleToStr(Time_Minute, 0);

  else {

    if (Time_Minute >= 0 && Time_Minute <= 9) Time_M="0"+DoubleToStr(Time_Minute, 0);

    else {

      Alert("525@=K9 ?0@0<5B@ OpenMinute");

      work=false;

    }

  }

  Time_H_M=Time_H+":"+Time_M;



  return(Time_H_M);

}

/*

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

//| >72@0I05B D;03 ACI5AB2>20=8O >@45@0 8;8 ?>78F88 ?> =><5@C       |

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

bool ExistOrder(int mn) {

  bool Exist=False;

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

    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

      if (OrderSymbol()==Symbol() && OrderMagicNumber()==magic+mn) {

        Exist=True; break;

      }

    }

  }

  return(Exist);

}

*/

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

//| >72@0I05B D;03 ACI5AB2>20=8O ?>78F88 ?> =><5@C                  |

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

bool ExistPosition(int mn) {

  bool Exist=False;

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

    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

      if (OrderSymbol()==Symbol() && OrderMagicNumber()==magic+mn) {

        if (OrderType()==OP_BUY) {

          Tip[ArraySize(Tip)-1]=OP_SELL;         // 1=>2;5=85 "8?0 ?>7. =>2>3> F8:;0

          Exist=True; break;

        }

        if (OrderType()==OP_SELL) {

          Tip[ArraySize(Tip)-1]=OP_BUY;          // 1=>2;5=85 "8?0 ?>7. =>2>3> F8:;0

          Exist=True; break;

        }

      }

    }

  }

  return(Exist);

}

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

//| 0:@KB85 ?>78F88                                                 |

//| 0@0<5B@K:                                                       |

//|   errproc - D;03 ?>2B>@5=8O ?>?K:8 70:@KB8O ?@8 =5:@8B. >H81:5   |

//|   cyc     - =><5@ F8:;0 ?>78F89                                  |

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

bool ClosePosition(bool errproc, int cyc) {

  int ticket;

  bool ans=false;

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

    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

      if (OrderSymbol()==Symbol() && OrderMagicNumber()==magic+cyc) {

        while(true) {                            // &8:; 70:@KB8O ?>7.

          if (OrderType()==OP_BUY) {             // B:@KB0 ?>78F8O Buy

            ticket = OrderTicket();

            Print (">?KB:0 70:@KBL ",Symbol()," Buy #",ticket,". 6840=85 >B25B0..");

            RefreshRates();                      // 1=>2;5=85 40==KE

            ans=OrderClose(ticket,OrderLots(),Bid,Slippage*dig_ret,LightBlue);

            if (ans==true) {                     // >;CG8;>AL :)

              Print ("0:@KB >@45@ ",Symbol()," Buy #",ticket);

              Tip[cyc]=OP_SELL;                  // 0?><8=05< B8? A;54CNI59 ?>7.

              return(ans);                       // KE>4 87 DC=:F88

            }

            if (Fun_Error(GetLastError())==1)    // 1@01>B:0 >H81>:

              if (errproc) continue;             // >2B>@=0O ?>?KB:0

            return(ans);                         // KE>4 87 DC=:F88

          }

          if (OrderType()==OP_SELL) {            // B:@KB0 ?>78F8O Sell

            ticket = OrderTicket();

            Print (">?KB:0 70:@KBL ",Symbol()," Sell #",ticket,". 6840=85 >B25B0..");

            RefreshRates();                      // 1=>2;5=85 40==KE

            ans=OrderClose(ticket,OrderLots(),Ask,Slippage*dig_ret,LightCoral);

            if (ans==true) {                     // >;CG8;>AL :)

              Print ("0:@KB >@45@ ",Symbol()," Sell #",ticket);

              Tip[cyc]=OP_BUY;                   // 0?><8=05< B8? A;54CNI59 ?>7.

              return(ans);                       // KE>4 87 DC=:F88

            }

            if (Fun_Error(GetLastError())==1)    // 1@01>B:0 >H81>:

              if (errproc) continue;             // >2B>@=0O ?>?KB:0

            return(ans);                         // KE>4 87 DC=:F88

          }

          break;                                 // KE>4 87 while

        }

      }

    }

  }

  return(ans);

}

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

//| B:@KB85 ?>78F88                                                 |

//| 0@0<5B@K:                                                       |

//|   opn     - =0?@02;5=85 ?>78F88                                  |

//|   cyc     - =><5@ F8:;0 ?>78F89                                  |

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

bool OpenPosition(int opn, int cyc) {

  int ticket;

  while(true) {                                  // &8:; >B:@KB8O ?>7.

    if (!ExistPosition(cyc) && opn==OP_BUY) {    // B:@KBKE ?>7. =5B + :@8B5@89 >B:@. Buy

      RefreshRates();                            // 1=>2;5=85 40==KE

      Print (">?KB:0 >B:@KBL ",Symbol()," Buy. 6840=85 >B25B0..");

      ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage*dig_ret,0,0,comment+" #"+cyc,magic+cyc,0,Blue);

      if (ticket > 0) {                          // >;CG8;>AL :)

        Print ("B:@KB >@45@ ",Symbol()," Buy #",ticket);

        return(true);                            // KE>4 87 DC=:F88

      }

      if (Fun_Error(GetLastError())==1)          // 1@01>B:0 >H81>:

        continue;                                // >2B>@=0O ?>?KB:0

      return(false);                             // KE>4 87 DC=:F88

    }

    if (!ExistPosition(cyc) && opn==OP_SELL) {   // B:@KBKE ?>7. =5B + :@8B5@89 >B:@. Sell

      RefreshRates();                            // 1=>2;5=85 40==KE

      Print (">?KB:0 >B:@KBL ",Symbol()," Sell. 6840=85 >B25B0..");

      ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage*dig_ret,0,0,comment+" #"+cyc,magic+cyc,0,Red);

      if (ticket > 0) {                          // >;CG8;>AL :)

        Print ("B:@KB >@45@ ",Symbol()," Sell #",ticket);

        return(true);                            // KE>4 87 DC=:F88

      }

      if (Fun_Error(GetLastError())==1)          // 1@01>B:0 >H81>:

        continue;                                // >2B>@=0O ?>?KB:0

      return(false);                             // KE>4 87 DC=:F88

    }

    break;                                       // KE>4 87 while

  }

  return(false);                                 // KE>4 87 DC=:F88

}

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

//| $C=:F8O >1@01>B:8 >H81>:                                         |

//| 0@0<5B@K:                                                       |

//|   Error     - :>4 >H81:8                                         |

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

int Fun_Error(int Error) {                       // $-8O >1@01>B >H81>:

  switch(Error) {                                // @5>4>;8<K5 >H81:8            

    case  4: Print("">@3>2K9 A5@25@ 70=OB. @>1C5< 5IQ @07..");

      Sleep(3000);                               // @>AB>5 @5H5=85

      return(1);                                 // KE>4 87 DC=:F88

    case  6: Print("5B A2O78 A B>@3>2K< A5@25@><. @>1C5< 5IQ @07..");

      Sleep(3000);                               // @>AB>5 @5H5=85

      return(1);                                 // KE>4 87 DC=:F88

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

      return(1);                                 // KE>4 87 DC=:F88

    case 129:Print("5?@028;L=0O F5=0. @>1C5< 5IQ @07..");

      RefreshRates();                            // 1=>28< 40==K5

      return(1);                                 // KE>4 87 DC=:F88

    case 135:

    case 138:Print("&5=0 87<5=8;0AL. @>1C5< 5IQ @07..");

      RefreshRates();                            // 1=>28< 40==K5

      return(1);                                 // KE>4 87 DC=:F88

    case 136:Print("5B F5=. 4Q< =>2K9 B8:..");

      while(RefreshRates()==false)               // > =>2>3> B8:0

        Sleep(1);                                // 045@6:0 2 F8:;5

      return(1);                                 // KE>4 87 DC=:F88

    case 137:Print("@>:5@ 70=OB. @>1C5< 5IQ @07..");

      Sleep(3000);                               // @>AB>5 @5H5=85

      return(1);                                 // KE>4 87 DC=:F88

    case 146:Print(">4A8AB5<0 B>@3>2;8 70=OB0. @>1C5< 5IQ..");

      Sleep(500);                                // @>AB>5 @5H5=85

      return(1);                                 // KE>4 87 DC=:F88

      // @8B8G5A:85 >H81:8

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

      return(0);                                 // KE>4 87 DC=:F88

    case  5: Print("!B0@0O 25@A8O B5@<8=0;0.");

      work=false;                                // >;LH5 =5 @01>B0BL

      return(0);                                 // KE>4 87 DC=:F88

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

      work=false;                                // >;LH5 =5 @01>B0BL

      return(0);                                 // KE>4 87 DC=:F88

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

      return(0);                                 // KE>4 87 DC=:F88

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

      return(0);                                 // KE>4 87 DC=:F88

    default: Print(">7=8:;0 >H81:0 #",Error);   // @C385 20@80=BK   

      return(0);                                 // KE>4 87 DC=:F88

  }

}

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

//| $C=:F8O :><<5=B0@8O =0 3@0D8:5                                   |

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

void CommentForChart() {

  color colortext;                               // 5@5<5==0O F25B0 B5:AB0

  int mempos = -1;

  int yescyc = GlobalVariableGet(comment+" CY.#")-nocyc-1;

  Comment("                       "+comment+

    "\n\n                       >;8G5AB2> F8:;>2 2 @01>B5 = "+DoubleToStr(yescyc, 0)+

    "\n                        0AAB>O=85 4> <0:A8<0;L=>3> " A5@88 = "+DoubleToStr(distTP, 0)+" ?8?A0(>2)");

  for (int i=0; i<yescyc; i++) {                 // &8:; ?> :>;-2C >@45@>2 2 @01>B5

    colortext = Goldenrod;                       // >72@0I05< F25B B5:AB0

    for (int j=0; j<ArraySize(Tip)-1; j++) {     // &8:; ?> :>;-2C F8:;>2

      if (GlobalVariableGet(comment+" TP."+j)!=0 && j>mempos) {      // A;8 " ACI. 8 =5 >1@010BK20;AO

        ObjectGet("Cycle TP."+i,OBJPROP_COLOR);            // 0?@>A F25B0

        if (GetLastError()==4202) {                        // A;8 >1J5:B0 =5B :(

          ObjectCreate("Cycle TP."+i, OBJ_LABEL, 0, 0, 0);           // !>740=85 >1J5:B0

          ObjectSet("Cycle TP."+i, OBJPROP_CORNER, 0);               // @82O7:0 C3>;

          ObjectSet("Cycle TP."+i, OBJPROP_XDISTANCE, 73);           // >>@48=0B0 %

          ObjectSet("Cycle TP."+i, OBJPROP_YDISTANCE, 63+i*12);      // >>@48=0B0 Y

        }

        if (j==numcyc && yescyc>1) colortext = OrangeRed;  // A;8 <0:A. ", 87<5=O5< F25B B5:AB0

        ObjectSetText("Cycle TP."+i,""5:CI89 " "+j+"-3> F8:;0 = "+

          DoubleToStr(GlobalVariableGet(comment+" TP."+j), 0)+" ?8?A0(>2)",7,"Arial",colortext);

        mempos = j;                              // 0?><8=05< =><5@ B5:. "

        break;

      }

    }

  }

  ObjectDelete("Cycle TP."+yescyc);

  WindowRedraw();

}

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

//| $C=:F8O C40;5=8O :><<5=B0@8O =0 3@0D8:5                          |

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

void DelCommentForChart() {

  for (int i=0; i<100-1; i++)

    ObjectDelete("Cycle TP."+i);

}

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



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