LockSystem_2f1_MM_v1_0

Author: VMQL Solutions
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt can change open orders parameters, due to possible stepping strategyIt Closes Orders by itself
0 Views
0 Downloads
0 Favorites
LockSystem_2f1_MM_v1_0
//+------------------------------------------------------------------+
//|                                              LockSystem 2+1.mq4 |
//|                                                   VMQL Solutions |
//|                                               http://www.vmql.ru |
//+------------------------------------------------------------------+
#property copyright "VMQL Solutions"
#property link      "http://www.vmql.ru"

extern int PipStepBuy = 200;// (â ïóíêòàõ, ðàññòîÿíèå ìåæäó îðäåðàìè Buy)
extern int DoublePipStepBuy = 400;// (â ïóíêòàõ, ðàññòîÿíèå ìåæäó âòîðè÷íûìè îðäåðàìè Buy)
extern int PipStepSell = 200; // (â ïóíêòàõ, ðàññòîÿíèå ìåæäó îðäåðàìè Sell)
extern int DoublePipStepSell = 400;// (â ïóíêòàõ, ðàññòîÿíèå ìåæäó âòîðè÷íûìè îðäåðàìè Buy)
extern int AllCloseProfit = 50; // (â ïóíêòàõ, çàêðûòèå âñåé ñåðèè îðäåðîâ ïðè âûõîäå â îáùèé ïëþñ)
extern bool Work_tp_average = false; // âêëþ÷åíèå ðåæèìà çàêðûòèÿ îðäåðîâ ïî áåçóáûòêó + TP_Average
extern int TP_Average = 20; // (â ïóíêòàõ, óðîâåíü ïðèáûëè ïðè óñðåäíåíèè ñåðèè óáûòî÷íûõ îðäåðîâ)
extern int Close_2_1 = 50; // (â ïóíêòàõ, çàêðûòèå îðäåðîâ ïî ñèñòåìå 2+1 (2 ïðèáûëüíûõ+1 óáûòî÷íûé))
extern bool Use_block = false; // ôëàã ðàáîòû ôóíêöèè áëîêèðîâêè
extern double Lot = 0; // (îáúåì ëîòà, åñëè 0, òî îòêëþ÷àåòñÿ, ðàáîòàåò ÌÌ)
extern double MM = 0.4; // (ìàíèìåíåäæåìåíò, â ïðîöåíòàõ, ðàçìåð ëîòà ðàññ÷èòûâàåòñÿ îò áàëàíñà, ïðè çíà÷åíèè lot áîëüøå 0, îòêëþ÷àåòñÿ)
extern int ID = 2059075978;

string main_id;
int init()
{  
   main_id = "@" + DoubleToStr(ID,0) + "@";
   return(0);
}

int deinit()
{
   switch( UninitializeReason() )
   {
      case (REASON_REMOVE || REASON_CHARTCLOSE): delete_glob ( main_id );
   }
   if ( IsTesting() )delete_glob ( main_id );
   return(0);
}

double l_bid = 0;
string COMMENT;
int start()
{
   if ( l_bid == 0 )
   {
      l_bid = Bid;
      return(0);
   }
   
   double l;
   string comment;
   
   COMMENT = "";
   COMMENT = COMMENT + "Òî÷êà îòñ÷åòà BUY: " + DoubleToStr(GlobalVariableGet(main_id + "START_PRICE_BUY"), Digits) + "\n";
   COMMENT = COMMENT + "Òî÷êà îòñ÷åòà SELL: " + DoubleToStr(GlobalVariableGet(main_id + "START_PRICE_SELL"), Digits) + "\n";
   
   if ( order_total() == 0 )
   {
      GlobalVariableSet(main_id + "START_TIME", TimeCurrent());
      GlobalVariableSet(main_id + "START_PRICE_BUY", Bid);
      GlobalVariableSet(main_id + "START_PRICE_SELL", Bid);
      l = get_lot();
      GlobalVariableSet(main_id + "FIRST_ORDER_LOT", l);
      GlobalVariableSet(main_id + "FIRST_ORDER_BUY", Bid);
      GlobalVariableSet(main_id + "FIRST_ORDER_BUY_2", Bid);
      GlobalVariableSet(main_id + "FIRST_ORDER_SELL", Bid);
      GlobalVariableSet(main_id + "FIRST_ORDER_SELL_2", Bid);
      comment = DoubleToStr(Bid, Digits) + "#";
      open( OP_BUY, l, 0, 0, comment);
      open( OP_SELL, l, 0, 0, comment);
   }
   else
   {
      set_other_order();
      update_start_price();
      if ( Work_tp_average )
      {
         work_bezubotok();
         update_start_price();
      }
      close_by_summ_profit();
      update_start_price();
      if ( (order_total(OP_BUY) > 1 && order_total(OP_SELL) > 0) || (order_total(OP_BUY) > 0 && order_total(OP_SELL) > 1) )
      {
         close_by_2_1();
         update_start_price();
      }
   }
   
   Comment(COMMENT);
   l_bid = Bid;
   
   return(0);
}

void update_start_price()
{
   int i, min_buy_ticket = 0, max_sell_ticket = 0, kol_buy = 0, kol_sell = 0, sell = 0, buy = 0;
   double min_buy = 999999, max_sell = 0, op_pr, l;
   string comment;
   
   if ( GlobalVariableCheck(main_id + "FIRST_ORDER_LOT") )l = GlobalVariableGet(main_id + "FIRST_ORDER_LOT");
   
   for( i = OrdersTotal() - 1; i >= 0; i-- )
   {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if( OrderMagicNumber() != ID )continue;
      op_pr = NormalizeDouble(StrToDouble(StringSubstr(OrderComment(),0,StringFind(OrderComment(),"#"))), Digits);
      
      if ( OrderType() == OP_BUY && op_pr < min_buy )
      {
         min_buy = op_pr;
         min_buy_ticket = OrderTicket();
      }
      
      if ( OrderType() == OP_SELL && op_pr > max_sell )
      {
         max_sell = op_pr;
         max_sell_ticket = OrderTicket();
      }
   }

   for( i = OrdersTotal() - 1; i >= 0; i-- )
   {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if( OrderMagicNumber() != ID )continue;
      op_pr = NormalizeDouble(StrToDouble(StringSubstr(OrderComment(),0,StringFind(OrderComment(),"#"))), Digits);
      if ( max_sell_ticket != 0 && OrderType() == OP_BUY && op_pr < max_sell )kol_buy ++;
      if ( min_buy_ticket != 0 && OrderType() == OP_SELL && op_pr > min_buy )kol_sell ++;
      if ( OrderType() == OP_BUY )buy ++;
      if ( OrderType() == OP_SELL )sell ++;
   }
   
   if
   (
      min_buy_ticket != 0 &&
      (
         (NormalizeDouble(l_bid,Digits) <= NormalizeDouble(min_buy,Digits) && NormalizeDouble(Bid,Digits) >= NormalizeDouble(min_buy,Digits)) ||
         (NormalizeDouble(l_bid,Digits) >= NormalizeDouble(min_buy,Digits) && NormalizeDouble(Bid,Digits) <= NormalizeDouble(min_buy,Digits))
      )
   )
   {
      GlobalVariableSet(main_id + "START_PRICE_BUY", min_buy);
      GlobalVariableSet(main_id + "FIRST_ORDER_BUY", min_buy);
      GlobalVariableSet(main_id + "FIRST_ORDER_BUY_2", min_buy);
      GlobalVariableSet(main_id + "FIRST_ORDER_SELL", min_buy);
      GlobalVariableSet(main_id + "FIRST_ORDER_SELL_2", min_buy);
      comment = DoubleToStr(min_buy, Digits) + "#";
      if ( kol_sell == 0 && order_total_by_comment(comment, OP_SELL) == 0 )open( OP_SELL, l, 0, 0, comment);
      Print("Îáíîâëåíèå òî÷êè îòñ÷åòà");
   }
   
   if
   (
      max_sell_ticket != 0 &&
      (
         (NormalizeDouble(l_bid,Digits) <= NormalizeDouble(max_sell,Digits) && NormalizeDouble(Bid,Digits) >= NormalizeDouble(max_sell,Digits)) ||
         (NormalizeDouble(l_bid,Digits) >= NormalizeDouble(max_sell,Digits) && NormalizeDouble(Bid,Digits) <= NormalizeDouble(max_sell,Digits))
      )
   )
   {
      GlobalVariableSet(main_id + "START_PRICE_SELL", max_sell);
      GlobalVariableSet(main_id + "FIRST_ORDER_BUY", max_sell);
      GlobalVariableSet(main_id + "FIRST_ORDER_BUY_2", max_sell);
      GlobalVariableSet(main_id + "FIRST_ORDER_SELL", max_sell);
      GlobalVariableSet(main_id + "FIRST_ORDER_SELL_2", max_sell);
      comment = DoubleToStr(max_sell, Digits) + "#";
      if ( kol_buy == 0 && order_total_by_comment(comment, OP_BUY) == 0 )open( OP_BUY, l, 0, 0, comment);
      Print("Îáíîâëåíèå òî÷êè îòñ÷åòà");
   }
}

void work_bezubotok()
{
   int kol_b = order_total(OP_BUY), kol_s = order_total(OP_SELL);
 
   if ( kol_b > 2 )close_orders_bezub(OP_BUY);
   if ( kol_s > 2 )close_orders_bezub(OP_SELL);
}

void close_by_2_1()
{
   int i, max_plus_b_1_t = 0, max_minus_b_1_t = 0, max_plus_b_2_t = 0, max_plus_s_1_t = 0, max_minus_s_1_t = 0, max_plus_s_2_t = 0;
   double profit, max_plus_b_1 = 0, max_minus_b_1 = 0, max_plus_b_2 = 0, max_plus_s_1 = 0, max_minus_s_1 = 0, max_plus_s_2 = 0;
   
   for( i = OrdersTotal() - 1; i >= 0; i-- )
   {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if( OrderMagicNumber() != ID )continue;
      
      profit = (OrderProfit() / (OrderLots() * MarketInfo(OrderSymbol(),MODE_TICKVALUE)));
      
      if ( OrderType() == OP_BUY )
      {
         if ( profit > max_plus_b_1 )
         {
            max_plus_b_1 = profit;
            max_plus_b_1_t = OrderTicket();
         }
         if ( profit < max_minus_b_1 )
         {
            max_minus_b_1 = profit;
            max_minus_b_1_t = OrderTicket();
         }
      }
      
      if ( OrderType() == OP_SELL )
      {
         if ( profit > max_plus_s_1 )
         {
            max_plus_s_1 = profit;
            max_plus_s_1_t = OrderTicket();
         }
         if ( profit < max_minus_s_1 )
         {
            max_minus_s_1 = profit;
            max_minus_s_1_t = OrderTicket();
         }
      }
   }
   
   if ( max_plus_b_1_t != 0 || max_plus_s_1_t != 0 )
   for( i = OrdersTotal() - 1; i >= 0; i-- )
   {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if( OrderMagicNumber() != ID )continue;
      
      profit = (OrderProfit() / (OrderLots() * MarketInfo(OrderSymbol(),MODE_TICKVALUE)));
      
      if ( OrderType() == OP_BUY )
      {
         if ( profit > max_plus_b_2 && OrderTicket() != max_plus_b_1_t )
         {
            max_plus_b_2 = profit;
            max_plus_b_2_t = OrderTicket();
         }
      }
      
      if ( OrderType() == OP_SELL )
      {
         if ( profit > max_plus_s_2 && OrderTicket() != max_plus_s_1_t )
         {
            max_plus_s_2 = profit;
            max_plus_s_2_t = OrderTicket();
         }
      }
   }
   
   COMMENT = COMMENT + "Çàêðûòèå ïî Close_2_1 BUY: #" + DoubleToStr(max_plus_b_1_t, 0) + " " + DoubleToStr(max_plus_b_1, 0) + " #" + DoubleToStr(max_plus_b_2_t, 0) + " " + DoubleToStr(max_plus_b_2, 0) + " #" + DoubleToStr(max_minus_s_1_t, 0) + " " + DoubleToStr(max_minus_s_1, 0) + "\n";
   COMMENT = COMMENT + "Çàêðûòèå ïî Close_2_1 SELL: #" + DoubleToStr(max_plus_s_1_t, 0) + " " + DoubleToStr(max_plus_s_1, 0) + " #" + DoubleToStr(max_plus_s_2_t, 0) + " " + DoubleToStr(max_plus_s_2, 0) + " #" + DoubleToStr(max_minus_b_1_t, 0) + " " + DoubleToStr(max_minus_b_1, 0) + "\n";

   if ( max_plus_b_1_t != 0 && max_plus_b_2_t != 0 && max_minus_s_1_t != 0 && max_plus_b_1_t != max_plus_b_2_t && max_plus_b_2_t != max_minus_s_1_t && max_plus_b_1_t != max_minus_s_1_t )
   {
      if ( max_plus_b_1 > 0 && max_plus_b_2 > 0 && max_minus_s_1 < 0 )if ( (max_plus_b_1 + max_plus_b_2) >= (MathAbs(max_minus_s_1) + Close_2_1) )
      {
         Print("Çàêðûòèå ïî Close_2_1 BUY: #" + DoubleToStr(max_plus_b_1_t, 0) + " " + DoubleToStr(max_plus_b_1, 0) + " #" + DoubleToStr(max_plus_b_2_t, 0) + " " + DoubleToStr(max_plus_b_2, 0) + " #" + DoubleToStr(max_minus_s_1_t, 0) + " " + DoubleToStr(max_minus_s_1, 0));
         close(max_plus_b_1_t);
         close(max_plus_b_2_t);
         close(max_minus_s_1_t);
      }
   }
   
   if ( max_plus_s_1_t != 0 && max_plus_s_2_t != 0 && max_minus_b_1_t != 0 && max_plus_s_1_t != max_plus_s_2_t && max_plus_s_2_t != max_minus_b_1_t && max_plus_s_1_t != max_minus_b_1_t )
   {
      if ( max_plus_s_1 > 0 && max_plus_s_2 > 0 && max_minus_b_1 < 0 )if ( (max_plus_s_1 + max_plus_s_2) >= (MathAbs(max_minus_b_1) + Close_2_1) )
      {
         Print("Çàêðûòèå ïî Close_2_1 SELL: #" + DoubleToStr(max_plus_s_1_t, 0) + " " + DoubleToStr(max_plus_s_1, 0) + " #" + DoubleToStr(max_plus_s_2_t, 0) + " " + DoubleToStr(max_plus_s_2, 0) + " #" + DoubleToStr(max_minus_b_1_t, 0) + " " + DoubleToStr(max_minus_b_1, 0));
         close(max_plus_s_1_t);
         close(max_plus_s_2_t);
         close(max_minus_b_1_t);
      }
   }
}

void close_by_summ_profit()
{
   int i;
   double buy_lots = 0, sell_lots = 0, prof = 0, b_point;
   
   for( i = OrdersTotal() - 1; i >= 0; i-- )
   {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if( OrderMagicNumber() != ID )continue;
      if ( OrderType() == OP_BUY )buy_lots += OrderLots();
      if ( OrderType() == OP_SELL )sell_lots += OrderLots();
      prof += OrderProfit();
   }
   
   if ( NormalizeDouble(buy_lots,2) != NormalizeDouble(sell_lots,2) && ( (NormalizeDouble(buy_lots,2) > 0 && NormalizeDouble(sell_lots,2) > 0) || (NormalizeDouble(buy_lots,2) > 0 && NormalizeDouble(sell_lots,2) == 0) || (NormalizeDouble(buy_lots,2) == 0 && NormalizeDouble(sell_lots,2) > 0) ) )
   {
      b_point = get_bezubitok_point();
      COMMENT = COMMENT + "Òî÷êà áåçóáûòêà: " + DoubleToStr(b_point,Digits) + "\n";
      if ( MathAbs((b_point - Bid)/Point) >= AllCloseProfit && prof > 0 )
      {
         Print("Çàêðûòèå ïî AllCloseProfit, ñóììàðíûé ïðîôèò â âàëþòå äåïîçèòà: " + DoubleToStr(prof, 0));
         close_all();
      }
   }
}

void close_orders_bezub ( int type )
{
   int i;
   double level;
   
   if ( type == OP_BUY )
   {
      level = NormalizeDouble( get_bezubitok_point( type ) + TP_Average * Point, Digits);
      if ( NormalizeDouble(Bid,Digits) >= level )
      {
         close_all(OP_BUY);
         Print("Çàêðûòèå ïî TP_Average");
      }
   }
   
   if ( type == OP_SELL )
   {
      level = NormalizeDouble( get_bezubitok_point( type ) - TP_Average * Point, Digits);
      if ( NormalizeDouble(Bid,Digits) <= level )
      {
         close_all(OP_SELL);
         Print("Çàêðûòèå ïî TP_Average");
      }
   }
}

void set_other_order()
{
   int i, type = -1, l_ticket_b = 0, l_ticket_s = 0;
   double l, op_pr;
   string comment;
   
   if ( GlobalVariableCheck(main_id + "FIRST_ORDER_LOT") )l = GlobalVariableGet(main_id + "FIRST_ORDER_LOT");
   
   if ( GlobalVariableCheck(main_id + "FIRST_ORDER_BUY") )op_pr = GlobalVariableGet(main_id + "FIRST_ORDER_BUY");
   if ( ((op_pr - Bid)/Point) >= PipStepBuy )
   {
      GlobalVariableSet(main_id + "FIRST_ORDER_BUY", NormalizeDouble(op_pr - PipStepBuy * Point, Digits));
      comment = DoubleToStr(op_pr - PipStepBuy * Point, Digits) + "#";
      if ( order_total_by_comment(comment, OP_BUY) == 0 && NormalizeDouble(Bid, Digits) >= NormalizeDouble(GlobalVariableGet(main_id + "START_PRICE_SELL"), Digits) )open( OP_BUY, l, 0, 0, comment);
   }
      
   if ( ((Bid - op_pr)/Point) >= PipStepBuy )
   {
      GlobalVariableSet(main_id + "FIRST_ORDER_BUY", NormalizeDouble(op_pr + PipStepBuy * Point, Digits));
      comment = DoubleToStr(op_pr + PipStepBuy * Point, Digits) + "#";
      if ( order_total_by_comment(comment, OP_BUY) == 0 && NormalizeDouble(Bid, Digits) >= NormalizeDouble(GlobalVariableGet(main_id + "START_PRICE_SELL"), Digits) )open( OP_BUY, l, 0, 0, comment);
   }
   
   if ( GlobalVariableCheck(main_id + "FIRST_ORDER_BUY_2") )op_pr = GlobalVariableGet(main_id + "FIRST_ORDER_BUY_2");
   if ( ((op_pr - Bid)/Point) >= DoublePipStepBuy )
   {
      GlobalVariableSet(main_id + "FIRST_ORDER_BUY_2", NormalizeDouble(op_pr - DoublePipStepBuy * Point, Digits));
      comment = DoubleToStr(op_pr - DoublePipStepBuy * Point, Digits) + "#";
      if ( order_total_by_comment(comment, OP_BUY) == 0 && NormalizeDouble(Bid, Digits) <= NormalizeDouble(GlobalVariableGet(main_id + "START_PRICE_BUY"), Digits) )open( OP_BUY, l, 0, 0, comment);
   }
      
   if ( ((Bid - op_pr)/Point) >= DoublePipStepBuy )
   {
      GlobalVariableSet(main_id + "FIRST_ORDER_BUY_2", NormalizeDouble(op_pr + DoublePipStepBuy * Point, Digits));
      comment = DoubleToStr(op_pr + DoublePipStepBuy * Point, Digits) + "#";
      if ( order_total_by_comment(comment, OP_BUY) == 0 && NormalizeDouble(Bid, Digits) <= NormalizeDouble(GlobalVariableGet(main_id + "START_PRICE_BUY"), Digits) )open( OP_BUY, l, 0, 0, comment);
   }
   
   if ( GlobalVariableCheck(main_id + "FIRST_ORDER_SELL") )op_pr = GlobalVariableGet(main_id + "FIRST_ORDER_SELL");
   if ( ((op_pr - Bid)/Point) >= PipStepSell )
   {
      GlobalVariableSet(main_id + "FIRST_ORDER_SELL", NormalizeDouble(op_pr - PipStepSell * Point, Digits));
      comment = DoubleToStr(op_pr - PipStepSell * Point, Digits) + "#";
      if ( order_total_by_comment(comment, OP_SELL) == 0 && NormalizeDouble(Bid, Digits) <= NormalizeDouble(GlobalVariableGet(main_id + "START_PRICE_BUY"), Digits) )open( OP_SELL, l, 0, 0, comment);
   }
      
   if ( ((Bid - op_pr)/Point) >= PipStepSell )
   {
      GlobalVariableSet(main_id + "FIRST_ORDER_SELL", NormalizeDouble(op_pr + PipStepSell * Point, Digits));
      comment = DoubleToStr(op_pr + PipStepSell * Point, Digits) + "#";
      if ( order_total_by_comment(comment, OP_SELL) == 0 && NormalizeDouble(Bid, Digits) <= NormalizeDouble(GlobalVariableGet(main_id + "START_PRICE_BUY"), Digits) )open( OP_SELL, l, 0, 0, comment);
   }
   
   if ( GlobalVariableCheck(main_id + "FIRST_ORDER_SELL_2") )op_pr = GlobalVariableGet(main_id + "FIRST_ORDER_SELL_2");
   if ( ((op_pr - Bid)/Point) >= DoublePipStepSell )
   {
      GlobalVariableSet(main_id + "FIRST_ORDER_SELL_2", NormalizeDouble(op_pr - DoublePipStepSell * Point, Digits));
      comment = DoubleToStr(op_pr - DoublePipStepSell * Point, Digits) + "#";
      if ( order_total_by_comment(comment, OP_SELL) == 0 && NormalizeDouble(Bid, Digits) >= NormalizeDouble(GlobalVariableGet(main_id + "START_PRICE_SELL"), Digits) )open( OP_SELL, l, 0, 0, comment);
   }
      
   if ( ((Bid - op_pr)/Point) >= DoublePipStepSell )
   {
      GlobalVariableSet(main_id + "FIRST_ORDER_SELL_2", NormalizeDouble(op_pr + DoublePipStepSell * Point, Digits));
      comment = DoubleToStr(op_pr + DoublePipStepSell * Point, Digits) + "#";
      if ( order_total_by_comment(comment, OP_SELL) == 0 && NormalizeDouble(Bid, Digits) >= NormalizeDouble(GlobalVariableGet(main_id + "START_PRICE_SELL"), Digits) )open( OP_SELL, l, 0, 0, comment);
   }
}

int order_total_by_comment ( string comment, int type )
{
   int i, result = 0;

   for( i = OrdersTotal() - 1; i >= 0; i -- )       
   {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if( OrderMagicNumber() == ID && OrderType() == type && StringFind(OrderComment(), comment) >= 0 )result ++;
   }
   
   return(result);   
}

void delete_glob ( string c )
{
   int i;
   for ( i = GlobalVariablesTotal()-1; i>= 0 ; i-- )if ( StringFind(GlobalVariableName(i),c) >= 0 )GlobalVariableDel(GlobalVariableName(i));
}

double get_lot()
{
   if ( Lot == 0 )return( NormalizeDouble((AccountBalance() * (MM/100)) / MarketInfo(Symbol(),MODE_MARGINREQUIRED), 2));
   else return (NormalizeDouble(Lot,2));
}

bool MarketExecution = true;
int number_try = 20;
int time_period_try = 1;
int open( int type, double l, double sl = 0, double tp = 0, string comm = "")
{
   bool res, block = false;
   int isOpened = 0, try = 0;
   double s,t, bezub_point, spread = MarketInfo(Symbol(),MODE_STOPLEVEL) * MarketInfo(Symbol(),MODE_POINT);
   color color_order;
   
   int i,ticket = 0, down_orders_b, up_orders_s;
   double l_b = 0, l_s = 0, l_op_pr_b = 0, l_op_pr_s = 9999999, h_op_pr_b = 9999999, h_op_pr_s = 0;
   datetime time = 0;
   
   for( i = OrdersTotal() - 1; i >= 0; i -- )
   {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);  
      if ( OrderMagicNumber() != ID )continue;
      
      if( OrderOpenTime() > time )
      {
         ticket = OrderTicket();
         time = OrderOpenTime();
      }
      
      if ( OrderType() == OP_BUY )
      {
         l_b += OrderLots();
         if ( OrderOpenPrice() > l_op_pr_b )l_op_pr_b = OrderOpenPrice();
         if ( OrderOpenPrice() < h_op_pr_b )h_op_pr_b = OrderOpenPrice();
      }
      if ( OrderType() == OP_SELL )
      {
         l_s += OrderLots();
         if ( OrderOpenPrice() < l_op_pr_s )l_op_pr_s = OrderOpenPrice();
         if ( OrderOpenPrice() > h_op_pr_s )h_op_pr_s = OrderOpenPrice();
      }
   }
   
   if ( ticket != 0 && l_b != l_s && l_b > 0 && l_s > 0 )
   {
      bezub_point = get_bezubitok_point();
      OrderSelect(ticket, SELECT_BY_TICKET);
      if ( (OrderOpenPrice() > bezub_point && (Ask + spread) < OrderOpenPrice()) || (OrderOpenPrice() < bezub_point && (Bid - spread) > OrderOpenPrice()) )block = true;
   }
   
   if ( block && l_op_pr_b != 0 && l_op_pr_s != 0 && h_op_pr_b != 0 && h_op_pr_s != 0 )
   {      
      if ( ((bezub_point > l_op_pr_b) || (bezub_point < l_op_pr_s)) && ((Ask > (h_op_pr_s + spread) && Bid < (h_op_pr_b - spread)) || (Bid < (h_op_pr_s - spread) && Ask > (h_op_pr_b + spread))) )block = false;
   }
   
   if ( Use_block && block )
   {
      
      Print("Îðäåð ïî öåíå " + DoubleToStr(Bid,Digits) + " íå îòêðûò, ïðî ïðè÷èíå áëîêèðîâêè");
      return(0);
   }
   
   while ( IsTradeContextBusy() )Sleep(500);
   RefreshRates();
   if ( sl != 0 && sl < MarketInfo(Symbol(),MODE_STOPLEVEL) )sl = MarketInfo(Symbol(),MODE_STOPLEVEL);
   if ( tp != 0 && tp < MarketInfo(Symbol(),MODE_STOPLEVEL) )tp = MarketInfo(Symbol(),MODE_STOPLEVEL);
   if ( type == 0 )
   {
      if ( tp == 0 )t = 0;
      if ( tp != 0 )t = Ask + tp * Point;
      if ( sl == 0 )s = 0;
      if ( sl != 0 )s = Ask - sl * Point;
   }
   if ( type == 1 )
   {
      if ( tp == 0 )t = 0;
      if ( tp != 0 )t = Bid - tp * Point;
      if ( sl == 0 )s = 0;
      if ( sl != 0 )s = Bid + sl * Point;
   }
   
   s = NormalizeDouble(s,Digits);
   t = NormalizeDouble(t,Digits);
   
   if ( MarketExecution )
   {
      if(type == 0)isOpened = OrderSend(Symbol(),type,check_lot(l),NormalizeDouble(Ask,Digits),100,0,0,comm,ID);
      if(type == 1)isOpened = OrderSend(Symbol(),type,check_lot(l),NormalizeDouble(Bid,Digits),100,0,0,comm,ID);
   }
   else
   {
      if(type == 0)isOpened = OrderSend(Symbol(),type,check_lot(l),NormalizeDouble(Ask,Digits),100,s,t,comm,ID);
      if(type == 1)isOpened = OrderSend(Symbol(),type,check_lot(l),NormalizeDouble(Bid,Digits),100,s,t,comm,ID);
   }
   
   while( isOpened < 0 )
   {
      Sleep(time_period_try * 1000);
      while ( IsTradeContextBusy() )Sleep(500);
      RefreshRates();
      if ( type == 0 )
      {
         if ( tp == 0 )t = 0;
         if ( tp != 0 )t = Ask + tp * Point;
         if ( sl == 0 )s = 0;
         if ( sl != 0 )s = Ask - sl * Point;
      }
      if ( type == 1 )
      {
         if ( tp == 0 )t = 0;
         if ( tp != 0 )t = Bid - tp * Point;
         if ( sl == 0 )s = 0;
         if ( sl != 0 )s = Bid + sl * Point;
      }
      s = NormalizeDouble(s,Digits);
      t = NormalizeDouble(t,Digits);
      try++;
      if ( MarketExecution )
      {
         if(type == 0)isOpened = OrderSend(Symbol(),type,check_lot(l),NormalizeDouble(Ask,Digits),100,0,0,comm,ID);
         if(type == 1)isOpened = OrderSend(Symbol(),type,check_lot(l),NormalizeDouble(Bid,Digits),100,0,0,comm,ID);
      }
      else
      {
         if(type == 0)isOpened = OrderSend(Symbol(),type,check_lot(l),NormalizeDouble(Ask,Digits),100,s,t,comm,ID);
         if(type == 1)isOpened = OrderSend(Symbol(),type,check_lot(l),NormalizeDouble(Bid,Digits),100,s,t,comm,ID);
      }
      if(try > number_try || isOpened >= 0) break;
   }
   
   if( isOpened < 0 ) Print("Îðäåð íå îòêðûò, îøèáêà :", GetLastError());
   else if ( MarketExecution )
   {
      OrderSelect(isOpened,SELECT_BY_TICKET);
      if ( OrderType() == 0 )color_order = Blue;
      if ( OrderType() == 1 )color_order = Red;
      try = 0;
      
      if ( NormalizeDouble(OrderStopLoss(),Digits) != NormalizeDouble(s,Digits) || NormalizeDouble(OrderTakeProfit(),Digits) != NormalizeDouble(t,Digits) )
      {
         while ( IsTradeContextBusy() )Sleep(500);
         res = OrderModify(OrderTicket(),OrderOpenPrice(),s,t,0,color_order);
         while ( !res )
         {
            while ( IsTradeContextBusy() )Sleep(500);
            res = OrderModify(OrderTicket(),OrderOpenPrice(),s,t,0,color_order);
            Sleep(time_period_try * 1000);
            try ++;
            Print("Îðäåð ñ òèïîì " + DoubleToStr(OrderType(),0) + " ïî öåíå " + DoubleToStr(OrderOpenPrice(),Digits) + " íå ìîäèôèöèðîâàí ïî OPEN, ïîïûòêà " + DoubleToStr(try,0));
            if ( try > number_try || res )break;
         }
      }
   }
   return (isOpened);
}

int order_total( int type_1 = -1, int type_2 = -1 )
{
   int i;
   int kol = 0;
   for( i = OrdersTotal() - 1; i >= 0; i -- )       
   {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if( OrderMagicNumber() == ID && (OrderType() == type_1 || OrderType() == type_2) )kol++;
      if( OrderMagicNumber() == ID && type_1 == -1 && type_2 == -1 )kol++;
   }
   return(kol);   
}

bool close(int ticket)
{
   bool isClosed = false;
   int try = 0;
   while ( IsTradeContextBusy() )Sleep(500);
   RefreshRates();
   isClosed = OrderSelect(ticket,SELECT_BY_TICKET);
   if ( !isClosed )return(false);
   if ( OrderType() < 2 )isClosed = OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(OrderClosePrice(),MarketInfo(OrderSymbol(),MODE_DIGITS)), 100, 0);
   if ( OrderType() > 1 )isClosed = OrderDelete(OrderTicket());
   while(!isClosed)
   {
      Sleep(time_period_try * 1000);
      while ( IsTradeContextBusy() )Sleep(500);
      RefreshRates();
      try ++;
      if ( OrderType() < 2 )isClosed = OrderClose(OrderTicket(), OrderLots(),NormalizeDouble(OrderClosePrice(),MarketInfo(OrderSymbol(),MODE_DIGITS)), 100, 0);
      if ( OrderType() > 1 )isClosed = OrderDelete(OrderTicket());
      if(try > number_try || isClosed) break;
   }
   if ( !isClosed ) Print("Îðäåð ", ticket, " íå çàêðûò, îøèáêà:", GetLastError());
   return ( isClosed );
}

void close_all( int type_1 = -1,int type_2 = -1, int bars = 0 )
{
   int i;
   bool flag = false;
   if ( bars != 0 )flag = true;
   for( i = OrdersTotal() - 1; i >= 0; i-- )
   {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if( OrderMagicNumber() == ID && ((OrderType() == type_1 || OrderType() == type_2) || (type_1 == -1 && type_2 == -1)) && !flag )close(OrderTicket());   
      if( OrderMagicNumber() == ID && ((OrderType() == type_1 || OrderType() == type_2) || (type_1 == -1 && type_2 == -1)) && flag && (iBarShift(OrderSymbol(),Period(),OrderOpenTime()) - bars) >= 0 )close(OrderTicket());
      
   }   
}

double check_lot(double &lo)
{
   double l = MarketInfo(Symbol(),MODE_LOTSTEP);
   int ok = 0;
   while ( l < 1 ){l*=10;ok++;}
   if( lo < MarketInfo(Symbol(),MODE_MINLOT) )lo = MarketInfo(Symbol(),MODE_MINLOT);
   if( lo > MarketInfo(Symbol(),MODE_MAXLOT) )lo = MarketInfo(Symbol(),MODE_MAXLOT);
   return(NormalizeDouble(lo,ok));
}

double get_bezubitok_point ( int type = -1 )
{
   int i,k1;
   double K1 = 0,K2 = 0,SUMM_LOT = 0, res, profit;
   string name;

   profit = 0;

   for( i = OrdersTotal() - 1; i >= 0; i -- )
   {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if ( OrderMagicNumber() != ID || OrderType() > 1 )continue;
      {
         if ( type != -1 && OrderType() != type )continue;
         if ( OrderType() == 0 )k1 = 1;
         if ( OrderType() == 1 )k1 = -1;
         SUMM_LOT += ( k1 * OrderLots());
         K1 += ( k1 * OrderOpenPrice() * OrderLots());
         K2 += (MarketInfo(OrderSymbol(),MODE_SPREAD) * MarketInfo(OrderSymbol(),MODE_POINT) * OrderLots());
      }
   }
   
   if ( NormalizeDouble(K1,2) != 0 )
   {
      res = NormalizeDouble((K1 + K2)/SUMM_LOT,MarketInfo(Symbol(),MODE_DIGITS));
      if ( res < (K1 + K2)/SUMM_LOT )res += MarketInfo(Symbol(),MODE_POINT);
   }   
   
   return (res);
}

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