[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 567

 
Catrock писал(а) >>

Another question from a beginner.

I use several user-defined functions in my Expert Advisor. How can I make these functions be combined into a library, and in the body of the Expert Advisor just call them from there.

I am not familiar with language syntax in this regard.

Please refer to documentation or tutorial section. I haven't found it there.


https://book.mql4.com/ru/build/structure

https://www.mql5.com/ru/articles/1462

+ to top it off... https://book.mql4.com/ru/appendix/examples

 

How to write a function that checks if a position is or has been opened on zero bar (preferably a choice of timeframe. OpenPosLastBar(string sym="", int tf=0, int op=-1, int mn=-1), or this function does that. As I understood it, it will only open positions at the moment, but if it was open and already closed, it will return -1.

//+----------------------------------------------------------------------------+
//| Returns the bar number of the last position opened or -1. |
//| Parameters: |
//| sym - instrument name ("" - current symbol) |
//| tf - timeframe ( 0 - current timeframe) |
//| op - operation (-1 - any position) |
//| mn - MagicNumber (-1 - any magik) |
//+----------------------------------------------------------------------------+
int NumberOfBarLastPos(string sym="", int tf=0, int op=-1, int mn=-1)
{
datetime oot;
int i, k=OrdersTotal();

if(sym=="")
sym=Symbol();
for(i=0; ik; i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol()==sym)
{
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
{
if(op0 || OrderType()==op)
{
if(mn0 || OrderMagicNumber()==mn)
{
if(ootOrderOpenTime()) oot=OrderOpenTime();
}
}
}
}
}
}
return(iBarShift(sym, tf, oot, True));
}
//+----------------------------------------------------------------------------+

 
We need two cycles. In the first one we go through the order history, in the second one we go through the current orders. In both cases, if the time of order opening is greater than or equal to Time[0], then the order was opened on the current candle. This is true for all timeframes.
 
drknn >>:
Нужно два цикла. В первом проходим по истории ордеров, во втором - по текущим ордерам. В обоих случаях если время открытия ордера больше или равно Time[0], то ордер был открыт на текущей свече. Это справедливо для всех таймфреймов.

Thank you for clicking. Could you ask for this in the code
 
gince >>:

Спадибо, что аткликнулся. А можно попросить это в коде

Here is an example of a history passage

//=========== SchSellHist()  ===============================
//  Функция возвращает количество Sell-ордеров, лежащих в истории сегодняшних торгов
//  SchSell  - счётчик Sell ордеров
//-----------------------------------------------------------
int SchSellHist(int MAGIC){
  string SMB=Symbol();
  int SchSell=0;
  int i;
  for (i=OrdersHistoryTotal()-1;i>=0;i--){
    if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) { WriteError(i);}
    else {
      if(OrderSymbol()!=SMB || OrderMagicNumber()!=MAGIC){ continue;} 
      if(OrderType()==OP_SELL){
        if(OrderOpenTime()>=Time_D0){// сегодняшний ордер
          SchSell++;
        }
      }
    }
  }
 return(SchSell);     
}    
             
//==================================================================================================

Try to come up with your own code, especially since you have the algorithm and a ready-made example in front of you.
 
Hello People! Help me to understand the code after it works a lot of rubbish is left on the chart arrows circles of the line how to remove them or so it itself deleted here is part of the code
 
void seta(string a_name_0, int a_window_8, double a_datetime_12, double a_price_20, int ai_28, colour a_color_32, int a_width_36) {
ObjectDelete(a_name_0);
ObjectCreate(a_name_0, OBJ_ARROW, a_window_8, a_datetime_12, a_price_20);
ObjectSet(a_name_0, OBJPROP_ARROWCODE, ai_28);
ObjectSet(a_name_0, OBJPROP_COLOR, a_color_32);
ObjectSet(a_name_0, OBJPROP_WIDTH, a_width_36);
}

void _setabuy(string as_0) {
g_str_concat_356 = StringConcatenate(as_0, gi_120);
seta(g_str_concat_356, 0, TimeCurrent(), Bid + 15.0 * Point, SYMBOL_ARROWUP, Blue, 2);
gi_120++;
}

void _setasell(string as_0) {
g_str_concat_356 = StringConcatenate(as_0, gi_120);
seta(g_str_concat_356, 0, TimeCurrent(), Bid - 15.0 * Point, SYMBOL_ARROWDOWN, Red, 2);
gi_120++;
}
 
drknn >>:

Вот пример прохода по истории


Попробуйте самостоятельно придумать код, тем более, что алгоритм и готовый пример у Вас перед глазами.

Thank you. I'll give it a try.
 
drknn >>:

Вот пример прохода по истории


Попробуйте самостоятельно придумать код, тем более, что алгоритм и готовый пример у Вас перед глазами.

Or here is a function that checks if a position was opened on the current bar (with a check on OrdersTotal() and OrdersHistoryTotal() ). Returns true if a Type order was opened.

bool CheckExists(int Type)                                  
 {
  bool Result = True;
  for(int i = 0; i < OrdersTotal(); i++)
   if(OrderSelect(i, SELECT_BY_POS))
    if(OrderType() == Type && OrderMagicNumber() == Magic && OrderSymbol() == Symbol())
     if(OrderOpenTime() >= Time[0])
      Result = False;
  for(i = 0; i < OrdersHistoryTotal(); i++)
   {
    if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
     if(OrderType() == Type && OrderOpenTime() >= Time[0]
        && OrderMagicNumber() == Magic && OrderSymbol() == Symbol())
      Result = False;
    }

  return(Result);
  }
 

Please tell me how to implement the condition.

If( there are pending orders) then do this and that ;