[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 29

 

Hello...

I wrote an EA, but ran into this problem:

I have opposite orders and e.g. for BUY I have 150 pips stop and 150 pips profit, for SELL the same levels are set, but only reversed (i.e. what is for buy profit, then for sell stop), i.e. actually I have to close everything simultaneously, both sell with buy and buy with sell..... but there is another thing, spread!))) And from those two opposite orders the other 8 are already opened (of course if all eight are needed, the conditions are different for them, they are also opposite 4/4)


How can I correctly program the code to close all the orders when they reach the same price simultaneously? For in the tester, when I test, the price sometimes reaches a desired level, for example, sell closed (not reaching a predetermined level, I believe it is a spread) and buy hanging and after a short time falls and buy closed at the stop, so I can not see the correct result.


I would be grateful if you could express your idea programmatically, i.e. code.


In order to understand it in more detail, I give you the part where my two opposite (first) orders are opened.

if (by == FALSE) 
   {   g_ticket_112 = OrderSend(Symbol(), OP_BUY, Lots, NormalizeDouble(Ask, Digits), 1, NormalizeDouble(Ask - SL * Point, Digits), 
       NormalizeDouble(Ask + TP * Point, Digits), "", 111, 0, Blue);
   if (g_ticket_112 > 0) by = TRUE;
   } 
     
     if (se == false) 
   {for (g_pos_116 = OrdersTotal() - 1; g_pos_116 >= 0; g_pos_116--) 
   {if (OrderSelect(g_pos_116, SELECT_BY_POS, MODE_TRADES)) 
   {if (OrderSymbol() != Symbol() || OrderMagicNumber() != 111) continue;
    if (se == false) 
   {g_ticket_113 = OrderSend(Symbol(), OP_SELL, Lots, NormalizeDouble(Bid, Digits), 1, 
    OrderTakeProfit(),OrderStopLoss() , "", 222, 0, Blue);
    if (g_ticket_113 > 0) se = TRUE;}}}} 
 
Do you guys have a script that stretches the schedule over all weekends, that is, that the schedule does not merge with Monday, and remained empty days Saturday and Sunday.
 

Hi all.

The question is, do you have a function that detects the opening of a new bar(no matter what timeframe), or, more precisely, different variants of finding a new bar.

 
dmitriy086:

Hi all.

The question is, do you have a function that detects the opening of a new bar (no matter what timeframe), or, more precisely, can different variants (trailers) find a new bar.


Remember time oldtime = Time[0] and compare the remembered time if( oldtime !=Time[0])Print("Hurray for a new bar!!!");

SZY: trailers are in iTime() function.

:)

 
//+------------------------------------------------------------------+
bool fNewBar()
  {
   static datetime NewTime=0;
   if(NewTime!=iTime (Symbol(),0,0))
    {
     if(NewTime==0)
      {
       NewTime=iTime (Symbol(),0,1);return(false);
      }      
     NewTime=iTime (Symbol(),0,0);
     return(true);
    }
   return(false);     
  }
//+----------------------------------------------------------------------------+

IgorM, the problem is that this f-fi is on a synthetic chart where iTime[0] can be equal to iTime[1], so how can we get rid of time so that the Expert Advisor opens only one trade on a new bar?
 
dmitriy086:
IgorM, the problem is that this f-fi is on a synthetic chart where iTime[0] may be equal to iTime[1], so how can we get rid of time so that the EA opens only one trade on a new bar?


if the time of bars 0 and 1 cannot be matched for some unknown reason - datetime type stores data in seconds, change the bar time by at least 1 second

then try to remember all characteristics of the first closed bar, i.e. OHLC, volume and open time - if you change even one parameter, it means a new bar

iBars() will show the number of bars in the current timeframe, and will be limited by the terminal settings, you can try to compare the number of bars with the stored bar number and at the moment of a new bar appearance, these values are different - but not always, it worked for me this way, but probably because of the MT4 build number this option may not work

 
Do you have a ready-made piece of code, by any chance? I can't figure out how to remember OHLC[1] and compare it with OHLC[0]...
 
dmitriy086:
You don't have a ready piece of code, do you? I can't figure out how to memorize OHLC[1] and compare it with OHLC[0]...


OHLC is a common acronym for Open,High,Low,Close

compare not with zero, but the first one with the one memorised earlier, i.e.

init(){

oldHigh = High[1];

///

}

start(){

double tmp = High[1];

if(tmp !=oldHigh) {oldHigh = tmp;

//check the rest of the bar parameters

}

}

 

Thanks for the tip I'll look into it...

 

Please advise how to set the correct check in the position opening condition - whether there is an open position for this instrument or not. And if there is a position for the symbol (any sell or buy), then the opening does not take place, otherwise the signal to open the position is triggered?

Here is the code (I think it is responsible for opening positions):

if (NumberOfPositions("" ,-1,Magic)>=2)  флаг1=1;//если число позиций >= 2
if (NumberOfPositions("" ,-1,Magic)<1 )  флаг1=0;//если нет откр. позиций
if ( TradeDOWN==true && флаг1==0) {//если условия соответствуют заданным  
//продаем 1-й символ и покупаем второй символ
//----------------------------------------
if (NumberOfPositions(Symbol_1,OP_SELL,Magic)<1){//если нет поз.селл и поз.бай по 1-му символу
 if (NumberOfPositions(Symbol_1,OP_BUY,Magic)<1)
 {
 SL=0;TP=0;//открываем позицию, задаем стопы :
if(StopLoss_1>0)   SL=Bid_Tiker1+POINT_Tiker1*StopLoss_1;
if(TakeProfit_1>0) TP=Bid_Tiker1-POINT_Tiker1*TakeProfit_1; 
 ti=OpenPosition(Symbol_1, OP_SELL, Lots_1,0 ,0,Magic);
if(StopLoss_1>0 || TakeProfit_1>0) {//ставим стопы 
if (OrderSelect(ti, SELECT_BY_TICKET)) ModifyOrder(-1, SL, TP, clModifySell);}
                                   }}//если нет поз.селл и поз.бай по 1-му символу
//--------------------------------------
if (NumberOfPositions(Symbol_2,OP_BUY,Magic)<1){//если нет поз. бай и поз. селл по 2-му символу 
 if (NumberOfPositions(Symbol_2,OP_SELL,Magic)<1)
 {
   SL=0;TP=0;//открываем позицию, задаем стопы :
if(StopLoss_2>0)   SL=Bid_Tiker2-POINT_Tiker2*StopLoss_2;
if(TakeProfit_2>0) TP=Ask_Tiker2+POINT_Tiker2*TakeProfit_2;   
ti=OpenPosition(Symbol_2, OP_BUY, Lots_2,0,0,Magic);
if(StopLoss_2>0 || TakeProfit_2>0) {//ставим стопы 
if (OrderSelect(ti, SELECT_BY_TICKET))ModifyOrder(-1, SL, TP, clModifyBuy); }
                                   }}//если нет поз. бай и поз. селл по 2-му символу

It seems to be correct by logic, but it still opens several positions for one symbol.

P.S. I am not the programmer. If it is possible in more details explain to the dummie.

Reason: