Piece of code that doesn't open 2 positions on the same price range

 

Hi all,


I'm coding an EA that locks into a custom indicator to give me signals and i want to include a if function which says to not to open anymore positions if the price of the new positions is equal or inside of a price range of another position.

For example EUR/USD I have a sell order at: 1.1000 and few minutes later my indicator gives me another sell position at: 1.1005 or even at: 1.010. If the price is on the new trade is inside of a let's say 50 pips difference from a previous open trade then i don't want my EA to open another order.

Basically is there any code that i can limit the number of new open positions?

I leave here a photo for an example.

If you see the photo my EA opened the 1st BUY trade, perfect.. then the price went even more down and the EA opened a 2nd position which is fine because there is a 200 pips difference but then it opened a 3rd position which have a 20 pips difference from my 2nd position. This is where I want to create a rule to limit this positions that are to close.


Many Thanks!!

Joao Santo

Files:
order.JPG  80 kb
 
  1. Define in concrete terms what you mean by "price range." Unless you can do that, it can't be coded.
  2. Go through your open orders and see if the market is in "price range" of any open orders.
  3. Show us your attempt (using the CODE button) and state the nature of your problem.
              No free help 2017.04.21

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum 2018.05.12

 

So i tried a few different codes but so far none of them work.

With this code, the EA opens the 1st positions and it crashes when it goes to open a second position with error : EATM5 EURUSD,H1: array out of range in 'Customfunctions.mqh' (202,17).

This is the function i created where if a new order comes along and it's between a price range from a previous order then the EA will no open new positions.

What i mean with price range is example:

Order #1 EUR/USD BUY at 1.1500 ----- FIRST ORDER COMES ALONG - PERFECT!!

Order #2 EUR/USD BUY at 1.0500 ----- THIS ORDER SHOULDN'T BE OPEN BECAUSE IT AS 10 PIPS DIFFERENCE

Order #3 EUR/USD BUY at 1.0000 ----- THIS ORDER CAN BE OPEN BECAUSE IT AS MORE THAN 10 PIPS DIFFERENCE FROM ORDER #1

The example is 10 pips price range but it can be 20 or 30 depending on what the user want. The same example also applies if the price goes up...

Order #4 EUR/USD BUY at 1.2000 ----- NO, THIS TRADE SHOULD NOT GO IN BECAUSE IT AS ONLY 5 PIPS DIFFERENCE FROM ORDER #1

Order #5 EUR/USD BUY at 1.3000 ----- IT'S OKEY, THIS ORDER CAN BE OPEN BECAUSE IT AS MORE THAN 10 PIPS DIFFERENCE FROM ORDER #1

It also applies for SELL orders.

//+------------------------------------------------------------------+
//| Don't allow to open new positions inside a price range           |
//+------------------------------------------------------------------+
bool NewOrderPriceRange (int magicNumber)
{
   RefreshRates();
   double OrderPrice[];
   int    NewOrder[];
   //Start a loop to scan all the orders
   for(int i=(OrdersTotal()-1);i>=0;i--)
   {
      //If the order cannot be selected throw and log an error
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
      {
         Print("ERROR - Unable to select the order - ",GetLastError()); break;
      } 
      if (OrderSymbol() != _Symbol || OrderMagicNumber() != magicNumber) continue;
      if (OrderSymbol() == _Symbol && OrderMagicNumber() == magicNumber)

      OrderPrice[i] = OrderOpenPrice();

         if (OrderPrice[i] > PipsBotAllowed(OrderPrice[i-1]) && OrderPrice[i] < PipsTopAllowed(OrderPrice[i-1]))
         {
            return false; // when false the EA should not open a new trade
         }        
   }
   return true; // new trade is allowed to be open
}


This is another function to calculate the price range to the top and to the bottom. but i don't get any error here. just putting the code so you guys can have a better idea.

//+------------------------------------------------------------------+
//| Checks the open price difference for a new order (TOP)           |
//+------------------------------------------------------------------+
double PipsTopAllowed(double OrderPrice)
{
   int    pipsDifference = 10;
   double priceTopAllowed;
   priceTopAllowed = OrderPrice + pipsDifference * Getpipvalue();
   return NormalizeDouble(priceTopAllowed,_Digits);
}
//+------------------------------------------------------------------+
//| Checks the open price difference for a new ordeR (BOTTOM)        |
//+------------------------------------------------------------------+
double PipsBotAllowed(double OrderPrice)
{
   int    pipsDifference = 10;
   double priceBotAllowed;
   priceBotAllowed = OrderPrice - pipsDifference * Getpipvalue();
   return NormalizeDouble(priceBotAllowed,_Digits);
}


Then on my main code this is the If function is using to decide if a new order should be open.

 if ( (bunch of code...) && NewOrderPriceRange(MagicNumber) == true)
        {
                // BUY ORDER
                 EnterTrade(true); //true means its a buy order 
        }


Hope all of this help you guys to understand my problem.

Many Thanks

 
João Santo:

Do not double post!

It is selfish and wastes other people's time.

I have deleted your duplicate topic.

 
Keith Watford:

Do not double post!

It is selfish and wastes other people's time.

I have deleted your duplicate topic.

can you help me then??

 
Keith Watford:

Do not double post!

 This is why i made a new post because so far no one answer my question!!

 
  1. Don't double post! You already had this thread open.

  2. João Santo:
    Order #1 EUR/USD BUY at 1.1500 ----- FIRST ORDER COMES ALONG - PERFECT!!
    Order #2 EUR/USD BUY at 1.0500 ----- THIS ORDER SHOULDN'T BE OPEN BECAUSE IT AS 10 PIPS DIFFERENCE
    Order #3 EUR/USD BUY at 1.0000 ----- THIS ORDER CAN BE OPEN BECAUSE IT AS MORE THAN 10 PIPS DIFFERENCE FROM ORDER #1
    1.1500 -  1.0500 = 0.1000 or 1000 PIPs not 10. 1.1500 - 1.0000 = 0.1500 or 1500 PIPs.

    PIP, Point, or Tick are all different in general.
              What is a TICK? - MQL4 programming forum

    Unless you manually adjust your SL/TP for each separate symbol, using Point means code breaks on 4 digit brokers, exotics (e.g. USDZAR where spread is over 500 points,) and metals. Compute what a PIP is and use it, not points.
              How to manage JPY pairs with parameters? - MQL4 programming forum
              Slippage defined in index points - Currency Pairs - Expert Advisors and Automated Trading - MQL5 programming forum

 
William Roeder:
  1. 1.1500 -  1.0500 = 0.1000 or 1000 PIPs not 10. 1.1500 - 1.0000 = 0.1500 or 1500 PIPs.

    PIP, Point, or Tick are all different in general.

Well what i really meant was 1.1510 - 1.1500 = 10PIPs.. i just wanted to make it more simple for a easy understanding..
But the code is correct in terms of pips and digits.

 

Guys any clue how i can put this code working?

Thanks!

Reason: