Can't see what's wrong

 

Hi all,


EA:

I can't get more than 1 long opened position per symbol at a time. So it is possible to open a short position if none is opened yet in that pair.

The second order can be executed by EA only if OrderOpenPrice() from the one already opened minus marketInfo() >= Pips_hedging


In other words, The EA can start hedging strategy only if marketPrice is (let's say 50 pips) away from the previous order opened.


The first part works because the EA opened one sell position but then hedging (most interesting part) doesn't work.

// none opened orders yet

    if (  (countLongTrades() == 0) && (countShortTrades() == 0) ){
    
            if (
                (Close_Candle_1 > Open_Candle_1) && ((Close_Candle_1 - Open_Candle_1)*10000 >= Pips_Trigger) )
           {openLongTrade();}        
         
            if (
               (Close_Candle_1 < Open_Candle_1) && ((Close_Candle_1 - Open_Candle_1)*(-10000) >= Pips_Trigger) )
           {openShortTrade();}
     
                   }
                  
                  
// If one long position is currently running...                
                  
      if (  (countLongTrades() == 1) && (countShortTrades() == 0) ){    
      
      for (int z = OrdersTotal()-1; i >= 0; z --) {
      OrderSelect(z, SELECT_BY_POS, MODE_TRADES);
      
      if (
      OrderSymbol() == Symbol() && OrderMagicNumber() == magicalNumber && OrderType() == OP_BUY && MarketInfo(Symbol(),MODE_BID)-OrderOpenPrice() >= Pips_Hedging && 
      (Close_Candle_1 < Open_Candle_1) && ((Close_Candle_1 - Open_Candle_1)*(-10000) >= Pips_Trigger) ) {
      {openShortTrade();} 
      
      }}}
              
                  
                  
// If one short position is currently running...                  
                  
      if (  (countLongTrades() == 0) && (countShortTrades() == 1) ){    
      
      for (int x = OrdersTotal()-1; x >= 0; x --) {
      OrderSelect(x, SELECT_BY_POS, MODE_TRADES);
      
      if (
      OrderSymbol() == Symbol() && OrderMagicNumber() == magicalNumber && OrderType() == OP_SELL && MathAbs(MarketInfo(Symbol(),MODE_ASK)-OrderOpenPrice()) >= Pips_Hedging && 
      (Close_Candle_1 > Open_Candle_1) && ((Close_Candle_1 - Open_Candle_1)*10000 >= Pips_Trigger) ) {
      {openLongTrade();}  
      
      }}}



Please provide easy advice as I'm not a killer programmer : )

Best regards

 

What error messages are you getting in the Expert_Log Tab or Journal Tab?

 

Too many braces and code is not layed-out properly. Include some Print statements for debugging purposes. I modified below one for checking Long hedge. You can do a similar one.

// If one long position is currently running...                
if( countLongTrades()==1 && countShortTrades()==0 )
{ 
   Print("Long trade found. Checking hedging condition.");
   for (int z=OrdersTotal()-1; i >= 0; z --) 
   {
      OrderSelect(z, SELECT_BY_POS, MODE_TRADES);
      Print("INFO: OrderOpenPrice: ",OrderOpenPrice()," Current Bid: ",Bid);  // you can print more values here to debug
      if( OrderSymbol() == Symbol() && OrderMagicNumber() == magicalNumber && OrderType() == OP_BUY 
         && MarketInfo(Symbol(),MODE_BID)-OrderOpenPrice() >= Pips_Hedging 
         && Close_Candle_1 < Open_Candle_1 
         && ( (Close_Candle_1 - Open_Candle_1)*(-10000)>=Pips_Trigger )               
         )
      { 
         Print("Hedging condition met. Opening short trade.");
         openShortTrade(); 
      } 
   }
} // END: if()
 

Hi,

Thank you all for helping.

The EA opened a short position while I was flat.

Then in tab expert I got "Short trade found. Checking hedging condition." because we are in the third case:

// If one short position is currently running...                
if( countLongTrades()==0 && countShortTrades()==1 )

So it's ok untill here!


Then I should get in the expert tab the following Print:

<< Print("INFO: OrderOpenPrice: ",OrderOpenPrice()," Current Bid: ",Bid) >> but it doesn't appear.


So I guess that the code is not working from...

   for (int z=OrdersTotal()-1; i >= 0; z --) 
   {
      OrderSelect(z, SELECT_BY_POS, MODE_TRADES);
      Print("INFO: OrderOpenPrice: ",OrderOpenPrice()," Current Bid: ",Bid);  // you can print more values here to debug
      if( OrderSymbol() == Symbol() && OrderMagicNumber() == magicalNumber && OrderType() == OP_BUY 
         && MarketInfo(Symbol(),MODE_BID)-OrderOpenPrice() >= Pips_Hedging 
         && Close_Candle_1 < Open_Candle_1 
         && ( (Close_Candle_1 - Open_Candle_1)*(-10000)>=Pips_Trigger )               
         )
      { 
         Print("Hedging condition met. Opening short trade.");
         openShortTrade(); 
      } 
   }



Could you please help a bit more?

Cheers

 
FrenchyTrader:

So I guess that the code is not working from...

What error message you got.?? Are you sure that countLongTrades() countShortTrades() are returning correct values

Your code has a error. Below I pointed out one instance. Cross check complete code again for like errors.

for (int z=OrdersTotal()-1; i >= 0; z --)  // what variable "i" is doing here.???

Also do below:

Replace 

OrderSelect(z, SELECT_BY_POS, MODE_TRADES);

with 

if( !OrderSelect(z, SELECT_BY_POS, MODE_TRADES) ) { Print("ERR: While selecting order# ",z," error is: ",GetLastError()); }
 
FrenchyTrader:
In other words, The EA can start hedging strategy only if marketPrice is (let's say 50 pips) away from the previous order opened.
Remember that hedging is forbedden in US
 
dineshydv:

What error message you got.?? Are you sure that countLongTrades() countShortTrades() are returning correct values

Your code has a error. Below I pointed out one instance. Cross check complete code again for like errors.

Also do below:



Here we go... that works.

Thank you a lot : )

 
WHRoeder:
Remember that hedging is forbedden in US

Yes I know that... but I don't care because in France as long as we trade on MetaTrader 4 it is allowed : )

Remember that if it has been forbidden it's probably because they want little trader like us to loose money on fundamental moves (or spike).

Hedging is the best strategy to manage account and not loose money... a natural way to make your equity stable... until you feel confident with the real direction of pair.... keeping the good position, let it growth and hedging again later on with a lotsize bigger.

If you see what I mean, you can simulate it on Excell... this can be very impressive with good technical analysis.

But if you are american... you can do hedging on different account ( 2 brokers... : ))

Good luck and thank you anyway.