Create a profit function

 
hello,

i would like to create for one of my EA a function which it looking in the last 5 closes orders of my EA (same magicnumber and symbol) the net profit value and save them in a array.
Then:
if array[0] is positive X =1;
if array[0] is negative and array[1] is positive X =0.9;
if array[0] & array[1] are negative and array[2] is positive X =0.8;
if array[0] to array[2] are negative and array[3] is positive X =0.7;
if array[0] to array[3] are negative and array[4] is positive X =0.6;
else X=0.5;

I try do it but it doesn't work ! someone can help me please?

Thanks in advance

TAAD
 
why don't scan thru the history trades instead of using array?

for(i = OrdersTotal() - 1; i >= 0; i--)
{
OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
 
doshur:
why don't scan thru the history trades instead of using array?

for(i = OrdersTotal() - 1; i >= 0; i--)
{
OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);


I try to do that but with no success !

Can you help me ?
 
Hi Taad,
post your code, so we can take a look at the problem.

Russell
 
Russell:
Hi Taad,
post your code, so we can take a look at the problem.

Russell


Here is part of the code :

extern double Lot = 1;
extern int nbIncrementationLot =10;
//+------------------------------------------------------------------+
int init()
{
//----
double lotMemory = Lot;
}

//+------------------------------------------------------------------+
void tailleLot()
{
int i;
Lots = lotMemory;
int a = nbIncrementationLot;
for(i = OrdersTotal() - 1; i >= 0; i--)
{
OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
{if (OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{if(OrderProfit()<0)
{Lots=Lot-(Lot/nbIncrementationLot);
a =a-1;
if (a<=0) break;}
else break;}
}
}
for(i = 1; i >= 0; i--)
{
OrderSelect(0, SELECT_BY_POS, MODE_HISTORY);
if (OrderCloseTime()<(TimeCurrent()-86400)) Lots =lotMemory;
}
}

x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x
Lots is lot size in my orders
nbIncrementationLot is the number of time that i will decrease the size of lot when i lost money
example : nbIncrementationLot =4 and Lot=1
if last trade (trade0) is a profit => Lots =1
if trade0 is a loss and trade1 is a profit => Lots = 0.75
if trade0 & trade1 are a loss and trade2 is a profit => Lots = 0.5
else Lots =0.25
x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x


Thanks for help !



 
hmm, creative risk management. In general most ea's do something like this
int start(){
 //...

   double ldLots = 0;
   double ldRiskPct = 3;
   ldLots = ((AccountFreeMargin()/100.0)*ldRiskPct)/(giSL*MarketInfo(Symbol(), MODE_TICKVALUE));
   lotSizeCheck(ldLots, 0.01, 50);

  //...
}

void lotSizeCheck(double& rdLots, double dMinLots, double dMaxLots){
    
   if (rdLots < MarketInfo(Symbol(), MODE_MINLOT)){
      rdLots = MarketInfo(Symbol(), MODE_MINLOT);
   } 
   if (rdLots < dMinLots){
      rdLots = dMinLots;
   }
   if (rdLots > MarketInfo(Symbol(), MODE_MAXLOT)){
      rdLots = MarketInfo(Symbol(), MODE_MAXLOT);
   } 
   if (rdLots > dMaxLots){
      rdLots = dMaxLots;
   }
}

but lets see if I can get this right

double tailleLot(){
   int i,j=0,liLastWin=-1;
   double ldLots = 1;
   Lots = lotMemory;
   int a = nbIncrementationLot;
   for(i = OrdersHistoryTotal() - 1; i >= 0; i--){
      OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
      if (OrderSymbol()==Symbol() && OrderMagicNumber()==Magic){
         j++;
         if(OrderProfit() > 0){
            liLastWin = j;
            break;
         } 
      }
   }
   if (liLastWin != -1){
        ldLots = Lot-(0.25*liLastWin);
   }
   if (ldLots < 0.25){
      ldLots = 0.25;
   } 
   return(ldLots);
}
something like that.... code is not tested; just typed it here.

hth

PS next time please use the SRC button before inserting code in the post
 
Russell:
hmm, creative risk management. In general most ea's do something like this
but lets see if I can get this right

something like that.... code is not tested; just typed it here.

Thanks for you help but ...

I test it, change the code many times but it still doesn't work.

in ordersend, the Lot are always equal to zero !
Do you know why ? have you an other idea ?

 
hmm this pretty much looking into a blackbox this way... a code example would be helpful. But i guess the following is going wrong. You probably had the Lot variable as a global. I made the function to return a value, this way it is easier to re-use in other EA's. So if you call the function you should "capture" the return output.
int start(){

   //... some code

   int liTicket = liTicket = -1;
   double ldLots = tailleLot();

   if (/* conditions for opening buy*/){
       liTicket = OrderSend(Symbol(), OP_BUY, ldLots, /* other params */);
   }
   if (/* conditions for opening sell*/){
       liTicket = OrderSend(Symbol(), OP_SELL, ldLots, /* other params */);
   }
}
 
TAAD:

Thanks for you help but ...

I test it, change the code many times but it still doesn't work.

in ordersend, the Lot are always equal to zero !
Do you know why ? have you an other idea ?

int init()
{
//----
double lotMemory = Lot;
}

//+------------------------------------------------------------------+
void tailleLot()
{
int i;
Lots = lotMemory;
int a = nbIncrementationLot;
for(i = OrdersTotal() - 1; i >= 0; i--)
{
OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
{if (OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
The code you pasted would not compile lotMemory is undefined in tailleLot().
In init() lotMemory is lost as soon as it returns.
Try:
double lotMemory;
int init() {
   lotMemory = Lot;
}

//+------------------------------------------------------------------+
void tailleLot() {
    Lots = lotMemory;
    int a = nbIncrementationLot;
    for(int i = OrdersTotal() - 1; i >= 0; i--) if (
       OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)
    && OrderSymbol()      == Symbol() 
    && OrderMagicNumber() == Magic) {
       //...
 
Thanks to WHRoeder and Russell

Base on your code i have found the solution !
Reason: