Closing with a 2 Times the atr, help

 

Hello everyone,

Problem with liquidateing positions when the price moves against the position by two times the ATR.

The following portion of code describes the situation.

//+------------------------------------------------------------------+
//| Checking for Close order conditions                              |
//+------------------------------------------------------------------+
void CheckForClose()
   {
double ma8; 
          ma8=iMA(NULL,0,8,0,MODE_SMA,PRICE_CLOSE,0);
double ma20;
          ma20=iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,0);           

double LowerBB;
          LowerBB=iBands(NULL,0,30,1,0,PRICE_WEIGHTED,MODE_LOWER,0);
double UpperBB;  
          UpperBB=iBands(NULL,0,30,1,0,PRICE_WEIGHTED,MODE_UPPER,0);
            
/*   int Profit = 50;
   if (Digits == 3 || Digits == 5) Profit = Profit*10;*/
   for(int i=0; i<OrdersTotal();i++)
      {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      //---- check order type 
      if(OrderType()==OP_BUY)
         {                                                                          
   if (OrderSelect(i,SELECT_BY_POS)==true)
         if (ma8 < ma20)  
         OrderClose(OrderTicket(),OrderLots(), Bid,3,Red);
    else 
//The following is for the position to liquidate when the price moves two times the ATR against the position.
// example ATR=140.  2*140=280. When the price moves 280 pips agianst the position, the position is liquidated.          
         if(NormalizeDouble(OrderOpenPrice()  - OrderClosePrice(),5) >= 2*iATR(NULL,0,24,0)*Point)//??????????????? 
         {
         OrderClose(OrderTicket(),OrderLots(), Bid,3,Red);      
            //{
            }
         }
      if(OrderType()==OP_SELL)
         {
    if (OrderSelect(i,SELECT_BY_POS)==true)
         if (ma8 > ma20) //Moving average 8 periods is crossed above the moving average 20 periods.
         OrderClose(OrderTicket(),OrderLots(),Ask,3,Blue); 
          
//The following is for the position to liquidate when the price moves two times the ATR against the position.
// example ATR=179.  2*179=358. When the price moves 358 pips agianst the position, the position is liquidated. 
         if(NormalizeDouble(OrderClosePrice() - OrderOpenPrice(),5) >= 2*iATR(NULL,0,24,0)*Point)//????????????????
// If neither of the two above conditions occurs, the position does not liquidate.
// The system continues repeating the funtions, looking for when a closing condition does occur.  
         
         {
         OrderClose(OrderTicket(),OrderLots(),Ask,3,Blue);   
           // {
            }
         }
      }
   } 

Where the ?????????????????? is where the problem is. There are two possible closing conditions. The first condition is OK. It was when I added on this new ATR condition is when the code breakdowns.

Any help out there please. Thank you in advance.

Regards

Huckleberry

 

Huckleberry:

Hello everyone,

Problem with liquidateing positions when the price moves against the position by two times the ATR.

The following portion of code describes the situation.

Where the ?????????????????? is where the problem is. There are two possible closing conditions. The first condition is OK. It was when I added on this new ATR condition is when the code breakdowns.

Any help out there please. Thank you in advance.

Regards

Huckleberry

Hi,

I think you should try the conditions with Bid/Ask instead of OrderClosePrice().

I'm not a programmer but it seems to me as a "logical bug". How do you "know" the Close order price if you haven't closed it yet?

 
xrafix:

Hi,

I think you should try the conditions with Bid/Ask instead of OrderClosePrice().

I'm not a programmer but it seems to me as a "logical bug". How do you "know" the Close order price if you haven't closed it yet?


Hello xrafix,

Sorry it took so long to check out your reply.

I'm no programmer myself. But the incoorect code I had posted is close to being correct. I remember an (OrderClosePrice() - OrderOpenPrice() >= ??????? used before in a different code which made a correct code.

The answer to the (OrderClosePrice blah,blah().... 2*iATR ..... I have not correctly coded.

I'll have a look around and post that correct code when I locate it.

Huckleberry

 

Hi,

Another try... :-)

2*iATR(NULL,0,24,0)*Point)??? why multiply in point? the values of iATR are already 0.00xx...

 
  1. ATR is already a double no need for *point
  2. OrderClosePrice() - OrderOpenPrice(),5) >= 2*iATR
    OrderClosePrice() usage is fine. It's either Bid (Buy order) or Ask(sell). The normalizeDouble isn't necessary since you're not comparing equality.
  3. You must adjust for 5 digit brokers, TP, SL, and slippage
    double  pips2points,    // slippage  3 pips    3=points    30=points
            pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    
    Further more
     if (Digits == 3 || Digits == 5) Profit = Profit*10;
    is unsafe. Each time you refresh, change timeframes or pairs, this code will increase the variable 10X. extern variables do not get reset on deinit/init cycles.
  4. You orderSelect twice the second is redundant and potentially a problem. In the presence of other EAs or manual trading or if your EA opens multiple orders, you must count down when closing or deleteing.
        for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)            // Only my orders w/
        &&  OrderMagicNumber() == Magic.Number         // my magic number
        &&  OrderSymbol()      == Symbol() ){          // and period and symbol
    

  5. // double ma8; 
    //           ma8=iMA(NULL,0,8,0,MODE_SMA,PRICE_CLOSE,0);
    // simplify
    double ma8=iMA(NULL,0,8,0,MODE_SMA,PRICE_CLOSE,0);
    

Reason: