TrailStop барахлит

 

что тут не так кто нибудь знает?

void TrailingStopBuy(double Ask) 
  {     
      // set the stop loss to 150 points
      double SL=NormalizeDouble(Ask-150*_Point,_Digits);
 
      // Go through all positions
      for(int i=PositionsTotal()-1; i>=0; i--)  
      {
                  string symbol=PositionGetSymbol(i); // get the symbol of the position     
                  
        if (_Symbol==symbol) // if currency pair is equal       
         // if we have a sell position
         if (PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_BUY)   
         {
            // get the ticket number
            ulong PositionTicket=PositionGetInteger(POSITION_TICKET); 
            
            // calculate the current stop loss
            double CurrentStopLoss=PositionGetDouble(POSITION_SL); 
            
            // if current stop loss is more than 150 points
            if (CurrentStopLoss>SL) 
            {
            // move the stop loss
            trade.PositionModify(PositionTicket,(CurrentStopLoss+10*_Point),0); 
            }   
         } // End if loop
           
      } // End for loop
  } // End            
void TrailingStopSell(double Bid) 
  {     
      // set the stop loss to 150 points
      double SL=NormalizeDouble(Bid+150*_Point,_Digits);
 
      // Go through all positions
      for(int i=PositionsTotal()-1; i>=0; i--)  
      {
                  string symbol=PositionGetSymbol(i); // get the symbol of the position     
                  
        if (_Symbol==symbol) // if currency pair is equal       
        
         // if we have a sell position
         if (PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL)   
         {
            // get the ticket number
            ulong PositionTicket=PositionGetInteger(POSITION_TICKET); 
            
            // calculate the current stop loss
            double CurrentStopLoss=PositionGetDouble(POSITION_SL); 
            
            // if current stop loss is more than 150 points
            if (CurrentStopLoss>SL) 
            {
            // move the stop loss
            trade.PositionModify(PositionTicket,(CurrentStopLoss-10*_Point),0); 
            }   
         } // End if loop
           
      } // End for loop
  } // End 
 

Причесал код к более красивому виду.

Замечание: забудьте и выкиньте слово "ордер" из своей головы. Оперировать нужно словами "позиция" или "сделка" ("сделка" может использоваться лицами достигшими совершеннолетия).

Итак:

ENUM_POSITION_TYPE

Identifier

Description

POSITION_TYPE_BUY

Buy

POSITION_TYPE_SELL

Sell


Исходя их этого:

//+------------------------------------------------------------------+
//| TrailingStop Buy                                                 |
//+------------------------------------------------------------------+
void TrailingStopBuy(double Ask)
  {
   double SL=NormalizeDouble(Ask-150*_Point,_Digits);                   // set the stop loss to 150 points
   for(int i=PositionsTotal()-1; i>=0; i--)                             // Go through all positions
     {
      string symbol=PositionGetSymbol(i);                               // get the symbol of the position
      if(_Symbol==symbol)                                               // if currency pair is equal
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)       // if we have a sell position
           {
            ulong PositionTicket=PositionGetInteger(POSITION_TICKET);   // get the ticket number
            double CurrentStopLoss=PositionGetDouble(POSITION_SL);      // calculate the current stop loss
            if(CurrentStopLoss>SL)                                      // if current stop loss is more than 150 points
              {
               trade.PositionModify(PositionTicket,(CurrentStopLoss+10*_Point),0); // move the stop loss
              }
           } // End if loop
     } // End for loop
  } // End
//+------------------------------------------------------------------+
//|  TrailingStop Sell                                               |
//+------------------------------------------------------------------+
void TrailingStopSell(double Bid)
  {
   double SL=NormalizeDouble(Bid+150*_Point,_Digits);                   // set the stop loss to 150 points
   for(int i=PositionsTotal()-1; i>=0; i--)                             // Go through all positions
     {
      string symbol=PositionGetSymbol(i);                               // get the symbol of the position
      if(_Symbol==symbol)                                               // if currency pair is equal
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)      // if we have a sell position
           {
            ulong PositionTicket=PositionGetInteger(POSITION_TICKET);   // get the ticket number
            double CurrentStopLoss=PositionGetDouble(POSITION_SL);      // calculate the current stop loss
            if(CurrentStopLoss>SL)                                      // if current stop loss is more than 150 points
              {
               trade.PositionModify(PositionTicket,(CurrentStopLoss-10*_Point),0);// move the stop loss
              }
           } // End if loop
     } // End for loop
  } // End


дальше не проверял.

 

А теперь с исправлениями:

//+------------------------------------------------------------------+
//| TrailingStop Buy                                                 |
//+------------------------------------------------------------------+
void TrailingStopBuy(double Ask)
  {
   double CalculatedStopLoss=NormalizeDouble(Ask-150*_Point,_Digits);   // set the stop loss to 150 points
   for(int i=PositionsTotal()-1; i>=0; i--)                             // Go through all positions
     {
      string symbol=PositionGetSymbol(i);                               // get the symbol of the position
      if(_Symbol==symbol)                                               // if currency pair is equal
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)       // if we have a sell position
           {
            ulong PositionTicket=PositionGetInteger(POSITION_TICKET);   // get the ticket number
            double CurrentStopLoss=PositionGetDouble(POSITION_SL);      // calculate the current stop loss
            if(CalculatedStopLoss>CurrentStopLoss)                                      // if current stop loss is more than 150 points
              {
               trade.PositionModify(PositionTicket,(CalculatedStopLoss+10*_Point),0); // move the stop loss
              }
           } // End if loop
     } // End for loop
  } // End
//+------------------------------------------------------------------+
//|  TrailingStop Sell                                               |
//+------------------------------------------------------------------+
void TrailingStopSell(double Bid)
  {
   double CalculatedStopLoss=NormalizeDouble(Bid+150*_Point,_Digits);   // set the stop loss to 150 points
   for(int i=PositionsTotal()-1; i>=0; i--)                             // Go through all positions
     {
      string symbol=PositionGetSymbol(i);                               // get the symbol of the position
      if(_Symbol==symbol)                                               // if currency pair is equal
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)      // if we have a sell position
           {
            ulong PositionTicket=PositionGetInteger(POSITION_TICKET);   // get the ticket number
            double CurrentStopLoss=PositionGetDouble(POSITION_SL);      // calculate the current stop loss
            if(CalculatedStopLoss<CurrentStopLoss)                                      // if current stop loss is more than 150 points
              {
               trade.PositionModify(PositionTicket,(CalculatedStopLoss-10*_Point),0);// move the stop loss
              }
           } // End if loop
     } // End for loop
  } // End


и картинка как это работает:


 
Vladimir Karputov:

А теперь с исправлениями:


 trade.PositionModify(PositionTicket,(CalculatedStopLoss+10*_Point),0); // move the stop loss

Ошибка возможно здесь, в PositionModify  уже не производится нормировка цены, надо в неё передавать нормированную  NormalizeDouble или в эксперте m_Symbol.NormalizePrice

 
Aleksey Mavrin:

Ошибка возможно здесь, в PositionModify  уже не производится нормировка цены, надо в неё передавать нормированную  NormalizeDouble или в эксперте m_Symbol.NormalizePrice

Это не ошибка. Пока специально не усложнял код.

Вот когда пользователь увидит, что трейлинг заработал, вот тогда можно спросить: "А ошибки вроде "неправильная цена" на возникает"? И только потом уже вставлять метод  NormalizePrice из торгового класса CSymbolInfo.

 
ааа, педагогика ) у меня с ней неважно ))
 

дописал код под себя получилось так

//+------------------------------------------------------------------+
//| TrailingStop Buy                                                 |
//+------------------------------------------------------------------+
void TrailingStopBuy(string symbol_, double Ask, int number)
  {
   double CalculatedStopLoss=NormalizeDouble(Ask-50*_Point,_Digits);   // set the stop loss to 150 points
   for(int i=PositionsTotal()-1; i>=0; i--)                             // Go through all positions
     {
      string symbol=PositionGetSymbol(i);                               // get the symbol of the position
      if(symbol_==symbol)                                               // if currency pair is equal
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)       // if we have a sell position
           {
            ulong PositionTicket=PositionGetInteger(POSITION_TICKET);   // get the ticket number
            double CurrentStopLoss=PositionGetDouble(POSITION_SL);      // calculate the current stop loss
            if(CalculatedStopLoss>CurrentStopLoss)                                      // if current stop loss is more than 150 points
              {
               trade[number].PositionModify(PositionTicket,(CalculatedStopLoss+10*_Point),0); // move the stop loss
              }
           } // End if loop
     } // End for loop
  } // End
//+------------------------------------------------------------------+
//|  TrailingStop Sell                                               |
//+------------------------------------------------------------------+
void TrailingStopSell(string symbol_, double Bid, int number)
  {
   double CalculatedStopLoss=NormalizeDouble(Bid+50*_Point,_Digits);   // set the stop loss to 150 points
   for(int i=PositionsTotal()-1; i>=0; i--)                             // Go through all positions
     {
      string symbol=PositionGetSymbol(i);                               // get the symbol of the position
      if(symbol_==symbol)                                               // if currency pair is equal
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)      // if we have a sell position
           {
            ulong PositionTicket=PositionGetInteger(POSITION_TICKET);   // get the ticket number
            double CurrentStopLoss=PositionGetDouble(POSITION_SL);      // calculate the current stop loss
            if(CalculatedStopLoss<CurrentStopLoss)                                      // if current stop loss is more than 150 points
              {
               trade[number].PositionModify(PositionTicket,(CalculatedStopLoss-10*_Point),0);// move the stop loss
              }
           } // End if loop
     } // End for loop
  } // End

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

bool OpenBuyOrder(const string symbol, int number)
{
    if (!PositionSelect(symbol))
    {
      double Ask=NormalizeDouble(SymbolInfoDouble(symbol,SYMBOL_ASK),_Digits); 
      trade[number].Buy(Lots,symbol,Ask,NormalizeDouble(Ask-100*_Point, _Digits),0,NULL);
      tickets[number] = trade[number].ResultDeal();
    }
     
    return(true);
    
}
//+------------------------------------------------------------------+
bool OpenSellOrder(const string symbol, int number)
{
    if (!PositionSelect(symbol))
    {
      double Bid=NormalizeDouble(SymbolInfoDouble(symbol,SYMBOL_BID),_Digits); 
      trade[number].Sell(Lots,symbol,Bid,NormalizeDouble(Bid+100*_Point,_Digits),0, NULL);//NormalizeDouble(Bid-100 * _Point, _Digits)
      tickets[number] = trade[number].ResultDeal();
    }
    
    return(true);
    
}

//+------------------------------------------------------------------+
 
вот скрин
Файлы:
chezkxfe08.png  11 kb
 
добавил NormalizeDouble какписал пользователь выше, всеравно ноль внимания
 
хотя нет вроде бы заработало, проверю отпишусь, всем огромное спасибо
 
Gaiatsu:

дописал код под себя получилось так

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

Я бы еще модифицировал ордер. Ну, или еще каким-нибудь способом просигналил серверу, что ордер уже модифицирован,- сервер сам может не догадаться. 

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