Причесал код к более красивому виду.
Замечание: забудьте и выкиньте слово "ордер" из своей головы. Оперировать нужно словами "позиция" или "сделка" ("сделка" может использоваться лицами достигшими совершеннолетия).
Итак:
Исходя их этого:
//+------------------------------------------------------------------+ //| 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
и картинка как это работает:
А теперь с исправлениями:
trade.PositionModify(PositionTicket,(CalculatedStopLoss+10*_Point),0); // move the stop loss
Ошибка возможно здесь, в PositionModify уже не производится нормировка цены, надо в неё передавать нормированную NormalizeDouble или в эксперте m_Symbol.NormalizePrice
Ошибка возможно здесь, в 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); } //+------------------------------------------------------------------+
дописал код под себя получилось так
но почемуто когда я в плюсе уже у меня несдвигается трейлстоп стоп лос как был изначальный так и остается, вот функции бай и селл
Я бы еще модифицировал ордер. Ну, или еще каким-нибудь способом просигналил серверу, что ордер уже модифицирован,- сервер сам может не догадаться.

- Бесплатные приложения для трейдинга
- 8 000+ сигналов для копирования
- Экономические новости для анализа финансовых рынков
Вы принимаете политику сайта и условия использования
что тут не так кто нибудь знает?