Download MetaTrader 5

How to adjust SL to average price?? [invalid stops]

To add comments, please log in or register
Do you know that MQL5 has structures?
rooster44
157
rooster44 2012.01.30 06:14 

Ok, I'm having 2 issues, first, when Adjust_sl_ap is true the EA should adjust SL to average price since im scaling in positions this would be PositionGetDouble(POSITION_CURRENT_PRICE) right??

and secondly, when adjust_sl_ap is false it should use a trailing position, but for some reason trailing isn't activated until after the scaling in has occured and it won't trail unless its value is greater than 100??

Here is the SL and Tral code:: Thank you!!

// trailing and Sl
        if(PositionSelect(S[I]))           {
        
        double gi_avpriceb = SymbolInfoDouble(S[I],SYMBOL_BID) - PositionGetDouble(POSITION_PRICE_CURRENT);
        double gi_avprices = PositionGetDouble(POSITION_PRICE_CURRENT) - SymbolInfoDouble(S[I],SYMBOL_ASK);  
           if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
               { 
                if ( PositionGetDouble(POSITION_VOLUME)==gs_lots && PositionGetDouble(POSITION_PRICE_OPEN) - SymbolInfoDouble(S[I], SYMBOL_ASK) > First_SL*gi_point   )  sell(gs_lots) ;         
                  if(ss==1)trX.PositionClose(S[I],100);
                  if ( Adjust_SL_AP ) { if((PositionGetDouble(POSITION_PRICE_CURRENT)<SymbolInfoDouble(S[I], SYMBOL_BID)&&PositionGetDouble(POSITION_PROFIT)>0) && (PositionGetDouble(POSITION_SL)<SymbolInfoDouble(S[I], SYMBOL_BID) - gi_avpriceb || PositionGetDouble(POSITION_SL)==0));
                             trX.PositionModify(S[I],SymbolInfoDouble(S[I], SYMBOL_BID)-gi_avpriceb,PositionGetDouble(POSITION_TP)); }
                  else { if(Trailing>0&&SymbolInfoDouble(S[I], SYMBOL_BID)-PositionGetDouble(POSITION_PRICE_OPEN) > gi_point*Trailing&& (PositionGetDouble(POSITION_SL)<SymbolInfoDouble(S[I], SYMBOL_BID) - gi_point*(Trailing+5) || PositionGetDouble(POSITION_SL)==0));
                             trX.PositionModify(S[I],SymbolInfoDouble(S[I], SYMBOL_BID) - gi_point*Trailing,PositionGetDouble(POSITION_TP)); }
               }
           if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
               { 
                if ( PositionGetDouble(POSITION_VOLUME)==gs_lots && SymbolInfoDouble(S[I], SYMBOL_ASK) - PositionGetDouble(POSITION_PRICE_OPEN)  > First_SL*gi_point   ) buy(gs_lots) ;         
                  if(ss==2)trX.PositionClose(S[I],100);
                  if ( Adjust_SL_AP ) { if((PositionGetDouble(POSITION_PRICE_CURRENT)<SymbolInfoDouble(S[I], SYMBOL_BID)&&PositionGetDouble(POSITION_PROFIT)>0)&& (PositionGetDouble(POSITION_SL)>SymbolInfoDouble(S[I], SYMBOL_ASK) + gi_avprices || PositionGetDouble(POSITION_SL)==0));
                             trX.PositionModify(S[I],SymbolInfoDouble(S[I], SYMBOL_ASK)+gi_avprices,PositionGetDouble(POSITION_TP)); }
                  else { if(Trailing>0&&PositionGetDouble(POSITION_PRICE_OPEN)-SymbolInfoDouble(S[I], SYMBOL_ASK) > gi_point*Trailing && (PositionGetDouble(POSITION_SL)>SymbolInfoDouble(S[I], SYMBOL_ASK) + gi_point*(Trailing+5) || PositionGetDouble(POSITION_SL)==0))
                             trX.PositionModify(S[I],SymbolInfoDouble(S[I], SYMBOL_ASK) + gi_point*Trailing,PositionGetDouble(POSITION_TP)); }
               }
           
         }
   

      }
Documentation on MQL5: Standard Constants, Enumerations and Structures / Indicator Constants / Price Constants
  • www.mql5.com
Standard Constants, Enumerations and Structures / Indicator Constants / Price Constants - Documentation on MQL5
To add comments, please log in or register