Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1654

 
MakarFX #:

If you know of another option, write...

You can help people.)

You make all variables separately in the inite and separate them by further processing (no need to be lazy).

Next, bracket the blocks and compare the return variables.

 
Volodymyr Zubov #:

Make all variables separately in the init and separate them by further processing (no need to be lazy).

Next, bracket the blocks and compare the return variables.

Show how.
 

Somewhere like this...

I wanted to post the whole owl, but the forum won't let me.

Which fragment do you want?

 
Volodymyr Zubov #:

Somewhere like this...

Which piece do you want?

Chalo...
 
I know)
 

Forum on trading, automated trading systems & strategy testing

Any questions from newbies in MQL4 and MQL5, help and discussion on algorithms and codes

MakarFX, 2021.10.08 18:43

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(CheckForOpen()==0)
     {
      if(OrderSend(Symbol(),OP_BUY,Lots(),Ask,Slip,Bid-StopLoss*Point,Ask+TakeProfit*Point,"",MagicNumber,0,Blue)) Print("BUY OK");
     }
   if(CheckForOpen()==1)
     {
      if(OrderSend(Symbol(),OP_SELL,Lots(),Bid,Slip,Ask+StopLoss*Point,Bid-TakeProfit*Point,"",MagicNumber,0,Red)) Print("SELL OK");
     }
  }
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
int CheckForOpen() // Открытие ордера по методу Пуриа
  {
   double malw,mas1,mas2,macd;
   int    res=-1, buy=0, sell=0;
   // Считывание параметров индикаторов 3 свечи
   malw=iMA(NULL,0,MovingPeriodLw,0,MODE_EMA,PRICE_CLOSE,3);
   mas1=iMA(NULL,0,MovingPeriodS1,0,MODE_LWMA,PRICE_LOW,3);
   mas2=iMA(NULL,0,MovingPeriodS2,0,MODE_LWMA,PRICE_LOW,3);
   macd=iMACD(NULL,0,15,26,1,PRICE_CLOSE,MODE_MAIN,3);
   if(malw>mas1&&malw>mas2&&macd>0) {buy+=1; sell=0;}
   if(malw<mas1&&malw<mas2&&macd<0) {buy=0; sell+=1;}
   // Считывание параметров индикаторов 2 свечи
   malw=iMA(NULL,0,MovingPeriodLw,0,MODE_EMA,PRICE_CLOSE,2);
   mas1=iMA(NULL,0,MovingPeriodS1,0,MODE_LWMA,PRICE_LOW,2);
   mas2=iMA(NULL,0,MovingPeriodS2,0,MODE_LWMA,PRICE_LOW,2);
   macd=iMACD(NULL,0,15,26,1,PRICE_CLOSE,MODE_MAIN,2);
   if(malw>mas1&&malw>mas2&&macd>0) {buy+=1; sell=0;}
   if(malw<mas1&&malw<mas2&&macd<0) {buy=0; sell+=1;}
   // Считывание параметров индикаторов 1 свечи
   malw=iMA(NULL,0,MovingPeriodLw,0,MODE_EMA,PRICE_CLOSE,1);
   mas1=iMA(NULL,0,MovingPeriodS1,0,MODE_LWMA,PRICE_LOW,1);
   mas2=iMA(NULL,0,MovingPeriodS2,0,MODE_LWMA,PRICE_LOW,1);
   macd=iMACD(NULL,0,15,26,1,PRICE_CLOSE,MODE_MAIN,1);
   if(malw>mas1&&malw>mas2&&macd>0) {buy+=1; sell=0;}
   if(malw<mas1&&malw<mas2&&macd<0) {buy=0; sell+=1;}
   // Считывание параметров индикаторов 0 свечи
   malw=iMA(NULL,0,MovingPeriodLw,0,MODE_EMA,PRICE_CLOSE,0);
   mas1=iMA(NULL,0,MovingPeriodS1,0,MODE_LWMA,PRICE_LOW,0);
   mas2=iMA(NULL,0,MovingPeriodS2,0,MODE_LWMA,PRICE_LOW,0);
   macd=iMACD(NULL,0,15,26,1,PRICE_CLOSE,MODE_MAIN,0);
   if(malw>mas1&&malw>mas2&&macd>0) {buy+=1; sell=0;}
   if(malw<mas1&&malw<mas2&&macd<0) {buy=0; sell+=1;}
   
   if(buy ==4) res=0;
   if(sell==4) res=1;
   return(res);
  }

I think you had to wrap identical indicator calls in a function, the code would be more compact, and who knows, now you're using indicator values for 4 bars, tomorrow for 5 ...

i.e.

int SignalByPuria(const int bar)
{
   malw=iMA(NULL,0,MovingPeriodLw,0,MODE_EMA,PRICE_CLOSE,bar);
   mas1=iMA(NULL,0,MovingPeriodS1,0,MODE_LWMA,PRICE_LOW,bar);
   mas2=iMA(NULL,0,MovingPeriodS2,0,MODE_LWMA,PRICE_LOW,bar);
......
}

sometimes there are indicators that don't have a trade signal on a certain bar. I usually use enum E_CMD{CMD_BUY,CMD_SELL,CMD_NONE};

and then the signature of the "signal" function will beE_CMD SignalByPuria(const int bar) , and the EA code itself will turn into something like this:

 E_CMD Signal_ind_1(const int bar)
{
....
}

 E_CMD Signal_ind_2(const int bar)
{
....
}

 E_CMD Signal_ind_2(const int bar)
{
....
}

void OnTick()
{
..... 

if(Signal_ind_1 = =CMD_BUY && Signal_ind_2 == CMD_BUY && Signal_ind_3 == CMD_BUY) // открываем ордер на покупку
 else if(Signal_ind_1 = =CMD_SELL && Signal_ind_2 == CMD_SELL && Signal_ind_3 == CMD_SELL) // открываем ордер на продажу

....
}

imho, this approach allows you to quickly add new trading signals to the already written EA code

 
Igor Makanu #:

I think you should have wrapped the same indicator calls in a function, the code would have been more compact, and who knows, now you use indicator values on 4 bars, tomorrow on 5...

i.e.

sometimes there are indicators that don't have a trade signal on a certain bar. I usually use enum E_CMD{CMD_BUY,CMD_SELL,CMD_NONE};

and then the signature of the "signal" function will beE_CMD SignalByPuria(const int bar) , and the EA code itself will turn into something like this:

imho, this approach allows you to quickly add new trading signals to the already written EA code

Thank you Igor, I will take it under advisement)
 

Back to our conversation, here is the market position opening function for MT4

//+------------------------------------------------------------------+
//|                                                   Zero_Level.mq4 |
//|                                         Copyright © 2007, Xupypr |
//+------------------------------------------------------------------+
// Функция вычисляющая уровни безубытка, на покупку, на продажу с учетом накопленных свопов.
double Zero_Level(string sy, int mn)
  {
   double ZeroLevel=0;
   double BuyLots=0;
   double SellLots=0;
   double BuyProfit=0;
   double SellProfit=0;
   double SellLevel;
   double BuyLevel;

   int Total=OrdersTotal();
   for(int i=Total-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {

         if(OrderSymbol()!=sy)
            continue;
         if(OrderMagicNumber() != mn)
            continue;

         if(OrderType()==OP_BUY)
           {
            BuyLots=BuyLots+OrderLots();
            BuyProfit=BuyProfit+OrderProfit()+OrderCommission()+OrderSwap();
           }
         if(OrderType()==OP_SELL)
           {
            SellLots=SellLots+OrderLots();
            SellProfit=SellProfit+OrderProfit()+OrderCommission()+OrderSwap();
           }
        }
     }

   double TickValue=MarketInfo(sy,MODE_TICKVALUE);
   if(BuyLots>0)
      BuyLevel=NormalizeDouble(MarketInfo(sy,MODE_BID)-(BuyProfit/(TickValue*BuyLots)*MarketInfo(sy,MODE_POINT)),(int)MarketInfo(sy,MODE_DIGITS));
   else
      BuyLevel=0;
   if(SellLots>0)
      SellLevel=NormalizeDouble(MarketInfo(sy,MODE_ASK)+(SellProfit/(TickValue*SellLots)*MarketInfo(sy,MODE_POINT)),(int)MarketInfo(sy,MODE_DIGITS));
   else
      SellLevel=0;
   if(BuyLevel>0)
      ZeroLevel=BuyLevel;
   if(SellLevel>0)
      ZeroLevel=SellLevel;

   return NormalizeDouble(ZeroLevel,_Digits);//Нормализовали полученную цену
  }
//--- End ---
//+---------------------------------------------------------------------------+
//|    Функция открытия рыночной позиции  (c) Boshetunmay 2021                |
//+---------------------------------------------------------------------------+
//|  Параметры:                                                               |
//|    sy - наименование инструмента                                          |
//|    op - операция                                                          |
//|    ll - лот                                                               |
//|    slipp - проскальзывание                                                |
//|    sl - уровень стоп                                                      |
//|    tp - уровень тейк                                                      |
//|    comment - коментарий                                                   |
//|    mn - MagicNumber                                                       |
//|    cl - цвет значка открытия                                              |
//+---------------------------------------------------------------------------+
//  OpenPosition(string symbol,int operation,double volume,int slippage,int stoploss,int takeprofit,string comment,int magic,color);
int OpenPosition(string sy, int op, double ll, int slipp, int sl, int tp, string comment, int mn,color cl)
  {
   if(op == OP_BUY)  // открытие BUY
     {
      // проверяем доступность свободных средств
      if((AccountFreeMarginCheck(sy,OP_BUY,ll)<=0) || (GetLastError()==134))
        {
         Print(sy," ",ll," It is impossible to open the order Buy, not enough money.");
         return(0);
        }
      RefreshRates();

      // открываем ордер OP_BUY
      int ticketbuy = OrderSend(sy,OP_BUY,ll,MarketInfo(sy,MODE_ASK),slipp,0,0,comment,mn,0,cl);
      if(ticketbuy<0)
         Print(sy," OpenPosition. OrderSend Buy fail #",GetLastError());
      else
         Print(sy," OpenPosition. OrderSend Buy successfully");

      Sleep(1000);

      // модифицируем ордер (выставляем тейпрофит и стоплосс)
      if(sl !=0 || tp !=0)
        {
         //--- получим минимальное значение Stop level
         double minstoplevel=MarketInfo(sy,MODE_STOPLEVEL);
         Print("Minimum Stop Level=",minstoplevel," points");
         //--- вычисленные значения цен SL и TP должны быть нормализованы
         double BSLoss = NormalizeDouble(Zero_Level(_Symbol,mn)-(sl+minstoplevel)*MarketInfo(sy,MODE_POINT),(int)MarketInfo(sy,MODE_DIGITS));
         double BTProfit = NormalizeDouble(Zero_Level(_Symbol,mn)+(tp+minstoplevel)*MarketInfo(sy,MODE_POINT),(int)MarketInfo(sy,MODE_DIGITS));
         //--- если входящие значения ноль то заменяем цену модификации на ноль
         if(sl == 0)
            BSLoss = 0;
         if(tp == 0)
            BTProfit = 0;

         bool resbuy = OrderModify(ticketbuy,OrderOpenPrice(),BSLoss,BTProfit,0,clrNONE);
         if(!resbuy)
            Print(sy," OpenPosition. OrderModify Buy fail #",GetLastError());
         else
            Print(sy," OpenPosition. OrderModify Buy successfully");
        }
     }

   if(op == OP_SELL)   // открытие Sell
     {
      // проверяем доступность свободных средств
      if((AccountFreeMarginCheck(sy,OP_SELL,ll)<=0) || (GetLastError()==134))
        {
         Print(sy," ",ll," It is impossible to open the order Sell, not enough money.");
         return(0);
        }
      RefreshRates();

      // открываем ордер OP_SELL
      int ticketsell = OrderSend(sy,OP_SELL,ll,MarketInfo(sy,MODE_BID),slipp,0,0,comment,mn,0,cl);
      if(ticketsell<0)
         Print(sy," OpenPosition. OrderSend Sell fail #",GetLastError());
      else
         Print(sy," OpenPosition. OrderSend Sell successfully");

      Sleep(1000);

      // модифицируем ордер (выставляем тейпрофит и стоплосс)
      if(sl !=0 || tp !=0)
        {
         //--- получим минимальное значение Stop level
         double minstoplevel=MarketInfo(sy,MODE_STOPLEVEL);
         Print("Minimum Stop Level=",minstoplevel," points");
         //--- вычисленные значения цен SL и TP должны быть нормализованы
         double SSLoss = NormalizeDouble(Zero_Level(_Symbol,mn)+(sl+minstoplevel)*MarketInfo(sy,MODE_POINT),(int)MarketInfo(sy,MODE_DIGITS));
         double STProfit = NormalizeDouble(Zero_Level(_Symbol,mn)-(tp+minstoplevel)*MarketInfo(sy,MODE_POINT),(int)MarketInfo(sy,MODE_DIGITS));
         //--- если входящие значения ноль то заменяем цену модификации на ноль
         if(sl == 0)
            SSLoss = 0;
         if(tp == 0)
            STProfit = 0;

         bool ressell = OrderModify(ticketsell,OrderOpenPrice(),SSLoss,STProfit,0,clrNONE);
         if(!ressell)
            Print(sy," OpenPosition. OrderModify Sell fail #",GetLastError());
         else
            Print(sy," OpenPosition. OrderModify Sell successfully");
        }
     }
   return (1);
  }
//--- End ---

Returns trade flag today

//+----------------------------------------------------------------------------+
//|  Описание : Возвращает флаг торгов сегодня.                                |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
int isTradeToDay(string sy="", int op=-1, int mn=-1)
  {
   int i, k=OrdersHistoryTotal();

   if(sy=="0")
      sy=_Symbol;
   for(i=0; i<k; i++)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
        {
         if(OrderSymbol()==sy || sy=="")
           {
            if(OrderType()==OP_BUY || OrderType()==OP_SELL)
              {
               if(op<0 || OrderType()==op)
                 {
                  if(mn<0 || OrderMagicNumber()==mn)
                    {
                     if(TimeDay(OrderOpenTime())==Day()
                        &&  TimeMonth(OrderOpenTime())==Month()
                        &&  TimeYear(OrderOpenTime())==Year())
                        return(1);
                    }
                 }
              }
           }
        }
     }
   k=OrdersTotal();
   for(i=0; i<k; i++)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderSymbol()==sy || sy=="")
           {
            if(OrderType()==OP_BUY || OrderType()==OP_SELL)
              {
               if(op<0 || OrderType()==op)
                 {
                  if(mn<0 || OrderMagicNumber()==mn)
                    {
                     if(TimeDay(OrderOpenTime())==Day()
                        &&  TimeMonth(OrderOpenTime())==Month()
                        &&  TimeYear(OrderOpenTime())==Year())
                        return(1);
                    }
                 }
              }
           }
        }
     }
   return(0);
  }
//--- End ---

Returns number of positions

//+----------------------------------------------------------------------------+
//|  Описание : Возвращает количество позиций.                                 |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
int NumberOfPositions(string sy="", int op=-1, int mn=-1)
  {
   int i, k=OrdersTotal(), kp=0;

   if(sy=="0")
      sy=_Symbol;
   for(i=0; i<k; i++)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderSymbol()==sy || sy=="")
           {
            if(OrderType()==OP_BUY || OrderType()==OP_SELL)
              {
               if(op<0 || OrderType()==op)
                 {
                  if(mn<0 || OrderMagicNumber()==mn)
                     kp++;
                 }
              }
           }
        }
     }
   return(kp);
  }
//--- End ---

All optimised for the current build of the terminal.

 
Volodymyr Zubov number of positions

All optimised for the current build of the terminal.

I don't understand what the fingers are for?
 
MakarFX #:
I do not understand why there are fingers?

There are fingers to the fact that it is inappropriate to only place OrderSend, and then not to complain that something is wrong. You always have to look for errors.

Reason: