Can't breakeven

David Diez  

Hi there I wrote this code to set AutoSL based on TP distance, as I've written it only has to change SL if manual is larger than auto. This way, I could manually breakeven an order once reached TP1:

            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY){
               //+-------------------------------------- AutoRisk ---!
               double AutoSL=NormalizeDouble(PO-(TP-PO)/1.618,SDigits);
               if(PositionGetDouble(POSITION_SL)<AutoSL){ // Modify
                  if(!trade.PositionModify(iTicket,AutoSL,TP)){
                     Print("PositionModify error ",trade.ResultRetcode());
                     return;
                     }
                  }

            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL){
            //+-------------------------------------- AutoRisk ---!
               double AutoSL=NormalizeDouble(PO+(PO-TP)/1.618,SDigits);
               if(PositionGetDouble(POSITION_SL)==0
               ||PositionGetDouble(POSITION_SL)>AutoSL){ // Modify
                  if(!trade.PositionModify(iTicket,AutoSL,TP)){
                     Print("PositionModify error ",trade.ResultRetcode());
                     return;
                     }
                  }

The problem I'm facing is that AutoSL is set automaticly even when SL is lesser than this value, thus I can't breakeven. But I've clearly specificied the rule to AutoSL just when SL is larger than this value.

Don't actually know why isn't working, hope someone can help on this matter. Thank you in advance.

Vladimir Karputov  
How do you get a position: by symbol or by ticket?
David Diez  
Vladimir Karputov:
How do you get a position: by symbol or by ticket?

By Symbol.

      for(int i=0;i<PositionsTotal();i++){
         ulong iTicket=PositionGetTicket(i);
         if(PositionGetString(POSITION_SYMBOL)==SName){
Vladimir Karputov  
David Diez :

By Symbol.

Is your trading account type: netting or hedging?

David Diez  
Vladimir Karputov:

Is your trading account type: netting or hedging?

Hedging.

Vladimir Karputov  
David Diez :

Hedging.

Take a position on the ticket.

Standard cycle:

   for(int i=PositionsTotal()-1; i>=0; i--)
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic)
           {
            if(m_position.PositionType()==POSITION_TYPE_BUY)
               {

               }
            if(m_position.PositionType()==POSITION_TYPE_SELL)
               {
                
               }
           }
David Diez  
Vladimir Karputov:

Take a position on the ticket.

Standard cycle:

That's the same I wrote.

Vladimir Karputov  
David Diez :

That's the same I wrote.

You haven't written anything. You choose a symbol position on the hedge account - you are making a mistake.

Correct your mistake. Read the help.

David Diez  
Vladimir Karputov:

You haven't written anything. You choose a symbol position on the hedge account - you are making a mistake.

Correct your mistake. Read the help.

I just want the AutoSL to trigger when larger than selected ratio.

            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY){
               //+-------------------------------------- AutoRisk ---!
               double AutoSL=NormalizeDouble(PO-(TP-PO)/1.618,SDigits);
               if(PositionGetDouble(POSITION_SL)<AutoSL){
                  if(!trade.PositionModify(iTicket,AutoSL,TP)){
                     Print("PositionModify error ",trade.ResultRetcode());
                     return;
                     }
                  }
Vladimir Karputov  
David Diez :

I just want the AutoSL to trigger when larger than selected ratio.

Until now, you haven't shown how you choose a position. Correct your mistake - the position must be selected by ticket, not by symbol.

See the example above. Read the help.

Reason: