Especialistas: Hedge any positions

 

Hedge any positions:

O EA abre uma posição oposta ao alcançar um lucro de N pips

Autor: Vladimir Karputov

 
Good afternoon my friend...
First I would like to congratulate you for the indicators and Open Source EA that you have made available, they are incredible.
I accessed an EA made by you, and I am using it as a basis for mine, but I have observed "irregular" behavior and I am not able to correct it ... could you help me?

What happens is that in some situations, he opens a duplicate hedge position, suppose the following;
-I set the distance to 5 pips and a coefficient of 2
-I open a BUY position with lot 0.01
-The price drops and the EA opens a position of 0.02
-The price goes up again and the EA opens two positions of 0.04 (total of 0.08)

I believe this is some kind of delay from the broker or something, but I am not managing to get around this problem, which occurs few times and in some cases only.

Would you help me?
Hedge any positions
Hedge any positions
  • www.mql5.com
The EA tracks all positions regardless of the symbol or magic number. As soon as a position (opened by another EA or manually) features a loss exceeding or equal to Losing , an opposite position is opened. The volume...
 
rmca :
Good afternoon my friend...
First I would like to congratulate you for the indicators and Open Source EA that you have made available, they are incredible.
I accessed an EA made by you, and I am using it as a basis for mine, but I have observed "irregular" behavior and I am not able to correct it ... could you help me?

What happens is that in some situations, he opens a duplicate hedge position, suppose the following;
-I set the distance to 5 pips and a coefficient of 2
-I open a BUY position with lot 0.01
-The price drops and the EA opens a position of 0.02
-The price goes up again and the EA opens two positions of 0.04 (total of 0.08)

I believe this is some kind of delay from the broker or something, but I am not managing to get around this problem, which occurs few times and in some cases only.

Would you help me?

Acho que, neste caso, nada pode ser feito - esta não é uma estratégia muito perfeita.

 

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?

 
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...
 
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

 
Basta deixar este EA ativo em algum gráfico que ele monitora e "protege" a posição de outro EA em outro gráfico , é isso ???
Razão: