Problemas con Trailing Stop

 

Greetings! I have a problem with my trailing stop settings.

The trailing stop is taken from 0. I want it to be taken when I already have 5 pips earned. Could you help me with that?

That is, call the function when the price is above the 5 pips already earned.

It's like it kicks in after 5 pips won. That if it goes down, always come out with 5 pips earned.

I leave my code:


int posTotal=PositionsTotal();
for(int posIndex=posTotal-1;posIndex>=0;posIndex--)
{
ulong ticket=PositionGetTicket(posIndex);
if(PositionSelectByTicket(ticket) && PositionGetInteger(POSITION_MAGIC)==MagicNumber)
{
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
{
if(TrailingStop>0)
{
if(SymbolInfoDouble(_Symbol,SYMBOL_BID)-PositionGetDouble(POSITION_PRICE_OPEN)>MyPoint*TrailingStop)
{
if(PositionGetDouble(POSITION_SL)<SymbolInfoDouble(_Symbol,SYMBOL_BID)-MyPoint*TrailingStop)
{
trade.PositionModify(ticket,SymbolInfoDouble(_Symbol,SYMBOL_BID)-MyPoint*TrailingStop,PositionGetDouble(POSITION_TP));
return;
}
}
}
}

if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
{
if(TrailingStop>0)
{
if(PositionGetDouble(POSITION_PRICE_OPEN)-SymbolInfoDouble(_Symbol,SYMBOL_ASK)>MyPoint*TrailingStop)
{
if(PositionGetDouble(POSITION_SL)>SymbolInfoDouble(_Symbol,SYMBOL_ASK)+MyPoint*TrailingStop)
{
trade.PositionModify(ticket,SymbolInfoDouble(_Symbol,SYMBOL_ASK)+MyPoint*TrailingStop,PositionGetDouble(POSITION_TP));
return;
}
}
}
}
}
}
return;
}
 
LupusLunam :

Greetings! I have a problem with my trailing stop settings.

The trailing stop is taken from 0. I want it to be taken when I already have 5 pips earned. Could you help me with that?

That is, call the function when the price is above the 5 pips already earned.

It's like it kicks in after 5 pips won. That if it goes down, always come out with 5 pips earned.

I leave my code:


Always use 'Code Styler ( Styler)' - and your code will look like this:

int posTotal=PositionsTotal();
for(int posIndex=posTotal-1; posIndex>=0; posIndex--)
  {
   ulong ticket=PositionGetTicket(posIndex);
   if(PositionSelectByTicket(ticket) && PositionGetInteger(POSITION_MAGIC)==MagicNumber)
     {
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
        {
         if(TrailingStop>0)
           {
            if(SymbolInfoDouble(_Symbol,SYMBOL_BID)-PositionGetDouble(POSITION_PRICE_OPEN)>MyPoint*TrailingStop)
              {
               if(PositionGetDouble(POSITION_SL)<SymbolInfoDouble(_Symbol,SYMBOL_BID)-MyPoint*TrailingStop)
                 {
                  trade.PositionModify(ticket,SymbolInfoDouble(_Symbol,SYMBOL_BID)-MyPoint*TrailingStop,PositionGetDouble(POSITION_TP));
                  return;
                 }
              }
           }
        }
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
        {
         if(TrailingStop>0)
           {
            if(PositionGetDouble(POSITION_PRICE_OPEN)-SymbolInfoDouble(_Symbol,SYMBOL_ASK)>MyPoint*TrailingStop)
              {
               if(PositionGetDouble(POSITION_SL)>SymbolInfoDouble(_Symbol,SYMBOL_ASK)+MyPoint*TrailingStop)
                 {
                  trade.PositionModify(ticket,SymbolInfoDouble(_Symbol,SYMBOL_ASK)+MyPoint*TrailingStop,PositionGetDouble(POSITION_TP));
                  return;
                 }
              }
           }
        }
     }
  }

Mistake #1: Use ' SymbolInfoDouble ' instead of

It is recommended to use SymbolInfoTick() if the function is used for getting information about the last tick. It may well be that not a single quote has appeared yet since the terminal is connected to a trading account. In such a case, the requested value will be indefinite.
Documentation on MQL5: Market Info / SymbolInfoDouble
Documentation on MQL5: Market Info / SymbolInfoDouble
  • www.mql5.com
SymbolInfoDouble - Market Info - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
LupusLunam:

Greetings! I have a problem with my trailing stop settings.

The trailing stop is taken from 0. I want it to be taken when I already have 5 pips earned. Could you help me with that?

That is, call the function when the price is above the 5 pips already earned.

It's like it kicks in after 5 pips won. That if it goes down, always come out with 5 pips earned.

I leave my code:


input double TStop  = 5.0;
input double TStart = 5.0;
for(int i=0;i<PositionsTotal();i++){
  ulong iTicket=PositionGetTicket(i);
  if(PositionGetString(POSITION_SYMBOL)==_Symbol
  &&PositionGetInteger(POSITION_MAGIC)==MagicNumber){
    double PosSL=PositionGetDouble(POSITION_SL);
    double PosTP=PositionGetDouble(POSITION_TP);
    double PosOpen=PositionGetDouble(POSITION_PRICE_OPEN);
    if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY){
      double TS=NormalizeDouble(Bid-(TStop*10)*_Point,_Digits);
      if(TS>NormalizeDouble(PosOpen+(TStart*10)*_Point,_Digits){
         if(TS>PosSL&&!trade.PositionModify(iTicket,TStop,PosTP)){
           Print("PositionModify error ",trade.ResultRetcode());
           return;
           }
         else{i--;}
        }
    if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL){
      double TS=NormalizeDouble(Ask+(TStop*10)*_Point,_Digits);
      if(TS<NormalizeDouble(PosOpen-(TStart*10)*_Point,_Digits){
         if((TS>PosSL||PosSL==0)&&!trade.PositionModify(iTicket,TStop,PosTP)){
           Print("PositionModify error ",trade.ResultRetcode());
           return;
           }
         else{i--;}
        }
      }
    }
  }
 
David Diez # :

You provided a non-MQL5 code - false variables 'Bid' and 'Ask'.

 
Vladimir Karputov #:

You provided a non-MQL5 code - false variables 'Bid' and 'Ask'.

double Ask=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
double Bid=SymbolInfoDouble(_Symbol,SYMBOL_BID);
 
David Diez # :

This part of the code must be immediately inserted into the message - otherwise, confusion and stratification of consciousness will result.

 
Vladimir Karputov #:

This part of the code must be immediately inserted into the message - otherwise, confusion and stratification of consciousness will result.

No.

 
LupusLunam #:

<Deleted>

This is an English language forum.
If you want to post here, post in English please.