EA - Advice re limit orders pls

 
/*------------------------------------------------------------------+
 |                                             fracmap_home         |
 |                                                 Copyright © 2011 |
 |                                                                  |
 +------------------------------------------------------------------*/
#property copyright "Copyright © 2011, fracmap.com"
//-----  
#define MagicNum 10001 
//-----
//-----
extern bool       Retrace    = False; // 
extern int       Band_Period   = 9;
extern int       price_type    = 0; // 0 = High/Low | 1 = Open/Close
extern double       phi    = 1.618034; // 
extern int       order    = 1; // 
extern double Lots = 0.1;

//+------------------------------------------------------------------+
int start()
 {

double New_Time_M1;
bool New_Bar_M1;



//new bar is born
if(New_Time_M1 != iTime(Symbol(),PERIOD_H1,0)) // Compare time --> new bar is born
{
New_Time_M1=iTime(Symbol(),PERIOD_H1,0); // New time set
New_Bar_M1=true; // A new bar detected

}
else
{
New_Bar_M1=false;
return;
}

   string IndicatorStyle;
   bool Golong = false;
   bool Goshort = false;
      

 //There are two different styles of indicator.

   if(Retrace==False)
     {
     IndicatorStyle="fracmap_NoRetrace";
     }
      else
     {
     IndicatorStyle="fracmap_trade";
     }

// only open one pos...
//    int count=0, pos;
//    for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
//        OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
//    &&  OrderMagicNumber()  == MagicNum             // my magic number
//    &&  OrderSymbol()       == Symbol() ){              // and my pair.
//        count++;
//    }
//    if (count > 0) return; // one open order per chart.

// 
 
   int cnt, ticket,total,Trade_num;

//New bar code for Min data

if(New_Bar_M1==true)
{  


   //-----
  
     double frac_sell=iCustom(NULL, 0, IndicatorStyle,Band_Period,price_type,phi,order,1,0);
     double frac_buy=iCustom(NULL, 0, IndicatorStyle,Band_Period,price_type,phi,order,0,0);

     double frac_sell_prev=iCustom(NULL, 0, IndicatorStyle,Band_Period,price_type,phi,order,1,1);
     double frac_buy_prev=iCustom(NULL, 0, IndicatorStyle,Band_Period,price_type,phi,order,0,1);

     double frac_sell_next=iCustom(NULL, 0, IndicatorStyle,Band_Period,price_type,phi,order,1,-1);
     double frac_buy_next=iCustom(NULL, 0, IndicatorStyle  ,Band_Period,price_type,phi,order,0,-1);

  
      //-------------------------------------------------------  this is where signals are filtered


      if(Close[0]>=frac_buy  && Low[0]<=frac_buy  && frac_buy_prev < frac_buy && frac_buy_next < frac_buy)
       {      
         Golong=true;
       }
       
      if(Close[0]<=frac_sell && High[0]>=frac_sell && frac_sell_prev > frac_sell && frac_sell_next > frac_sell)
       {
         Goshort = true;
       }            
      //-------------------------------------------------------  


// Break the cycle when ping pong in same bar                   

      if(Low[0]<=frac_buy  && High[0]>=frac_sell)return(0);



if(OrdersTotal()>0) 
      {
      OrderSelect(0, SELECT_BY_POS, MODE_TRADES);
                     
      if(Golong==true && OrderType()>0)
       {      
        ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,0,0,"Buy Entry",MagicNum,0,Green);
//            return;
             }
            
      if(Goshort==true && OrderType()<1)
       {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,0,0,"Sell Entry",MagicNum,0,Red);
//            return;
       }
     }
 else
     {
         if(Golong==true )// && New_Bar_M1==true)
       {      
        ticket=OrderSend(Symbol(),OP_BUY,1,Ask,1,0,0,"Buy Entry",MagicNum,0,Green);
            New_Bar_M1=false;
             }
            
         if(Goshort==true )// && New_Bar_M1==true)
       {
         ticket=OrderSend(Symbol(),OP_SELL,1,Bid,1,0,0,"Sell Entry",MagicNum,0,Red);
            New_Bar_M1=false;
       }
    }
//
total = OrdersTotal();
int i;
for(i = 0; i <= total; i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber() == MagicNum)
{
if (OrderType()==OP_BUY && Goshort==true)OrderClose(OrderTicket(),OrderLots(),Bid,3,Green);
if (OrderType()==OP_SELL && Golong==true )OrderClose(OrderTicket(),OrderLots(),Ask,3,Green);
if (OrderType()>1)OrderDelete(OrderTicket()); 
}
} 

      //-------------------------------------------------------  

      //-------------------------------------------------------  
 

 }
 }
 

Hi - Thanks for earlier advice. Very helpful and I managed to get the EA working (pls see above).

I'm now attempting to modify so that uses limit orders rather than market orders. Am struggling.

Is there perhaps a simplere way to achieve this?

The changes I have made to try and make work as limit orders are as follows - advice appreciated as always. Tks Fracmap.


if(OrdersTotal()>0) 
      {
      OrderSelect(0, SELECT_BY_POS, MODE_TRADES);
                     
      if(Golong==true && OrderType()>0)
       {      
        ticket=OrderSend(Symbol(),OP_BUYLIMIT,2,frac_buy,1,0,0,"Buy Entry",MagicNum,0,Green);
//            return;
             }
            
      if(Goshort==true && OrderType()<1)
       {
         ticket=OrderSend(Symbol(),OP_SELLLIMIT,2,frac_sell,1,0,0,"Sell Entry",MagicNum,0,Red);
//            return;
       }
     }
 else
     {
         if(Golong==true )// && New_Bar_M1==true)
       {      
        ticket=OrderSend(Symbol(),OP_BUYLIMIT,1,frac_buy,1,0,0,"Buy Entry",MagicNum,0,Green);
            New_Bar_M1=false;
             }
            
         if(Goshort==true )// && New_Bar_M1==true)
       {
         ticket=OrderSend(Symbol(),OP_SELLLIMIT,1,frac_sell,1,0,0,"Sell Entry",MagicNum,0,Red);
            New_Bar_M1=false;
       }
    }
//
total = OrdersTotal();
int i;
for(i = 0; i <= total; i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber() == MagicNum)
{
//if (OrderType()==OP_BUYLIMIT && Goshort==true)OrderClose(OrderTicket(),OrderLots(),Bid,3,Green);
//if (OrderType()==OP_SELLLIMIT && Golong==true )OrderClose(OrderTicket(),OrderLots(),Ask,3,Green);
if (OrderType()>1)OrderDelete(OrderTicket()); 
}
} 


 
fracmap:

Hi - Thanks for earlier advice. Very helpful and I managed to get the EA working (pls see above).

I'm now attempting to modify so that uses limit orders rather than market orders. Am struggling.


Struggling in what way ? from a point of view of syntax your code looks OK to me.

For your BUYLIMIT to work frac_buy would have to be below the Ask price when you place the order. Then you have to consider what you will do if price doesn't fall sufficiently to pick up the order ? will you leave it in place indefinitely (you have set Expiration to 0) or will you close it if, for example, the TP level is passed (although you don't have a TP level set) ?

 
RaptorUK:

Struggling in what way ? from a point of view of syntax your code looks OK to me.

For your BUYLIMIT to work frac_buy would have to be below the Ask price when you place the order. Then you have to consider what you will do if price doesn't fall sufficiently to pick up the order ? will you leave it in place indefinitely (you have set Expiration to 0) or will you close it if, for example, the TP level is passed (although you don't have a TP level set) ?



Hi Raptor - Thanks for that. I've now read up on the expiration issue (was all new to me!) and inserted a 3600 expiration as am using hourly data. The limit orders should only be open for the duration of the current bar.

Unfortunately still no trades being executed in the back test period, since I changed the order from market to limit (works perfectly when market orders). I take your other point that the limit orders need to be either above or below the current Bid/Ask in order to be valid, and that is the case.

Thanks again for input.

Fracmap