Is this a bug or is code wrong ??

To add comments, please log in or register
Forex Trader
114484
Forex Trader  
Greetings all,
Below is a code for a basic SAR indicator expert.
It works perfectly except it continually tries to modify the trailing stop after it has already been modified.
Here is what it looks like in my journal

2005.10.18 15:27:43 '130470': modify order #2204816 sell 10.40 EURUSD at 1.1994 sl: 1.2052 tp: 0.0000 -> sl: 1.2052 tp: 0.0000
2005.10.18 15:27:43 '130470': modify order #2204816 sell 10.40 EURUSD at 1.1994 sl: 1.2052 tp: 0.0000 -> sl: 1.2052 tp: 0.0000
2005.10.18 15:27:39 '130470': modify order #2204816 sell 10.40 EURUSD at 1.1994 sl: 1.2052 tp: 0.0000 -> sl: 1.2052 tp: 0.0000
2005.10.18 15:27:39 '130470': modify order #2204816 sell 10.40 EURUSD at 1.1994 sl: 1.2052 tp: 0.0000 -> sl: 1.2052 tp: 0.0000
2005.10.18 15:27:39 '130470': modify order #2204816 sell 10.40 EURUSD at 1.1994 sl: 1.2052 tp: 0.0000 -> sl: 1.2052 tp: 0.0000
2005.10.18 15:27:38 '130470': modify order #2204816 sell 10.40 EURUSD at 1.1994 sl: 1.2052 tp: 0.0000 -> sl: 1.2052 tp: 0.0000
2005.10.18 15:27:28 '130470': modify order #2204816 sell 10.40 EURUSD at 1.1994 sl: 1.2052 tp: 0.0000 -> sl: 1.2052 tp: 0.0000
2005.10.18 15:27:27 '130470': modify order #2204816 sell 10.40 EURUSD at 1.1994 sl: 1.2052 tp: 0.0000 -> sl: 1.2052 tp: 0.0000
and so on.....................

I can live with this but it uses mega memory, the meter in the botttom right corner of mt ticks over faster than most crosses do...

Any help appreciated.......

extern double TakeProfit = 0;
extern double Lots = 1;
extern double TrailingStop = 0;
extern double Stoploss = 0;
extern double Perc =10;
extern double step = 0.02;
extern double max = 0.2;

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+

int start()
{
  double Levv,  equity, Lotss, trd,sar ;
  int cnt=0, total ;
         
  sar= iSAR(NULL, 0, step,max,0);
  DoubleToStr(sar,4);
  
  
 //======================================================================
           
  if(Bars<2)
  {
    Print("bars less than 100");
    return(0); }
  //======================================================================   
  total=OrdersTotal();
   trd =0 ;
   
   for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol()==Symbol()) trd++;
}
  
 //======================================================================
  {
    if(AccountFreeMargin()<(1*Lots))
    {
      Print("We have no money");
      return(0);}
      
   equity =AccountEquity();
   
  Lotss = MathAbs((equity*Perc/100/100)/10);
   
  Levv=NormalizeDouble(Lotss,1);
          
 //======================================================================
  
    // (BUY)
    
    if (trd != 1 && Bid > sar )
    {
      OrderSend(Symbol(),OP_BUY,Levv,Ask,3,0,0,0,0,0,Blue);
      
      return(0);
    }
    
    
    // (SELL)
    if ( trd != 1 && Ask < sar ) 
    {
      OrderSend(Symbol(),OP_SELL,Levv,Bid,3, 0,0,0,0,0,Red);
     
      
      return(0);
    } 
   }
          
 //======================================================================     
      
  //CLOSE
  
   for ( int i = 0; i < OrdersTotal(); i++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      
      if(OrderType()<=OP_SELL && 
         OrderSymbol()==Symbol())   
        {
         if(OrderType()==OP_BUY)   
           {
            
            if( Ask < sar )
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); 
                 return(0); 
                }
                }
                }
               
  for ( i = 0; i < OrdersTotal(); i++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      
      if(OrderType()<=OP_SELL && 
         OrderSymbol()==Symbol())   
        {
         if(OrderType()==OP_SELL)   
           {
            
            if(Bid > sar )
                {
                 OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); 
                 return(0); 
                }
                }
                }
 //================================================================================
   // Modify            
         for ( i = 0; i < OrdersTotal(); i++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
             if( OrderSymbol()==Symbol() && OrderType()==OP_BUY)  
              {                 
                if(OrderStopLoss()!=sar)
                    {
            OrderModify(OrderTicket(),OrderOpenPrice(),sar,0,0,Green);
                     return(0);
                    }
                 }
              }

     for ( i = 0; i < OrdersTotal(); i++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    if( OrderSymbol()==Symbol()&& OrderType()==OP_SELL)  
              {                 
               
                  if((OrderStopLoss()!=sar))
                    {
       OrderModify(OrderTicket(),OrderOpenPrice(),sar,0,0,Red);
                     return(0);
                    }
                 }
              }
               }}}   



Forex Trader
114484
Forex Trader  
Well, you could add a Print("sar= ", sar, " OrderSL=", OrderStopLoss); inside your loop to see which values are there. You might also want to check the result code for OrderModify() to see if that says anything meaningful.


Markus
Forex Trader
114484
Forex Trader  

Well, you could add a Print("sar= ", sar, " OrderSL=", OrderStopLoss); inside your loop to see which values are there. You might also want to check the result code for OrderModify() to see if that says anything meaningful.


Markus


Thanks Markus, just tried Print and both seem to equal each other. That is why I added DoubleToStr(sar,4); to make sure both were 4 decimal places...
I have tried many different combo's for the OrderModify() code but this one has really got me stumped.
Forex Trader
114484
Forex Trader  
Try printing

OrderStopLoss()-sar

inside the if command and check what you get there.
Or use MathRound() on the result (difference).


Markus
Forex Trader
114484
Forex Trader  

Try printing

OrderStopLoss()-sar

inside the if command and check what you get there.
Or use MathRound() on the result (difference).


Markus


Thank again Markus but still no good, talk about doing my head in
If someone from MT could just test this code and tell me if it is a bug or not would be a great help..
Thanks :)
Forex Trader
114484
Forex Trader  
Checking decimal values for equality is sometimes troublesome on all languages, so I'm not sure if they will be able to fix it.

Last try as a workaround.

If (MathAbs(OrderStopLoss()-sar)>Point) {
       Print("Modifying SL from  ", OrderStopLoss(), " to ", sar);

}



Make sure to check the print output.

Cheers


Markus

Forex Trader
114484
Forex Trader  
Thanks Markus but it prints nothing to the experts journal thus I am asuming that there is no difference between the two. AHHHHHHHHHHHH.

I just wish I knew weather it was a bug or in the code, all my other experts with trailing stops work fine....

Anyway thanks again and gt.........
To add comments, please log in or register