Can't breakeven

 

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.

 
How do you get a position: by symbol or by ticket?
 
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){
 
David Diez :

By Symbol.

Is your trading account type: netting or hedging?

 
Vladimir Karputov:

Is your trading account type: netting or hedging?

Hedging.

 
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)
               {
                
               }
           }
 
Vladimir Karputov:

Take a position on the ticket.

Standard cycle:

That's the same I wrote.

 
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.

 
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;
                     }
                  }
 
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.