Calculating maxvalue and minvalue of a position

 

Is this code correct to calculate the maximum or minimum price achived by a position? I Used OrderOpenPrice to verifiy if a position is opened. From this max e min values I would like to code a trailing stop. Is it possible?

Best regards.

int maxvalue()
   {  
   double Current_high;    //new high detected higher than previous bar high, after opened position
   double Previous_high;   //highest high after opened position
   
   if(OrderOpenPrice()>0)  //checking for opened position
      {
       Current_high = High[0];
       Previous_high = Current_high;
       if(High[0]>High[1])    //checking if current bar high is higher than previous bar high
          {
           Current_high=High[0]; 
             {
              if(Current_high > Previous_high)   //if current high is higher than previous bars high,
                {
                 Previous_high = Current_high;   //previous high gets the value of current high
                 return(Previous_high);
                }
             }
          }
       }
    }


int minvalue()
   {  
   double Current_low;     //new low detected lower than previous bar low, after opened position
   double Previous_low;    //lowest low after opened position
   
   if(OrderOpenPrice()>0)  //checking for opened position
      {
       Current_low = Low[0];
       Previous_low = Current_low;
       if(Low[0]<Low[1])    //checking if current bar low is lower than previous bar low  
          {
           Current_low=Low[0]; 
             {
              if(Current_low < Previous_low)   //if current low is lower than previous bars low,
                {
                 Previous_low = Current_low;   //previous low gets the value of current low
                 return(Previous_low);
                }
             }
          }  
       }   
    }
 

to use OrderOpenPrice() Order must be first selected by the OrderSelect() function.

 
qjol:

to use OrderOpenPrice() Order must be first selected by the OrderSelect() function.


Yes, I agree, the function maxvalue() would be called on checking if a long position should be closed after:

for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&       // check for opened position 
         OrderSymbol()==Symbol() &&    // check for symbol
         OrderMagicNumber() == MAGICMA)// check for magic number
        {
         if(OrderType()==OP_BUY)   // long position is opened
           {
            // should it be closed?

I guess it would be correct.

But what I don't know is if the functions code I mentioned maxvalue() and minvalue() should work correctly.

 

I do not understand what you are trying to accomplish

 
  1. if(Current_low < Previous_low)   //if current low is lower than previous bars low,
    {
       Previous_low = Current_low; 
    Previous_low must be declared static.
  2. Current_low = Low[0];
    Previous_low = Current_low;
    
    This invalidate Previous_low
  3. Is this code correct to calculate the maximum or minimum price achived by a position

    Why not count=IBarShift(OrderOpenTime()); H=High[Highest(count+1,0)]

 
WHRoeder:
  1. Previous_low must be declared static.
  2. This invalidate Previous_low
  3. Is this code correct to calculate the maximum or minimum price achived by a position

    Why not count=IBarShift(OrderOpenTime()); H=High[Highest(count+1,0)]


Does it returns the maxprice or minprice the open position achieved? To code a trailingstop I need to know what was the max or min price an open position achieved in order to close the position if the price moves back a certain amount in the direction of the OpenPrice.

Thanks for the help.

Best regards.

 
int   OpenBar=iBarShift(NULL,1,OrderOpenTime(),false); // the bar at which the order was opened
int   CloseBar=0; // Assume the selected order is still open, verify assumption in next step
//
if(OrderCloseTime()!=0) CloseBar=iBarShift(NULL,1,OrderCloseTime(),false); // This captures a closed order such that the searched bar range is the correct span of time
//
int   LowestBar=iLowest(NULL,1,MODE_LOW,OpenBar-CloseBar+1,CloseBar); // find the bar that had the lowest market price
int   HighestBar=iHighest(NULL,1,MODE_HIGH,OpenBar-CloseBar+1,CloseBar); // find the bar that had the highest market price
//
double Minimum_Price = iLow(NULL,1,LowestBar); // the lowest market price that occurred in the timespan of the order
double Maximum_Price = iHigh(NULL,1,HighestBar); // the highest market price that occurred in the timespan of the order
The caveat with this code is the high and low prices for the opening bar are considered "viable" price extremes when that could possibly be false. For instance if you opened a trade in an M1 candle after the low price for that candle had already been reached and it turned out that the low price of that position-opening bar also happened to be the lowest price the market reached over the ensuing lifespan of the position then the above code would capture that opening bar low value and assume it was the lowest price the trade had experienced (which wouldn't be true).
Reason: