Putting ECN stops, getting Error 145

 

Hi, after sending a market order without stops I call this function to place SL and TP... both at once but failing that (in part or in full) the stops are to be placed

in later passes until both SL and TP are in place.

Problem is that I'm getting E 145, not many but enough to indicate there is a problem in the code. If I place SL and TP far away there are no errors.

If I set SL and/or TP like 2 points outside StopLevel I get these errors.

Test conditions are: Spread = 16, SL = 12, TP = 12, StopLevel = 10, FreezeLevel = 10. StopLevel are set elsewhere to the lowest of StopLevel and FreezeLevel.

Anyone see whats the problem ?

//-------------------------------
void DoInitialStops()
   {//StopLoss, TakeProfit, and StopLevel are all in points...no "pips" anywhere...
   double SL = StopLoss;
   double TP = TakeProfit;
   bool Result;
   int Orders = OrdersTotal() - 1;
   for (int i = Orders; i >= 0; i--)
      {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
           
      if (OrderMagicNumber() == MagicNumber  && OrderSymbol() == Symbol())
         {
         if (OrderType() == OP_BUY)
            {   

               if (OrderStopLoss() == 0.0 && OrderTakeProfit() == 0.0)//SL + TP
                  {
                  if(SL > StopLevel && ((Bid - (OrderOpenPrice() - SL * Point)) > StopLevel * Point)  )//SL
                     {
                     if(TP > StopLevel && (((OrderOpenPrice() + TP * Point) - Bid) > StopLevel * Point)  )//TP
                        {
                        //Result = OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(OrderOpenPrice() - SL * Point, Digits), NormalizeDouble(OrderOpenPrice() + TP * Point, Digits), 0, Blue);   
                        continue;
                        }
                     }
                  }                                    
               if (OrderStopLoss() == 0.0 && SL > StopLevel && ((Bid - (SL * Point)) > StopLevel * Point) )//SL
                  {
                  if ((OrderOpenPrice() - Bid) > (StopLevel * Point))//No modify inside StopLevel or FreezeLevel
                     {
                     Result = OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(OrderOpenPrice() - SL * Point, Digits), OrderTakeProfit(), 0, Blue);   
                     } 
                  }           
               if (OrderTakeProfit() == 0.0 && TP > StopLevel && ((Bid - (TP * Point)) > StopLevel * Point)  )//TP
                  {
                  if ((OrderOpenPrice() - Bid) > (StopLevel * Point))//No modify inside StopLevel or FreezeLevel
                     {                         
                     Result = OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), NormalizeDouble(OrderOpenPrice() + TP * Point, Digits), 0, Blue);
                     }
                  }   
               }
           
//*******
         if (OrderType() == OP_SELL)
            {
 

This is wrong ...

TP > StopLevel && ((Bid - (TP * Point)) > StopLevel * Point

TP is presumably a level like 30, so that is fine in the first part of the test

but the second part the values are off

1.23253 - 0.00011 > 0.00012

is never going to fail

 

Well the code above was both messy and buggy. The problems are fixed now. THIS code generates no errors, and as long as the resulting SL price is sufficiently away from current market

it puts the stop and allows me to set SL right on, anywhere below, or anywhere above (-SL) the entry price.

//-------------------------------
void DoInitialStops()
   {//StopLoss, TakeProfit, and StopLevel are all in points...no "pips" anywhere...
   double SL = StopLoss;
   double TP = TakeProfit;
   double NewSLprice;
   bool Result;
   //

   //
   int Orders = OrdersTotal() - 1;
   for (int i = Orders; i >= 0; i--)
      {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
           
      if (OrderMagicNumber() == MagicNumber  || OrderMagicNumber() == MagicNumber1 && OrderSymbol() == Symbol())
         {
         
         if (  MathAbs(TimeCurrent() - OrderOpenTime()) < 3600) return(0);//test: place stop after market has moved away from entryprice
         
         if (OrderType() == OP_BUY && OrderStopLoss() == 0.0)
            {   
            NewSLprice = NormalizeDouble(OrderOpenPrice() - SL * Point, Digits);//SL Refered to entryprice
            
            
            //if the new SL is sufficiently away from current market as determined by stoplevel and freezelevel:                                    
            if (  (Bid - NewSLprice) > StopLevel * Point )
               {                 
               Result = OrderModify(OrderTicket(), OrderOpenPrice(), NewSLprice, 0, 0, Aqua);                      
               }           
            }
//----------            
 
DayTrader:

Well the code above was both messy and buggy. The problems are fixed now. THIS code generates no errors, and as long as the resulting SL price is sufficiently away from current market

it puts the stop and allows me to set SL right on, anywhere below, or anywhere above (-SL) the entry price.

Ok, but you lose one brownie point for failing to test your OrderSelect for success,

and another half a brownie point for hard coding a 3600 in there.

And this test ...

(Bid - NewSLprice) > StopLevel * Point

is poor because you are exactly at the StopLevel (ok 1 point away), but slippage could easily make that fail. I would normally allow a bit more margin.

 

Testing OrderSelect: I sometimes do but if the Select should fail for some reason the following OP type and Magic statements would return FALSE and we'd go around to select the next order?

Some time would be wasted but I don't think I've ever had any errors from not testing OrderSelect.

That time or duration check was only for debugging (getting away from current price before placing stop). Line is already removed from code.

Your last point though could result in some 130 or 145s on forward test...

 
DayTrader:

Testing OrderSelect: I sometimes do but if the Select should fail for some reason the following OP type and Magic statements would return FALSE and we'd go around to select the next order?

Some time would be wasted but I don't think I've ever had any errors from not testing OrderSelect.

Well you might be right in this case. I don't know. It is a lot of work to prove beyond reasonable doubt that an incorrect OrderSelect will not cause a problem. I find it hard to understand how an OrderSelect by position can fail. I have had problems with OrderSelect by Ticket though. I used to not check them. Then I changed to checking them. And recently I have changed again to check them and report any error.

It is easier (quicker and cheaper in programming time) to check them than figure out if their failure will cost you money!

Perhaps you need to wait until a non-test costs you money to make you a believer :-)

Reason: