Nagging SL adjustment

 
void start()
{         

              string _trade_ticker=Symbol();

if(_orders>0){
               i=0;
               double _stop_loss;
               for(i=OrdersTotal()-1;i>=0;i--){
                  if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false){ continue;}
                  if(OrderSymbol()!=_trade_ticker){ continue;}
                  if(OrderType()==OP_BUY){
                     _stop_loss=MarketInfo(_trade_ticker,MODE_BID)-1*Ticks(_trade_ticker,0.3,_lots);}
                  if(OrderType()==OP_SELL){
                     _stop_loss=MarketInfo(_trade_ticker,MODE_ASK)+1*Ticks(_trade_ticker,0.3,_lots);}
                  if((OrderType()==OP_BUY&&_stop_loss>OrderStopLoss())||(OrderType()==OP_SELL&&_stop_loss<OrderStopLoss())||OrderStopLoss()==0.0){
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),_stop_loss,OrderTakeProfit(),_request_slippage,CLR_NONE)){
                        int _last_error=GetLastError();
                        Print(" order modify err: ",_last_error);
                        if(_last_error==130){
                           string _order_type=" buy ";
                           if(OrderType()==OP_SELL) { _order_type=" sell "; }
                           Print(_order_type," sl: ",_stop_loss,", current sl: ",OrderStopLoss(),", open price: ",OrderOpenPrice(),", current price: ",MarketInfo(_trade_ticker,MODE_BID));}}}}}
      }
  

  
double Ticks(string TradeTicker,double TradeProfit,double TradeVolume)
      {

         double _ticks = 1*MarketInfo(TradeTicker,MODE_POINT)*(MarketInfo(TradeTicker,MODE_STOPLEVEL)+MarketInfo(TradeTicker,MODE_SPREAD));
Print(" ticks @ stoplevel: ",_ticks);
         _ticks+=NormalizeDouble(TradeProfit*MarketInfo(TradeTicker,MODE_POINT)/(TradeVolume*MarketInfo(TradeTicker,MODE_TICKVALUE)),6);
Print(" ticks @ profit: ",_ticks);
         //if(StringSubstr(TradeTicker,0,3)=="XAG"||StringSubstr(TradeTicker,0,3)=="XAU") { _ticks/=MarketInfo(StringSubstr(TradeTicker,0,3)+"USD",MODE_BID); }
         return(NormalizeDouble(MathAbs(_ticks),8));
      }
The above cannot set or adjust stop loss. I keep getting invalid sl/tp. (error 130)
 
ssn:
The above cannot set or adjust stop loss. I keep getting invalid sl/tp. (error 130)

OK,  so what was the Bid, Ask, new SL price and Stoplevel  at time you tried to place the OrderModify and what OrderType()  ?

Does your OrderModify() comply with thiss ?   Requirements and Limitations in Making Trades

 

This is part of the logs in 'Expert' tab

 

2013.03.06 15:26:25 ea XAGUSD,M15:  buy  sl: 28.56, current sl: 0, open price: 28.694, current price: 28.73

 

With a spread of 0.04, this seems reasonable. 

 
ssn:

This is part of the logs in 'Expert' tab

 2013.03.06 15:26:25 ea XAGUSD,M15:  buy  sl: 28.56, current sl: 0, open price: 28.694, current price: 28.73

What was your Stoplevel ?

Have you tried changing this . . .

return(NormalizeDouble(MathAbs(_ticks),8));

//  change to

return(NormalizeDouble(MathAbs(_ticks), Digits));
Reason: