Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1831

 
Tretyakov Rostyslav #:

You set it correctly. Did you change something inside TrailingOrders() function?

I changed it and put it "as is" and the result was the same.

 
законопослушный гражданин #:

I've changed it and put it "as is" and the result is the same.

It is necessary to see the input parameters, because the log shows that the trawl works correctly
 
Tretyakov Rostyslav #:
We need to see the input parameters, because according to the log the trawl works correctly

2021.12.24 16:10:14.788 2021.12.17 00:00:00 111 - copy: Lot=0.01; StopLoss=200; TakeProfit=560; Slippage=3; Magic=1; K_Martin1=0.01; K_Martin2=1.9; K_Martin3=1.4; OrdersClose=3; OrdersClose2=5; DigitsLot=2; PeriodMA=21; MovingShift=1;


 
законопослушный гражданин #:

2021.12.24 16:10:14.788 2021.12.17 00:00:00 111 - copy: Lot=0.01; StopLoss=200; TakeProfit=560; Slippage=3; Magic=1; K_Martin1=0.01; K_Martin2=1.9; K_Martin3=1.4; OrdersClose=3; OrdersClose2=5; DigitsLot=2; PeriodMA=21; MovingShift=1;


maybe i put the function in the code incorrectly?

void OnTick()
  {
// Получим значение индикатора
   dMA = iMA(Symbol(), 0,PeriodMA, MovingShift, MODE_SMA, PRICE_CLOSE, 0); // MODE_SMA - простое усреднение , значение 0. PRICE_CLOSE- цена закрытия, значение 0.
   TrailingOrders();
// Если нет открытых ордеров, то входим в условие
      if(CountOrders()==0)
     {

// Если появился сигнал на покупку, то откроем ордер на покупку
      if(bSignalBuy() == true)
         vOrderOpenBuy();

// Если появился сигнал на продажу, то откроем ордер на продажу
      if(bSignalSell() == true)
         vOrderOpenSell();
     } 
       DrawLABEL("nextlot",1,5,0,Color1(),StringConcatenate("CURRENT LOT: ",DoubleToStr(LOT(),2)));
       DrawLABEL("currentlot",1,5,0,Color2(),StringConcatenate("NEXT LOT: ",DoubleToStr(LOT(),2))); 
  }

and put the function itself separately

//-------------------------------------------------------------------+

void TrailingOrders()
  {
   for(int i = OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
           {
            if(OrderType() == OP_BUY)
              {
               if(Bid >= OrderOpenPrice()+(TakeProfit*0.75*_Point) && OrderStopLoss()<OrderOpenPrice()-20*_Point)
                 {
                  if(OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss()+20*_Point, OrderTakeProfit()+20*_Point, 0))
                     Print("Модификации ордера на покупку успешна!");
                  else
                     Print("Ошибка модификации ордера на покупку! - ",GetLastError());
                 }
               if(Bid >= OrderOpenPrice()+(TakeProfit*0.9*_Point) && OrderStopLoss()>=OrderOpenPrice()-20*_Point)
                 {
                  if(OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss()+30*_Point, OrderTakeProfit()+10*_Point, 0))
                     Print("Модификации ордера на покупку успешна!");
                  else
                     Print("Ошибка модификации ордера на покупку! - ",GetLastError());
                 }
              }
           }
        }
     }
  }
 
законопослушный гражданин #:

have I put the function in the code incorrectly?

and put the function itself separately

//-------------------------------------------------------------------+

void TrailingOrders()
  {
   
   for(int i = OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
           {
            if(OrderType() == OP_BUY)
              {
               if(Bid >= OrderOpenPrice()+(TakeProfit*0.75*_Point) && OrderStopLoss()<=OrderOpenPrice()-StopLoss*_Point)
                 {
                  if(OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice()-(StopLoss+20)*_Point, OrderTakeProfit()+20*_Point, 0))
                     Print("Модификации ордера на покупку успешна!");
                  else
                     Print("Ошибка модификации ордера на покупку! - ",GetLastError());
                 }
               if(Bid >= OrderOpenPrice()+(TakeProfit*0.9*_Point) && OrderStopLoss()>=OrderOpenPrice()-(StopLoss+20)*_Point)
                 {
                  if(OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss()+30*_Point, OrderTakeProfit()+10*_Point, 0))
                     Print("Модификации ордера на покупку успешна!");
                  else
                     Print("Ошибка модификации ордера на покупку! - ",GetLastError());
                 }
              }
           }
        }
     }
  }

The trail is correct.

Try it like this

 
Tretyakov Rostyslav #:

The trolley is standing correctly.

Try this.

take off into space

stop stops first, then instantly flies into space along with the take (closing the order on the way)

this is not the issue as i understand it.

correct me if I am wrong: void OnTick()

if there are no open orders and there is a signal, then vOrderOpenBuy() / vOrderOpenSell()

Then comes void vOrderModify(int iOTi) - i.e. they set stops and takei and then check for errors.

That is, until the order closes at a stop or a take, it isn't possible to correct it in terms of trailing.

You need one (maximum two) multiple of stop and take.

If you want to change the trailing stop and take a profit, then you should do it manually.

i'm still thinking how to implement it.

Files:
111.mq4  25 kb
 
законопослушный гражданин #:

take off into space

the stop first stands, then instantly flies into space after the take (closing the order on the way)

This is not the point, as I understand it.

correct me if I am wrong: void OnTick()

if there are no open orders and there is a signal, then vOrderOpenBuy() / vOrderOpenSell()

Then comes void vOrderModify(int iOTi) - i.e. they set stops and takei and then check for errors.

That is, until the order closes at stop or take, it isn't possible to correct it in terms of trailing.

You need one (maximum two) multiple of stop and take.

If you want to change the trailing stop and take a profit, then you should do it manually.

i'm still thinking how to implement it.

Corrected

2021.12.24 21:31:12.245 2021.11.01 00:00:00  111 inputs: Lot=0.01; StopLoss=200; TakeProfit=400; Slippage=3; Magic=1; K_Martin1=2; K_Martin2=2; K_Martin3=2; OrdersClose=5; OrdersClose2=5; DigitsLot=2; PeriodMA=14; MovingShift=1; 
2021.12.24 21:31:12.292 2021.11.01 00:05:00  111 EURUSD,M5: open #1  buy 0.01 EURUSD at 1.15583 ok
2021.12.24 21:31:12.292 2021.11.01 00:05:00  111 EURUSD,M5: modify #1  buy 0.01 EURUSD at 1.15583 sl: 1.15383 tp: 1.15983 ok
2021.12.24 21:32:52.782 2021.11.01 17:37:18  111 EURUSD,M5: modify #1  buy 0.01 EURUSD at 1.15583 sl: 1.15403 tp: 1.16003 ok
2021.12.24 21:32:52.782 2021.11.01 17:37:18  111 EURUSD,M5: Модификации ордера на покупку успешна!
2021.12.24 21:33:39.178 2021.11.01 19:06:30  111 EURUSD,M5: modify #1  buy 0.01 EURUSD at 1.15583 sl: 1.15433 tp: 1.16013 ok
2021.12.24 21:33:39.178 2021.11.01 19:06:30  111 EURUSD,M5: Модификации ордера на покупку успешна!
2021.12.24 21:34:08.768 2021.11.01 20:12:05  Tester: take profit #1  at 1.16013 (1.16014 / 1.16019)
2021.12.24 21:34:14.632 2021.11.01 20:17:55  Tester: stop button pressed
Files:
111.mq4  25 kb
 
законопослушный гражданин #:

take off into space

the stop first stands, then instantly flies into space followed by the take (closing the order on the way)

That's because your rules are not working:

Forum on trading, automated trading systems and testing of trading strategies.

Any questions from newbies on MQL4 and MQL5, help and discussion of algorithms and codes

Mihail Matkovskij, 2021.12.23 22:01

In general, the rules are as follows.

  • The calculated SL is less than the order's SL (less profitable) - do nothing.
  • Settlement SL is equal to the order - do nothing
  • The calculated SL exceeds the order's SL (more profitable) - modify it with the calculated value
These rules of trawl creation would be suitable for creating almost any trawl with any SL calculation algorithm.

They would have been done a long time ago and nailed it. But no...

I couldn't help but find a ready-made example:https://www.mql5.com/ru/code/30808

And an article: https://www.mql5.com/ru/articles/134

Although it's complicated for beginners but I think you will manage it if you are really interested in the subject.

Простой трейлинг-стоп
Простой трейлинг-стоп
  • www.mql5.com
Очень простой советник для сопровождения сделок трейлинг-стопом.
 
Tretyakov Rostyslav #:

Corrected

Thank you!

I didn't know you could put a double inside a void function

 
Mihail Matkovskij #:

That's because your rules don't work:

You should have done it a long time ago and scored. But no...

I couldn't stand it and found you a ready-made example:https://www.mql5.com/ru/code/30808

And an article:https://www.mql5.com/ru/articles/134

It may be complicated for beginners but I think you will understand it if you are really interested in the subject.

Thank you