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

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

hello!

Decided to make owls a bit more complicated.

I want to do something like trailing, but with a single / double action

idea:

price passed 75% to take - stop decreased (for example) by 20 pips and take increased by 20 pips.

price passed 90% to take- stop decreased (for example) by 30 pips and take increased by 10 pips.


int TakeProfit=200;
int StopLoss  =100;
//+----------------------------------------------------------------------------+
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());
                 }
              }
           }
        }
     }
  }
 
Tretyakov Rostyslav #:

Thank you. it's interesting. I'll try to figure it out!

it turns out thatif(Bid is the current price parameter - which is not called in any way - you can just compare certain values with it?

and the unit ofBid is ticks? (it's not expressed in money)

 
Tretyakov Rostyslav #:


I have only one small thing to worry about. All orders are closing now and then (i.e. all and always!), but sometimes, if i open 3-4 orders, only 2 or 1 of them might close. I think there is a special i-check for this, may be you can hint.

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CloseBuyPositions1()
  {

   for(int i=OrdersTotal()-1; i>=0; i--)
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))



         if(OrderType()==OP_BUY)
           {
            if(OrderClose(OrderTicket(), OrderLots(), Bid, 0, NULL))
              {
               Print("Order Close");
              }
           }

  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CloseSellPositions2()
  {

   for(int i=OrdersTotal()-1; i>=0; i--)
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))



         if(OrderType()==OP_SELL)
           {
            if(OrderClose(OrderTicket(), OrderLots(), Ask, 0, NULL))
              {
               Print("Order Close");
              }
           }

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

Thank you. it's interesting. I'll try to figure it out!

it turns out thatif(Bid is the current price parameter - which is not called in any way - you can just compare certain values with it?

and the unit ofBid is ticks? (it's not expressed in money)

Bid is current price, so compare with the opening price +/- condition

As for ticks, neither Bid nor Ask is measured in ticks. The ticks are the arrival of a new price.

 
Tretyakov Rostyslav #:

Bid is current price so we have to compare it with the opening price +/- condition

As for ticks, neither Bid nor Ask is measured in ticks. The ticks are the arrival of a new price.

I see. Then what is it measured in? pips/pips?

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

I see. How is it measured then? in points/pips?

MarketInfo(_Symbol,MODE_TICKSIZE);
 
Tretyakov Rostyslav #:

I see. Thank you.

 
Порт-моне тв #:

I have only one small thing to worry about. All orders are closing now and then (i.e. all and always!), but sometimes, if i open 3-4 orders, only 2 or 1 of them might close. I think there is a special i-check, may be you have a clue.

Do not neglect brackets, try this

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CloseBuyPositions1()
  {
   int pos=OrdersTotal()-1;
   for(int i=pos; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderType()==OP_BUY)
           {
            if(OrderClose(OrderTicket(), OrderLots(), Bid, 0, NULL))
              {
               Print("Order Close");
              }
           }
        }
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CloseSellPositions2()
  {
   int pos=OrdersTotal()-1;
   for(int i=pos; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderType()==OP_SELL)
           {
            if(OrderClose(OrderTicket(), OrderLots(), Ask, 0, NULL))
              {
               Print("Order Close");
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
 
How to get extremes of ZZ in D' format 2021.01.05
over several years?
 
Vladimir Baskakov #:
How to get extremes of ZZ in D' format 2021.01.05
in a few years?
if(ZZ[i]!=EMPTY_VALUE) string iDate=TimeToString(time[i],TIME_DATE|TIME_MINUTES);