Close Condition

 

Hello

i use grid system 

between 2 trade gap is randomly

so i want close all condition on my first open trade price

when above 1 trade are open

Like 1 no trade profit 0

       2 no trade profit 1

      3 no trade profit 3

      4 no trade profit 9

plz help me

Example if 5 order open then  

example

 
Sumit Dutta :


Show your code please (use  Attach file button).

 
this is the code
Files:
MA_h1m.mq5  21 kb
 
Sumit Dutta #:
this is the code

I try some close condition but its not work

i want close its on price when hit first open trade

 
Sumit Dutta # :

***

i want close its on price when hit first open trade

:)

You cannot control the price! For example, you have a 'BUY' position at a price of 100. Now the price is '50'. If you wish to close a position at the moment, you will close that position at the price of '50'. This is clear?

 
Vladimir Karputov #:

:)

You cannot control the price! For example, you have a 'BUY' position at a price of 100. Now the price is '50'. If you wish to close a position at the moment, you will close that position at the price of '50'. This is clear


extample 3 open trade on 50,100,200 then current price come on 50 then close all trade

 

i want its FirstTradePrice

double LastTradePrice(int direction)
  {
   double result = 0;
   for(int i = PositionsTotal()-1; i >= 0; i--)
     {
      if(PositionGetTicket(i) <= 0) continue;
      if(PositionGetInteger(POSITION_TYPE) > 1) continue;
      if((direction < 0 && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) || (direction > 0 && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)) continue;
      if(PositionGetString(POSITION_SYMBOL) == Symbol() && PositionGetInteger(POSITION_MAGIC) == MagicNumber)
        {
         result = PositionGetDouble(POSITION_PRICE_OPEN);
         break;
        }
     } 
 
Sumit Dutta:

Hello

i use grid system 

between 2 trade gap is randomly

so i want close all condition on my first open trade price

when above 1 trade are open

Like 1 no trade profit 0

       2 no trade profit 1

      3 no trade profit 3

      4 no trade profit 9

plz help me

How interesting no trade profit 9.

 
Sumit Dutta # :

extample 3 open trade on 50,100,200 then current price come on 50 then close all trade

Now everything is clear, thanks for the clarification.

 
Vladimir Karputov #:

Now everything is clear, thanks for the clarification.

first trade on price 50

second trade on price 100 

third trade on price 200 

then price go down and hit the price of 50 then close all trade third trade profit 150 second trade profit 100 and first one trade profit 0 then close

Grid sir
 
Sumit Dutta # :

i want its FirstTradePrice

My algorithm is as follows: we go through all the positions and look at the position opening time, at the same time we save the position price.

//+------------------------------------------------------------------+
//| Calculate all positions                                          |
//+------------------------------------------------------------------+
void CalculateAllPositions(double &buy_price,double &sell_price)
  {
   buy_price            = 0.0;
   sell_price           = 0.0;
   datetime buy_time    = D'2090.01.01';
   datetime sell_time   = D'2090.01.01';
   for(int i=PositionsTotal()-1; i>=0; i--)
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic)
           {
            if(m_position.PositionType()==POSITION_TYPE_BUY)
              {
               if(m_position.Time()<buy_time)
                 {
                  buy_price=m_position.PriceOpen();
                  buy_time=m_position.Time();
                 }
              }
            else
              {
               if(m_position.Time()<sell_time)
                 {
                  sell_time=m_position.PriceOpen();
                  sell_price=m_position.Time();
                 }
              }
           }
  }