//Import inputal class #include #include #include #include #include CPositionInfo m_position; // trade position object CTrade m_trade; // trading object CSymbolInfo m_symbol; // symbol info object CAccountInfo m_account; // account info wrapper COrderInfo m_order; //+------------------------------------------------------------------+ //| TRAILING STOP | //+------------------------------------------------------------------+ void TrailingStop() { double SL_in_Pip = 0; for(int i = PositionsTotal() - 1; i >= 0; i--) { if(m_position.SelectByIndex(i)) { // selects the orders by index for further access to its properties if((m_position.Magic() == InpMagicNumber) && (m_position.Symbol() == m_symbol.Name())) { // For Buy oder if(m_position.PositionType() == POSITION_TYPE_BUY) { //--Calculate SL when price changed SL_in_Pip = NormalizeDouble(Bid - m_position.StopLoss(), _Digits) / Pips2Double; if(SL_in_Pip > InpSL_Pips) { double newSL = NormalizeDouble(Bid - InpSL_Pips * Pips2Double, _Digits); if(!m_trade.PositionModify(m_position.Ticket(), newSL, m_position.TakeProfit())) { Print(__FUNCTION__,"--> OrderModify error ", m_trade.ResultComment()); continue; } } } //For Sell Order else if(m_position.PositionType() == POSITION_TYPE_SELL) { //--Calculate SL when price changed SL_in_Pip = NormalizeDouble(m_position.StopLoss() - Bid, _Digits) / Pips2Double; if(SL_in_Pip > InpSL_Pips){ double newSL = NormalizeDouble(Bid + (InpSL_Pips) * Pips2Double, _Digits); if(!m_trade.PositionModify(m_position.Ticket(), newSL, m_position.TakeProfit())) { Print(__FUNCTION__,"--> OrderModify error ", m_trade.ResultComment()); //continue; } } } } } } //--- Modify pending order for(int i=OrdersTotal()-1; i>=0; i--) {// returns the number of current orders if(m_order.SelectByIndex(i)) { // selects the pending order by index for further access to its properties if(m_order.Symbol() == m_symbol.Name() && m_order.Magic()==InpMagicNumber) { if(m_order.OrderType() == ORDER_TYPE_BUY_STOP) { SL_in_Pip = NormalizeDouble(Bid - m_order.StopLoss(), _Digits) / Pips2Double; if(SL_in_Pip < InpSL_Pips/2) { double newOP = NormalizeDouble(Bid + (InpSL_Pips/2) * Pips2Double, _Digits); double newTP = NormalizeDouble(newOP + InpTP_Pips * Pips2Double, _Digits); double newSL = NormalizeDouble(Bid - (InpSL_Pips/2) * Pips2Double, _Digits); if(!m_trade.OrderModify(m_order.Ticket(), newOP, newSL, newTP, ORDER_TIME_GTC,0)) { Print(__FUNCTION__,"--> Modify PendingOrder error!", m_trade.ResultComment()); continue; } } } else if(m_order.OrderType() == ORDER_TYPE_SELL_STOP) { SL_in_Pip = NormalizeDouble(m_order.StopLoss() - Ask, _Digits) / Pips2Double; if(SL_in_Pip < InpSL_Pips/2){ double newOP = NormalizeDouble(Ask - (InpSL_Pips/2) * Pips2Double, _Digits); double newTP = NormalizeDouble(newOP - InpTP_Pips * Pips2Double, _Digits); double newSL = NormalizeDouble(Ask + (InpSL_Pips/2) * Pips2Double, _Digits); if(!m_trade.OrderModify(m_order.Ticket(), newOP, newSL, newTP, ORDER_TIME_GTC,0)) { Print(__FUNCTION__,"--> Modify PendingOrder error!", m_trade.ResultComment()); //continue; } } } } } } }