how to calculate number of bars elapsed since 2 successive loosing trends

 

hi guys can im new to EA Programming thus can any one specify a function or a algorithm to calculate number of bars elapsed since 2 successive loosing trends

thankz

 
double lastProfit1,lastProfit2=0.0;

if(OrderSelect(0,SELECT_BY_POS,MODE_HISTORY)==true){

lastProfit1=OrderProfit();

}

if(OrderSelect(1,SELECT_BY_POS,MODE_HISTORY)==true){

lastProfit2=OrderProfit();

}

if((lastProfit1<0) && (lastProfit2<0)){

//wait for five bars;

}


is this code correct if so can any one tell me how to wait for lets say 5 bars

 

if((lastProfit1<0) && (lastProfit2<0))

int 2_losses = Bars;

if (Bars < 2_losses + 5)

return;


"Bars" returns the number of bars in the chart. Wenn your two losses happend, store the number of the chart´s bars in a variable an let your EA skip till the bars in the chart are raised to + 5.

 
nirVaan:
double lastProfit1,lastProfit2=0.0;


is this code correct if so can any one tell me how to wait for lets say 5 bars

Don't use Bars, it is unreliable, when your chart gets to Max bars on chart Bars will no longer increment.

Your current time frame/period in minutes is given using Period(), you can use the Sleep function to wait, Sleep(time) (time specified in milliseconds) . . so . . .

Sleep(5 * Period() * 60 * 1000);    // wait for 5 bars * Bar length in minutes * 60 sec * 1000 (converts to milliseconds))

RefreshRates():         //  good idea to do this if you are staying in start() and need to access bid, ask, etc
 

ok here is the modified code.given below but the problem now im facing is when to make the flag isSuccLoss zero again.im thinking at the point a order has been closed. but im not sure how to exactly do this

int isSuccLoss=0;

int start()
  {
//----
   double lastProfit1,lastProfit2=0.0;
   if(isSuccLoss==0){
   
      if(OrderSelect(0,SELECT_BY_POS,MODE_HISTORY)==true){
   
         lastProfit1=OrderProfit();
      
      }
   
     if(OrderSelect(1,SELECT_BY_POS,MODE_HISTORY)==true){
   
         lastProfit2=OrderProfit();
      
     }
   
     if((lastProfit1<0) && (lastProfit2<0)){
   
         isSuccLoss=1;   //global variable 
         //isSuccLossBars=Bars;
     }
   
   }
   
   if(isSuccLoss==1){
      
      Sleep(5 * Period() * 60 * 1000);    // wait for 5 bars * Bar length in minutes * 60 sec * 1000 (converts to milliseconds))
      evalLongShortSetupCondition();
   }else{ 
      evalLongShortSetupCondition();
   }
   
//----
   return(0);
  }
 

I know losses suck . . but what does isSuccLoss mean ? does it mean Two Successive Losses ? if it does why not say that ? and it might be clearer to use a bool, so bool TwoSuccessiveLosses = false;

Add it directly after the Sleep ?

 

yup sorry about my variable naming it is mean to be Two Successive Losses.well according to the code i have(forgive me if im wrong ) if i put TwoSuccessiveLosses = false; right after sleep, every time start () executes the sleep function will execute untill there is a successful trade.

for example let say initially there were two successive losses so you sleep then you execute evalLongShortSetupCondition() but there were no setups then again start() will execute this time i don't want to wait for 5 bars since i have already done that but if i put TwoSuccessiveLosses = false; right after sleep it will again sleep for 5 bars.i hope my explanation is clear.

 
nirVaan:

for example let say initially there were two successive losses so you sleep then you execute evalLongShortSetupCondition() but there were no setups then again start() will execute this time i don't want to wait for 5 bars since i have already done that but if i put TwoSuccessiveLosses = false; right after sleep it will again sleep for 5 bars.i hope my explanation is clear.


OK, I see what you are saying, you saw the two losses and slept, you now want to check for 2 different successive losses before you sleep again.

One way round this is to use an additional variable to record the ticket number for the 2nd loss . . then next time you look for the two losses you also check the ticket numbers of the looses you are looking at, if either of them match the ticket number stored you don't sleep. When you find two new losses you set the SleepTicketNo to the new 2nd loss trade ticket number.

 
ok i was thinking of the same thing bit differently. the code is given below can you kindly verify the logic of the code. thanks
bool alreadySlept=false;
int lastLossTktNum=0;

int start()
  {
//----
   double lastProfit1,lastProfit2=0.0;
   
    if(OrderSelect(0,SELECT_BY_POS,MODE_HISTORY)==true){
   
         if(OrderTicket()!=lastLossTktNum){
         
               TwoSuccessiveLosses=false;
               alreadySlept=false;
         }
      
     }
   
   
   if(TwoSuccessiveLosses==false){
   
   
     if(OrderSelect(1,SELECT_BY_POS,MODE_HISTORY)==true){
   
         lastProfit2=OrderProfit();
      
     }
     
      if(OrderSelect(0,SELECT_BY_POS,MODE_HISTORY)==true){
   
         lastProfit1=OrderProfit();
      
      }
   

   
     if((lastProfit1<0) && (lastProfit2<0)){
   
         TwoSuccessiveLosses=true;   //global variable 
         lastLossTktNum=OrderTicket();
         
         //isSuccLossBars=Bars;
     }else {
     
        TwoSuccessiveLosses=false;
     }
   
   }
   
   if(TwoSuccessiveLosses==true){
      
      if(alreadySlept==false){
      
         Sleep(5 * Period() * 60 * 1000);    // wait for 5 bars * Bar length in minutes * 60 sec * 1000 (converts to milliseconds))
      }
      alreadySlept=true;
      evalLongSetupCondition();
   }else{ 
      evalLongSetupCondition();
   }
   
//----
   return(0);
  }
 
nirVaan:
ok i was thinking of the same thing bit differently. the code is given below can you kindly verify the logic of the code. thanks

From a quick look . . it looks OK.

I wouldn't have used alreadySlept, I think it can be done with just the addition of the lastLossTktNum . . . but the most important thing is does it work ? if it works you have done a good job, optimising code can come later if needed.

 

I think this is how I would do it . . . .

int lastLossTktNum=0;

int start()
  {
//----
   double lastProfit1,lastProfit2=0.0;
   
   if(OrderSelect(1,SELECT_BY_POS,MODE_HISTORY)) lastProfit2=OrderProfit();
   
   if(OrderSelect(0,SELECT_BY_POS,MODE_HISTORY)) lastProfit1=OrderProfit();
   
   if((lastProfit1<0) && (lastProfit2<0) && OrderTicket()!=lastLossTktNum )
      {
      Sleep(5 * Period() * 60 * 1000);    // wait for 5 bars * Bar length in minutes * 60 sec * 1000 (converts to milliseconds))
      lastLossTktNum=OrderTicket();       // uses Ticket Number from order position 0
      }

   evalLongSetupCondition();
   
   
//----
   return(0);
  }
Reason: