Download MetaTrader 5

Problem on simple ea

To add comments, please log in or register
crazyfede
12
crazyfede  

Hi guys, I got a problem with a simple EA (just few days I started in free-time).

Here below the code.

My original intention was to open first 2 pending orders and then at  new bar coming modify the 2 pendings orders adjusting the price values.

Actually the program each bar open 2 new orders without closing or modify the first two and I don't understand the reason.. why  do at new bar ticketB and ticketS  become 0 ?

thank you very much for your help 

 

 

extern double    TakeProfit = 0.0090;
          
extern double    Lots=0.2;

extern double    A = 0.0020;

extern double    slippage = 15;

extern int       intervallo = 10;

extern int       barra_attuale = 0;

int        ticketB=0;
int        ticketS=0;





//+------------------------------------------------------------------+
//| inizializzazione programma e setting valute di cambio            |
//+------------------------------------------------------------------+


int init()
  {
  
 // Funzione per cambio valuta di trading 
 Alert(Symbol(),Ask,Bid);
 bool verifica=ChartSetSymbolPeriod(ChartID(),"EURUSD",PERIOD_M1);
 return(0);
}
 
 
 
 

 
 
  
//+------------------------------------------------------------------+
//| deinizializzazione programma                                     |
//+------------------------------------------------------------------+


int deinit()
  {

   return(0);
  }



//+------------------------------------------------------------------+
//| definizione funzioni e oggetti                                   |         |
//+------------------------------------------------------------------+




   

//+------------------------------------------------------------------+
//| PARTE PRINCIPALE                                          |
//+------------------------------------------------------------------+



int start()
  {
  
 //Apertura dei primi ordini limite all inizio da modificare ogni barra

if (Symbol()=="EURUSD" && ticketB==0 && ticketS==0)

{
 
 ticketB=OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+A,slippage,Ask,Ask+TakeProfit,"curva in salita",12345,0,Green);
 ticketS=OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-A,slippage,Bid,Bid-TakeProfit,"curva in discesa",12345,0,Red); 
 barra_attuale = Bars;
 
 
if(ticketB>0){
if(OrderSelect(ticketB,SELECT_BY_TICKET,MODE_TRADES)) Alert("ordine acquisto andato a buon fine al prezzo : ",OrderOpenPrice());
else Alert("Errore nell'inoltro ordine : ",GetLastError());
}

if(ticketS>0){
if(OrderSelect(ticketS,SELECT_BY_TICKET,MODE_TRADES)) Alert("ordine vendita andato a buon fine al prezzo : ",OrderOpenPrice());
else Alert("Errore nell'inoltro ordine : ",GetLastError());    

}}  



if(OrderSelect(ticketB, SELECT_BY_POS)==true)
int order_typeB=OrderType();

if(OrderSelect(ticketS, SELECT_BY_POS)==true)
int order_typeS=OrderType();



//  Criteri di chiusura ordini

if (ticketB != 0 && ticketS != 0 && (order_typeB==0 || order_typeS==1) && Bars>barra_attuale && (Ask<High[1] || Bid>Low[1])){ 

if(OrderSelect(ticketB, SELECT_BY_TICKET)==true) {
OrderClose(ticketB,Lots,Bid,slippage,Red);       }


if(OrderSelect(ticketS, SELECT_BY_TICKET)==true) {
OrderClose(ticketS,Lots,Bid,slippage,Red);       }

ticketB = 0;
ticketS = 0;
}




//Modifica dei valore limiti ordini ogni volta che finisce una candela




if (Bars != barra_attuale)   {

if(OrderSelect(ticketB, SELECT_BY_TICKET)==true){
OrderModify(ticketB,Ask+A,Ask,Ask+TakeProfit,0,Blue);}
Alert("ticketb",ticketB); 

if(OrderSelect(ticketS, SELECT_BY_TICKET)==true){
OrderModify(ticketS,Bid-A,Bid,Bid-TakeProfit,0,Blue);}

barra_attuale = Bars;}


}
To add comments, please log in or register