[WARNING CLOSED!] Any newbie question, so as not to clutter up the forum. Professionals, don't go by. Can't go anywhere without you. - page 774

 
drknn:

I do not understand what I have a line in the market order opening, I copy it and add a new one to the Order but the same 4107 error does not work, if you care to look at what I missed

Files:
test_21_1.mq4  15 kb
 
FoxUA:

I don't get it, I have this line in the market order opening, I copy it into a pending order and add a new one, but the same 4107 error still writes to the log, but pending orders are put, if you care to look at what I missed.


You have a damn poorly organised code. And variable names have abbreviations that also make it hard to read.

The code should be organized in blocks. Assign names to variables in such a way that it is clear at a glance what the code is about. Constant names (get into the habit) write them in uppercase letters. Initialization of constants with values must be placed in an initialization block. So, for example, you have an absolutely unjustified use of the string

string sy = Symbol();

at the start of the EA. Well, tell me why on every tick why do you need to call the Symbol() function, if during the whole trade the symbol name stays the same? It's enough to initialize once with value of variable sy in initialization block, and that's all. Further, this code block

pral=StopLossLastPos(sy,OP_BUY,-1) ;   // цена по StopLoss последнего открытого BUY на покупку BUYLIMIT
pras=TakeProfitLastPos(sy,OP_BUY,-1);  // цена по TakeProfit последнего открытого BUY на покупку BUYSTOP
prbl=StopLossLastPos(sy,OP_SELL,-1);   // цена по StopLoss последнего открытого SELL на продажу SELLLIMIT
prbs=TakeProfitLastPos(sy,OP_SELL,-1) ;// цена по TakeProfit последнего открытого SELL на продажу SELLSTOP

It is fundamentally wrong! To find out the stop loss of the last order, it (the order) must first be selected using OrderSelect() - see the example in the order counter below. This means that if you need stop levels for the last orders, you will need to do this via a loop. Next. The design of

int total = OrdersTotal();
if(total == 0){

}

is not justified. Function OrdersTotal() will return the total number of orders already opened in the terminal. This value includes orders opened by other trading instruments, as well as orders that were not opened by the Expert Advisor, but by you personally manually for the same instrument, which the Expert Advisor has placed.

If you are not going to trade by hand and only trade on the symbol to which the EA has been thrown, then this design will work. But as soon as you want to trade with your hands and the EA does not interfere with your manual trades, or you want the trades to be executed with other symbols as well, the design will really let you down. So you'd better write separate order counters for each order type. Here is an example of such a counter.

//=========== SchBuy()  ===============================
//  Функция возвращает количество Buy-ордеров
//   SchBuy      - счётчик Buy ордеров
//-----------------------------------------------------------
int SchBuy(int MAGIC){
  string SMB=Symbol();
  int SchBuy=0;
  for (int i=OrdersTotal()-1;i>=0;i--) {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){
      Print("Ошибка № ",GetLastError()," при выборе ордера № ",i);
    }
    else {
      if(OrderSymbol()!= SMB || OrderMagicNumber()!= MAGIC){continue;}// если не наш, то смотрим следующий
      if(OrderType()==OP_BUY){ 
        SchBuy++;
      }  
    }
  }
  return(SchBuy);
}                  

Rewrite the code taking into account the above recommendations!!!

 

Sorry, I didn't realise that strings like

pral=StopLossLastPos(sy,OP_BUY,-1) ;   // цена по StopLoss последнего открытого BUY на покупку BUYLIMIT

call a subprogram.

But it's still better to organize it in a different way.

>> double StopLossLastPos(string sy="", int op=-1, int mn=-1) {

You don't need to pass a symbolic name of a trading symbol into the subprogram. Your subroutine is in the Expert Advisor, not in the included library. Since the variable sy is declared at the global level of the Expert Advisor, this variable is in the scope of such subprograms. Further, you pass the second parameter to the subprogram, but when you call the subprogram itself, you initialize this parameter with a value ( int op=-1) - you don't need to do this. The same is true for the next parameter.

The line

>> if (op<0 || OrderType()==op) {

again is unreadable. Look how it's organised in my counter!

 

And then there's this. The code block

pral=StopLossLastPos(sy,OP_BUY,-1) ;   // цена по StopLoss последнего открытого BUY на покупку BUYLIMIT
pras=TakeProfitLastPos(sy,OP_BUY,-1);  // цена по TakeProfit последнего открытого BUY на покупку BUYSTOP
prbl=StopLossLastPos(sy,OP_SELL,-1);   // цена по StopLoss последнего открытого SELL на продажу SELLLIMIT
prbs=TakeProfitLastPos(sy,OP_SELL,-1) ;// цена по TakeProfit последнего открытого SELL на продажу SELLSTOP

is called on every tick. But it is not needed on every tick. You only need to know the stop/profit order in some cases. These are the cases when you need to call your subroutine.

 
spidey:

Hello, can you please attach an alert to the indicator (green one crosses red one from top to bottom - sell, back - buy)?

Well someone is posting stolen codes on the forum again. :))))))))

I doubt anyone would want to dig through this mishmash of numbered variables for free :)

 
spidey:

Good afternoon, could you please add an alert to the turkey (green one crosses the red one from top to bottom - sell, back - buy)?

If you do it again I will be forced to banish you from the game.

This is a violation of forum rules.

 
should the code be posted?
 
drknn:

Sorry, I didn't realise that strings like

call a subprogram.

But it's still better to organize it in a different way.

>> double StopLossLastPos(string sy="", int op=-1, int mn=-1) {

You don't need to pass a symbolic name of a trading symbol into the subprogram. Your subprogram is in the Expert Advisor, not in the included library. Since the variable sy is declared at the global level of the Expert Advisor, this variable is in the scope of such subprograms. Further, you pass the second parameter to the subprogram, but when you call the subprogram itself, you initialize this parameter with a value ( int op=-1) - you don't need to do this. The same is true for the next parameter.

The line

>> if (op<0 || OrderType()==op) {

again is unreadable. Look how it is organized in my counter!

So I guess the function call can be written simply

PrAskLim = StopLossLastPos(OP_SELL); in the function itself

double StopLossLastPos(int op=-1,) like this?

I already told that I took the functions from this site, their work has been tested because I don't dare to change their variables for more readable ones, because I may lack experience in what you can and what you cannot change; this is fraught with the danger that I might change something wrong in a working function and you'll needle in a pile of code all over again,

but I fixed as much as I could what I added myself,

I already added them myself but it didn't solve my problems with log 4107 and I have to ask how I can change price and stops because I didn't change anything fundamentally in the function for pending orders.

The second question is how to add a spread to this price PrAskLim = StopLossLastPos(OP_SELL) ; (Spread =MarketInfo(sy, MODE_SPREAD); )

Here's the modified version of the EA

Files:
test_21_2.mq4  16 kb
 
spidey:
should the code be posted?
You don't need to post the stolen stuff.
 
drknn:
You don't have to post stolen stuff.

You don't have to steal.

And the rules of this resource do not prohibit it...

https://www.mql5.com/ru/code/8089

;)