Help with EA code

 

Godd evining,

I'm newbie in programming EA. I'd like to to an EA that does that

- If price is under lower BB and volumes is higher than a certain parameter it must buy;
- If price is upper Higher BB and volumes is higher than a certain parameter it must sell;

I programmed also a auto closed operation when price is under/upper the lower/higher BB for sell/buy position

Here is my code


//------------------------------------------------------------------------------------------
input int    MagicNo=1234;
input bool   Use_CloseAtNext=true;
input int    Spread=8;
 double bolinger_upper_band=iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_UPPER,1);
  double bolinger_lower_band=iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_LOWER,1);
 double bolinger_upper_band2=iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_UPPER,0);
  double bolinger_lower_band2=iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_LOWER,0);
  input int volumes=230;
extern double  Lots=0.05; 

input string Takeprofit_Stoploss____________________="Takeprofit_Stoploss_____________";
input double Takeprofit=0, 
             Stoploss=0;
             


//+------------------------------------------------------------------+
//|                         Local variables                          |
//+------------------------------------------------------------------+
datetime BarTime;
double   point;
int      Q;
int      Lot_Decimal;
//+------------------------------------------------------------------+
//|                          Expert init                             |
//+------------------------------------------------------------------+
int OnInit()
{
 PrintInfoToChart();
 if(_Digits==5||_Digits==3)Q=10;else Q=1;                            
 if(_Digits<4)point=0.01;else point=0.0001;if(_Digits<=2)point=1;
  if(MarketInfo(Symbol(),MODE_MINLOT)<0.1)Lot_Decimal=2;else Lot_Decimal=1;
 return(INIT_SUCCEEDED);    
}

//+----------
void PrintInfoToChart()
{
 string temp="";
 Comment(temp);
}

//+------------------------------------------------------------------+
//|                          Expert deinit                           |
//+------------------------------------------------------------------+
void OnDeinit(const int reason){Comment("");}

int Orderscnt(int type=-1)
{
 int cnt=0;
 for(int i=0;i<OrdersTotal();i++)
 {
  if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
  {
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNo&&(OrderType()==type||type==-1))
   {
    cnt++;
   }
  }
 }
 return(cnt);
} 






static double bb=0,ttt=0;
//+------------------------------------------------------------------+
//|                           Expert start                           |
//+------------------------------------------------------------------+
void OnTick()
{ 

 
 if(Use_CloseAtNext)
 {
  if(Orderscnt(OP_SELL)>0&&iClose(0,0,1)<bolinger_lower_band2){CloseDeleteOrders(OP_SELL);}
  
  if(Orderscnt(OP_BUY)>0&&iClose(0,0,1)>bolinger_upper_band2){CloseDeleteOrders(OP_BUY);} 
  }

 //-----------------------BUY Order
if (
iVolume(Symbol(),PERIOD_M1,1)>volumes&&(Ask-Bid)<Spread*point&&iClose(0,0,1)<bolinger_lower_band
)
 {

  if((MaxTrades>0&&Orderscnt()<MaxTrades)||MaxTrades==0)
  {
 
   if(Stoploss!=0){SL=Bid-Stoploss*point;}else SL=0;
   if(Takeprofit!=0){TP=Bid+Takeprofit*point;}else TP=0;
   ticket=OrderSend(Symbol(),OP_BUY,Lots,NormalizeDouble(Ask,Digits),5*Q,SL,TP,"DS",MagicNo,0,Blue);  
   BarTime=Time[0];}

 }
 //-----------------------SELL Order
if (
iVolume(Symbol(),PERIOD_M1,1)>volumes&&(Ask-Bid)<Spread*point&&iClose(0,0,1)>bolinger_upper_band
)
 {

  if((MaxTrades>0&&Orderscnt()<MaxTrades)||MaxTrades==0)
  {
 
   if(Stoploss!=0){SL=Ask+Stoploss*point+(Ask-Bid);}else SL=0;
   if(Takeprofit!=0){TP=Ask-Takeprofit*point;}else TP=0;
   ticket=OrderSend(Symbol(),OP_SELL,Lots,NormalizeDouble(Bid,Digits),5*Q,SL,TP,"DS",MagicNo,0,Red); 
   BarTime=Time[0];}
  
 }  
 
}


void CloseDeleteOrders(int type=-1)
{
 bool select,close,del;
 for(int i=OrdersTotal()-1;i>=0;i--)
 {
  select=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
  string sy=OrderSymbol();
  int    tt=OrdersTotal(),
         tk=OrderTicket(),
         ot=OrderType(),
         mn=OrderMagicNumber();
  double lo=OrderLots();
  
  if(tt>0&&sy==Symbol()&&mn==MagicNo)
  {
   if(ot==type||type==-1)
   {
    if(ot==OP_BUY)close=OrderClose(tk,lo,NormalizeDouble(Bid,Digits),3*Q);       
    if(ot==OP_SELL)close=OrderClose(tk,lo,NormalizeDouble(Ask,Digits),3*Q);
    if(ot>OP_SELL)del=OrderDelete(tk);
   } 
  }
 }
}



int OrderCloseCount(double openprice,datetime opentime)
{
 int closecount=0;
 int i=0;
 while(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
 {
  i++;
  if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNo)
  {
   if(OrderOpenPrice()==openprice&&OrderOpenTime()==opentime)closecount++;
  }
 } 
 return(closecount);   
}


It doesn't close the position in the right way.

There's somebody can help me?

Thanks a lot

 
Claudio Lasso:

It doesn't close the position in the right way.

You need to read out the indicator values on every tick. If you put it to the top it means you are defining it globally. Global variables are initialized only once upon startup.

Then you need to normalize SL and TP. And check the return code of OrderSend, if it's -1 it means an error occurred.
 
If you have problems with closing maybe try to SELECT_BY_TICKET and maybe try to store ticket[i] in an array