How To hedge properly?

 

I want to hedge but my ea will only open trades both(buy & sell) at the same instead of 10 pips.

please help if you know the problem

#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\AccountInfo.mqh>
#include <Trade\OrderInfo.mqh>

CTrade trade;
CPositionInfo m_position;
CSymbolInfo m_symbol;
COrderInfo m_order;
CAccountInfo m_account;

int MagicNumber;
bool Buy=0;
bool Sell=0;
int HedgeNow=10;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int NumberOfOrders()
 {
  int Count=0;
  for(int a=PositionsTotal()-1;a>=0;a--)
  {
   string symbol=PositionGetSymbol(a);
   if(Symbol()==symbol)
   {
    Count++;
   }
  }
  
  return(Count);
 } 
 
double Pips()
 {
  double PipPoint=0;
  int Digit=(int)SymbolInfoInteger(Symbol(),SYMBOL_DIGITS);
  if(Digit==1||Digit==2||Digit==3){PipPoint=Point()*10;}
  if(Digit==4||Digit==5){PipPoint=Point()*10;}
  return(PipPoint);
 }
 
void Trade()
 {
  double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK); 
  double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
  
  MqlRates Candle[];
  ArraySetAsSeries(Candle,true);
  int rates=CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),Candle);
  
  if(Candle[1].close<Candle[1].open)
  {
   trade.Buy(0.01,Symbol(),Ask,0,0,"TestHedge");
   
   if(Bid>HedgeNow*Pips())
   {
    Sell=trade.Sell(0.01,Symbol(),Bid,0,0,"TestHedge");
   }
  }
  
  if(Candle[1].close>Candle[1].open)
  {
   trade.Sell(0.01,Symbol(),Bid,0,0,"TestHedge");
   
   if(Bid<HedgeNow*Pips())
   {
    Buy=trade.Buy(0.01,Symbol(),Ask,0,0,"TestHedge");
   }
  }
 } 

void OnTick()
 {
  if(NumberOfOrders()<1){Trade();}

 }

 

 
Jack Buda: I want to hedge but my ea will only open trades both(buy & sell) at the same instead of 10 pips. please help if you know the problem

Your broken English question is unclear. Please explain your question in more detail.

Also your value from "HedgeNow*Pips()" is a price range in points, not a quote price. So it is not comparable to the Bid (or Ask) quote prices. They are of different units and scale.

EDIT: Also, your Pips() function is useless, as it returns "10 * Point" for both an odd and even number of digits.

 
Fernando Carreiro #:

Your broken English question is unclear. Please explain your question in more detail.

Also your value from "HedgeNow*Pips()" is a price range in points, not a quote price. So it is not comparable to the Bid (or Ask) quote prices. They are of different units and scale.

EDIT: Also, your Pips() function is useless, as it returns "10 * Point" for both an odd and even number of digits.

I want to hedge a trade by opening another trade, so for e.g if a buy trade is open & price goes against it then open Sell trade within 10 pips.
 
Jack Buda #: I want to hedge a trade by opening another trade, so for e.g if a buy trade is open & price goes against it then open Sell trade within 10 pips.

In that case, then first fix what I have already identified and then come back with the new code for further analyses, but fix the following as well ...

As it stands now, as soon as you place one trade, then no further processing is done because your Trade() function is only called when no positions exist.

So separate the code for the first trade from the code for the hedge trade so that it will still run after the first trade is placed.

Fix this too, before presenting your updated code.

 
Fernando Carreiro #:

In that case, then first fix what I have already identified and then come back with the new code for further analyses, but fix the following as well ...

As it stands now, as soon as you place one trade, then no further processing is done because your Trade() function is only called when no positions exist.

So separate the code for the first trade from the code for the hedge trade so that it will still run after the first trade is placed.

Fix this too, before presenting your updated code.

I've used this function a lot of times and I've not encountered any problems and it works fine on every pair (FX, XAU/USD & indices).

double Pips()
 {
  double PipPoint=0;
  int Digit=(int)SymbolInfoInteger(Symbol(),SYMBOL_DIGITS);
  if(Digit==1||Digit==2||Digit==3){PipPoint=Point()*10;}
  if(Digit==4||Digit==5){PipPoint=Point()*10;}
  return(PipPoint);
 }

If you know the problem please correct me where I'm wrong.

 
Fernando Carreiro #:

In that case, then first fix what I have already identified and then come back with the new code for further analyses, but fix the following as well ...

As it stands now, as soon as you place one trade, then no further processing is done because your Trade() function is only called when no positions exist.

So separate the code for the first trade from the code for the hedge trade so that it will still run after the first trade is placed.

Fix this too, before presenting your updated code.

Same problem as before

void Trade()
 {
  double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK); 
  double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
  
  MqlRates Candle[];
  ArraySetAsSeries(Candle,true);
  int rates=CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),Candle);
  
  double BuyHedge=Bid-HedgeNow*_Point;
  double SellHedge=Bid+HedgeNow*_Point;
  
  if(Candle[1].close<Candle[1].open)
  {
   Buy=trade.Buy(0.01,Symbol(),Ask,0,0,"TestHedge");
   
   if(Buy>0&&BuyHedge)
   {
    Sell=trade.Sell(0.01,Symbol(),Bid,0,0,"TestHedge");
   }
  }

  if(Candle[1].close>Candle[1].open)
  {
   Sell=trade.Sell(0.01,Symbol(),Bid,0,0,"TestHedge");
   
   if(Sell>0&&SellHedge)
   {
    Buy=trade.Buy(0.01,Symbol(),Ask,0,0,"TestHedge");
   }
  }
  
 } 
 
Jack Buda #: I've used this function a lot of times and I've not encountered any problems and it works fine on every pair (FX, XAU/USD & indices). If you know the problem please correct me where I'm wrong.
double Pips() {
  double PipPoints = _Point;
  switch( _Digits ) { case 1: case 3: case 5: PipPoints *= 10; };
  return PipPoints;
};

Please note, that you should only use Pips for those symbols that support it (e.g. Forex). Don't use it for other symbols (e.g. Stocks or Futures).

 
Jack Buda #: Same problem as before.
Obviously it is the same problem. You did not separate the code, and you also did not show any changes in OnTick().
 

Fernando Carreiro #:
Obviously it is the same problem. You did not separate the code, and you also did not show any changes in OnTick().

double Pips2()
 {
  double point=_Point;
  switch(_Digits){case 1:case 3:case 5:point*=10;}
  return(point);
 } 
 
void Trade()
 {
  double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK); 
  double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
  
  MqlRates Candle[];
  ArraySetAsSeries(Candle,true);
  int rates=CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),Candle);
  
  double BuyHedge=Bid-HedgeNow*Pips2();
  double SellHedge=Bid+HedgeNow*Pips2();
  
  if(Candle[1].close<Candle[1].open)
  {
   Buy=trade.Buy(0.01,Symbol(),Ask,0,0,"TestHedge");
  }

  if(Candle[1].close>Candle[1].open)
  {
   Sell=trade.Sell(0.01,Symbol(),Bid,0,0,"TestHedge");
  }
  
 } 
 
void Hedge()
 {
  double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK); 
  double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
  
  MqlRates Candle[];
  ArraySetAsSeries(Candle,true);
  int rates=CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),Candle);
  
  double BuyHedge=Bid-HedgeNow*Pips2();
  double SellHedge=Bid+HedgeNow*Pips2();
  
  if(Candle[1].close<Candle[1].open)
  {
   Sell=trade.Sell(0.01,Symbol(),Bid,0,0,"TestHedge");
  }

  if(Candle[1].close>Candle[1].open)
  {
   Buy=trade.Buy(0.01,Symbol(),Ask,0,0,"TestHedge");
  }
  
 } 

void OnTick()
 {
  if(NumberOfOrders()<1){Trade();}
  else if(NumberOfOrders()<2){Hedge();}
 }

Is this what you meant,tried change it to following but does not open a sell trade in the opposite direction

if(Buy>0&&BuyHedge)
{
 //Buy
}

if(Sell>0&&SellHedge)
{
//Sell
}
 
Fernando Carreiro #:
Obviously it is the same problem. You did not separate the code, and you also did not show any changes in OnTick().

I separated as you stated before, this time no sell trade of opening at all:

void Trade()
 {
  double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK); 
  double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
  
  MqlRates Candle[];
  ArraySetAsSeries(Candle,true);
  int rates=CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),Candle);
  
  double BuyHedge=Bid-HedgeNow*Pips2();
  double SellHedge=Bid+HedgeNow*Pips2();
  
  if(Candle[1].close<Candle[1].open)
  {
   Buy=trade.Buy(0.01,Symbol(),Ask,0,0,"TestHedge");
  }
  
  else if(Buy>0&&BuyHedge)
  {
   Sell=trade.Sell(0.01,Symbol(),Bid,0,0,"TestHedge");
  }

 } 

Ontick function i tried adding else if as my previous post but still the same as open buy & sell at the same time

void OnTick()
 {
  if(NumberOfOrders()<1){Trade();}
 }
 

I tried modifying that would not work out made changes but still opens at the same time instead of 10 pips apart.

void Trade()
 {
  double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK); 
  double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
  
  MqlRates Candle[];
  ArraySetAsSeries(Candle,true);
  int rates=CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),Candle);
  
  double BuyHedge=Bid-HedgeNow*Pips2();
  double SellHedge=Bid+HedgeNow*Pips2();
  
  if(Candle[1].close<Candle[1].open)
  {
   Buy=trade.Buy(0.01,Symbol(),Ask,0,0,"TestHedge");
  }
  
  if(Buy>0)
  {
   if(Bid>BuyHedge)
   {
    Sell=trade.Sell(0.01,Symbol(),Bid,0,0,"TestHedge");
   } 
  }
  
  if(Candle[1].close>Candle[1].open)
  {
   Sell=trade.Sell(0.01,Symbol(),Bid,0,0,"TestHedge");
  }
  
  if(Sell>0)
  {
   if(SellHedge<Bid)
   {
    Buy=trade.Buy(0.01,Symbol(),Ask,0,0,"TestHedge");
   } 
  }

 }