Lógica de aprendizaje - página 11

 
Bien, ¿cuál es la opción correcta que propones, con todos los errores controlados?
 
gip:

Yo, en cambio, lo saqué de un montón de código:

Una pregunta: ¿por qué los resbalones? Y el 10 (número de repeticiones) debería trasladarse a las variables globales.

lea:

¿Qué te parece este?

Puntuación.
 

Hay diferentes resbalones. Los que están en el bucle son... Cuando tienes a una chica sentada, tienes que repetirlo diez veces y esperar a que se pinte los labios. Cuando una chica está sentada, tiene que repetirlo diez veces y esperar hasta que se maquille. Y a veces el servidor se ralentiza. Todo esto es para la "DC real".

Se desliza después de PlaySound, para que otros sonidos no le hagan sombra, PlaySound interrumpe el sonido del anterior.

Lasvariables globales están inteligentemente ponderadas por el terminal. Y no es necesario, el algoritmo externo debería funcionar normalmente con las órdenes que no están cerradas.

 
gip:

Las variables globales están inteligentemente ponderadas por el terminal.

No es terminal. Programas. Es decir, en el ámbito global.

Ah, se me olvidaba -- RefreshRates al principio del bucle después de capturar el contexto, entonces no tendrás que jugar con Ask y Bid. Y también otros valores.

 

RefreshRates no está ahí para nada más. Así es como intenté tratar las situaciones en las que OrderClose devuelve false, pero la orden realmente se cierra. Después de la pausa y RefreshRates viene OrderSelect(). No sé cómo está ahí, pero parece que funciona.

Preguntar y ofertar son una auténtica maravilla, a veces hay que normalizarlas :)

---

Lo de las diez repeticiones y para qué en las variables globales, no lo entiendo. Son diez, con margen suficiente. ¿Es externo?

 

¿Y si el pedido se cierra en un minuto? No se puede superar eso con un código. Se trata de un fallo de comunicación del terminal.

gip:

No entiendo lo de las diez repeticiones y para qué en las variables globales.

Hay que evitar los números mágicos y es fácil de configurar.
 

Simplemente reduce el número de fallos por un factor de dos. Por supuesto, no todas las situaciones se manejan exactamente en este bloque. Si una orden se cierra durante un minuto, el programa tiene que esperar todo ese tiempo. Estas esperas, aunque se corten, retrasan el bucle y al final todo funciona como debería.

---

> Para evitar los números mágicos y el ajuste fácil.

¿Ajustar este diez? Sí es con tanto margen, que es más fácil cambiar el DC :) Y por el bien de megalot, si es así tuvo que, y puede estar en el código para corregir.

 

Hay un consejero. Tenemos que hacer que parezca decente.

//+------------------------------------------------------------------+
// открытие доп ордеров
// OrderTip - тип ордер который нужно открыть, imagic - маджик 
// с которым надо октрыть, exp - экспонент (на что умножать объем), 
//+------------------------------------------------------------------+
bool OpenDopOrder(int OrderTip, int imagic, double exp){
double iLots = NormalizeDouble((FindLastLot(imagic) * exp), Digits);

int ticket = 0;
int i = 0;
int KolvoPopitok = 10;
int err = 0;
if (OrderTip==OP_BUY) {
   color CL_Close=OrderColorBuy;
   string cmd = "BUY";
   //double Price = NormalizeDouble(Ask, Digits); 
   double Price = Ask; 
   } else {
   CL_Close=OrderColorSell;
   cmd = "SELL";
   //Price=NormalizeDouble(Bid, Digits);
   Price=Bid;
   }
   
         for (i = 0; i < KolvoPopitok; i++) {
         ticket = OrderSend(Symbol(), OrderTip, iLots, Price, M1_Slippage, 0, 0, M1_EaComment, imagic, 0, CL_Close);
         err = GetLastError();
         if(err>0){
         Comment("Ошибка открытия дополнительного ордера " + ErrorDescription(err));
         timeprev = Time[1];
         return(false);
         }
         if (err == 0){
         Comment("Дополнительный ордер " + cmd+" # " + ticket+ " создан"); 
         return(true);
         break;}
         if (!(err == 4 || err == 137 || err == 146 || err == 136)) break;
         Sleep(3000);
         }
}
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
// функция определения тренда
//+------------------------------------------------------------------+
int SignalRegr(){

   int x=0;
   
   string indicator_name = "i-Regr";
   
   double ma = iCustom(NULL, Mas_TF_Regr[TF_Regr], indicator_name, Regr.degree1, Regr.kstd1, SPer, Regr.shift1, 0, 0);
   double ma1 = iCustom(NULL, Mas_TF_Regr[TF_Regr], indicator_name, Regr.degree1, Regr.kstd1, SPer, Regr.shift1, 0, 1);   
   
   if (kanal==1){
   double m_up = iCustom(NULL, Mas_TF_Regr[TF_Regr], indicator_name, Regr.degree1, Regr.kstd1, SPer, Regr.shift1, 1, 0);
   double m_d = iCustom(NULL, Mas_TF_Regr[TF_Regr], indicator_name, Regr.degree1, Regr.kstd1, SPer, Regr.shift1, 2, 0);      
   if (ma>ma1 && Bid>m_d) x=1;
   if (ma<ma1  && Ask<m_up) x=-1;
   }
   
   if (kanal==0){
   if (ma>ma1) x=1;
   if (ma<ma1) x=-1;
   }   
   
   if (kanal==2){
   m_up = iCustom(NULL, Mas_TF_Regr[TF_Regr], indicator_name, Regr.degree1, Regr.kstd1, SPer, Regr.shift1, 1, 0);
   m_d = iCustom(NULL, Mas_TF_Regr[TF_Regr], indicator_name,    Regr.degree1, Regr.kstd1, SPer, Regr.shift1, 2, 0);      
   if (ma>ma1 && Bid>m_d && Ask<m_up && forSignalRegr()==1) x=1;
   if (ma<ma1  && Ask<m_up && Bid>m_d && forSignalRegr()==-1) x=-1;
   }   
return(x);
}
//+------------------------------------------------------------------+
int forSignalRegr(){
int x=0;
   string indicator_name = "i-Regr";
   double ma = iCustom(NULL, Mas_TF_Regr[TFSmall], indicator_name, Regr.degree1mall, Regr.kstd1mall, SPermall, Regr.shift1mall, 0, 0);
   double ma1 = iCustom(NULL, Mas_TF_Regr[TFSmall], indicator_name, Regr.degree1mall, Regr.kstd1mall, SPermall, Regr.shift1mall, 0, 1); 
   double m_up = iCustom(NULL, Mas_TF_Regr[TFSmall], indicator_name, Regr.degree1mall, Regr.kstd1mall, SPermall, Regr.shift1mall, 1, 0);
   double m_d = iCustom(NULL, Mas_TF_Regr[TFSmall], indicator_name, Regr.degree1mall, Regr.kstd1mall, SPermall, Regr.shift1mall, 2, 0);      
   if (ma>ma1 && Bid>m_d && Ask<m_up ) x=1;
   if (ma<ma1  && Ask<m_up && Bid>m_d ) x=-1;
return(x);
}

//+------------------------------------------------------------------+
//динамичный лот
//+------------------------------------------------------------------+
double GetLot(double iLots, double iRisk){
   double Lots_New;
   string Symb   =Symbol();                    
   double One_Lot=NormalizeDouble(MarketInfo(Symb,MODE_MARGINREQUIRED), Digits);
   double Min_Lot=NormalizeDouble(MarketInfo(Symb,MODE_MINLOT), Digits);
   double Step   =NormalizeDouble(MarketInfo(Symb,MODE_LOTSTEP), Digits);
   double Free   =AccountFreeMargin();        

   if (iLots>0){                                       
   double Money=iLots*One_Lot;              
   if(Money<=AccountFreeMargin())         
   Lots_New=iLots;                      
   else                                   
   Lots_New=MathFloor(Free/One_Lot/Step)*Step;
   }

   else{                                       
   if (iRisk > 100)                     
   iRisk=100;                       
   if (iRisk==0)                       
   Lots_New=Min_Lot;                   
   else                                   
   Lots_New=MathFloor(Free*iRisk/100/One_Lot/Step)*Step;
   }

   if (Lots_New < Min_Lot)                    
   Lots_New=Min_Lot;                       
   if (Lots_New*One_Lot > AccountFreeMargin()){                                         
}

return(Lots_New);                             
}

//+----------------------------------------------------------------------------+
//|функция модификация ордера                                                  |
//|PriceOpen - цена открытия позиции, установки ордера                         |
//|SL - ценовой уровень стопа                                                  |
//|TP - ценовой уровень тейка                                                  |
//+----------------------------------------------------------------------------+
bool ModifyOrder(double PriceOpen=-1, double SL=0, double TP=0) {
  string Symb=Symbol();
  bool   Rez_Modify;
  int NumberOfTry=3;
  int PauseAfterError=10;

  int err, kolvo;

  if (PriceOpen<=0) PriceOpen=OrderOpenPrice();
  if (SL<0) SL=OrderStopLoss();
  if (TP<0) TP=OrderTakeProfit();
  
  PriceOpen=RoundToTickSize(PriceOpen);
  SL=RoundToTickSize(SL);
  TP=RoundToTickSize(TP);

  if (PriceOpen!=OrderOpenPrice() || SL!=OrderStopLoss() || TP!=OrderTakeProfit()) {
    for (kolvo=1; kolvo<=NumberOfTry; kolvo++) {
      if (!IsTesting() && (!IsExpertEnabled() || IsStopped())) break;
      while (!IsTradeAllowed()) Sleep(5000);
      RefreshRates();
      if (NormalizeDouble(TP,Digits)!=OrderTakeProfit()) 
      Rez_Modify=OrderModify(OrderTicket(), NormalizeDouble(PriceOpen,Digits), NormalizeDouble(SL,Digits), NormalizeDouble(TP,Digits), 0, OrderColorModify);
      if (Rez_Modify) {
      Comment("Ордер # " + OrderTicket()+ " успешно модифицирован : TP "+DoubleToStr(TP, Digits));
      break;
      } else {
        err=GetLastError();
        Comment("Ошибка модификации ордера # " + OrderTicket()+ " : " +ErrorDescription(err)+" ("+err+"), попытка: "+kolvo);
        timeprev = Time[1];
        Sleep(1000*PauseAfterError);
      }
    }
  }
  return(Rez_Modify);
}
//+----------------------------------------------------------------------------+
//|для функция модификация ордера                                              |
//+----------------------------------------------------------------------------+
double RoundToTickSize(double price){
return(NormalizeDouble(MathRound(price/MarketInfo(Symbol(), 
MODE_TICKSIZE))*MarketInfo(Symbol(), 
MODE_TICKSIZE), MarketInfo(Symbol(), MODE_DIGITS)));
}
//+----------------------------------------------------------------------------+
Estas son las funciones del mismo. Hay una manera de hacer que parezca decente
Archivos adjuntos:
prosto_m.mq4  21 kb
 

He añadido dos funciones para simplificar el trabajo

Uno comprueba el tipo de orden y el segundo busca la última orden abierta

//+------------------------------------------------------------------+
//|                                     Функция контроля типа ордера |
//|                                Copyright © 2010, Victor Nicolaev |
//|                                            e-mail: vinin@mail.ru |
//| isOrderType(type);                                               |
//+------------------------------------------------------------------+
//| Параметер - проверяемый тип                                      |
//+------------------------------------------------------------------+
bool isOrderType(int type){
   int gOrderType[]={OP_BUY, OP_SELL, OP_BUYLIMIT, OP_SELLLIMIT, OP_BUYSTOP, OP_SELLSTOP};
   bool Res=false;
   for (int i=0;i<ArraySize(gOrderType);i++) {
      if (gOrderType[i]==type){
         Res=true;
         break;
      }
   }
   return(Res);
}

//+------------------------------------------------------------------+
//|                       Функция поиска последнего открытого ордера |
//|                               (поиск только по открытым ордерам) |
//|                                          Возвращает номер тикета |
//|                                Copyright © 2010, Victor Nicolaev |
//|                                            e-mail: vinin@mail.ru |
//| FindLastOpenTime(tip, imagic);                                   |
//+------------------------------------------------------------------+
//| Параметер - тип позиции и магик                                  |
//+------------------------------------------------------------------+

int FindLastOpenTime(int tip, int imagic) {
   int Res=-1;
   int lOrderOpenTime=-1;
   
   for (int i=OrdersTotal()-1; i>=0; i--) {
      if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))   continue;
      if (OrderSymbol() != Symbol())                     continue;
      if (OrderMagicNumber() != imagic)                  continue;
      if (!(tip==-1 || isOrderType(tip)))                continue;

      if (lOrderOpenTime==-1) { 
         lOrderOpenTime=OrderOpenTime(); 
         Res=OrderTicket();
      } else if (lOrderOpenTime<OrderOpenTime()) {
         lOrderOpenTime=OrderOpenTime(); 
         Res=OrderTicket();
      }
   }
   return (Res);
}
//+------------------------------------------------------------------+
 

Parte de las funciones del autor reelaboradas


//+------------------------------------------------------------------+
//функция подсчета открытых ордеров по типу и маджику
//tip - тип ордера БАЙ или СЕЛЛ, imagic - маджик номер
//возвращает кол-во открытых оредров
//+------------------------------------------------------------------+
int CountTrades(int tip, int imagic) {
   int count = 0;
   for (int trade = OrdersTotal() - 1; trade >= 0; trade--) {
      if (!OrderSelect(trade, SELECT_BY_POS, MODE_TRADES))  continue;
      if (OrderSymbol() != Symbol())                        continue;
      if (OrderMagicNumber() != imagic)                     continue;
      if (!(tip==-1 || isOrderType(tip)))                   continue;
      
      count ++;

   }
   return (count);
}
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//поиск последней цены
//tip - тип ордера БАЙ или СЕЛЛ, imagic - маджик номер
//возвращает цену открытия последнего открытого ордера
//+------------------------------------------------------------------+
double FindLastPrice(int tip, int imagic) {

   int ticket=FindLastOpenTime(tip, imagic);
   if (ticket==-1) return(-1);
   OrderSelect(ticket, SELECT_BY_TICKET);
   double order_price = OrderOpenPrice();

   return (order_price);
}
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//поиск последнего лота
//imagic - маджик номер
//возвращает объем последнего открытого ордера
//+------------------------------------------------------------------+
double FindLastLot(int imagic) {

   int ticket=FindLastOpenTime(-1, imagic);
   if (ticket==-1) return(-1);
   OrderSelect(ticket, SELECT_BY_TICKET);
   double order_lot=OrderLots();

   return (order_lot);
}
//+------------------------------------------------------------------+