[Archive!] Any rookie question, so as not to clutter up the forum. Professionals, don't pass it by. Couldn't go anywhere without you - 2. - page 84

 
NameLess:


As you may have noticed, the real reason is not to look at the code, but to understand how it works. If you trust the Expert Advisor, I wish you success and everything will work out)

I personally invented a bicycle and wrote my owl from scratch, but I know where and how things work and know why they don't work and where to look for errors)

that's about it)


Thank you. I'm already on the right track!))) I had to dig around, but I implemented what I wanted (at least it works somehow :))) ).
 
NameLess:
Do you know how to set up order number control? I agree, the question probably brings an undisguised smile, turning into laughter in places.))) But I've only been poking around in mt-sh codes for about a week now. I still do not know all the words )))) Smart )))
 
nemo811:
Do you know how to set up order number control? I agree, the question probably causes an undisguised smile, turning into laughter in some places)))) But I've only been poking around in mt-sh codes for about a week now. I still do not know all the words )))) smart )))


i can't call myself an expert)))

I'm not quite sure what number control means? If you need to open an order twice under certain conditions, you need to create this condition.

In general, I started writing my own program about half a year ago, and one simple detail has helped me: think about what you want the EA to do or a specific piece of code.

In this case, it may be easier to give an example, and then I will be able to say more specifically what to do and how to do it, if I know the solution:)

 

example on page 83, task from sergeev

The code worked - thank you. But when experienced people say such words, you begin to wonder what's wrong.

)))

 
nemo811:

example on page 83, task from sergeev

The code worked - thank you. But when experienced people say such words, you begin to wonder what's wrong.

)))


If the code does what it's supposed to do - then that's it))) what I probably meant was that the code can open orders non-stop, but we did it so that everything opened and deferred once. so it's OK)
 
NameLess:

If the code does what it's supposed to do, then it's OK)) what I probably meant was that the code can open orders non-stop, but we've done it once per opening and deferring.)

Good. Thank you again.
 
nemo811:

By the way, I recommend to put something like

if ( OrdersTotal()!=0)

{

BUY = false;

BUY_STOP = false;

BUY_LIMIT = false;

SELL = false;

SELL_STOP = false;

SELL_LIMIT = false;

}

this is a protection in case you have open positions in case you need to restart the client, otherwise it will reopen a whole order batch

i also recommend to ask: think logically about each line - what can prevent it from working.

 
nemo811:

Here's a slightly outdated version of what I'm currently working on. The author drknn gave me permission to upload it.


He didn't just give permission to put it out there, he openly gave permission to change the code however you want, sell it and even copyright it to whoever you want :)

Just don't get into a fight over rights there :)

 
thank you!
 
NameLess:

By the way, I recommend to put something like

if ( OrdersTotal()!=0)

{

BUY = false;

....

this is a protection in case you have open positions in case you need to restart the client. otherwise, it will reopen a whole order batch

Thank you - this is an important point. I have solved this problem and added counters (thanks to sergeev).

The code now looks as follows (it does not open unnecessary data when restarting the terminal):

//--------------------------------------------------------------------
extern int     MAGIC       = 0;        //уникальный номер ордера
extern bool    BUY         = false;    //открыть ордер BUY
extern bool    BUY_STOP    = false;    //поставить ордер BUY STOP
extern bool    BUY_LIMIT   = false;    //поставить ордер BUY LIMIT
extern bool    SELL        = false;    //открыть ордер SELL
extern bool    SELL_STOP   = false;    //поставить ордер SELL STOP
extern bool    SELL_LIMIT  = false;    //поставить ордер SELL LIMIT
extern double  Lot         = 0.1;      //объем ордера
extern int     takeprofit  = 0;        //уровень выставления TP, если 0, то TP не выставляется
extern int     stoploss    = 0;        //уровень выставления SL, если 0, то SL не выставляется
extern int     DistanceSet = 40;       //расстояние от рынка для отложенника
extern int     Proskalz    = 3;        //максимально допустимое отклонение цены для рыночных ордеров
//--------------------------------------------------------------------
double SL,TP;
//--------------------------------------------------------------------
int start()
{
   if (BUY)
   {
      if (takeprofit!=0) TP  = NormalizeDouble(Ask + takeprofit*Point,Digits); else TP=0;
      if (stoploss!=0)   SL  = NormalizeDouble(Ask - stoploss*Point,Digits); else SL=0;     
      OPENORDER ("Buy");
      BUY=false;
   }
   if (SELL)
   {  
      if (takeprofit!=0) TP = NormalizeDouble(Bid - takeprofit*Point,Digits); else TP=0;
      if (stoploss!=0)   SL = NormalizeDouble(Bid + stoploss*Point,Digits);  else SL=0;              
      OPENORDER ("Sell");
      SELL=false;
   }
   if (BUY_STOP)
   {
      if (takeprofit!=0) TP  = NormalizeDouble(Ask + DistanceSet*Point + takeprofit*Point,Digits); else TP=0;
      if (stoploss!=0)   SL  = NormalizeDouble(Ask + DistanceSet*Point - stoploss*Point,Digits); else SL=0;     
      OPENORDER ("Buy Stop");
      BUY_STOP=false;
   }
   if (SELL_STOP)
   {  
      if (takeprofit!=0) TP = NormalizeDouble(Bid - DistanceSet*Point - takeprofit*Point,Digits); else TP=0;
      if (stoploss!=0)   SL = NormalizeDouble(Bid - DistanceSet*Point + stoploss*Point,Digits);  else SL=0;              
      OPENORDER ("Sell Stop");
      SELL_STOP=false;
   }
   if (BUY_LIMIT)
   {
      if (takeprofit!=0) TP  = NormalizeDouble(Ask - DistanceSet*Point + takeprofit*Point,Digits); else TP=0;
      if (stoploss!=0)   SL  = NormalizeDouble(Ask - DistanceSet*Point - stoploss*Point,Digits); else SL=0;     
      OPENORDER ("Buy Limit");
      BUY_LIMIT=false; 
   }
   if (SELL_LIMIT)
   {  
      if (takeprofit!=0) TP = NormalizeDouble(Bid + DistanceSet*Point - takeprofit*Point,Digits); else TP=0;
      if (stoploss!=0)   SL = NormalizeDouble(Bid + DistanceSet*Point + stoploss*Point,Digits);  else SL=0;              
      OPENORDER ("Sell Limit");
      SELL_LIMIT=false;
   }
return(0);
}
void OPENORDER(string ord)
{
   int SchBuy=SchBuy_b(MAGIC);
   int SchSell=SchSell_b(MAGIC);
   int SchBuyStop=SchBuyStop_b(MAGIC);
   int SchSellStop=SchSellStop_b(MAGIC);
   int SchBuyLimit=SchBuyLimit_b(MAGIC);
   int SchSellLimit=SchSellLimit_b(MAGIC);
   int ticket = -1;
   int err;
   while (ticket<0)
   {
      if (SchBuy<1) {if (ord=="Buy") ticket=OrderSend(Symbol(),OP_BUY, Lot,NormalizeDouble(Ask,Digits),Proskalz,SL,TP,"",MAGIC,0);}
      if (SchSell<1) {if (ord=="Sell") ticket=OrderSend(Symbol(),OP_SELL,Lot,NormalizeDouble(Bid,Digits),Proskalz,SL,TP,"",MAGIC,0);}
      if (SchBuyStop<1) {if (ord=="Buy Stop") ticket=OrderSend(Symbol(),OP_BUYSTOP, Lot,NormalizeDouble(Ask + DistanceSet*Point,Digits),Proskalz,SL,TP,"",MAGIC,0);}
      if (SchSellStop<1) {if (ord=="Sell Stop") ticket=OrderSend(Symbol(),OP_SELLSTOP,Lot,NormalizeDouble(Bid - DistanceSet*Point,Digits),Proskalz,SL,TP,"",MAGIC,0);}
      if (SchBuyLimit<1) {if (ord=="Buy Limit") ticket=OrderSend(Symbol(),OP_BUYLIMIT, Lot,NormalizeDouble(Ask - DistanceSet*Point,Digits),Proskalz,SL,TP,"",MAGIC,0);}
      if (SchSellLimit<1) {if (ord=="Sell Limit") ticket=OrderSend(Symbol(),OP_SELLLIMIT,Lot,NormalizeDouble(Bid + DistanceSet*Point,Digits),Proskalz,SL,TP,"",MAGIC,0);}      
      if (ticket==-1) //неудачная попытка
      {  
         ShowERROR();
         err++;Sleep(2000);RefreshRates();
      }
      if (ticket || err >10) return;
   }
return;
}                                    
//--------------------------------------------------------------------
void ShowERROR()
{
   int err=GetLastError();
   switch ( err )
   {                  
      case 1:   return;
      case 2:   Alert("Нет связи с торговым сервером ",Symbol());return;
      case 3:   Alert("Error неправильные параметры ",Symbol());return;
      case 130: Alert("Error близкие стопы   Ticket ",Symbol());return;
      case 134: Alert("Недостаточно денег   ",Symbol());return;
      case 146: Alert("Error Подсистема торговли занята ",Symbol());return;
      case 129: Alert("Error Неправильная цена ",Symbol());return;
      case 131: Alert("Error Неправильный объем ",Symbol());return;
      case 4200:Alert("Error Объект уже существует ",Symbol());return;
      default:  Alert("Error  " ,err," ",Symbol());return;
   }
}

+ appropriate counters.

Thank you.