min max profit/loss before order closure - page 3

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


Yes,

Today I will insert the code in EA and see if it runs!

I'm not so sure about the results of this part of the code:

 

         // Find if this is an old or a new order (duration > 1):
         if( iBarShift(NULL,0,OrderOpenTime(),false) > 1 )
  

Because it can happens 0 < Shift < 1 and the profit > Protection_Level !!

.

 
strutch:


Yes,

Today I will insert the code in EA and see if it runs!

I'm not so sure about the results of this part of the code:

Because it can happens 0 < Shift < 1 and the profit > Protection_Level !!

.

Or you could just do this . . .

if(!OpenOrdersForThisEA())
   {
   Max_Profit_Short = 0;
   Max_Profit_Long = 0;
   }

bool OpenOrdersForThisEA()
   {
   for(int pos = OrdersTotal() - 1; pos >= 0; pos--) 
       {                                                        // Find my open order,
       if( OrderSelect(pos, SELECT_BY_POS)                      // with my magic
       &&  OrderMagicNumber()  == MagicNumber                   // <-- Magic Number for the EA ! !
       &&  OrderSymbol()       == Symbol() 
       && (OrderType() == OP_BUY || OrderType() == OP_SELL) ) 
         {
         return(true);
         }
       }
   retrun(false); 
   }
 
strutch:


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


You will get an error doing this . . . .

double Max_Profit_Short

. . . too many times.

What happens if your OrderModfy() fails ? don't you want to know that it has failed ?

Read this: What are Function return values ? How do I use them ?

 
strutch:

I need to protect the gains on my EA. I was wondering about something that puts the stop in the break when LEVEL_PROTECT is achieved.

The hard way (for me) is to know the MAX and MIN profit of the current orders by it magic number.


. As you see strutch we all want to help you

if you also show you trie to learn and tell us what you want and to get it and show us what you tried

We know now that you work with an EA and you have inside that EA for sell and buy different magicnumber

My idea (maybe i'm wrong in what you want)

To protect the gain of your EA you have to know what the profit is.... i think you can work with total profit all open trades your EA

When total profit becomes>=0 when all trades are calculated you will have at that price a breakeven point where your protection level can start if the gain is in profit

XXX pips from that (protection) level in right direction you can go trail this level If the price comes back to that protection level close all trades

Keep also in mind If the EA opens a new trade then the total breakeven is not the same so count total trades of your EA

I think also about total lots buy EA total lots sell EA

If TotalProfit >0 and total lots buy > total lots sell then protection level has to move up with more profit

If TotalProfit >0 and total lots buy < total lots sell then protection level has to move down with more profit

This is how I look to your problem.... at this moment

 

.

I realy would like to use different MagicNumbers for buy and sell orders in this EA to analyse it separatly in MyForexBook.

I think for that I've got only to duplicate your (SO MUCH SIMPLE!!!) function...

if(!OpenOrdersForThisEA(Magic_UP))
   {
   Max_Profit_Long = 0;
   }

if(!OpenOrdersForThisEA(Magic_DOWN))
   {
   Max_Profit_Short = 0;
   }


bool OpenOrdersForThisEA(int MagicNumb)
   {
   for(int pos = OrdersTotal() - 1; pos >= 0; pos--) 
       {                                                        // Find my open order,
       if( OrderSelect(pos, SELECT_BY_POS)                      // with my magic
       &&  OrderMagicNumber()  == MagicNumb                     // <-- Magic Number (LONG or SHORT orders!!!)
       &&  OrderSymbol()       == Symbol() 
       && (OrderType() == OP_BUY || OrderType() == OP_SELL) ) 
         {
         return(true);
         }
       }
   retrun(false); 
   }

Thank you RaptorUK! With this code I'm almost where I never get alone!!! :)

PS - About the "securiy system" you talk... I'm thinking doing that when I finish the write of EA.. One step at a time!! :P

.

 

.

I think it's done the calculation of max profit of each order... :)

thank you so much for the help!

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////
//                                      //
// Biggest Profit since the order open  //
//                                      //
//////////////////////////////////////////



////////////////////////
//                    //
// External Variables //
//                    //
////////////////////////

extern int     Magic_UP = 1234;                      // Magic Number for long orders
extern int     Magic_DOWN = 4321;                    // Magic Number for short orders



////////////////////////
//                    //
// Cycle              //
//                    //
////////////////////////



// Declare Variables,

double Max_Profit_Short, Max_Profit_Long;
                          

// Reset Variables (if there are no open orders)

   if(!OpenOrdersForThisEA(Magic_UP))
      {
      Max_Profit_Long = 0;
      }
 
   if(!OpenOrdersForThisEA(Magic_DOWN))
      {
      Max_Profit_Short = 0;
      }


// Biggest profit of short open orders:

    for(int pos = OrdersTotal() - 1; pos >= 0; pos--) 
       {                                                        
       if( OrderSelect(pos, SELECT_BY_POS)  &&  OrderMagicNumber() == Magic_DOWN  &&  OrderSymbol() == Symbol() ) 
         {
         Max_Profit_Short = MathMax(Max_Profit_Short ,(OrderProfit()+OrderCommission()+OrderSwap()) );
         }
       }
       
       
// Biggest profit of long open orders:
       
    for(int poss = OrdersTotal() - 1; poss >= 0; poss--) 
       {
       if( OrderSelect(poss, SELECT_BY_POS)   &&  OrderMagicNumber() == Magic_UP  &&  OrderSymbol() == Symbol() ) 
         {      
         Max_Profit_Long = MathMax(Max_Profit_Long ,(OrderProfit()+OrderCommission()+OrderSwap()) );
         }
       }



////////////////////////
//                    //
// Auxiliar Function  //
//                    //
////////////////////////

// Verify Open orders by magic number.


bool OpenOrdersForThisEA(int MagicNumb)
   {
   for(int pos = OrdersTotal() - 1; pos >= 0; pos--) 
       {                                                        // Find my open order,
       if( OrderSelect(pos, SELECT_BY_POS)                      // with my magic
       &&  OrderMagicNumber()  == MagicNumb                     // <-- Magic Number (LONG or SHORT orders!!!)
       &&  OrderSymbol()       == Symbol() 
       && (OrderType() == OP_BUY || OrderType() == OP_SELL) ) 
         {
         return(true);
         }
       }
   return(false); 
   }


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

I tried your formulas but failed to get the highest floating profit value as if the trade’s profit dropped, it can’t keep the highest floating profit value. I modified the formulas a little bit as follows :-

   double cBUYMxPft=0;//Max Floating Net Profit/Loss per Trade in Value
   double cSELMxPft=0;//Max Floating Net Profit/Loss per Trade in Value
   for(int i=OrdersTotal()-1; i>=0; i--)
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
   //if(OrderSelect(i,SELECT_BY_POS))
   if(OrderMagicNumber()==MagicNumber) {
      if(OrderType()==OP_BUY) {
         cBUYMxPft=MathMax(cBUYMxPft,OrderProfit()+OrderSwap()+OrderCommission());
      }
      if(OrderType()==OP_SELL) {
         cSELMxPft=MathMax(cSELMxPft,OrderProfit()+OrderSwap()+OrderCommission());
      }
      }

Kindly have a look where does it go wrong, thank you.

Alert

 
Dhanaka: I tried your formulas but failed to get the highest floating profit value as if the trade’s profit dropped, it can’t keep the highest floating profit value.

Because you don't remember anything. *MxPft are not global or static. They are set to the current highest.

 
William Roeder:

Because you don't remember anything. *MxPft are not global or static. They are set to the current highest.

Hi William, thanks. I've no programming background, hope you don't mind, could you please write down your codes how to set the MxPft to be global or static and able to refresh the max floating profit value to zero if the trade is closed ? Thanks.