And let's make a "cartoon" out of it (multicurrency) - page 7

 
Swan >> :

It was

When StopLoss is true, the new value is assigned,

otherwise the order is modified.

no error, but you have to take off either a cross or put on trousers).

I didn't get it right away))... the condition might not cause a stop!

Maybe we should just remove else? It seems that logic does not suffer... Why do I need the second condition if only one is enough?

StopLoss=MathMin( s0, s1);//Функция возвращает минимальное из двух числовых значений
if( StopLoss-Ask< StopLevel-0.5*Point) StopLoss=MathMax( s0, s1);
OrderModify(OrderTicket(),OrderOpenPrice(), StopLoss,OrderTakeProfit(),0,Red);

Or so...

if( StopLoss-Ask< StopLevel-0.5*Point) StopLoss=MathMax( s0, s1);
if( StopLoss-Ask> StopLevel-0.5*Point) StopLoss=MathMin( s0, s1);
OrderModify(OrderTicket(),OrderOpenPrice(), StopLoss,OrderTakeProfit(),0,Red);


Swan >> :

see comparison of real numbers.

I will read it... So, is it absolutely necessary? I've just never done it).

 
ALex2008 >> :

I didn't get it right away... The condition might not cause a stop!

Can't we just remove else? Seems like logic does not suffer...

Maybe) 4h candle is usually larger than StopLevel...


ALex2008 >> :

I'll read it... I mean, is it really necessary? I've just never done it that way)

all sorts of weird things happen when compared values are almost equal)

when checking a double-type number for equality - mandatory.


 StopLoss-Ask< StopLevel-0.5*Point

In Russian: StopLoss minus Ask is less than StopLevel with accuracy 0.5*Point

(StopLoss,Ask,StopLevel are normalized)

 
StopLoss=MathMin( s0, s1);//Функция возвращает минимальное из двух числовых значений
if( StopLoss-Ask< StopLevel-0.5*Point) StopLoss=MathMax( s0, s1);
//StopLoss может присвоиться новое значение, желательно и его проверить на StopLevel
OrderModify(OrderTicket(),OrderOpenPrice(), StopLoss,OrderTakeProfit(),0,Red);
It's a bit of a mess, but you have to think about it.)
 

So what about this option? It seems reasonable to keep it...

if( StopLoss-Ask< StopLevel-0.5*Point) StopLoss=MathMax( s0, s1);
if( StopLoss-Ask> StopLevel-0.5*Point) StopLoss=MathMin( s0, s1);
OrderModify(OrderTicket(),OrderOpenPrice(), StopLoss,OrderTakeProfit(),0,Red);

We should also check the trade context, otherwise we get errors that it is busy - TradeDispatcher: trade context is busy ... It turns out that if there are a lot of copies of the EA, all of them simultaneously close the current order on their symbols and set new ones... a traffic jam will form)

this is probably how it works...

//-------Поиск входа для установки ордеров, удаление старых ордеров и установка новых
void UpTrend(){
     if((iOpen(NULL,PERIOD_H4,1) - iClose(NULL,PERIOD_H4,1) <= 0) &&
        (iOpen(NULL,PERIOD_H4,2) - iClose(NULL,PERIOD_H4,2) > 0)){
         Enter=iHigh(NULL,PERIOD_H4,1)+(Ask-Bid)+10*Point;
         if(IsTradeAllowed()){
            DellAllOrders();
            if(Ask< Enter- StopLevel+0.5*Point){
               OrderSend(Symbol(), OP_BUYSTOP, 0.1, Enter, 0, 0, Enter+ Profit, 0, 0,0, Green);}
         else Sleep(1000);
         }
      }
  }
void DownTrend(){
     if((iOpen(NULL,PERIOD_H4,1) - iClose(NULL,PERIOD_H4,1) >= 0) &&
        (iOpen(NULL,PERIOD_H4,2) - iClose(NULL,PERIOD_H4,2) < 0)){
         Enter=iLow(NULL,PERIOD_H4,1)-10*Point;
         if(IsTradeAllowed()){
            DellAllOrders();
            if(Bid> Enter+ StopLevel-0.5*Point){
               OrderSend(Symbol(), OP_SELLSTOP, 0.1, Enter, 0, 0, Enter- Profit, 0, 0,0, Green);}
         else Sleep(1000);
         }
      }
  }

I.e. before closing the current order and setting a new pending order, we check the trade flow... If it's busy, pause for 1 second.

 
ALex2008 >> :

So what about this option? Seems sensible to keep it...

if( StopLoss-Ask< StopLevel-0.5*Point) StopLoss=MathMax( s0, s1);
if( StopLoss-Ask> StopLevel-0.5*Point) StopLoss=MathMin( s0, s1);
OrderModify(OrderTicket(),OrderOpenPrice(), StopLoss,OrderTakeProfit(),0,Red);

No, it's not. We have to make up our minds.)

three options:

            StopLoss=MathMin( s0, s1);//Функция возвращает минимальное из двух числовых значений
            if( StopLoss-Ask< StopLevel-0.5*Point) StopLoss=MathMax( s0, s1);
            if( StopLoss-Ask> StopLevel-0.5*Point)
            OrderModify(OrderTicket(),OrderOpenPrice(), StopLoss,OrderTakeProfit(),0,Red);

            StopLoss=MathMin( s0, s1);//Функция возвращает минимальное из двух числовых значений
            if( StopLoss-Ask< StopLevel-0.5*Point) StopLoss=Ask+ StopLevel;//+x*Point
            OrderModify(OrderTicket(),OrderOpenPrice(), StopLoss,OrderTakeProfit(),0,Red);

            StopLoss=MathMin( s0, s1);//Функция возвращает минимальное из двух числовых значений
            if( StopLoss-Ask> StopLevel-0.5*Point)
            OrderModify(OrderTicket(),OrderOpenPrice(), StopLoss,OrderTakeProfit(),0,Red);

ALex2008 wrote >>

Also, we need to check the trade thread, otherwise it may get errors of being busy... Because if there are many charts, they all close current orders at the same time...
int start() {
   if(!IsTradeAllowed()) return(0);

if it doesn't help, before all OrderSend,OrderModify,OrderClose,OrderDelete check IsTradeContextBusy()


ALex2008 >>:
Only where to place it correctly I think... I mean, for example, the old orders are not deleted because the thread is busy, but the program must place new ones... And the program must only place new ones when all the old orders have been deleted.

rewrite it easier. in the functions UpTrend() and DownTrend() check conditions, orders are removed and put,

too many different things)

 
Swan >> :

No, that's the way it works. >> you got to make up your mind.)

I don't get it, will it or won't it?)

 
ALex2008 >> :

I don't get it - will it work or not?))

*not gonna happen.

In your last variant.

         if( Type==0){
            if(Bid- StopLoss< StopLevel-0.5*Point) StopLoss=MathMin( b0, b1); 
            if(Bid- StopLoss> StopLevel-0.5*Point) StopLoss=MathMax( b0, b1);
            OrderModify(OrderTicket(),OrderOpenPrice(), StopLoss,OrderTakeProfit(),0,Red);

in the first if() StopLoss is not defined.

 
Swan >> :

*not gonna happen.

In your last variant.

in the first if() StopLoss is not defined.


so what is it?

in the first if, the stop takes the minimum (at the price) low of 2 candlesticks... i.e. far from the order

StopLoss=MathMin(b0,b1); 

in the second if the stop receives the maximal (at the price) low of 2 candlesticks. i.e. it is close to the order


StopLoss=MathMax(b0,b1);
 
ALex2008 >> :
         if( Type==0){
//здесь StopLoss нипанятно какой)
            if(Bid- StopLoss< StopLevel-0.5*Point) StopLoss=MathMin( b0, b1); 
            if(Bid- StopLoss> StopLevel-0.5*Point) StopLoss=MathMax( b0, b1);
            OrderModify(OrderTicket(),OrderOpenPrice(), StopLoss,OrderTakeProfit(),0,Red);
 

//здесь StopLoss нипанятно какой)

What exactly is unclear? It is zero when entering the function. Maybe I don't understand something....


Above there is a condition - If order is open and its stop = 0, then place a stop

if ((Type<=1)&&(Stop==0))SetStop();

and the function itself to set a stop


//-------Вычисление стопа и установка
void SetStop(){
      RefreshRates();
      b0=iLow(NULL,PERIOD_H4,0)-10*Point;
      b1=iLow(NULL,PERIOD_H4,1)-10*Point;
      s0=iHigh(NULL,PERIOD_H4,0)+(Ask-Bid)+10*Point;
      s1=iHigh(NULL,PERIOD_H4,1)+(Ask-Bid)+10*Point;
            
         if( Type==0){
            if(Bid- StopLoss< StopLevel-0.5*Point) StopLoss=MathMin( b0, b1); 
            if(Bid- StopLoss> StopLevel-0.5*Point) StopLoss=MathMax( b0, b1);
            OrderModify(OrderTicket(),OrderOpenPrice(), StopLoss,OrderTakeProfit(),0,Red);
         }
         if( Type==1){
            if( StopLoss-Ask< StopLevel-0.5*Point) StopLoss=MathMax( s0, s1);
            if( StopLoss-Ask> StopLevel-0.5*Point) StopLoss=MathMin( s0, s1);
            OrderModify(OrderTicket(),OrderOpenPrice(), StopLoss,OrderTakeProfit(),0,Red);
         }
   }
Reason: