Questions from Beginners MQL4 MT4 MetaTrader 4 - page 222

 

You can check the trading robot

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

//| Experts1.mq4 |

//| Copyright 2017, MetaQuotes Software Corp. |

//| https://www.mql5.com |

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

#property copyright "Copyright 2017, MetaQuotes Software Corp."

#property link "https://www.mql5.com"

#property version "1.00"

#property strict

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

//| https://www.mql5.com |

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

#property copyright "Copyright 2020, MetaQuotes Software Corp.

#property link "https://www.mql5.com"


input double Lots=0.01;

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

//| |

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

int OnInit()

( OrderSend(Symbol(),OP_BUY,Lots,3,0,Ask+0.0084,Ask-0.0084)


double lot=Lots;

if profit then Lots=lot+0.01 else Lots=lot-0.01

if Lots=0 then Lots=0.01

if Lots>0.03 then Lots=0.01

//---

//---

return();

}


Открой новые возможности в MetaTrader 5 с сообществом и сервисами MQL5
Открой новые возможности в MetaTrader 5 с сообществом и сервисами MQL5
  • www.mql5.com
One Click Close The script allows users to easily close positions if their profit/loss reaches or exceeds a value specified in pips. Please set slippage value first. Sometimes some positions do not close due to high volatility of the market. Please set larger slippage or restart the script. The free demo version is: ...
 
Hello, dear Professionals, advise me how to write the condition whenthe pending order ismoved once at a certain price movement, for example, Sellstop order was placed at 1.28, the price moved to 1.50 and the pending order moved to the price at a certain distance from the price, but did not move behind the price constantly, it moved only once, i.e. moved for example by ten pips and stopped even if the price went further. Regards, Alexander!
Документация по MQL5: Константы, перечисления и структуры / Торговые константы / Свойства ордеров
Документация по MQL5: Константы, перечисления и структуры / Торговые константы / Свойства ордеров
  • www.mql5.com
Приказы на проведение торговых операций оформляются ордерами. Каждый ордер имеет множество свойств для чтения, информацию по ним можно получать с помощью функций Идентификатор позиции, который ставится на ордере при его исполнении. Каждый исполненный ордер порождает сделку, которая открывает новую или изменяет уже существующую позицию...
 

Good afternoon, dear experts!

Please help, tips on how to do it, where to read, writing part of the code.

I have attached a part of the code. I need to be able to enter my comment when opening an order or a limit order. I.e., not right in the code, but when opening the EA panel.

extern double Lots     = 0.1;
extern int StopLoss    = 250;
extern int TakeProfit  = 150;
extern int Magic       = 1;

double SL, TP;

int ticket;


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   double price = Ask;

   if(BUY() == 0)
     {
      SL = NormalizeDouble(Ask - StopLoss*Point, 5);
      TP = NormalizeDouble(Ask + TakeProfit*Point, 5);
      ticket = OrderSend(Symbol(), OP_BUY, Lots, price, 3, SL, TP, "", Magic, 0, Red);
      if(ticket<0)
         Print("Не удалось открыть ордер");
     }
  }
//+------------------------------------------------------------------+
int BUY()
  {
   int count = 0;
   for(int i=OrdersTotal()-1; i>=X; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true &&
         OrderMagicNumber() == Magic &&
         OrderType() == OP_BUY)
        {
         count++;
        }
     }
   return(count);
  }
//+------------------------------------------------------------------+
 
d4vv:

I need to be able to enter my comment when opening an order or a limit order.

input string Mycomment="Preved Medved";


ticket = OrderSend(Symbol(), OP_BUY, Lots, price, 3, SL, TP, Mycomment, Magic, 0, Red);

The comment is written when the order is created and cannot be changed again.

 
Aleksei Stepanenko:

The comment is written when the order is created and it can no longer be changed.

Thank you very much. This is exactly what I need.

 
Knelson:
Please advise how to write a condition whena pending orderwill be moved once at a certain price movement,

1. Create an array in which to store the tickets of open orders and a flag that the order has already been moved.

struct Orders
   {
   int ticket;
   bool moved;
   } orders[];

2. Keep track of newly appearing orders and record in this array information about the open order with the flag "has not moved".

bool finded;
int index==ArraySize(orders)-1;
for(int i=OrdersTotal()-1; i>=0; i--)
   {
   if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
   if(OrderSymbol()!=symbol) continue;
   if(OrderMagicNumber()!=magic) continue;
   if(OrderType()!=OP_BUYSTOP && OrderType()!=OP_SELLSTOP) continue;
   finded=false;
   for(int j=index; j>=0; j--)
      {
      if(orders[j].ticket==OrderTicket())
         {
         finded=true;
         break;
         }
      }
   if(!finded)
      {
      ArrayResize(orders,++index+1);
      orders[index].ticket=OrderTicket();
      orders[index].moved=false;
      }
   }

3. Delete information from the array if the order is closed, deleted, or transformed.

4. Check the condition for moving, if the condition is met and the flag "did not move", then move and change the flag to "moved".

 
Aleksei Stepanenko:

1. Create an array in which to store the tickets of open orders and a flag that the order has already been moved.

2. Keep track of newly appearing orders and record in this array information about the open order with the flag "has not moved".

3. Delete information from the array if the order is closed, deleted, or transformed.

4. Check condition for moving, if condition has come and flag "did not move", then move and change flag to "moved"

I don't think it's reliable.

I would solve such a problem in a simpler way: put a pending order with magic number 2 and delete a pending order with magic number 1 under the condition of which the order should be moved

The code would be simple - after the tick receipt, the loop would loop through the order with magic number 1 if it is found and needed to move, then we would delete this order and place a new one with magic number 2

 

Yes, it's a good option.

 
Aleksei Stepanenko:

Yes, that's a good option.

UPD: the code will be simple - when the tick comes the loop on the order with magic number 1 if found and need to move, then delete this order and set a new order with magic number 2

.... then we will try to set a new order with magic number 2 and if it is OK then we will delete the order with magic number 1

so less checks to come up with - for example levels of stop level has expanded and will not be able to move the order once and we have already deleted the order found

that way ;)

 
Observing the human brain (mostly my own, but not only), I find one property - tunneling. The thinking process takes place inside thinking stereotypes, as if in a tunnel. And it is not easy to jump out of it with my own efforts. The "I" is inseparable from me and it seems that the train of thought is correct, absolutely correct. But then knowledge from the outside comes in, and you begin to realise that you have been a prisoner of your ideas. Miracles...