[ARCHIVE] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 3. - page 539

 

I am writing an EA based on QQEA indicator. So, the idea for the EA is: when the red line crosses the yellow line upwards, one Buy order should open, and when the red line crosses the yellow line downwards, one Sell order should open. But I am not able to open just one order. The orders are opened as long as the condition is fulfilled. Yes, and only buy orders are opened HAYDNT ?!

//

EA code

//--- input parameters
extern double MaxRisk=1.0;
extern double FixLot = 0.01;
extern double Exponent=2.0;
extern int Magic = 888;

// костыли

extern int TakeProfit=100;
extern int StopLoss=100;

int init()
  return(0);
}
int deinit()
{
//----

//----
return(0);
}
 int start()
{
//----
int Count=0;
double b0,b1;
int ticket;

// параметры индикатора
int SF = 5; // original 5
int RSI_Period = 14; // original 14
double DARFACTOR = 4.236; //original 4.236

//------------ Параметры из индикатора QQEA -----------------------
// Buffer0 -- красная жирная
string Buffer0 = iCustom(NULL, 0, "QQEA" , SF, RSI_Period, DARFACTOR,0 , 0); 
// Buffer1 -- жёлтый пунктир
string Buffer1 = iCustom(NULL, 0, "QQEA" , SF, RSI_Period, DARFACTOR,1 , 0);

b0=StrToDouble(Buffer0);
b1=StrToDouble(Buffer1);


double Lot=GetLot(MaxRisk);

// если лот <0 выводим сообщение об ошибке
if(Lot==0) 
{
Alert("Недостаточно средств!");
return(0);
} 

if (Lot!=0 && b0>b1) // если лот <> 0 и красная выше жёлтой
{
ticket=NewOrder(OP_BUY,Lot);
if (ExistOrders(Symbol(), 1, 888, 0) == true ) // проверяем наличие ордера sell
{
CloseOrder();
}
}

if (Lot!=0 && b0<b1) // если лот <> 0 и красная выше жёлтой
{
ticket=NewOrder(OP_SELL,Lot);
if (ExistOrders(Symbol(), 0, 888, 0) == true ) // проверяем наличие ордера buy
{
CloseOrder();
}
} 

Comment("Red line: ",b0,"Yellow line: ",b1);
return(0);
}
//-------------------------------------------------------------
//расчёт лота

double GetLot(int Risk)
{double Free =AccountFreeMargin();
double One_Lot =MarketInfo(Symbol(),MODE_MARGINREQUIRED);
double Min_Lot =MarketInfo(Symbol(),MODE_MINLOT);
double Max_Lot =MarketInfo(Symbol(),MODE_MAXLOT);
double Step =MarketInfo(Symbol(),MODE_LOTSTEP);
double Lot =MathFloor(Free*Risk/100/One_Lot/Step)*Step;
if(Lot<Min_Lot) Lot=Min_Lot;
if(Lot>Max_Lot) Lot=Max_Lot;
if(Lot*One_Lot>Free) return(0.0);
return(Lot);}


bool ExistOrders(string sy="", int op=-1, int Magic=-1, datetime ot=0)
{
int i, k=OrdersTotal(), ty;

if (sy=="0") sy=Symbol();
for (i=0; i<k; i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
ty=OrderType();
if (ty>1 && ty<6) {
if ((OrderSymbol()==sy || sy=="") && (op<0 || ty==op)) {
if (Magic<0 || OrderMagicNumber()==Magic) {
if (ot<=OrderOpenTime()) return(True);
}
}
}
}
}
return(False);
}


//открытие нового ордера
int NewOrder(int Cmd,double Lot)
{double TP=0; //тейкпрофит
double SL=0; //стоплосс
double PR=0; //Цена
while(!IsTradeAllowed()) Sleep(100);
if(Cmd==OP_BUY)
{PR=Ask;
if(TakeProfit>0) TP=Ask+TakeProfit*Point;
if(StopLoss>0) SL=Ask-StopLoss*Point;}
if(Cmd==OP_SELL)
{PR=Bid;
if(TakeProfit>0) TP=Bid-TakeProfit*Point;
if(StopLoss>0) SL=Bid+StopLoss*Point;}
int tic=OrderSend(Symbol(),Cmd,Lot,PR,3,SL,TP," ",0,0,Green);
if(tic<0) Print("Ошибка открытия ордера: " ,GetLastError());
return(tic);}


// закрытие ордера
void CloseOrder()
{double PR=0;
while(!IsTradeAllowed()) Sleep(100);
if(OrderType()==OP_BUY) PR=Bid;
if(OrderType()==OP_SELL) PR=Ask;
if(!OrderClose(OrderTicket(),OrderLots(),PR,3,Red))
Print("Ошибка закрытия ордера: " ,GetLastError());
return;}
Files:
qqea_1.mq4  4 kb
 
PAZITIV:

I am writing an EA based on QQEA indicator. So, the idea for the EA is: when the red line crosses the yellow line upwards, one Buy order should open, and when the red line crosses the yellow line downwards, one Sell order should open. But I am not able to open just one order. The orders are opened as long as the condition is fulfilled. Yes, and only buy orders are opened HAYDNT ?!

//

EA code

//--- input parameters
extern double MaxRisk=1.0;
extern double FixLot = 0.01;
extern double Exponent=2.0;
extern int Magic=888;

// костыли

extern int TakeProfit=100;
extern int StopLoss=100;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {
//----

//----
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
//----
   int Count=0;
   double b0,b1;
   int ticket;

// параметры индикатора
   int SF=5; // original 5
   int RSI_Period=14; // original 14
   double DARFACTOR=4.236; //original 4.236

//------------ Параметры из индикатора QQEA -----------------------
// Buffer0 -- красная жирная
   string Buffer0=iCustom(NULL,0,"QQEA",SF,RSI_Period,DARFACTOR,0,0);
// Buffer1 -- жёлтый пунктир
   string Buffer1=iCustom(NULL,0,"QQEA",SF,RSI_Period,DARFACTOR,1,0);

   b0=StrToDouble(Buffer0);
   b1=StrToDouble(Buffer1);


   double Lot=GetLot(MaxRisk);
// если лот <0 выводим сообщение об ошибке
   if(Lot==0)
     {
      Alert("Недостаточно средств!");
      return(0);
     }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
   if(Lot!=0 && b0>b1) // если лот <> 0 и красная выше жёлтой
     {
      ticket=NewOrder(OP_BUY,Lot);
      if(ExistOrders(Symbol(),1,888,0)==true) // проверяем наличие ордера sell
        {
         CloseOrder();
        }
     }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
   if(Lot!=0 && b0<b1) // если лот <> 0 и красная выше жёлтой
     {
      ticket=NewOrder(OP_SELL,Lot);
      if(ExistOrders(Symbol(),0,888,0)==true) // проверяем наличие ордера buy
        {
         CloseOrder();
        }
     }

   Comment("Red line: ",b0,"Yellow line: ",b1);
   return(0);
  }
//-------------------------------------------------------------
//расчёт лота

double GetLot(int Risk)
  {
   double Free=AccountFreeMargin();
   double One_Lot =MarketInfo(Symbol(),MODE_MARGINREQUIRED);
   double Min_Lot =MarketInfo(Symbol(),MODE_MINLOT);
   double Max_Lot =MarketInfo(Symbol(),MODE_MAXLOT);
   double Step=MarketInfo(Symbol(),MODE_LOTSTEP);
   double Lot =MathFloor(Free*Risk/100/One_Lot/Step)*Step;
   if(Lot<Min_Lot) Lot=Min_Lot;
   if(Lot>Max_Lot) Lot=Max_Lot;
   if(Lot*One_Lot>Free) return(0.0);
   return(Lot);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool ExistOrders(string sy="",int op=-1,int Magic=-1,datetime ot=0)
  {
   int i,k=OrdersTotal(),ty;

   if(sy=="0") sy=Symbol();
   for(i=0; i<k; i++)
      //+------------------------------------------------------------------+
      //|                                                                  |
      //+------------------------------------------------------------------+
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         ty=OrderType();
         if(ty>1 && ty<6)
           {
            if((OrderSymbol()==sy || sy=="") && (op<0 || ty==op))
              {
               if(Magic<0 || OrderMagicNumber()==Magic)
                 {
                  if(ot<=OrderOpenTime()) return(True);
                 }
              }
           }
        }
     }
   return(False);
  }
//открытие нового ордера
int NewOrder(int Cmd,double Lot)
  {
   double TP=0; //тейкпрофит
   double SL=0; //стоплосс
   double PR=0; //Цена
   while(!IsTradeAllowed()) Sleep(100);
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
   if(Cmd==OP_BUY)
     {
      PR=Ask;
      if(TakeProfit>0) TP=Ask+TakeProfit*Point;
      if(StopLoss>0) SL=Ask-StopLoss*Point;
     }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
   if(Cmd==OP_SELL)
     {
      PR=Bid;
      if(TakeProfit>0) TP=Bid-TakeProfit*Point;
      if(StopLoss>0) SL=Bid+StopLoss*Point;
     }
   if(GetOrdersCount(Magic,Cmd)>0)return(0);
   int tic=OrderSend(Symbol(),Cmd,Lot,PR,3,SL,TP," ",0,0,Green);
   if(tic<0) Print("Ошибка открытия ордера: ",GetLastError());
   return(tic);
  }
// закрытие ордера
void CloseOrder()
  {
   double PR=0;
   while(!IsTradeAllowed()) Sleep(100);
   if(OrderType()==OP_BUY) PR=Bid;
   if(OrderType()==OP_SELL) PR=Ask;
   if(!OrderClose(OrderTicket(),OrderLots(),PR,3,Red))
      Print("Ошибка закрытия ордера: ",GetLastError());
   return;
  }
//+------------------------------------------------------------------+
// подсчет кол-ва открытых позиций
int GetOrdersCount(int MagicNumber,int Type)
  {
   int count=0;

   for(int i=0; i<OrdersTotal(); i++)
     {
      // already closed
      if(OrderSelect(i,SELECT_BY_POS)==false) continue;
      // not current symbol
      if(OrderSymbol()!=Symbol()) continue;
      // order was opened in another way
      if(OrderMagicNumber()!=MagicNumber) continue;

      if(OrderType()==Type)
        {
         count++;
        }
     }

   return(count);
  }
//-------------------------------------------------------
Write in a more readable way, it will be easier later on.
 
PAZITIV:

I am writing an EA based on QQEA indicator. So, the idea for the EA is: when the red line crosses the yellow line upwards, one Buy order should open, and when the red line crosses the yellow line downwards, one Sell order should open. But I am not able to open just one order. The orders are opened as long as the condition is fulfilled. And only buy orders are opened.

//

EA code


Let me ask you a question.

On what basis is the string type used?

//------------ Параметры из индикатора QQEA -----------------------
// Buffer0 -- красная жирная
   string Buffer0=iCustom(NULL,0,"QQEA",SF,RSI_Period,DARFACTOR,0,0);
// Buffer1 -- жёлтый пунктир
   string Buffer1=iCustom(NULL,0,"QQEA",SF,RSI_Period,DARFACTOR,1,0);
 
   double diMA60=iMA(NULL,60,Period_indikatora1,0,Mod_MA,PRICE_CLOSE,sdvig);
   double diMA30=iMA(NULL,30,Period_indikatora2,0,Mod_MA,PRICE_CLOSE,sdvig)

guys EA uses two different timeframes (30 and 60), tell me what period to put in the tester, and whether the test period in the EA will not change?

 
NOT less than the minimum, i.e. in your case M30
 

What do these log entries mean:

2012.01.31 14:34:45 Memory handler: cannot allocate 10436536 bytes of memory

2012.01.31 14:34:45:45 HistoryBase: not enough memory 'EURGBP1' [206996 bars]

? Is it possible to fix if there is any problem?

 

Help with the arrays. It doesn't work the way I want it to.

There is a price array p[]. I need to create a new array, the length of which is one element less, and it is counted as the difference between two adjacent elements from the first array. If the difference is negative, then multiply by -1.

there is p [6]={1, 5, 9, 4, 6, 2, 3}

We should get

p_diff[5] = {-1*(1-5), -1*(5-9), 9-4, -1*(4-6), 6-2, -1*(2-3)} i.e. p_diff[5] = {4, 4, 5, 2, 4, 1}

int start()
{
  if( !NewBar() ) return(0);                                           
  int i, n, k,
      j = 0;                             
  for(i=0; i<=Bars_count; i++)
  {
      ZZ[i]=iCustom(NULL,0,"ZigZag",ExtDepth,ExtDeviation,ExtBackstep,0,i);  
      if(ZZ[i]!=0) 
      {
         Print(ZZ[j]);
         j = j + 1;
         k = j - 1;
         Print("index = ",k);
      }
  }
  Print("iiii = ", k);
  for(n = 0; n <= k-1; n++)
  { 
      ZZ_diff[n] = (ZZ[n] -ZZ[n+1]);
      if(ZZ_diff[n] < 0)  
         ZZ_diff[n] = ZZ_diff[n] * (-1);
      Print(ZZ_diff[n], "   index diff = ", n);
  }
return(0);
}
 
-Aleksey-:

What do these log entries mean:

2012.01.31 14:34:45 Memory handler: cannot allocate 10436536 bytes of memory

2012.01.31 14:34:45:45 HistoryBase: not enough memory 'EURGBP1' [206996 bars]

? Is it possible to fix if there is any problem?


Increase RAM capacity, reduce max. bars in window.
 
gince:

Help with the arrays. It doesn't work the way I want it to.

There is a price array p[]. I need to create a new array, the length of which is one element less, and it is counted as the difference between two adjacent elements from the first array. If the difference is negative, then multiply by -1.

there is p [6]={1, 5, 9, 4, 6, 2, 3}

We should get

p_diff[5] = {-1*(1-5), -1*(5-9), 9-4, -1*(4-6), 6-2, -1*(2-3)} i.e. p_diff[5] = {4, 4, 5, 2, 4, 1}

double MathAbs( double value)

The function returns the absolute value (modulo value) of the number passed to it

ZZ_diff[n] = MathAbs(ZZ[n] -ZZ[n+1]);
 
double zz_arr[1000];
double preZz=0;
int i,ii;
for(i=5000;i>=0;i--){
   double zz = iCustom(NULL,0,"ZigZag",ExtDepth,ExtDeviation,ExtBackstep,0,i); 
   if(zz!=0){
      if(preZz==0){preZz=zz;continue;}
      zz_arr[ii]=MathAbs(zz-preZz);
      preZz=zz;
      ii++;
   }
}
ArrayResize(zz_arr,ii);