can't make trade

 
//+------------------------------------------------------------------+
//|                                                GBPUSD trader.mq4 |
//|                      Copyright © 2011, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "sergey antipin"
#property link      "http://www.metaquotes.net"



extern double buffer=0;           
extern double stoploss=20;            
extern double takeprofit=350;                
extern double filter=50;               
extern double ma_value=218;  
double max_account_balance=0;
double max_spread=4;   
extern double Z=1;



double M=1;  // risk 
bool price_above=false;
bool trade_ok=true;
int ticket1=-1;


//------------------------------------------------------------------------------------------------~~~~~~!!!!!!!!!!!!!!!!






//------------------------------------------------------------------------------------------------~~~~~~!!!!!!!!!!!!!!!!

     
double entryprice=0;
int K = 1 ;  // point * K   (in demo account use 10, in real account use 1)






int init()
  {
if ( Ask > iMA(Symbol(),15,ma_value,0,MODE_SMA,PRICE_TYPICAL,0) )
price_above=true;

   return(0);
  }
int deinit()
  {
   return(0);
  }
  
  
  
  
int start()
  {
  

  
  
double SMA_10day=iMA(Symbol(),15,ma_value,0,MODE_SMA,PRICE_TYPICAL,0);  //  simple ma_value day moving average value 
double ATR=iATR(Symbol(),60,200,0);
//double adx=iADX(Symbol(),30,14,PRICE_TYPICAL,MODE_SMA,0);

//----- risk
//Alert("lot size:",MarketInfo(Symbol(), MODE_LOTSIZE));
//Alert("minlot:",MarketInfo(Symbol(), MODE_MINLOT));
//Alert("lot step:",MarketInfo(Symbol(), MODE_LOTSTEP));
//Alert("max lot:",MarketInfo(Symbol(), MODE_MAXLOT));


//M = MathRound ( MathLog (AccountBalance()/(367.88/M+1) ) / MathLog (2.72)  );


M=(AccountBalance()*0.005*Z)/(0.1*stoploss) ;  // x% risk per trade

if (M<1) M=1;




if (AccountBalance()>max_account_balance)
max_account_balance=AccountBalance();

//-----end of risk



//---------------- open new trades

//--- * short trade




if  (trade_ok==true)

{

if ( ( Bid- SMA_10day  < buffer*Point*K ) && ( price_above==true) && (ATR<0.0040) )     
  
  {  

     ticket1=-1;
     
   Alert(MarketInfo(Symbol(),MODE_ASK)-MarketInfo(Symbol(),MODE_BID),"short trade"," atr=", ATR);
      
    
     while (MarketInfo(Symbol(),MODE_ASK)-MarketInfo(Symbol(),MODE_BID)<max_spread*Point*K)  // spread < 4
     {
  
     RefreshRates();
     ticket1 = OrderSend(Symbol() , OP_SELL , 0.01*M , Bid , 2 , Ask+stoploss*Point*K , Ask-takeprofit*Point*K); 
     Sleep(2000);
     if (ticket1>0)
     { 
     entryprice=Bid; 
     break;
     }
     }

trade_ok=false; 

  }
  
    //--- * end of short trade
     
     else 
     
    
       
//--- * long trade

if ( ( SMA_10day -  Ask < buffer*Point*K ) && (price_above==false) && (ATR<0.0040))     
  
  {   
          Alert(MarketInfo(Symbol(),MODE_ASK)-MarketInfo(Symbol(),MODE_BID),"long trade"," atr=", ATR,price_above);
     ticket1=-1;
     

     

     while (MarketInfo(Symbol(),MODE_ASK)-MarketInfo(Symbol(),MODE_BID)<max_spread*Point*K) //spread < 4
     {

     RefreshRates();     
     ticket1 = OrderSend(Symbol() , OP_BUY , 0.01*M , Ask , 2 , Bid-stoploss*Point*K , Bid+takeprofit*Point*K);
     Sleep(2000);
     if (ticket1>0)
     {
     entryprice=Ask;
     break;
     } 
     }
     

     trade_ok=false;    
  }

//--- * end of long trade
  
}
  

//---- end of open new trades




//-----x pip noice filter
          
       if (trade_ok==false)
      {
         if  (price_above==true)  //-- price currently above 
           
           {
                if (Bid < entryprice - filter*Point*K)       //-- price moved below and is not random noice
                
                  {
                       price_above=false;
                       trade_ok=true;
                  }
                  
                  
              else
              
               
                if (Ask > entryprice + filter*Point*K)     //-- price stayed above and is not random noice
                   trade_ok=true;
               
           }

      else


         if (price_above==false)  //-- price currently below
  
           {

                if (Ask > entryprice + filter*Point*K)  //-- price moved above and is not random noice
  
                  {
                      price_above=true;
                      trade_ok=true; 
                  }   
   
             else
               
               if (Bid < entryprice - filter*Point*K)  //-- price stayed below and is not random noice
               trade_ok=true;
          
           }
        }   
  
  //----- end of filter
  
  
   return(0);
  }
  
  

hi !

until ATR went above 0.0040

the program used to work good !

now after ATR went below 0.0040 and the conditions for a trade are met it fails to make trades

please help

 

I ran your EA in the Strategy tester on GBPUSD from 1st Jan 2011 to 19 August 2011 (last Friday), it didn't start making any orders until 25th July and it placed 20 orders.

Did you really mean to have a TP of 350 pips ?

I don't think your K variable is doing what you would like it to do, check it's effect on your spread check and it's effect on the TP

 
bool price_above=false;
int init(){
   if ( Ask > iMA(Symbol(),15,ma_value,0,MODE_SMA,PRICE_TYPICAL,0) )
      price_above=true;
  1. Each time you change pair, TF, or refresh the chart, you go through a deinit/init cycle. The EA is NOT reloaded. Thus once price_above gets set to true it will ALWAYS be true.
  2. Don't you want that variable to be correct on every tick? Must be in start() then
    price_above = ( Ask > iMA(Symbol(),15,ma_value,0,MODE_SMA,PRICE_TYPICAL,0) );

  3. int K = 1 ;  // point * K   (in demo account use 10, in real account use 1)
    
    Not true. It depends on both the broker and the account type. IBFX has both 4 and 5 digit real accounts (last time I looked.) Don't hard code. Also you must adjust ALL pip values and that includes SLIPPAGE
    //++++ These are adjusted for 5 digit brokers.
    int     pips2points;    // slippage  3 pips    3=points    30=points
    double  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
    

  4. M=(AccountBalance()*0.005*Z)/(0.1*stoploss) ;  // x% risk per trade
    If you open one lot on EURUSD with a 10 pip stop loss you are risking 10 pips * $10/pip/lot or $100. If you open the same one lot with a 100 pip stop loss you are risking $1000. Eventually you're going to have to use a real calculation, see my Lotsize code.
  5. (ATR<0.0040)
    This makes the EA incompatible with any JPY pair. Don't hard code numbers: ATR < 40*pips2dbl
 
WHRoeder:
  1. Each time you change pair, TF, or refresh the chart, you go through a deinit/init cycle. The EA is NOT reloaded. Thus once price_above gets set to true it will ALWAYS be true.
  2. Don't you want that variable to be correct on every tick? Must be in start() then
  3. Not true. It depends on both the broker and the account type. IBFX has both 4 and 5 digit real accounts (last time I looked.) Don't hard code. Also you must adjust ALL pip values and that includes SLIPPAGE
  4. If you open one lot on EURUSD with a 10 pip stop loss you are risking 10 pips * $10/pip/lot or $100. If you open the same one lot with a 100 pip stop loss you are risking $1000. Eventually you're going to have to use a real calculation, see my Lotsize code.
  5. This makes the EA incompatible with any JPY pair. Don't hard code numbers: ATR < 40*pips2dbl

thanks a lot !!!!!
Reason: