Прошу помощи у профов написал код для трейлинга в MQL4 но почему-то на Buy работает а на Sell нет - страница 2

 
Vitaliy29:
Спасибо, попробую!
Попробовал, ничего не изменилось, по прежнему modify происходит только на buy, грешил на отступы брокера но ведь они на sell и buy одинаковы, да и в спецификации к инструменту отступы =0 а я даю 10 и все равно не происходит modify на sell, ордера по sell профиту в роботе работают но опять повторюсь без modify!
 
Vitaliy29:
Попробовал, ничего не изменилось, по прежнему modify происходит только на buy, грешил на отступы брокера но ведь они на sell и buy одинаковы, да и в спецификации к инструменту отступы =0 а я даю 10 и все равно не происходит modify на sell, ордера по sell профиту в роботе работают но опять повторюсь без modify!

Что вам мешает использовать то что уже написано,

Пример;

input double TrailingStop  =10;//в шапке советника
  int total=OrdersTotal();
   for(int cnt=0;cnt<total;cnt++)
     {
      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
         continue;
      if( OrderSymbol()==Symbol())  // check for symbol
        {
         //--- long position is opened
         if(OrderType()==OP_BUY)
           {
            //--- check for trailing stop
            if(TrailingStop>0)
              {
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     //--- modify order and exit
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 }
              }
           }
         else
           {
          
           //--- check for trailing stop
            if(TrailingStop>0)
              {
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     //--- modify order and exit
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 }
              }
           }
        }
     }


 .......MACD Sample

 
Alekseu Fedotov:

Что вам мешает использовать то что уже написано,

Пример;

input double TrailingStop  =10;//в шапке советника
  int total=OrdersTotal();
   for(int cnt=0;cnt<total;cnt++)
     {
      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
         continue;
      if( OrderSymbol()==Symbol())  // check for symbol
        {
         //--- long position is opened
         if(OrderType()==OP_BUY)
           {
            //--- check for trailing stop
            if(TrailingStop>0)
              {
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     //--- modify order and exit
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 }
              }
           }
         else
           {
          
           //--- check for trailing stop
            if(TrailingStop>0)
              {
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     //--- modify order and exit
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 }
              }
           }
        }
     }


 .......MACD Sample

Alekseu Fedotov:

Что вам мешает использовать то что уже написано,

Пример;

input double TrailingStop  =10;//в шапке советника
  int total=OrdersTotal();
   for(int cnt=0;cnt<total;cnt++)
     {
      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
         continue;
      if( OrderSymbol()==Symbol())  // check for symbol
        {
         //--- long position is opened
         if(OrderType()==OP_BUY)
           {
            //--- check for trailing stop
            if(TrailingStop>0)
              {
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     //--- modify order and exit
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 }
              }
           }
         else
           {
          
           //--- check for trailing stop
            if(TrailingStop>0)
              {
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     //--- modify order and exit
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 }
              }
           }
        }
     }


 .......MACD Sample

Alekseu Fedotov:

Что вам мешает использовать то что уже написано,

Пример;

input double TrailingStop  =10;//в шапке советника
  int total=OrdersTotal();
   for(int cnt=0;cnt<total;cnt++)
     {
      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
         continue;
      if( OrderSymbol()==Symbol())  // check for symbol
        {
         //--- long position is opened
         if(OrderType()==OP_BUY)
           {
            //--- check for trailing stop
            if(TrailingStop>0)
              {
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     //--- modify order and exit
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 }
              }
           }
         else
           {
          
           //--- check for trailing stop
            if(TrailingStop>0)
              {
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     //--- modify order and exit
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 }
              }
           }
        }
     }


 .......MACD Sample 

не совсем то что нужно, прийдется переделывать да и мой код гораздо проще нужно понять в чем ошибка!!! 

 
TrallStopLoss_Ask==0 - TrallStopLoss_Ask в первоначальный момент равно 0? Попробуйте CompareDoubles(TrallStopLoss_Ask,0) использовать.
 
Vitaliy29:

Прошу помощи у профов написал код для трейлинга в MQL4 но почему-то на Buy работает а на Sell нет

подскажите в чем прикол а то я уже голову сломал!!! 

Выводите ваши переменные на Print() или в Comment(). Увидите, какие значения они принимают, найдёте ошибки.

Так будет всё тралить. Организацию обработки ошибок посмотрите в предложенных выше примерах.


if (OrdersTotal()>=1&&TrailingStop==1)//определение количества открытых ордеров и разрешения на трейлинг
{
for(int i=OrdersTotal()-1;i>=0;i--)
    
{Orders=OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true;//выбор ордера
Ticket_ordera_TRSTOP=OrderTicket();
if ((OrderType()==OP_SELL)&&(Ask<(OrderOpenPrice()-NormalizeDouble(TR_otstup*Point,5))&&((TrallStopLoss_Ask>(Ask+NormalizeDouble(Point*TR_otstup,5)))||(TrallStopLoss_Ask==0))))
{
TrallStopLoss_Ask=Ask+Point*TR_otstup;
Orders=OrderModify(Ticket_ordera_TRSTOP,OrderOpenPrice(),(Ask+NormalizeDouble(Point*TR_otstup,5)),OrderTakeProfit(),0,clrDarkViolet);
//Print ("ошибка трейлинга- "+GetLastError());
}
if ((OrderType()==OP_BUY)&&(Bid>(OrderOpenPrice()+NormalizeDouble(TR_otstup*Point,5)))&&(TrallStopLoss_Bid<(Bid-NormalizeDouble(Point*TR_otstup,5))))
{
TrallStopLoss_Bid=Bid-Point*TR_otstup;
Orders=OrderModify(Ticket_ordera_TRSTOP,OrderOpenPrice(),(Bid-NormalizeDouble(Point*TR_otstup,5)),OrderTakeProfit(),0,clrDarkViolet);
//Print ("ошибка трейлинга- "+GetLastError());
}
}
}
 
sile:

Выводите ваши переменные на Print() или в Comment(). Увидите, какие значения они принимают, найдёте ошибки.

Так будет всё тралить. Организацию обработки ошибок посмотрите в предложенных выше примерах.


if (OrdersTotal()>=1&&TrailingStop==1)//определение количества открытых ордеров и разрешения на трейлинг
{
for(int i=OrdersTotal()-1;i>=0;i--)
    
{Orders=OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true;//выбор ордера
Ticket_ordera_TRSTOP=OrderTicket();
if ((OrderType()==OP_SELL)&&(Ask<(OrderOpenPrice()-NormalizeDouble(TR_otstup*Point,5))&&((TrallStopLoss_Ask>(Ask+NormalizeDouble(Point*TR_otstup,5)))||(TrallStopLoss_Ask==0))))
{
TrallStopLoss_Ask=Ask+Point*TR_otstup;
Orders=OrderModify(Ticket_ordera_TRSTOP,OrderOpenPrice(),(Ask+NormalizeDouble(Point*TR_otstup,5)),OrderTakeProfit(),0,clrDarkViolet);
//Print ("ошибка трейлинга- "+GetLastError());
}
if ((OrderType()==OP_BUY)&&(Bid>(OrderOpenPrice()+NormalizeDouble(TR_otstup*Point,5)))&&(TrallStopLoss_Bid<(Bid-NormalizeDouble(Point*TR_otstup,5))))
{
TrallStopLoss_Bid=Bid-Point*TR_otstup;
Orders=OrderModify(Ticket_ordera_TRSTOP,OrderOpenPrice(),(Bid-NormalizeDouble(Point*TR_otstup,5)),OrderTakeProfit(),0,clrDarkViolet);
//Print ("ошибка трейлинга- "+GetLastError());
}
}
}
Большое спасибо, заработало!!! Чувствовал что ошибка где-то на поверхности, но в упор не видел!!!
 

Всем большое спасибо проблема решена тема закрыта!!!

Отдельное спасибо  sile: за решение проблемы!!!

Причина обращения: