Questions from Beginners MQL4 MT4 MetaTrader 4 - page 72

 
Vitalie Postolache:


I'll say the same thing. You are not good at logic. What is the problem to immediately calculate the initial lot, based on available funds and the value of risk (I think it was written about 3%)? Why do you have to do everything in one place?

Take the value of the free margin, multiply by the risk, divide by 100 and the value of the margin for 1 lot - here is the simplest formula for calculating the lot with a specified percentage of the free margin. Also, you need to take into account the step of lot change and prevent exceeding the min/max lot size, permitted by brokerage company:


I told you that the EA trades in a small interval of time in which there is a probability that 9 consecutive opposite orders overlapping each other will open and each next order is multiplied by 2, so the margin may not hold and the last pending order may not open, and therefore we have to calculate from the last maximum possible lot. I figured the best number of orders in 5-10 min interval is 9. In general I need to calculate a margin in case all the orders will be opened and on the basis of these data in reverse order to calculate the first lot.

 
if(OrdersTotal() >= OrderCount)
{
return;
}
//----
if(r > 50 && p > m) //open Buy order
{
ticketB = OrderSend(Symbol(),OP_BUY,0.1,Ask,5,0,0,",111,0,Green); //open buy order
}
//-----
if(r < 50 && p < m) //open Sell order conditions
{
ticketS = OrderSend(Symbol(),OP_SELL,0.1,Bid,5,0,0,",111,0,Blue);
}
//----- close orders
for (int i=1; i<=OrdersTotal(); i++)
{
if(OrderSelect(ticketB,SELECT_BY_TICKET)==true)
{
if(r < 50 && p < m)
{
OrderClose(ticketB,0.1,Bid,5,Red);
}
}

}

Where did I spell it wrong? I think I took it from an example, could you please tell me how to spell it correctly so the order would close

on reverse buy conditions? I'm racking my brains.

 
funnyrain8:
if(OrdersTotal() >= OrderCount)
{
return;
}
//----
if(r > 50 && p > m) //open Buy order
{
ticketB = OrderSend(Symbol(),OP_BUY,0.1,Ask,5,0,0,",111,0,Green); //open buy order
}
//-----
if(r < 50 && p < m) //open Sell order conditions
{
ticketS = OrderSend(Symbol(),OP_SELL,0.1,Bid,5,0,0,",111,0,Blue);
}
//----- close orders
for (int i=1; i<=OrdersTotal(); i++)
{
if(OrderSelect(ticketB,SELECT_BY_TICKET)==true)
{
if(r < 50 && p < m)
{
OrderClose(ticketB,0.1,Bid,5,Red);
}
}

}

Where did I spell it wrong? I think I took it from an example, could you please tell me how to spell it correctly so the order would close

on reverse buy conditions? I'm racking my brains.


 if(OrdersTotal()>=OrderCount)  //Обрати внимание на эту конструкцию
     {                            //
      return;
     }
//---- 
   if(r>50 && p>m) //условия открытия ордера на покупку
     {
      for(int i=1; i<=OrdersTotal(); i++)
        {
         if(OrderSelect(ticketB,SELECT_BY_TICKET)==true)
           {
            if(OrderType()==OP_SELL)
              {
               OrderClose(OrderTicket(),0.1,Ask,5,Red);
              }
           }
        }
      ticketB=OrderSend(Symbol(),OP_BUY,0.1,Ask,5,0,0,"",111,0,Green); //открыnие ордера на покупку     

     }

//-----     
   if(r<50 && p<m) //условия открытия ордера на продажу
     {
     for(int i=1; i<=OrdersTotal(); i++)
        {
         if(OrderSelect(ticketB,SELECT_BY_TICKET)==true)
           {
            if(OrderType()==OP_BUY)
              {
               OrderClose(OrderTicket(),0.1,Bid,5,Red);
              }
           }
        }
      ticketS=OrderSend(Symbol(),OP_SELL,0.1,Bid,5,0,0,"",111,0,Blue);
     }

Like this
 
funnyrain8:
if(OrdersTotal() >= OrderCount)
{
return;
}
//----
if(r > 50 && p > m) //open Buy order
{
ticketB = OrderSend(Symbol(),OP_BUY,0.1,Ask,5,0,0,",111,0,Green); //open buy order
}
//-----
if(r < 50 && p < m) //open Sell order conditions
{
ticketS = OrderSend(Symbol(),OP_SELL,0.1,Bid,5,0,0,",111,0,Blue);
}
//----- close orders
for (int i=1; i<=OrdersTotal(); i++)
{
if(OrderSelect(ticketB,SELECT_BY_TICKET)==true)
{
if(r < 50 && p < m)
{
OrderClose(ticketB,0.1,Bid,5,Red);
}
}

}

Where did I spell it wrong? I think I took it from an example, could you please tell me how to spell it correctly so the order would close

on reverse buy conditions? I'm racking my brains.


If the order is selected on the ticket, you don't need an action in the loop. If you wantto correct the code, use the SRC button topaste the code into the message.
 
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict    
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int ticketB,ticketS;
void OnTick()
//------------------------------------------------------------------------------------------------
{
double p = iClose("EURUSD",PERIOD_M15,0);                                    //показание текущей цены 
double r = iRSI  ("EURUSD",PERIOD_M15,21,PRICE_CLOSE,0);                     //значение индюка RSI
double m = iMA   ("EURUSD",PERIOD_M15,89,0,MODE_EMA,PRICE_CLOSE,0);          //значение индюка МА
int    OrderCount  = 1;

//---------------ПОДСЧЕТ ОРДЕРОВ------------------------------------------------------------------
if(OrdersTotal() >= OrderCount)
          {
             return;
          }   
//-------------ОТКРЫТИЕ ОРДЕРА НА ПОКУПКУ--------------------------------------------------------------------
/*if(r > 50 && p > m)                                                        //условия открытия ордера на покупку
    {   
        ticketB = OrderSend(Symbol(),OP_BUY,0.1,Ask,5,0,0,"",111,0,Green);         
    }
//------------ЗАКРЫТИЕ ОРДЕРА НА ПРОДАЖУ--------------------------------------------------------
if(r<50 && p<m)                                                           
     {
      for(int i=1; i<=OrdersTotal(); i++)
        {
         if(OrderSelect(ticketB,SELECT_BY_TICKET)==true)
           {
            if(OrderType()==OP_BUY)
              {
                  ticketB = OrderClose(OrderTicket(),0.1,Bid,5,Red);
              }
           }
        }
     }*/
//------------------ОТКРЫТИЕ ОРДЕРА НА ПРОДАЖУ---------------------------------------------------------
if(r < 50 && p < m)                                                        //условия открытия ордера на продажу
    {
        ticketS = OrderSend(Symbol(),OP_SELL,0.1,Bid,5,0,0,"",111,0,Blue); 
    } 
//---------------------ЗАКРЫТИЕ ОРДЕРА НА ПРОДАЖУ---------------------------------------------------------
if(r>50 && p>m) 
     {
   for(int i=1; i<=OrdersTotal(); i++)
        {
         if(OrderSelect(ticketS,SELECT_BY_TICKET)==true)
           {
            if(OrderType()==OP_SELL)
              {
                  ticketS = OrderClose(OrderTicket(),0.1,Ask,5,Red);
              }
           }
        }
     }
}
//+------------------------------------------------------------------+
Thanks for the SRC), this is what it looks like, ts looks like this, sell should happen when candle closed below the wop and rsi below 50, and close the order at opposite conditions, and simultaneously after close buy should happen and after close, then one order and no more and so on cyclically ... but now I have one order open and that's it, and stops are not working ...
 
funnyrain8:
Thanks for the SRC), this is what it looks like, ts looks like this, sell should happen when candle closed below the wrist and rsi below 50, and close the order at the opposite conditions, and at the same time after closing should happen buying and after closing, then one order and no more and so on cyclically ... but now I open one order and all, and stops do not work ...


I have rewritten it in my own style, I hope you understand, if something is unclear, do not hesitate to ask

//+------------------------------------------------------------------+
//|                                                   funnyrain8.mq4 |
//|                                                   Sergey Gritsay |
//|                         https://www.mql5.com/ru/users/sergey1294 |
//+------------------------------------------------------------------+
#property copyright "Sergey Gritsay"
#property link      "https://www.mql5.com/ru/users/sergey1294"
#property version   "1.00"
#property strict

input int             MagicNumber  = 20110315;       // Номер копии
input string          CommentOrder = "Origenal";     // Комментарий к ордеру
input int             Slippage=100;             // Проскальзование (Пипс)
input double          Lot=0.01;           // Объем лота 
input int             SignalBar=0; // Сигнальный бар 

sinput string            s1=NULL; // Setting indicator slow МА
input int                ma_period=89;    // Period
input ENUM_MA_METHOD     ma_method=MODE_EMA;//Method calculation
input ENUM_APPLIED_PRICE ma_price=PRICE_CLOSE;//Price calculation

sinput string            s2=NULL; // Setting indicator RSI
input int                rsi_period=21; // Period
input ENUM_APPLIED_PRICE rsi_price=PRICE_CLOSE;//Price calculation
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(TotalOrder()==0)OrderOpen(GetSignal());
   else OrderClose(GetSignal());
  }
//+------------------------------------------------------------------+
int GetSignal()
  {
   double rsi = iRSI(_Symbol,PERIOD_CURRENT,rsi_period,rsi_price,SignalBar);                     //значение индюка RSI
   double ma = iMA   (_Symbol,PERIOD_CURRENT,ma_period,0,ma_method,ma_price,SignalBar);          //значение индюка МА

   if(Close[SignalBar]>ma && rsi>50.0)return(OP_BUY);
   if(Close[SignalBar]<ma && rsi<50.0)return(OP_SELL);
   return(WRONG_VALUE);
  }
//+------------------------------------------------------------------+
int TotalOrder()
  {
   int value=0;
   int total=OrdersTotal();
   for(int i=total-1; i>=0; i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS))continue;
      if(OrderSymbol()!=_Symbol)continue;
      if(OrderMagicNumber()!=MagicNumber)continue;
      if(OrderType()>1)continue;
      value++;
     }
   return(value);
  }
//+------------------------------------------------------------------+
void OrderOpen(int type)
  {
   if(type==WRONG_VALUE)return;
   double price_open=(type==OP_BUY)?NormalizeDouble(Ask,_Digits):NormalizeDouble(Bid,_Digits);
   int ticket=OrderSend(_Symbol,type,Lot,price_open,Slippage,0,0,CommentOrder,MagicNumber);
   if(ticket<0)Print("Ошибка открытия ордера № - ",GetLastError());
  }
//+------------------------------------------------------------------+
void OrderClose(int type)
  {
   if(type==WRONG_VALUE)return;
   int total=OrdersTotal();
   for(int i=total-1; i>=0; i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS))continue;
      if(OrderMagicNumber()!=MagicNumber)continue;
      if(OrderSymbol()!=_Symbol)continue;
      if(OrderType()!=type)continue;
      double price_close=(type==OP_BUY)?NormalizeDouble(Bid,_Digits):NormalizeDouble(Ask,_Digits);
      bool res=OrderClose(OrderTicket(),OrderLots(),price_close,Slippage);
      if(!res)Print("Ошибка закрытия ордера № - ",GetLastError());
     }
  }
//+------------------------------------------------------------------+

...

 
Sergey Gritsay:


rewritten in my own style, hope you understand, if you don't understand anything ask

...


oh, i thought i wrote almost everything except the closing and i still have a lot to learn...thanks a lot now at least i have an example to go on from...tin, thanks again)
 
Hello. I have downloaded Metatrader 5 software. I signed up for 2 signals. My account balance is positive 20$. I am trying to install virtual hosting and nothing works. I press navigator, connect to trading account, I even have no such tab in the table virtual hosting. I am sending you screenshots. How do I connect to the shared hosting?
Files:
3p46y0.jpg  273 kb
g4ppe22.jpg  264 kb
 
Жанна Платонова:
Hello. I have downloaded Metatrader 5 software. I signed up for 2 signals. My account balance is positive 20$. I am trying to install virtual hosting and nothing works. I press navigator, connect to trading account, I even have no such tab in the table virtual hosting. I am sending you screenshots. How do I connect to the shared hosting?

Click help in the main menu.
 
Hello. I have a question - on the mac (sierra) in all delivered (through wine) from different companies platforms mt4 the same glitch - the terminal if you release it from its normal place at the bottom to move it as a free window - it shrinks and work through it is completely impossible. Disabling the auto-arranger does not work. To return the terminal on the former place absolutely impossible - i.e. it simply does not attach and does not open any more. I have a question - can anyone know a shortcut or button to return the entire interface to its original place. Sincerely.
Reason: