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

```#property copyright "Copyright 2022, MetaQuotes Ltd."
#property version   "1.00"
#property strict

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

int MagicNumber;
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);
}

{
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)
{

if(Bid>HedgeNow*Pips())
{
}
}

if(Candle[1].close>Candle[1].open)
{

if(Bid<HedgeNow*Pips())
{
}
}
}

void OnTick()
{

}```

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 #:

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 Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);

MqlRates Candle[];
ArraySetAsSeries(Candle,true);
int rates=CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),Candle);

double SellHedge=Bid+HedgeNow*_Point;

if(Candle[1].close<Candle[1].open)
{

{
}
}

if(Candle[1].close>Candle[1].open)
{

if(Sell>0&&SellHedge)
{
}
}

} ```

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);
}

{
double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);

MqlRates Candle[];
ArraySetAsSeries(Candle,true);
int rates=CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),Candle);

double SellHedge=Bid+HedgeNow*Pips2();

if(Candle[1].close<Candle[1].open)
{
}

if(Candle[1].close>Candle[1].open)
{
}

}

void Hedge()
{
double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);

MqlRates Candle[];
ArraySetAsSeries(Candle,true);
int rates=CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),Candle);

double SellHedge=Bid+HedgeNow*Pips2();

if(Candle[1].close<Candle[1].open)
{
}

if(Candle[1].close>Candle[1].open)
{
}

}

void OnTick()
{
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)
{
}

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 Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);

MqlRates Candle[];
ArraySetAsSeries(Candle,true);
int rates=CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),Candle);

double SellHedge=Bid+HedgeNow*Pips2();

if(Candle[1].close<Candle[1].open)
{
}

{
}

}

```

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()
{
}```

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 Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);

MqlRates Candle[];
ArraySetAsSeries(Candle,true);
int rates=CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),Candle);

double SellHedge=Bid+HedgeNow*Pips2();

if(Candle[1].close<Candle[1].open)
{
}

{
{
}
}

if(Candle[1].close>Candle[1].open)
{