min max profit/loss before order closure - page 2

 
Dohung:

What I propose is


OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES);

{

max = MathMax(max,OrderProfit());

min = MathMin(min,OrderProfit());

}
 

.

I'm having some ideas using the:

 OpenBar=iBarShift(NULL,PERIOD_M1,OrderOpenTime(),false);

And than make a iHigh() and iLow() for know the maximum and minimum...

What do you say?

.

 
strutch:
 OpenBar=iBarShift(NULL,PERIOD_M1,OrderOpenTime(),false);
And than make a iHigh() and iLow() for know the maximum and minimum...

You need OpenBar, Highest and High[] (or iHigh()). Then you could use (ignoring Commissions and Swap:)

double dvpl = DeltaValuePerLot(),
       oop  = OpenOrderPrice(), 
       ls   = OrderLotSize(),
       profitAtHH = (HH-OOP) * ls*dvpl;
See my code
 

Like this???

for(int pos = OrdersTotal() - 1; pos >= 0; pos--) 
   {
   if( OrderSelect(pos, SELECT_BY_POS)&&OrderMagicNumber()== SHORT_POSITIONS_MAGIC_NUMBER &&OrderSymbol()== Symbol() ) 
      {
      Max_Profit_Short = MathMax(max,OrderProfit()); 
      }
   } 

I belive (in my very humble opinion!!) that this code:

--> Select my short position by it's MAGIC_NUMBER (I've 1 Magic for short positions and other for long positions).

--> Discovers it's Profit and compare it with the previous

--> If the profit it's bigger than it keeps that value.

The problem I see is when that order is closed and another one opens. The Max_Profit_Short will only change when the profit is bigger than the previous.

I'm I wrong?

Thanks a lot for the help!!! :)

.

 
 
strutch:

Like this???

I belive (in my very humble opinion!!) that this code:

1. --> Select my short position by it's MAGIC_NUMBER (I've 1 Magic for short positions and other for long positions).

2. --> Discovers it's Profit and compare it with the previous

3. --> If the profit it's bigger than it keeps that value.

4. The problem I see is when that order is closed and another one opens. The Max_Profit_Short will only change when the profit is bigger than the previous.

I'm I wrong?

Thanks a lot for the help!!! :)

1. Why ? what is wrong with using OrderType() ?

2. What is max ?

3. If the profit is bigger than max

4. When an order is closed and a new one opens reset Max_Profit_Short to 0.0

 

.

1 . Because to analyse the shorts and longs separated I thought that would be a nice thing to do...

2 - Yes.. My mistake... I want to write:

Max_Profit_Short = MathMax(Max_Profit_Short ,OrderProfit()); 


3 - Answerd in 2.... :)

4 - Because I'm too newbie I will have to think in a solution to that...

One problem closed opens other!!! That's the newbie life...

 
strutch:

Hi deVries,

I'm learning MQL4 language but I din't learn yet the use of arrays in Expert Advisors (I couldn't find nothing about arrays like the codeguru's course..).

That's why I'm stucked in this problem.. :(

WHRoeder code help me to make a cicle than runs all orders and select the one I want (by it's MagicNumber). But I don't know how to find that order Maximum Profit! : (

Any ideas?

.


Why do you think you need an array to do this ??

Come on, this is no attempt yours to make it....

You have done nothing, you are here only asking in the hope someone is gonna make it

Hint

//+------------------------------------------------------------------+
//|                                                 OrderClosure.mq4 |
//|                                Copyright © 2012, Tjipke de Vries |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, Tjipke de Vries"
#property link      ""

extern double  MinProfit = 345.00;
extern double  MaxLoss = 1335.00;
extern int     MagicNumber = 34567;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
       for(int pos = OrdersTotal() - 1; pos >= 0; pos--) { // Find my open order,
        if( OrderSelect(pos, SELECT_BY_POS)             // with my magic
        &&  OrderMagicNumber()  == MagicNumber          // number, in my chart.
        &&  OrderSymbol()       == Symbol() ) 
         {
//          .........Make your code
          
          
          
          
          
          }
        }  
//----
   return(0);
  }
//+------------------------------------------------------------------+

.

for every trade

Profit = OrderProfit() + OrderCommission() + OrderSwap();

So total will be if you sum it Profit = ........ + OrderProfit() + OrderCommission() + OrderSwap();
And before you begin the loop you have to set Profit to zero..

So for you to work on..... then you can ask again

 

.

deVries, I'm not the lazy guy you are thinking! I simply didn't know how to start...

Now that you gave me the ideas and here it is... My atempt to make the code.

I've no time to test it because I've got to sleep!!!

Tomorow I'll try to insert this "blocks" in my EA to see the results..

Once again, Thank you for your help guys! :)

/////////////////////////////////////////////////////////////////////////////////////////
//
// External Variables
//
/////////////////////////////////////////////////////////////////////////////////////////
   
extern int     Magic_UP = 1234;             // Magic Number long orders   
extern int     Magic_DOWN = 4321;           // Magic Number short orders   
extern int     DIST_TSL = 10;               // Distance for trailing stop calculation period      
extern int     Protection_Level             // Profit (in PIPS) that takes the stop to break-even



/////////////////////////////////////////////////////////////////////////////////////////
//
// Calculate the Biggest profit of the open orders:
//
/////////////////////////////////////////////////////////////////////////////////////////


double Max_Profit_Short, Max_Profit_Long;                          // Declare Variables,


// 1 - Biggest profit of short open orders:

    for(int pos = OrdersTotal() - 1; pos >= 0; pos--) 
       {                                                        // Find my open order,
       if( OrderSelect(pos, SELECT_BY_POS)                      // with my magic
       &&  OrderMagicNumber()  == MagicNumber_DOWN              // number, in my chart.
       &&  OrderSymbol()       == Symbol() ) 
         {
         
         // Find if this is an old or a new order (duration > 1):
         if( iBarShift(NULL,0,OrderOpenTime(),false) > 1 )
            {
            // If it is an old order
            // Checks if the order profit is bigger than the last tick,
            // if so, keeps the new value of profit:
            double Max_Profit_Short = MathMax(Max_Profit_Short ,(OrderProfit()+OrderCommission()+OrderSwap()) );
            }
         else
            // Else (it is a new order) reset Max_Profit_Short
            {
            double Max_Profit_Short = (OrderProfit()+OrderCommission()+OrderSwap()); 
            }
         
         }


// 2 - Biggest profit of long open orders:

    for(int pos = OrdersTotal() - 1; pos >= 0; pos--) 
       {                                                        // Find my open order,
       if( OrderSelect(pos, SELECT_BY_POS)                      // with my magic
       &&  OrderMagicNumber()  == MagicNumber_UP                // number, in my chart.
       &&  OrderSymbol()       == Symbol() ) 
         {
         
         // Find if this is an old or a new order (duration > 1):
         if( iBarShift(NULL,0,OrderOpenTime(),false) > 1 )
            {
            // If it is an old order
            // Checks if the order profit is bigger than the last tick,
            // if so, keeps the new value of profit:
            double Max_Profit_Long = MathMax(Max_Profit_Long ,(OrderProfit()+OrderCommission()+OrderSwap()) );
            }
         else
            // Else (it is a new order) reset Max_Profit_Short
            {
            double Max_Profit_Long = (OrderProfit()+OrderCommission()+OrderSwap()); 
            }
         
         }



/////////////////////////////////////////////////////////////////////////////////////////
//
// CALCULATE TRAILING STOP LOSS
//
/////////////////////////////////////////////////////////////////////////////////////////



// Protection Stop Loss for long and short orders:

double Protect_Stop_DOWN =   NormalizeDouble( OrderOpenPrice() , Digits);
double Protect_Stop_UP = NormalizeDouble( OrderOpenPrice() , Digits);

// Normal Trailing Stop Loss for long and short orders:

double TSL_DOWN = NormalizeDouble(iHigh(NULL,0,iHighest(NULL,0,MODE_HIGH,DIST_TSL,1))+ATR/3,Digits);      // Stop loss for short Positions
double TSL_UP =   NormalizeDouble(iLow(NULL,0,iLowest(NULL,0,MODE_LOW,DIST_TSL,1))-ATR/3,Digits);         // Stop loss for long Positions


/////////////////////////////////////////////////////////////////////////////////////////
//
// MODIFY TRAILING STOP LOSS
//
/////////////////////////////////////////////////////////////////////////////////////////



   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      
         if(OrderMagicNumber()==Magic_UP)                           // Select long order
           {
            if( (Max_Profit_Long/(Point*10)) >= Protection_Level )                                    
               {         
                OrderModify(OrderTicket(),OrderOpenPrice(),MathMax(Protect_Stop_UP ,TSL_UP),OrderTakeProfit(),0,Green);   
                return(0);
               }
            else
               {         
                OrderModify(OrderTicket(),OrderOpenPrice(),MathMax(TSL_UP),OrderTakeProfit(),0,Green);   
                return(0);
               }
           } 
         else                                                      
            {
             if(OrderMagicNumber()==Magic_DOWN)                    // Select short order
               {
               if( (Max_Profit_Long/(Point*10)) >= Protection_Level )                                    
                 {         
                  OrderModify(OrderTicket(),OrderOpenPrice(),MathMax(Protect_Stop_DOWN ,TSL_DOWN),OrderTakeProfit(),0,Green);   
                  return(0);
                 }
             else
                 {         
                  OrderModify(OrderTicket(),OrderOpenPrice(),TSL_DOWN,OrderTakeProfit(),0,Green);   
                  return(0);
                 }
            } 
     }
     
      
/////////////////////////////////////////////////////////////////////////////////////////
//
// 
//
/////////////////////////////////////////////////////////////////////////////////////////   
 
strutch:

.

deVries, I'm not the lazy guy you are thinking! I simply didn't know how to start...

Now that you gave me the ideas and here it is... My atempt to make the code.

I've no time to test it because I've got to sleep!!!

Tomorow I'll try to insert this "blocks" in my EA to see the results..

Once again, Thank you for your help guys! :)

Are you testing your code in the Strategy Tester ?
Reason: