[WARNING CLOSED!] Any newbie question, so as not to clutter up the forum. Professionals, don't go by. Can't go anywhere without you. - page 867

 

Dear experts. I am learning to program. I made my first EA, but it opens a deal and does not do anything else. It has not written an error in the journal. I need your help!

//+------------------------------------------------------------------+
//|                                                Monetny dvor1.mq4 |
//|                                                            fanat |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "fanat"
#property link      "http://www.metaquotes.net"

extern double Lot=0.1;          //Рабочий лот
extern double TakeProfit=149;    //
extern double StopLoss=275;      //          
extern int Total=1;             //Колличество ордеров   
//+------------------------------------------------------------
int start()
{//1
if(TakeProfit<10)
{//2 
  Alert("Ошибка,TakeProfit слишком близко");
  return;
}//2
     
if(StopLoss<10)
{//2
  Alert("Ошибка,StopLoss слишком близко");
  return;
}//2

if(Total<1)
{//2
  Alert("Ошибка, неизвестно колличество ордеров");
  return;
}//2
bool Buy=false;
bool Sell=false;
bool BuyClose=false;
bool SellClose=false;                                                                             
if(iHigh(NULL,PERIOD_M15,2)<iClose(NULL,PERIOD_M15,1))Buy=true;
if(iLow(NULL,PERIOD_M15,2)>iClose(NULL,PERIOD_M15,1))Sell=true;
if(iClose(NULL,PERIOD_M15,2)>iClose(NULL,PERIOD_M15,1))BuyClose=true;
if(iClose(NULL,PERIOD_M15,2)<iClose(NULL,PERIOD_M15,1))SellClose=true;
if(Buy==false&&Sell==false&&BuyClose==false&&SellClose==false)return;
int err;                                                 //
int ticket;                                              //
int Order_Count[6];                                      //
int OP=-1;                                               //
ArrayInitialize(Order_Count,0);                          //
                                                          //Блок учета ордеров
for(int i=1; i<=OrdersTotal(); i++)                      //
{//2                                                     //
  if(OrderSelect(i-1, SELECT_BY_POS)==true)               // 
  Order_Count[OrderType()]++;                             //
  if(OP!=-1) return(Order_Count[OP]);                     //                         
}//2
  if(OrderSelect(ticket,SELECT_BY_TICKET))                //Закрытие ордеров Sell
{//2                                                     // 
  if((OrderType()==1)&&(Buy==true||SellClose==true))      //  
  {//3                                                    //
  OrderClose(ticket,Lot,Ask,4);                           //
  err=GetLastError();                                     //Обработка ошибок 
  if(err==0)                                              //
  return;                                                 //  
  else                                                    //
  {//4                                                    //
  Sleep(1000);                                            //
  Alert(err);                                             //
  }//4                                    
  }//3
}//2 
  if(OrderSelect(ticket,SELECT_BY_TICKET))                //Закрытие ордеров Buy
{//2                                                     //
  if((OrderType()==0)&&(Sell==true||BuyClose==true))      // 
  {//3                                                    //  
  OrderClose(ticket,Lot,Bid,4);                           // 
  err=GetLastError();                                     //Обработка ошибок
  if(err==0)                                              //
  return;                                                 //
  else                                                    //
  {//4                                                    //
  Sleep(1000);                                            //
  Alert(err);                                             //
  }//4
  }//3
}//2 
if(Order_Count[OP_BUY]>=Total&&Order_Count[OP_SELL]>=Total) return;                  
                                                                                      
if(Order_Count[OP_BUY]<Total&&Order_Count[OP_SELL]==0&&Buy==true)                    //Открытие ордеров Buy
{//2                                                                                 //
  OrderSend(Symbol(), OP_BUY, Lot, Ask, 4, Ask-StopLoss*Point, Ask+TakeProfit*Point); // 
  err=GetLastError();                                                                 //Обработка ошибок
  if(err==0)                                                                          //
  return;                                                                             //
  else                                                                                //
  {//3                                                                                // 
  Sleep(1000);                                                                        //
  Alert(err);                                                                         //
  }//3
}//2         
           
if(Order_Count[OP_SELL]<Total&&Order_Count[OP_BUY]==0&&Sell==true)                   //Открытие ордеров Sell 
{//2                                                                                 //
  OrderSend(Symbol(), OP_SELL, Lot, Bid, 4, Bid+StopLoss*Point, Bid-TakeProfit*Point);//
  err=GetLastError();                                                                 //Обработка ошибок
  if(err==0)                                                                          //
  return;                                                                             //
  else                                                                                //
  {//3                                                                                //
  Sleep(1000);                                                                        //
  Alert(err);                                                                         //
  }//3
}//2

return;
}//1
 

Can you tell me how to set a trade prohibit clause?

For example: if iStochastic >80 then nottreid (trade prohibited (or position opening prohibited)?

 
fanat:

Dear experts. I am learning to program. I made my first Expert Advisor, but it opens a deal and does not do anything else. It has not written an error in the journal. I need your help!


Hell of a mix, but it works (in the tester)

 
Vinin:


Hell of a mix, but it works (in the tester)


I realise that the conditions for opening a deal are silly.
 
Vinin:


Hell of a mix, but it works (in the tester)


Build 226. Mine doesn't even work in the tester. Can you tell me why?
 
fanat:

Build 226. It doesn't even work in my tester. Can you tell me why?

I don't know why it doesn't work for you. The code works
 
fanat:

Build 226. It doesn't even work in my tester. Can you tell me why?

Download the terminal elsewhere the same Build 226.

 
Vinin:

Why it doesn't work for you, I don't know. The code works.

Got it. Thank you. I'll try reinstalling MT.
 

Dear experts. I sent the code this morning. It has conditions for closing trades by the Expert Advisor itself. But it closes trades solely on TP or SL. And if I set the TP and SL to 900, it opens only one trade and that's it. I am asking for help.

 
fanat:

Dear experts. I sent the code this morning. There are conditions for closing trades by the Expert Advisor itself. But it closes trades solely on TP or SL. And if I set the TP and SL to 900, it opens only one trade and that's it. I am asking for help.


How else can it be if ticket is always 0. It has to be preassigned to something

For example

   int ticket;        

   for(int i=0 i<OrdersTotal(); i++)                      
      if(OrderSelect(i, SELECT_BY_POS))
         ticket=OrderTicket();
Reason: