Question for connoisseurs - page 2

 

I've found a variant of the code here.

how it can be inserted into my EA????????????

//+----------------------------------------------------------------------------+
//|  Возвращает номер бара открытия последней позиции или -1.                  |
//|  Параметры:                                                                |
//|    sym - наименование инструмента  ("" - текущий символ)                   |
//|    tf  - таймфрейм                 ( 0 - текущий таймфрейм)                |
//|    op  - операция                  (-1 - любая позиция)                    |
//|    mn  - MagicNumber               (-1 - любой магик)                      |
//+----------------------------------------------------------------------------+
int NumberOfBarOpenLastPos(string sym="", int tf=0, int op=-1, int mn=-1) {
  datetime oot;
  int      i, k=OrdersTotal();
 
  if (sym=="") sym=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderSymbol()==sym) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (oot<OrderOpenTime()) oot=OrderOpenTime();
            }
          }
        }
      }
    }
  }
  return(iBarShift(sym, tf, oot, True));
}
 
Kostay:

The strange thing is going on with the Expert Advisor now. it's not working with the position closures. there may be several positions of the same type open and active at the same time. And the result: everything is losing on the same problem, though in a modified version!

Fixed it, try it.

extern double Lots = 0.1;

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start() {
   double P_up0,P_down0,P_up1,P_down1;
   double st_m1, st_s1, st_m2, st_s2,Pivot,ma_s1;
   int i, cnt, ticket, total;
   bool flag=true;

   if(Bars<100) {
      Print("bars less than 100");
      return(0); 
   }
// Проверяем стоит ли открываться
   
   cnt=OrdersHistoryTotal()-1;
   for(i=cnt;i>=0;i--) {
      if(!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY )) continue;
      if(OrderOpenTime()>=Time[0]) { // Time[0] - если позиция открывается на нулевом баре текущего символа
         flag=false;
         break;
      }
   }
   cnt=OrdersTotal()-1;
   for(i=cnt;i>=0;i--) {
      if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
      if(OrderOpenTime()>=Time[0]) { // Time[0] - если позиция открывается на нулевом баре текущего символа
         flag=false;
         break;
      }
   }
   
//-----------------------------------------------
   P_up0=iCustom(0,0,"Ценовой канал",11,0,0);
   P_down0=iCustom(0,0,"Ценовой канал",11,1,0);
   P_up1=iCustom(0,0,"Ценовой канал",11,0,1);
   P_down1=iCustom(0,0,"Ценовой канал",11,1,1);
   st_m1=iStochastic(0,0,10,3,3,MODE_SMA,0,MODE_MAIN,1);
   st_s1=iStochastic(0,0,10,3,3,MODE_SMA,0,MODE_SIGNAL,1);
   st_m2=iStochastic(0,0,10,3,3,MODE_SMA,0,MODE_MAIN,2);
   st_s2=iStochastic(0,0,10,3,3,MODE_SMA,0,MODE_SIGNAL,2);
   Pivot=iCustom(0,0,"Pivot",0,1);
   ma_s1=iMA(0,0,4,0,MODE_SMA,PRICE_CLOSE,1);
//задали все данные 

// Проверка свободной маржи
   if(AccountFreeMargin()<(1000*Lots)) {
      Print("We have no money. Free Margin = ", AccountFreeMargin());
      return(0); 
   }
   total=OrdersTotal();
// Условие открытие позиции BUY
   if(st_m2>st_s2&&st_m1>st_s1&&st_m2<25&&Pivot<ma_s1) {
      if((total>0) || (!flag)) return(0);
      ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"",0,0,Green);
      if(ticket>0) {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
            Alert("Покупка: ",OrderOpenPrice()); 
      }  else Alert("ошибка:по цене ",OrderOpenPrice()); 
      return(0); 
   }
      
// Условие открытие позиции SELL
   if(st_m2<st_s2&&st_m1<st_s1&&st_m2>75&&Pivot>ma_s1) {
      if((total>0) || (!flag)) return(0);
      ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"",0,0,Red);
      if(ticket>0) {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
            Alert("Покупка: ",OrderOpenPrice()); 
      }  else Alert("ошибка:по цене ",OrderOpenPrice()); 
      return(0); 
   }
  
   for(i=cnt;i>=0;i--) {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()) {
         if(OrderType()==OP_BUY) {// длинная позиция открыта
// условие закрытие длинной позиции
            if(P_down1>P_down0) {
               OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); 
               return(0); 
            }
         }  else {
// условие закрытия короткой позиции
               if(P_up1<P_up0) {
                  OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); 
                  return(0); 
               }
            }
      }
   }
   return(0);
}
 

Hello.

When programming a neural network, I encountered the following problem.

I have an array:

double X[3];
   X[0]=1;
   X[1]=2;
   X[2]=3;
   X[3]=4;

Comment(X[3]);

When I test it, I get 0 instead of 4. The same thing is in a two-dimensional array, what should I do?

And taking the opportunity to ask a second question: as I understand mql4, you can not set a specific range for assigning a variable to random numbers?

In Delphi it looks like this

Randomize;

i:=Random(10); // the same range from 0 to 10

 
Chris_Brown >> :

..I have an array:

When testing, I get 0 instead of 4. The same thing happens in a two-dimensional array, what do I do?

The array is simple, you defined dimension [3] and you call the fourth missing cell.

Write double X[4]; and everything will work.

 
granit77 >> :

With an array it's simple, you've defined dimension [3], but you're calling the fourth, missing cell.

Write double X[4]; and everything will work.

Thanks, I'll try it, but what about random ?

 

And if there is a two-dimensional array, a 4 by 9 matrix and each element is assigned a value of 0.1, would that be about right?


int i, j;

double W[4][9];
   for ( i=0; i<=3; i++)
   for ( j=0; j<=8; j++)
       W[ i][ j]=0.1; 
 
Chris_Brown >> :

And if you have a two-dimensional array, a matrix of 4 by 9, and assign value 0.1 to each element, it will be like this?

It should work. If you don't feel lazy and display the whole matrix line by line in the comment, you'll see the result.

By the way, you can make the comment a function and use it to check the contents of the array.

And about the randomness, I'll pass. I was just passing by and saw the mistake. :))

 
Chris_Brown писал(а) >>

Thanks, I'll give it a try, but what about random?

double i=MathRand()/32767.0*10; 
the help's in there.
 
Chris_Brown писал(а) >>

And if there is a two-dimensional array, a 4-by-9 matrix and each element is assigned a value of 0.1, would it be something like this?

Can be used

int ArrayInitialize( double &array[], double value)
Sets all elements of a numeric array to the same value. Returns the number of initialized elements.

 
Chris_Brown писал(а) >>

And if you have a two-dimensional array, a 4-by-9 matrix and assign a value of 0.1 to each element, would it work like this?

int i, j;

double W[4][9];
   for ( i=0; i<=3; i++)
   for ( j=0; j<=8; j++)
       W[ i][ j]=0.1; 

It would work, but it should be written like this:

int i, j;

double W[4][9];

   for ( i=0; i<4; i++)
      for ( j=0; j<9; j++)
         W[ i][ j]=0.1; 
Reason: