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 reachedChecks for the total of closed orders
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
trend bot
ÿþ//+------------------------------------------------------------------+

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

//|                                                cmillion@narod.ru |

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

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

#property link      "cmillion@narod.ru"

#property strict

/*

!>25B=8: >B:@K205B 425 2AB@5G=K5 ?>78F88 buy 8 sell =0G0;L=K< ;>B>< Lot1

!B028B 8< SL 8 TP 2 ?C=:B0E

40;55 5A;8 70:@K205BAO ?>78F8O ?> TP B> >B:@K205B =>2CN 2 B>< 65 =0?@02;5=88 => A ;>B>< Lot2

A;8 ?>78F8O 70:@K205BAO ?> SL B> >B:@K205BAO =>20O 2 B>< 65 =0?@02;5=88 A ;>B>< Lot1

A;8 ?>78F8O A ;>B>< Lot2 70:@K205BAO B> A>25B=8: >B:@K205B 2 55 =0?@02;5=88 =>2CN, => A ;>B>< Lot1

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



extern int     Stoploss             = 50;

extern int     Takeprofit           = 50;

               

extern double  Lot_1                = 0.10;

extern double  Lot_2                = 0.20;



extern int     Magic                = 777888;

double  Lot1,Lot2;

int slippage=100;

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

int OnInit()

{ 

   Lot1=CheckVolumeValue(Lot_1);

   Lot2=CheckVolumeValue(Lot_2);

   return(INIT_SUCCEEDED);

}

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

void OnTick()

{

   if (!IsTradeAllowed()) return;



   //---



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

   double OSL,OTP,OOP,StLo=0,SL=0,TP=0,Profit=0,ProfitB=0,ProfitS=0;

   int b=0,s=0,tip;

   for (int 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 (OSL==0 && Stoploss!=0)

               {

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

                  if (SL>Bid-STOPLEVEL*Point) SL=0;

               } 

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

               {

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

                  if (TP<Ask+STOPLEVEL*Point) TP=0;

               } 

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

               {  

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

               }

            }                                         

            if (tip==OP_SELL)        

            {

               s++;

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

               {

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

                  if (SL<Ask+STOPLEVEL*Point) SL=0;

               }

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

               {

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

                  if (TP>Bid-STOPLEVEL*Point) TP=0;

               }

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

               {  

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

               }

            } 

         }

      }

   } 



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

   

   if (b==0)

   {

      if (!SendOrder(OP_BUY, LOT(OP_BUY),NormalizeDouble(Ask,Digits)))

      Print("H81:0 >B:@KB8O >@45@0 <<",Error(GetLastError()),">>  ");

   }

   if (s==0)

   {

      if (!SendOrder(OP_SELL,LOT(OP_SELL),NormalizeDouble(Bid,Digits)))

      Print("H81:0 >B:@KB8O >@45@0 <<",Error(GetLastError()),">>  ");

   }

}

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

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

{

   if(!IsNewOrderAllowed()) return(false);

   if (tip<2)

   {

      if (AccountFreeMarginCheck(Symbol(),tip,lots)<0)

      {

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

      else Print("lots=",lots);

      Sleep(500);

      RefreshRates();

      if (IsStopped()) return(false);

   }

   return(false);

}

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

double LOT(int OT)

{

   for (int j = OrdersHistoryTotal()-1; j >= 0; j--)

   {

      if (OrderSelect(j, SELECT_BY_POS,MODE_HISTORY))

      {

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

         {

            if (OrderType()!=OT) continue;

            if (OrderLots()==Lot2) return(Lot1);

            if (OrderProfit()>0) return(Lot2);

            else return(Lot1);

         }

      }

   }

   return(Lot1);

}

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

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

   }

}

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

bool IsNewOrderAllowed()

{

   int max_allowed_orders=(int)AccountInfoInteger(ACCOUNT_LIMIT_ORDERS);

   if(max_allowed_orders==0) return(true);

   if(OrdersTotal()<max_allowed_orders) return(true);

   return(false);

}

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

double CheckVolumeValue(double volume)

{

   double min_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);

   if(volume<min_volume) return(min_volume);

   double max_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);

   if(volume>max_volume) return(max_volume);

   double volume_step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);

   int ratio=(int)MathRound(volume/volume_step);

   if(MathAbs(ratio*volume_step-volume)>0.0000001) return(ratio*volume_step);

   return(volume);

}

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

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