Experts: Hedge any positions - page 2

[Deleted]  

Good afternoon my friend...

I keep studying about MQL5 programming and its codes have served as an inspiration and base of studies, I have learned a lot, but sometimes I get a little lost and I can't understand exactly what certain codes do and why they are there ...

I was able to understand the whole code of "Hedge any positions", just a part of the code that I am having difficulty understanding what it does and why it is there, could help me understand what it is about and why it was added to the code?

bool RefreshRates(const string symbol,
                  const ulong magic,
                  double freeze_level,
                  double stop_level)
{
   if(!m_symbol.Name(symbol))
   {
      return(false);
   }

   m_trade.SetExpertMagicNumber(magic);
   m_trade.SetDeviationInPoints(0);
   m_trade.SetTypeFillingBySymbol(m_symbol.Name());
   m_trade.SetMarginMode();

   if(!m_symbol.RefreshRates())
   {
      Print("RefreshRates error");
      return(false);
   }

   if(!m_symbol.Refresh())
   {
      Print("Refres error");
      return(false);
   }
   
   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
   {
      return(false);
   }

   freeze_level=m_symbol.FreezeLevel()*m_symbol.Point();
   if(freeze_level==0.0)
   {
      freeze_level=(m_symbol.Ask()-m_symbol.Bid())*3.0;
   }
   freeze_level*=1.1;

   stop_level=m_symbol.StopsLevel()*m_symbol.Point();
   if(stop_level==0.0)
   {
      stop_level=(m_symbol.Ask()-m_symbol.Bid())*3.0;
   }
   stop_level*=1.1;

   if(freeze_level<=0.0 || stop_level<=0.0)
   {
      return(false);
   }
   return(true);
}
 
rmca :

Good afternoon my friend...

I keep studying about MQL5 programming and its codes have served as an inspiration and base of studies, I have learned a lot, but sometimes I get a little lost and I can't understand exactly what certain codes do and why they are there ...

I was able to understand the whole code of "Hedge any positions", just a part of the code that I am having difficulty understanding what it does and why it is there, could help me understand what it is about and why it was added to the code?

What exactly is not clear?

[Deleted]  
Vladimir Karputov:

What exactly is not clear?

freeze_level=m_symbol.FreezeLevel()*m_symbol.Point();
   if(freeze_level==0.0)
   {
      freeze_level=(m_symbol.Ask()-m_symbol.Bid())*3.0;
   }
   freeze_level*=1.1;

   stop_level=m_symbol.StopsLevel()*m_symbol.Point();
   if(stop_level==0.0)
   {
      stop_level=(m_symbol.Ask()-m_symbol.Bid())*3.0;
   }
   stop_level*=1.1;

   if(freeze_level<=0.0 || stop_level<=0.0)
   {
      return(false);
   }
   return(true);
}

I didn't understand why "freeze_level" and "stop_level" are checked, why in case they return the value "0" does the calculation (Ask-Bid) * 3 occur? And when the value "0" does not return, does multiplication by 1.1 occur?

What are these "freeze_level" and "stop_level" about?

I looked in the documentation and I didn't really understand their purpose and why this check and multiplication is done in cases of "0" return or not.

 
rmca :

I didn't understand why "freeze_level" and "stop_level" are checked, ***

Why check: read The checks a trading robot must pass before publication in the Market


rmca:

***why in case they return the value "0" does the calculation (Ask-Bid) * 3 occur? ***

Elementary protection: '0' can mean floating level. In such cases, it is customary to multiply by '3'.


My new Expert Advisors use a modified approach:

***
input group             "Additional features"
***
input uchar    InpFreezeCoefficient = 1;           // Coefficient (if Freeze==0 Or StopsLevels==0)
***
//+------------------------------------------------------------------+
//| Check Freeze and Stops levels|
//+------------------------------------------------------------------+
void FreezeStopsLevels(double &freeze,double &stops)
  {
//--- check Freeze and Stops levels
   /*
 SYMBOL_TRADE_FREEZE_LEVEL shows the distance of freezing the trade operations
 for pending orders and open positions in points
 ------------------------|--------------------|--------------------------------------------
 Type of order/position | Activation price | Check
 ------------------------|--------------------|--------------------------------------------
 Buy Limit order| Ask | Ask-OpenPrice>= SYMBOL_TRADE_FREEZE_LEVEL
 Buy Stop order |Ask | OpenPrice-Ask >= SYMBOL_TRADE_FREEZE_LEVEL
 Sell Limit order | Bid| OpenPrice-Bid >= SYMBOL_TRADE_FREEZE_LEVEL
 Sell Stop order | Bid| Bid-OpenPrice >= SYMBOL_TRADE_FREEZE_LEVEL
 Buy position| Bid| TakeProfit-Bid >= SYMBOL_TRADE_FREEZE_LEVEL
 || Bid-StopLoss >= SYMBOL_TRADE_FREEZE_LEVEL
 Sell position | Ask| Ask-TakeProfit >= SYMBOL_TRADE_FREEZE_LEVEL
 || StopLoss-Ask >= SYMBOL_TRADE_FREEZE_LEVEL
 ------------------------------------------------------------------------------------------

 SYMBOL_TRADE_STOPS_LEVEL determines the number of points for minimum indentation of the
 StopLoss and TakeProfit levels from the current closing price of the open position
 ------------------------------------------------|------------------------------------------
 Buying is done at the Ask price | Selling is done at the Bid price
 ------------------------------------------------|------------------------------------------
 TakeProfit>= Bid| TakeProfit <= Ask
 StopLoss<= Bid| StopLoss >= Ask
 TakeProfit - Bid >= SYMBOL_TRADE_STOPS_LEVEL | Ask - TakeProfit >= SYMBOL_TRADE_STOPS_LEVEL
 Bid - StopLoss >= SYMBOL_TRADE_STOPS_LEVEL | StopLoss - Ask >= SYMBOL_TRADE_STOPS_LEVEL
 ------------------------------------------------------------------------------------------
 */
   double coeff=(double)InpFreezeCoefficient;
   if(!RefreshRates() || !m_symbol.Refresh())
      return;
//--- FreezeLevel -> for pending order and modification
   double freeze_level=m_symbol.FreezeLevel()*m_symbol.Point();
   if(freeze_level==0.0)
      if(InpFreezeCoefficient>0)
         freeze_level=(m_symbol.Ask()-m_symbol.Bid())*coeff;
//--- StopsLevel -> for TakeProfit and StopLoss
   double stop_level=m_symbol.StopsLevel()*m_symbol.Point();
   if(stop_level==0.0)
      if(InpFreezeCoefficient>0)
         stop_level=(m_symbol.Ask()-m_symbol.Bid())*coeff;
//---
   freeze=freeze_level;
   stops=stop_level;
//---
   return;
  }
The checks a trading robot must pass before publication in the Market
The checks a trading robot must pass before publication in the Market
  • www.mql5.com
Before any product is published in the Market, it must undergo compulsory preliminary checks, as a small error in the expert or indicator logic can cause losses on the trading account. That is why we have developed a series of basic checks to ensure the required quality level of the Market products. If any errors are identified by the Market...
[Deleted]  
Vladimir Karputov:

Why check: read The checks a trading robot must pass before publication in the Market


Elementary protection: '0' can mean floating level. In such cases, it is customary to multiply by '3'.


My new Expert Advisors use a modified approach:

Thanks for the reply, I read all the documentation you sent me and made some changes to the code:

bool RefreshRates(const string symb,
                  const ulong magi)
{
   if((!m_symbol.Name(symb))
   && (!m_symbol.RefreshRates() || !m_symbol.Refresh())
   && (m_symbol.Ask()==0 || m_symbol.Bid()==0))
   {
      return(false);
   }
   m_trade.SetExpertMagicNumber(magi);
   m_trade.SetDeviationInPoints(0);
   m_trade.SetTypeFillingBySymbol(m_symbol.Name());
   m_trade.SetMarginMode();
   return(true);
}

Do you think this is a good approach to update (and check for updates) quotes and configuration for a trade?

 
rmca :

Thanks for the reply, I read all the documentation you sent me and made some changes to the code:

Do you think this is a good approach to update (and check for updates) quotes and configuration for a trade?

These are the lines:

   m_trade.SetDeviationInPoints(0);
   m_trade.SetTypeFillingBySymbol(m_symbol.Name());
   m_trade.SetMarginMode();

once you need to register in OnInit.



You have set zero slippage - be prepared for the fact that you will receive a rejection very often

 
All you have to do is leave this EA active on a chart and it will monitor and "protect" the position of another EA on another chart, is that it?
 
One question, what happens when the lot coefficient is less than 1, for example 0.5?
 
Eduardo Alvarado :
One question, what happens when the lot coefficient is less than 1, for example 0.5?

A position with a smaller volume will be opened.

 
Is it possible to upgrade this EA, to filter orders and only apply hedge by specific magic number