I NEED A HELP IN CODING HEDGE

 

HELLO EVERY ONE

HOPE 2016 WILL BE A GREAT YEAR SAME THE 2015

:)

 

I AM A NEW PROGRAMMER IN MQL4

I AM C/C++, JAVA, INTERNED DEVELOPER ... 

ONLY A SINGLE QUESTION PLZ

 

CAN ANYONE INFORM ME THE CODE OF HEDGING IN MQL4 LANGUAGE:

I WANT TO MAKE IT AS AN INPUT FROM USER TO ENABLE OR DISABLE THE HEDGING WITH SAME PAIR

 

PLZ LOOK AT THIS CODE HERE:

 

 

//This function will be used to manage the opened positions by this EA only by calling magic number and to increase the trades when needed...

extern int total_positions = 1;
extern bool enable_hedge = true;
int magic = 1982;

bool trade_by_magic(int magic, int type = -1)
{
   for (int cnt = total_positions-1; cnt < OrdersTotal(); cnt++)
   {
      if (!OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)) continue;
      if (OrderMagicNumber() == magic && (OrderType() == type || type == -1))
      return (true);
   }
   return (false);
}

void OnTick()
{
   if (!trade_by_magic(magic))
   {
      //.....
   }
}

 

THE CODE ABOVE WILL OPEN ONLY THE ALLOWED TRADES COUNT ENTERED BY USER (1 AS THE CODE)

BUT...

THE (EXTERN BOOL ENABLE_HEDGE) HAS THE TRUE VALUE WITHOUT CODE

THIS CODE WHAT I NEED

IN THE CASE ABOVE ... THE NUMBER 1 FOR THE ENABLED TRADES WILL BE ALSO MULTIPLIED (IN HEDGE ENABLING) WITH 2 TO OPEN A REVERSE TRADE WITH OUT CLOSING THE 1ST ONE

 
hello!
 

try to put two copies of them in two charts one for buying only and the another for selling only

 
Mohammad Soubra:

try to put two copies of them in two charts one for buying only and the another for selling only

no Soubra...

this is not the solution

if you understand the code correctly you will find that the:

extern int total_positions = 1;

 is allowing trades by quantity not by hedge

so...

thanks for your reply

I hope some one will give the trick soon

 

SEEMS ALL PEOPLE ARE BUSY TO CELEBRATE THE 2016 

 
Mitsubishi4arab:

no Soubra...

this is not the solution

if you understand the code correctly you will find that the:

 is allowing trades by quantity not by hedge

so...

thanks for your reply

I hope some one will give the trick soon

 

SEEMS ALL PEOPLE ARE BUSY TO CELEBRATE THE 2016 

dear

I understand your code 100%

my point of view is to put the same EA in two charts but by changing the magic number

you have the magic number 1982

let it to be 19820 and 19821 or something else... 

 

The code you are looking for could be different than the one in mind or I used to use since years ago.

Let us know what exactly you want to do, like ... Do you want to open both long and short orders at once if hedge is enabled ...

or just, enabling the other order when a specific condition is  happen.

If both are generated together, then, it is enough to check that there are no orders are opened using a code like this ...

and the condition is ... if(!ExistPositionBuy() && !ExixstPositionSell()) ... then you may open both orders at once ...

or you can inspect if any order is opened and prevent to open the other one if Hedging is not allowed ...

By the way ... both functions can be mixed into one to do the same taskby passing Magic number to it as an argument,

like ... bool ExistPosition(int magic){ ... } ... then call it twice, each time with different magic number and inspect returned results.

Anyway, ideas are endless but always programmer need to know the exact idea needed to be coded to give the right solution. 

bool ExistPositionBuy() {
   for (int i=0; i<OrdersTotal(); i++) {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol()==Symbol() && OrderMagicNumber()==MagicBuy) {
            return(true);
         }
      } 
   } 
   return(false);
}

bool ExistPositionSell() {
   for (int i=0; i<OrdersTotal(); i++) {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol()==Symbol() && OrderMagicNumber()==MagicSell) {
            return(true);
         }
      } 
   } 
   return(false);
}

 I can go deeply in the matter - if you like - after getting to know what exactly you want to do concerning opening orders.