Author: Sergey Eroshenko
Profit factor:
0.00
Orders Execution
It automatically opens orders when conditions are reachedChecks for the total of open ordersIt can change open orders parameters, due to possible stepping strategyIt Closes Orders by itself
1 Views
0 Downloads
0 Favorites
Specnaz2_3
ÿþ//+------------------------------------------------------------------+

//|                                                      Specnaz 2.3 |

//|                                 Copyright 2017, Sergey Eroshenko |

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

#property copyright "Sergey Eroshenko"



int T=8,ticket,count_sell,count_buy,sell_p,buy_p,day_trig=1;

int dop_buy,dop_sell,lock_sell=0,lock_buy=0,trig_u_p_sell=0,trig_u_p_buy=0;

double summ_sell_price,summ_buy_price,TP_unlock_sell=0,TP_unlock_buy=0;

double TP_sell,TP_buy,buy_stop=0,sell_stop=0;

double Mas_Sell[1000][4],Mas_Buy[1000][4];

datetime H;

input double lots=0.01;

input int TakeProfit=200;

input int dly=1000; // Lock level

input int flat=400; // Unlock level



double start_balance=AccountBalance();

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

//|                                                                  |

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

void OnInit()

  {

   Counter();

   TP_calc();

   Print("Balance = ",start_balance);

   Print("Lots = ",lots);

   Print("B:@KBKE ?>:C?>: = ",count_buy," , >B:@KBKE ?@>406 = ",count_sell);

   Print("TP_sell= ",TP_sell," , TP_buy = ",TP_buy);

   if(count_sell==0 && count_buy>0) sell_stop=TP_sell-dly*Point;//0?><8=05< F5=C 4;O 70<:0 

   if(count_buy==0 && count_sell>0) buy_stop=TP_sell+dly*Point;//0?><8=05< F5=C 4;O 70<:0

   Print("sell_stop= ",sell_stop," , buy_stop = ",buy_stop);

  }

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

//|                                                                  |

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

void OnTick()

  {

   H=TimeHour(TimeCurrent());



//////////////;>: >B:@KB8O >@45@>2. 654=52=> 2> 2@5<O T ///////////////////////////////

   if(H==T-1 && day_trig==1)

     {

      day_trig=0; //"@8335@ =>2>3> 4=O

      Counter();

     }



   if(H==T && day_trig==0)//A;8 2@5<O " 8 =>2K9 45=L =0ABC?8; >B:@K205< Buy 8 Sell ?> B5:CI8< F5=0< 

     {

      ticket=0;

      ticket=OrderSend(Symbol(),OP_SELL,lots,Bid,30,0,0,"",0,0,Red);

      if(ticket>0)

        {

         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

            Print("SELL order opened : ",OrderOpenPrice());

        }

      else

         Print("Error opening SELL order : ",GetLastError());



      ticket=0;

      ticket=OrderSend(Symbol(),OP_BUY,lots,Ask,30,0,0,"",0,0,Red);

      if(ticket>0)

        {

         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

            Print("BUY order opened : ",OrderOpenPrice());

        }

      else

         Print("Error opening Buy order : ",GetLastError());



      Counter();

      TP_calc();

      day_trig=1;

      Print("B:@KBKE ?>:C?>: = ",count_buy," , >B:@KBKE ?@>406 = ",count_sell);

      Print("TP_sell= ",TP_sell," , TP_buy = ",TP_buy);

     }

//////////////;>: >B:@KB8O >@45@>2 '//////////////////////////



//////////////;>: 70:@KB8O >@45@>2//////////////////////////////////

   if(count_sell>0 && Ask<=TP_sell && TP_sell>0 && lock_sell==0)

     {

      Cls_Sell();

      sell_stop=TP_sell-dly*Point;//0?><8=05< F5=C 4;O 70<:0

      Print("0:@KB0 F5?>G:0 ?@>406");

     }



   if(count_buy>0 && Bid>=TP_buy && TP_buy>0 && lock_buy==0)

     {

      Cls_Buy();

      buy_stop=TP_buy+dly*Point;//0?><8=05< F5=C 4;O 70<:0

      Print("0:@KB0 F5?>G:0 ?>:C?>:");

     }

//////////////;>: 70:@KB8O >@45@>@2 '/////////////////////////



//////////////;>: CAB0=>2:8 70<:>2//////////////////////////////////

   if(Bid<sell_stop && sell_stop!=0 && lock_sell==0)// B:@KB85 ?@>406 ?> 7040==>9 F5=5

     {

      Balance_sell();

      Print("Balance sell");

      lock_sell=1;

     }

//   

   if(Ask>buy_stop && buy_stop!=0 && lock_buy==0)// B:@KB85 ?>:C?>: ?> 7040==>9 F5=5

     {

      Balance_buy();

      Print("Balance buy");

      lock_buy=1;

     }

//////////////;>: CAB0=>2:8 70<:>2 >:>=G5=//////////////////////////



//////////////;>: @07<K:0=8O 70<:>2/////////////////////////////////

   if(lock_sell==1 && Bid>TP_buy-flat*Point && trig_u_p_sell==0)//A;8 AB>8B 70<>: 87 ?@>406 8 F5=0 2>H;0 2 :>@84>@ : ?>:C?:0< 

     {

      Counter();

      TP_calc();

      TP_unlock_sell=TP_sell;//0405< F5=C 4;O @07<K:0=8O 70<:0 ?@>406

      Print("040=0 F5=0 @07<K:0=8O 70<:0 ?@>406 = ",TP_unlock_sell);

      trig_u_p_sell=1;

     }

//    

   if(Bid<=TP_unlock_sell && TP_unlock_sell!=0)//A;8 F5=0 >?CAB8;0AL 4> C@>2=O @07<K:0=8O 70<:0 ?@>406

     {

      Cls_Sell();//0:@K205< ?@>4068

      TP_unlock_sell=0;//#18@05< F5=C @07<K:0=8O 70<:0 ?@>406

      lock_sell=0;//B:@K205< B@8335@ 70<:0 ?@>406

      Print(" 07><:=CB 70<>: A> AB>@>=K ?@>406 ?> F5=5 = ",Bid);

      trig_u_p_sell=0;

      sell_stop=TP_unlock_sell-dly*Point;//0?><8=05< F5=C 4;O 70<:0     

     }

////

   if(lock_buy==1 && Ask<TP_sell+flat*Point && trig_u_p_buy==0)//A;8 AB>8B 70<>: 87 ?>:C?>: 8 F5=0 2>H;0 2 :>@84>@ : ?@>4060< 

     {

      Counter();

      TP_calc();

      TP_unlock_buy=TP_buy;//0405< F5=C 4;O @07<K:0=8O 70<:0 ?>:C?>:

      Print("040=0 F5=0 @07<K:0=8O 70<:0 ?>:C?>: = ",TP_unlock_buy);

      trig_u_p_buy=1;

     }

//    

   if(Ask>=TP_unlock_buy && TP_unlock_buy!=0)//A;8 F5=0 ?>4=O;0AL 4> C@>2=O @07<K:0=8O 70<:0 ?>:C?>:

     {

      Cls_Buy();//0:@K205< ?>:C?:8

      TP_unlock_buy=0;//#18@05< F5=C @07<K:0=8O 70<:0 ?>:C?>:

      lock_buy=0;//B:@K205< B@8335@ 70<:0 ?>:C?>:

      Print(" 07><:=CB 70<>: A> AB>@>=K ?>:C?>: ?> F5=5 = ",Ask);

      trig_u_p_buy=0;

      buy_stop=TP_unlock_buy+dly*Point;//0?><8=05< F5=C 4;O 70<:0     

     }

//////////////;>: @07<K:0=8O 70<:>2 >:>=G5=/////////////////////////////////

  }

//////////////"5;> ?@>3@0<<K >:>=G5=>/////////////////////////////////



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

//|$C=:F8O >?@545;5=8O :>;8G5AB20 >B:@KBKE A45;>: 8 8E F5=,          |

//|?> B8?0< Sell  Buy. !>7405B 420 <0AA820.                         |

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

void Counter()

  {

   count_sell=0;                       // 1=C;5=85 AGQBG8:0 >@45@>2

   count_buy=0;                        // 1=C;5=85 AGQBG8:0 >@45@>2

   ArrayInitialize(Mas_Sell,0);        // 1=C;5=85 <0AA820 Sell

   ArrayInitialize(Mas_Buy,0);         // 1=C;5=85 <0AA820 Buy



   for(int i=0; i<=OrdersTotal(); i++) // >4AG5B >@45@>2 Sell

     {

      if((OrderSelect(i,SELECT_BY_POS)==true)         //A;8 5ABL A;54CNI.

         && (OrderType()==OP_SELL))                      // 8 5A;8 >@45@ Sell

        {

         count_sell++;                                // >;8G. >@45@>2 Sell

         Mas_Sell[count_sell][1]=OrderOpenPrice();    // &5=0 A45;:8

         Mas_Sell[count_sell][2]=OrderTicket();       // ><5@ >@45@0

         Mas_Sell[count_sell][3]=OrderMagicNumber();  // 038G5A:>5 G8A;> 

        }

     }



   for(int j=0; j<=OrdersTotal(); j++) // >4AG5B >@45@>2 Buy

     {

      if((OrderSelect(j,SELECT_BY_POS)==true)       //A;8 5ABL A;54CNI.

         && (OrderType()==OP_BUY))                     // 8 5A;8 >@45@ Buy

        {

         count_buy++;                               // >;8G. >@45@>2 Buy

         Mas_Buy[count_buy][1]=OrderOpenPrice();    // &5=0 A45;:8

         Mas_Buy[count_buy][2]=OrderTicket();       // ><5@ >@45@0

         Mas_Buy[count_buy][3]=OrderMagicNumber();  // 038G5A:>5 G8A;> 

        }

     }

  }

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

//|$C=:F8O @0AAG5B0 TakeProfit 4;O A45;>: Sell 8 Buy                 |

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

void TP_calc()

  {

   summ_sell_price=0;

   summ_buy_price=0;

   TP_sell=0;

   TP_buy=0;



   if(count_sell>0)

     {

      for(int i=0; i<=count_sell; i++) // !C<<8@>20=85 F5= Sell

        {

         summ_sell_price=summ_sell_price+Mas_Sell[i][1];

        }

      TP_sell=summ_sell_price/count_sell-TakeProfit*Point;

     }



   if(count_buy>0)

     {

      for(int j=0; j<=count_buy; j++) // !C<<8@>20=85 F5= Buy

        {

         summ_buy_price=summ_buy_price+Mas_Buy[j][1];

        }



      TP_buy=summ_buy_price/count_buy+TakeProfit*Point;

     }

  }

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

//|$C=:F8O <>48D8:0F88 TakeProfit ?> 2A5< >@45@0<                    |

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

void TP_corr()

  {

   for(int i=0; i<=OrdersTotal(); i++) // 7<5=5=85 TakeProfit >@45@>2 Sell

     {

      if((OrderSelect(i,SELECT_BY_POS)==true) //A;8 5ABL A;54CNI.

         && (OrderTakeProfit()!=TP_sell)

         && (OrderType()==OP_SELL)) // 8 5A;8 >@45@ Sell

        {

         if(TP_sell<Ask)

           {

            bool res=OrderModify(OrderTicket(),OrderOpenPrice(),0,TP_sell,0,Blue);

            if(!res) Print("H81:0 <>48D8:0F88 SELL. >4 >H81:8=",GetLastError());

           }

         if(TP_sell>Ask)

           {

            res=OrderModify(OrderTicket(),OrderOpenPrice(),TP_sell,0,0,Blue);

            if(!res) Print("H81:0 <>48D8:0F88 SELL. >4 >H81:8=",GetLastError());

           }

        }

     }



   for(int j=0; j<=OrdersTotal(); j++) // 7<5=5=85 TakeProfit >@45@>2 Buy

     {

      if((OrderSelect(j,SELECT_BY_POS)==true) //A;8 5ABL A;54CNI.

         && (OrderTakeProfit()!=TP_buy)

         && (OrderType()==OP_BUY)) // 8 5A;8 >@45@ Buy

        {

         if(TP_buy>Bid)

           {

            bool res2=OrderModify(OrderTicket(),OrderOpenPrice(),0,TP_buy,0,Blue);

            if(!res2) Print("H81:0 <>48D8:0F88 BUY. >4 >H81:8=",GetLastError());

           }

         if(TP_buy<Bid)

           {

            res2=OrderModify(OrderTicket(),OrderOpenPrice(),TP_buy,0,0,Blue);

            if(!res2) Print("H81:0 <>48D8:0F88 BUY. >4 >H81:8=",GetLastError());

           }

        }

     }

  }

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

//|$C=:F8O 70:@KB8O 2A5E >B:@KBKE Sell                               |

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

void Cls_Sell()

  {

   Counter();

   for(int i=0; i<=count_sell; i++) //

     {

      ticket=Mas_Sell[i][2];

      if(OrderSelect(ticket,SELECT_BY_TICKET)==true) //A;8 5ABL A;54CNI.

        {

         bool res=OrderClose(ticket,OrderLots(),Ask,50,Blue);

        }

     }

  }

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

//|$C=:F8O 70:@KB8O 2A5E >B:@KBKE Buy                                |

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

void Cls_Buy()

  {

   Counter();

   for(int i=0; i<=count_buy; i++) //

     {

      ticket=Mas_Buy[i][2];

      if(OrderSelect(ticket,SELECT_BY_TICKET)==true) //A;8 5ABL A;54CNI.

        {

         bool res=OrderClose(ticket,OrderLots(),Bid,50,Blue);

        }

     }

  }

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

//|$C=:F8O >B:@KB8O 10;0=A8@CNI59 F5?>G:8 ?@>406                     |

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

void Balance_sell()

  {

   Counter();

   buy_p=count_buy-count_sell;



   for(int i=1; i<=(buy_p); i++)

     {

      ticket=0;

      ticket=OrderSend(Symbol(),OP_SELL,lots,Bid,30,0,0,"",0,0,Red);

      if(ticket>0)

        {

         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

            Print("SELL order opened : ",OrderOpenPrice());

        }

      else

         Print("Error opening SELL order : ",GetLastError());

     }

  }

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

//|$C=:F8O >B:@KB8O 10;0=A8@CNI59 F5?>G:8 ?>:C?>:                    |

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

void Balance_buy()

  {

   Counter();

   sell_p=count_sell-count_buy;



   for(int j=1; j<=(sell_p); j++)

     {

      ticket=0;

      ticket=OrderSend(Symbol(),OP_BUY,lots,Ask,30,0,0,"",0,0,Red);

      if(ticket>0)

        {

         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

            Print("BUY order opened : ",OrderOpenPrice());

        }

      else

         Print("Error opening BUY order : ",GetLastError());

     }

  }

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

//|DC=:F8O >B:@KB8O 4>? >@45@>2                                      |

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

void Open_dop()

  {

   TP_calc();

   if(Bid<summ_sell_price/count_sell-TakeProfit*Point)

     {

      ticket=OrderSend(Symbol(),OP_SELL,lots,Bid,30,TP_buy,Bid-TakeProfit*Point,"",0,0,Red);

      if(ticket>0)

        {

         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

            Print("B:@KB0 4>?>;=8B5;L=0O ?@>4060 ",OrderOpenPrice());

         dop_buy=1;

        }

      else

         Print("Error opening SELL order : ",GetLastError());

     }



   if(Ask>summ_buy_price/count_buy+TakeProfit*Point)

     {

      ticket=OrderSend(Symbol(),OP_BUY,lots,Ask,30,TP_sell,Ask+TakeProfit*Point,"",0,0,Red);

      if(ticket>0)

        {

         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

            Print("B:@KB0 4>?>;=8B5;L=0O ?>:C?:0 ",OrderOpenPrice());

         dop_sell=1;

        }

      else

         Print("Error opening Buy order : ",GetLastError());

     }

  }

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

Comments