Open trade Buy/Sell at same or 1pips away from MA price.

 
double Var1 = iMA(NULL, PERIOD_M15, 5, 0, MODE_LWMA, PRICE_HIGH, 0);

double Var2 = iMA(NULL, PERIOD_M15, 5, 0, MODE_LWMA, PRICE_LOW, 0);

Hi, from above code i want to execute Buy on this condition:

The current open candle are same or just 1pips higher from  current Var2 iMA, and difference between Var1 and Var2 are same or higher than 12 pips.

 I have try this code :

if (Open[0] == Var2 && (Var1 - Var2)>=12) Order = SIGNAL_BUY; 

But Buy trade never happen.

Hope you can give me the correct code for Buy 

 

You need to convert your pip value to that relevant for the symbol.

On EURUSD, you don't want the difference to be at least 12, you want it to be 0.0012

   if(Digits==3 || Digits==5)
     {
      PipDecimal=Point*10;
     }
   else
     {
      PipDecimal=Point;
     }

 and

   if ((Open[0]-Var2<=PipDecimal && Open[0]>=Var2 ) && (Var1 - Var2)>=12*PipDecimal) Order = SIGNAL_BUY; 
 
GumRai:

You need to convert your pip value to that relevant for the symbol.

On EURUSD, you don't want the difference to be at least 12, you want it to be 0.0012

 and

Thanks for your quick reply.

Its working for now, i'll keep on backtest and if there is any problem i'll come back.

Thanks again. 

 
GumRai:

You need to convert your pip value to that relevant for the symbol.

On EURUSD, you don't want the difference to be at least 12, you want it to be 0.0012

 and

Hi,

here my new question:

The problem is, it will open 1 trade and have wait to close even there is another strategies met.

I want it to open trade everytime the strategies met But limit to 3 trades running.

Should i make adjustment here? :

//Check position

   bool IsTrade = False;


   for (int i = 0; i < Total; i ++) {

      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

      if(OrderType() <= OP_SELL &&  OrderSymbol() == Symbol()) {

         IsTrade = True;

         if(OrderType() == OP_BUY) {

            //Close  

 
Sorry, but I have no idea what you are trying to achieve
 
GumRai:
Sorry, but I have no idea what you are trying to achieve

Sorry.. Let me try to explain it again.

The simplest way is... I want my EA to executed trade everytime the condition are met, which is :  if (Open[0] == Var2 && (Var1 - Var2)>=12) Order = SIGNAL_BUY;

even there are already running trade for that pair BUT LIMIT to 3 running trade per pair.

The problem now: 

Lets say on GBP/USD there are 1 trade executed by EA which is BUY, The EA wait till this trade to close before open new trade even theres a condition met while the 1st trade running.

So, i want EA to open trade even there are still running trade executed BUT LIMIT to 3 trade only at one time.

I Have use this code but it just keep opening new trade without LIMIT to only 3 trade.

 //Check position

   bool IsTrade = False;

    for (int i = 0; i > Total; i ++) {

      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

      if(OrderType() <= OP_SELL &&  OrderSymbol() == Symbol()) {

         IsTrade = True;

         if(OrderType() == OP_BUY) {

            //Close  

I hope my EA can LIMIT to 3 trade running per pair.

 

Hope this explaination helps you to help me..peace! 

 
int counter=0;

 Loop through the orders and when you find a trade

 if(OrderType() <= OP_SELL &&  OrderSymbol() == Symbol())
   {
    counter++;
    //
   }

 then

 if(counter<MaxTrades)     //better set as an input variable
   {
    //Code to check for trade conditions
    //Code to place order if conditions are true
   }

 Because you will only need to check the indicators  and send an order if there are less than 3 trades open.

 
GumRai:

 Loop through the orders and when you find a trade

 then

 Because you will only need to check the indicators  and send an order if there are less than 3 trades open.

 

Yup! Its Work..

Thanks a lot...

I'll continue on testing